Slight Review

Pada pertemuan materi algoritma yang ke 11 ini kami membahas tentang kodingan yang selama ini sudah diajarkan, kami mereview materi,

kita membahas tentang file.

untuk info lebih jelasnya kami diberikan alamat blig buatan dosen tercinta yang berisi banyak kode yang bisa di pakai buat belajar atau iseng iseng

http://a-l-g-o-r-i-t-m-a.blogspot.co.id/

kita membahas acara menggunakan struck dan cara mengoper ngopernya ke fungsi fungsi,
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) i++; while (strcmp(baju[j].kodeBaju, pivotChar) > 0) j–; }
else if (strcmp(by, “name”) == 0) { while (strcmp(baju[i].namaBaju, pivotChar) < 0) i++; while (strcmp(baju[j].namaBaju, pivotChar) > 0) j–; }
else if (strcmp(by, “qty”) == 0) { while (baju[i].qty < pivotInt) i++; while (baju[j].qty > pivotInt) j–; }
else if (strcmp(by, “price”) == 0) { while (baju[i].hargaSatuan < pivotInt) i++; while (baju[j].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)<3 || strlen(baju[totalItem].namaBaju)>20);
do{
printf(“Input your item’s qty [1..1000] : “);
scanf(“%d”, &baju[totalItem].qty); fflush(stdin);
} while (baju[totalItem].qty<1 || baju[totalItem].qty>1000);
do{
printf(“Input your item’s price [10000..1000000] : “);
scanf(“%d”, &baju[totalItem].hargaSatuan); fflush(stdin);
} while (baju[totalItem].hargaSatuan<10000 || baju[totalItem].hargaSatuan>1000000);
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;
}

Leave a Reply

Your email address will not be published. Required fields are marked *