Pages
Categories
Archives
Meta
Pages
Categories
Archives
KEEP CALM AND CHEERS WITH US
Posted in: Uncategorized by wreckcs19 on January 20, 2016
- stdio : standart input output
- FILE >> harus ditulis dengan huruf besar semua
- Catatan tambahan :
Struct lebih baik digunakan, sehingga tidak perlu membuat banyak variabel berbeda.
Contoh cara membuka file :
- Membuka File menggunakan fopen ():
- FILE *fopen (const char *filename, const char *mode);
- FILE *file = fopen(“data.txt”,”r”);
“r” membuka file untuk dibaca.
“w” membuat file untuk ditulis.
“A” membuka file untuk data append.
“r+” membuka file untuk membaca / menulis.
“w+” membuat file untuk membaca / menulis.
“a+” membuka file untuk membaca / tambahkan
“rb” membuka File (binary) untuk dibaca.
“wb” membuat file (binary) untuk menulis.
Close File :
int fclose (FILE *stream);
– fclose () didefinisikan di
– fclose () akan mengembalikan 0 jika berhasil, dan EOF jika error
– EOF (End Of File) sama dengan -1 (EOF itu dibuat otomatis)
– fclose () akan masuk ke buffer area dulu dan segera mengirimkan data yang tersisa untuk file.
– !feof(file) -> membaca sampai akhir file
– Selain itu !=EOF
“Contoh Codingan (Toko Baju)dari Skyconnectiva/
source : http://a-l-g-o-r-i-t-m-a.blogspot.co.id/#include
#include
#include
#includestruct Baju{
char kodeBaju[7];
char namaBaju[100];
int qty;
int hargaSatuan;
};int initProgram(Baju baju[]){
FILE *file = fopen(“data.txt”, “r”);
int index = 0;
char tempHarga[100], tempQty[100];
while (!feof(file)){
fscanf(file, “%[^;];%[^;];%[^;];%[^\n]\n”, baju[index].kodeBaju, baju[index].namaBaju, tempQty, tempHarga);
baju[index].qty = atoi(tempQty);
baju[index].hargaSatuan = atoi(tempHarga);
index++;
}
fclose(file);
return index;
}void generateTable(Baju baju[], int totalItem){
printf(“%-4s%-12s%-20s%-5s%-15s\n”, “No.”, “Code”, “Name”, “Qty”, “Price”);
printf(“============================================================\n”);
for (int i = 0; i < totalItem; i++)
printf(“%-4d%-12s%-20s%-5d%-15d\n”, (i + 1), baju[i].kodeBaju, baju[i].namaBaju, baju[i].qty, baju[i].hargaSatuan);
if(totalItem == 0) printf(“There is no data!”);
printf(“\n\n\n”);
}char *generateCode(Baju baju[], int totalItem){
static char kode[10], kodeReturn[10];
int count;
strcpy(kode, baju[totalItem – 1].kodeBaju);
kode[0] = ‘0’; kode[1] = ‘0’;
count = atoi(kode) + 1;
if (count < 10) strcpy(kodeReturn, “JF00”);
else if (count < 100) strcpy(kodeReturn, “JF0”);
else if (count < 1000) strcpy(kodeReturn, “JF”);
itoa(count, kode, 10);
strcat(kodeReturn, kode);
return kodeReturn;
}int checkCode(char key[], Baju baju[], int totalItem){
int left = 0, right = totalItem – 1, mid;
do{
mid = (left + right) / 2;
if (strcmp(baju[mid].kodeBaju, key) == 0) return mid;
else if (strcmp(baju[mid].kodeBaju, key) == -1) left = mid + 1;
else right = mid – 1;
} while (left<=right);
return -1;
}void sort(Baju baju[], int left, int right, char by[], char option[]){
int i = left, j = right;
int pivotInt;
char pivotChar[100];
Baju tempBaju;
if (strcmp(by, “code”) == 0)strcpy(pivotChar, baju[(left + right) / 2].kodeBaju);
else if (strcmp(by, “name”) == 0)strcpy(pivotChar, baju[(left + right) / 2].namaBaju);
else if (strcmp(by, “qty”) == 0)pivotInt = baju[(left + right) / 2].qty;
else if (strcmp(by, “price”) == 0)pivotInt = baju[(left + right) / 2].hargaSatuan;
while (i <= j){
if (strcmp(by, “code”) == 0) { while (strcmp(baju[i].kodeBaju, pivotChar) 0) j–; }
else if (strcmp(by, “name”) == 0) { while (strcmp(baju[i].namaBaju, pivotChar) 0) j–; }
else if (strcmp(by, “qty”) == 0) { while (baju[i].qty pivotInt) j–; }
else if (strcmp(by, “price”) == 0) { while (baju[i].hargaSatuan pivotInt) j–; }
if (i <= j){
tempBaju = baju[i];
baju[i] = baju[j];
baju[j] = tempBaju;
i++; j–;
}
}
if (left < j) sort(baju, left, j, by, option);
if (i < right) sort(baju, i, right, by, option);
}void saveData(Baju baju[], int totalItem){
FILE *file = fopen(“data.txt”, “w”);
for (int i = 0; i < totalItem; i++)
fprintf(file, “%s;%s;%d;%d\n”, baju[i].kodeBaju, baju[i].namaBaju, baju[i].qty, baju[i].hargaSatuan);
fclose(file);
}int main(){
Baju baju[100];
int totalItem = initProgram(baju);
int choose, avail;
char tempCode[10], tempBy[10], tempSrt[10];
do{
system(“cls”);
printf(“\t\t\tJackForce Store\n\n”);
generateTable(baju, totalItem);
printf(“Menu\n”);
printf(“====\n”);
printf(“1. Add Item\n”);
printf(“2. Remove Item\n”);
printf(“3. Sort Item\n”);
printf(“4. Exit\n”);
printf(“\nInput menu : “);
scanf(“%d”, &choose); fflush(stdin);
switch (choose){
case 1:
sort(baju, 0, (totalItem-1), “code”, “asc”);
do{
printf(“Input your item’s name [3..20] : “);
gets(baju[totalItem].namaBaju);
} while (strlen(baju[totalItem].namaBaju)20);
do{
printf(“Input your item’s qty [1..1000] : “);
scanf(“%d”, &baju[totalItem].qty); fflush(stdin);
} while (baju[totalItem].qty1000);
do{
printf(“Input your item’s price [10000..1000000] : “);
scanf(“%d”, &baju[totalItem].hargaSatuan); fflush(stdin);
} while (baju[totalItem].hargaSatuan1000000);
strcpy(baju[totalItem].kodeBaju, generateCode(baju, totalItem));
totalItem++;
printf(“Success insert the new item!”);
getchar();
break;
case 2:
avail = -99;
do{
printf(“Input your item’s code [input ‘cancel’ to cancel] : “);
scanf(“%s”, tempCode); fflush(stdin);
if (strcmp(tempCode, “cancel”) != 0 && (avail = checkCode(tempCode, baju, totalItem)) == -1){
printf(“Can’t find the item that you mean.. Please check again the code.\n”);
getchar();
}
} while (strcmp(tempCode, “cancel”) != 0 && avail == -1);
if (avail != -99){
for (int i = avail + 1; i >= 1 && i < totalItem; i++){
baju[i-1] = baju[i];
}
totalItem -= 1;
printf(“Success delete an item!”);
getchar();
}
break;
case 3:
do{
printf(“Sort by [code/name/qty/price] : “);
scanf(“%s”, tempBy);
} while (strcmp(tempBy, “code”) != 0 && strcmp(tempBy, “name”) != 0 && strcmp(tempBy, “qty”) != 0
&& strcmp(tempBy, “price”) != 0);
sort(baju, 0, (totalItem-1), tempBy, “asc”);
printf(“The data has been sorted!”);
getchar();
break;
}
} while (choose != 4);
saveData(baju, totalItem);
printf(“Exit from the System.. Press Any Key to Continue…”);
getchar();
return 0;
}
SALAM CyberSecurityORCyberSecurityCommunity ^_^
HACK PASSIONATELY
ACT RIGHTEOUSLY
THANKSSSSS
KEEP CALM AND CHEERS WITH US
Posted in: Uncategorized by wreckcs19 on January 13, 2016
“SORTING AND SEARCHING”
DATE : Wednesday, 7 January 2016
- BUBBLE SORT : void Bubble(int *DataArr, int n)
{
int i, j;
for(i=1; i<n; i++)
for(j=n-1; j>=i; j–)
if(DataArr[j-1] > DataArr[j])
Swap(&DataArr[j-1],&DataArr[j]);
} - SELECTION SORT :
for(i=0; i<=N-2; i++){ /* N=number of data */for(j=i; j<=N-1; j++){Note the index of smallest value between A[j] s/d A[N-1],Save it in variable k.Swap A[i] with A[k].}}
- INSERTION SORT :
for(i=1; i<n; i++) {x = A[i], insert x to its suitable place between A[0] and A[i-1].}
- QUICK SORT :
void QuickSort(int left, int right){if(left < right){//arrange elements R[left],…,R[right] that//producing new sequence:R[left],…,R[J-1] < R[J] and R[J+1],…,R[right] > R[J].QuickSort(left, J-1);QuickSort(J+1, right);}}
- MERGE SORT :
Menyorting algoritma berdasarkan pada algoritma membagi dan mengatasi :– Divide : membagi data masukan dalam dua himpunan penguraian– Recur : memecah masalah yang terkait dengan subset– Conquer : menggabungkan solusi untuk setiap bagiann dalam solusi
Searching merupakan proses yang fundamental dalam pemograman, berguna menemukan data(nilai) tertentu di dalam sekumpulan data yang bertipe sama. Fungsi pencarian itu sendiri adalah untuk memvalidasi(mencocokan) data.
Tipe Searching
- LINEAR SEARCH :
void main(){int ar[100],beg,mid,end,i,n,search;clrscr();cout<<“How many numbers in the array: “;cin>>n;cout<<“Enter “<<n<<” numbers in ascending order –> “;for(i=0;i<n;i++)cin>>ar[i];beg=0;end=n-1;cout<<“Enter a number to search: “;cin>>search;while(beg<=end){mid=(beg+end)/2;if(ar[mid]==search){cout<<“\nItem found at position “<<(mid+1);getch();}if(search>ar[mid])beg=mid+1;elseend=mid-1;}cout<<“\nSorry! “<<search<<” doesnot found.”;getch();}
- BINARY SEARCH :
int data[10]={1,3,4,7,12,25,40,65,78,90};int binary_search(int cari){int l,r,m;int n=10;l=0;r=n-1;int ketemu=0;while (l<=r && ketemu==0){m=(l+r)/2;if (data[m]==cari)ketemu=1;elseif(cari<data[m])r=m-1;else l=m+1;}if(ketemu==1) return 1; else return 0;}void main(){clrscr();int cari, hasil;cout<<“masukan data yang ingin di cari= “;cin>>cari;hasil = binary_search(cari);if(hasil==1){cout<<“data ada!”<<endl;}elseif(hasil==0)cout<<“data tidak ada!”<<endl;getch();}
-
INTERPOLATION SEARCH :int main(int argc, char *argv[]){int tempFound = 0;int kodeBarang[] = {101,102,201,301,401,402,501,601,602,701};string namaBarang[] = {“Flashdisk Kingston”, “Flashdisk Data Traveler”, “RAM VGEN”,“VGA ATI RADEON”, “Laptop Asus”, “Netbook HP”, “CD ROM”, “Mouse”,“Keyboard”, “Monitor LG”};int stokBarang[] = {5, 7, 8, 9, 2, 3, 4, 6, 4, 5};string lokasiBarang[] = {“Rak 5B”, “Rak AA”, “Rak 12D”, “Rak B6″, “Rak VC7″, “Rak AB12″,“Rak G23″, “Rak K9″, “Rak 5J”, “Rak D5″};int kodeKunci;cout << “\n\tMasukkan kode barang : “;cin >> kodeKunci;tempFound = interpolationSearch(kodeBarang, kodeKunci, (sizeof(kodeBarang)/4));if(tempFound>=0){cout << “\n\n\tBarang yang Anda cari ditemukan, berikut detailnya : ” <<endl;cout << endl;cout << “\tNama Barang : ” << namaBarang[tempFound] <<endl;cout << “\tStok Barang : ” << stokBarang[tempFound] <<endl;cout << “\tLokasi : ” << lokasiBarang[tempFound] <<endl <<endl;cout << “\t”;}else{cout << “\n\n\tMohon maaf, barang yang Anda cari belum ada\n\t” <<endl;}system(“pause”);return EXIT_SUCCESS;}
SALAM CyberSecurityORCyberSecurityCommunity ^_^
HACK PASSIONATELY
ACT RIGHTEOUSLY
THANKSSSSS
THANKSS
Posted in: Uncategorized by wreckcs19 on January 13, 2016
CLEAR
Posted in: Uncategorized by wreckcs19 on January 13, 2016
KEEP CALM AND CHEERS WITH US
Posted in: Uncategorized by wreckcs19 on January 6, 2016
ALGORITMA PROGRAMMING “STREAM & BUFFER”
DATE : Wednesday, 17 Desember 2015
File and Stream
Definition Stream:
-To keep key in data from keyboard need to be saved at secondary storage device as a data file
-Stream is a sequence of character
There are 3 standard streams activated in C
-Standard Input Stream
-Standard Output Stream
-Standard Error Stream
File Definition:
-File is a collection of record
-Record is a collection of field
-Field is a block of byte
Buffer Area
Buffer Area Definiton:
-Buffer area is part of the memory used as a temporary space before data moved to a file
-Also known as stream pointer
Open File
Opening a File Using fopen() :
-FILE*fopen(const char*filename, const char*mode);
-fopen() return a pointer to the start of a buffer area
-NULL will be returned if file unable to open.
contoh syntax :
- Open File
Membuka File menggunakan fopen ():
FILE *fopen (const char *filename, const char *mode);
contoh mode :
“r” membuka file untuk dibaca.
“w” membuat file untuk ditulis.
“A” membuka file untuk data append.
“r+” membuka file untuk membaca / menulis.
“w+” membuat file untuk membaca / menulis.
“a+” membuka file untuk membaca / menambahkan
“rb” membuka File (binary) untuk dibaca.
“wb” membuat file (binary) untuk menulis
SALAM CyberSecurityORCyberSecurityCommunity ^_^
HACK PASSIONATELY
ACT RIGHTEOUSLY
THANKSSSSS