Jan
19
2016

Review Algorithm & Programming Class PART.10

Hari, tanggal : Kamis, 14 Januari 2016

Materi             :  Material Review

Tempat           : Ruang 800 Kampus Anggrek Binus Univ.

 

  • 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”);

Keterangan contoh mode :
“r” membuka file untuk dibaca.
“w” membuat file untuk ditulis.
“A” membuka file untuk data append.
“r+” membuka file untuk membaca dan menulis.
“w+” membuat file untuk membaca dan menulis, menambahkan bagian awal.
“a+” membuka file untuk membaca dam tambahkan bagian belakang.
“rb” membuka File (binary) untuk membaca.
“wb” membuat file (binary) untuk menulis.
(jika membuat game sebaiknya save menggunakan binary yaitu “rb” atau “wb” karena tidak bisa dibuka dengan notepad biasa)

– Close File
int fclose (FILE *stream);
– 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
– boleh juga pake !=EOF

Jika ingin mengubah fungsi char dapat menggunakan :

  • atoi : ascii to int
  • itoa : int to ascii

NB :

jangan lupa fclose(file)

strcat >> menggabung string

fflush >> menghapus buffer

strcmp >> membandingkan 2 string

strcmpi >> tidak membedakan huruf besar dan kecil (ignore case)

Contoh Coding (toko baju):
source : http://a-l-g-o-r-i-t-m-a.blogspot.co.id/

#include
#include
#include
#include

struct 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;
}

Written by Nathania Elisha in: Algoritma & Pemrograman |

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress. Kredit, Streaming Audio | Theme by TheBuckmaker.