Skip to main content
//RAND()SUPERMARKET





#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<windows.h>
void clear(){
system("cls");
}
void delay(int second){
int milsec = 1000 * second;
clock_t startTime = clock();
while(clock() < (startTime + milsec));
}
struct item{
char name[31];
int qty;
int price;
struct item *next,*prev;
}*head,*tail,*curr,*curr2;
struct item *news(char name[],int qty){
struct item *temp=(struct item*)malloc(sizeof(struct item));
temp->qty=qty;
temp->price=rand()%100000+1;
strcpy(temp->name,name);
return temp;
}
int countlist(){
curr=head;
int flag=0;
while(curr!=NULL){
flag++;
curr=curr->next;
}
return flag;
}
void drop(int x){
if(x<=countlist()){
if(head==tail){
head=tail=curr=NULL;
}
else if(x==1){
curr=head->next;
free(head);
head=curr;
head->prev=NULL;
}
else if(x==countlist()){
curr=tail->prev;
free(tail);
tail=curr;
tail->next=NULL;
}
else{
curr=head;
for(int i=0;i<x-1;i++){
curr=curr->next;
}
curr->next->prev=curr->prev;
curr->prev->next=curr->next;
free(curr);
}
}
}
bool deletion(char name[]){
curr=head;
for(int i=1;curr!=NULL;i++){
if(strcmpi(name,curr->name)==0){
drop(i);
return true;
}
curr=curr->next;
}
return false;
}
void push(char name[],int qty){
if(head==NULL){
head=news(name,qty);
head->next=NULL;
head->prev=NULL;
tail=head;
}
else {
curr=news(name,qty);
if(strcmp(name,head->name)<0){
head->prev=curr;
curr->next=head;
curr->prev=NULL;
head=curr;
}
else if(strcmp(name,tail->name)>0){
tail->next=curr;
curr->prev=tail;
curr->next=NULL;
tail=curr;
}
else{
curr2=head;
while(curr2->next!=tail&&strcmp(name,curr2->name)>0){
curr2=curr2->next;
}
curr->next=curr2->next;
curr->prev=curr2;
curr2->next->prev=curr;
curr2->next=curr;
}

}
}
void view(){
curr=head;
printf(" ______________________________________________________\n");
printf("| No.  | Item Name                      | qty | Price  |\n");
printf("+======================================================+\n");
for(int i=0;curr!=NULL;i++){
printf("| ");
printf("%d.",i+1);
if(i+1<10){
printf("  ");
}
else if(i+1<100){
printf(" ");
}
printf(" | ");
printf("%s",curr->name);
int len=strlen(curr->name);
for(int t=0;t<30-len;t++){
printf(" ");
}
printf(" | ");
printf("%d",curr->qty);
if(curr->qty>99){

}
else if(curr->qty>9){
printf(" ");
}
else printf("  ");
printf(" | ");
printf("%d",curr->price);
if(curr->price<10){
printf("    ");
}
else if(curr->price<100){
printf("   ");
}
else if(curr->price<1000){
printf("   ");
}
else if(curr->price<10000){
printf("  ");
}
else if(curr->price<100000){
printf(" ");
}
else printf("");
printf(" |\n");
curr=curr->next;
}
printf("+======================================================+\n");
}
bool qtyplus(char name[31],int qty){
curr=head;
while(curr!=NULL){
if(strcmpi(name,curr->name)==0){
curr->qty=curr->qty+qty;
printf("Item already existed...\nUpdating existing item's Qty instead...\n");
if(curr->qty>100){
curr->qty=100;
printf("Uhhh... Qty cant be over 100...\nSorry pal..\n");
}
return false;
}
curr=curr->next;
}
return true;
}

bool qtyupdate(char name[31],int qty){
curr=head;
while(curr!=NULL){
if(strcmpi(name,curr->name)==0){
curr->qty=qty;
return true;
}
curr=curr->next;
}
return false;
}
int keypress(){
int benar=0;
while(benar==0){
int hit = kbhit();
int ch;
if (hit){
ch = getch();
benar=1;
return ch;
}
}
}
int main(){
srand(time(NULL));
int menu=0;
int mulai=0;
printf("Welcome, shop will you ?\n");
printf("Press any key to continue...?\n");
keypress();
while(menu!=5){
clear();
printf("Welcome to random shop...\n");
printf("1. Add item\n");
printf("2. View items\n");
printf("3. Delete an item\n");
printf("4. Change item qty\n");
printf("5. Check Out\n");
char input[20]={0};
do{
printf(">>");
if(mulai==1)getchar();
mulai=1;
scanf("%[^\n]",&input);
}while(strcmp(input,"1")!=0&&strcmp(input,"2")!=0&&strcmp(input,"3")!=0&&strcmp(input,"4")!=0&&strcmp(input,"5")!=0);
menu=input[0]-48;
if(menu==1){
clear();
char inp[100]={0};
int qty=0;
do{
printf("If you input an existing item (Not case sensitive),\nit will add qty into the existing one...\n");
printf("(Word length : 5-30)>>");
getchar();
scanf("%[^\n]",&inp);
}while(strlen(inp)>30||strlen(inp)<5);
while(qty<1||qty>100){
printf("(QTY (MIN 1, MAX 100)))>>");
scanf("%d",&qty);
}
if(qtyplus(inp,qty)){
push(inp,qty);
printf("Add Success !\n");
}
else{


printf("Item succesfully updated...\n");
printf("Press any key to continue...\n");
keypress();
}
}
if(menu==2){
clear();
if(countlist()>0){

view();
}
else printf("no data....\n");
printf("Press any key to continue...\n");
keypress();
}
if(menu==3){
clear();
if(countlist()>0){
view();
char inp[100]={0};
int qty=0;
do{
printf("Which one do you want to delete(Not case sensitive)?\n");
printf("(Word length : 5-30)>>");
getchar();
scanf("%[^\n]",&inp);
}while(strlen(inp)>30||strlen(inp)<5);
if(deletion(inp)){
printf("Delete Succes...\n");
}
else {
printf("No such data...\n");

}
}
else printf("No data to delete...\n");
printf("Press any key to continue...");
keypress();
}
if(menu==4){
clear();
if(countlist()>0){
view();
char inp[100]={0};
int qty=0;
do{
printf("Which one do you want to update(Not case sensitive)?\n");
printf("(Word length : 5-30)>>");
getchar();
scanf("%[^\n]",&inp);
}while(strlen(inp)>30||strlen(inp)<5);
while(qty<1||qty>100){
printf("(QTY (MIN 1, MAX 100)))>>");
scanf("%d",&qty);
}
if(qtyupdate(inp,qty)){
printf("Update success !\n");
}
else{
printf("No such item exist...\n");
}
}
else printf("No data to update....");
printf("Press any key to continue...");
keypress();
}
}
}

Comments

Popular posts from this blog

Rankuman Akhir

Rangkuman Akhir 10 Juni 2020 Nama: Johanes Peter Vincentius NIM: 2301864461 Nama Dosen:Henry Chong(D4460) & Ferdinand Ariandy Luwinda (D4522) Before continuing to my summary, i just want to make a clarification that I did not copas, and if the language is simillar, it's just because I summarize directly from PPT and use a bit of internet for clarification. The point is, I studied while making this summary, sorry if it might have a slight resemblance to the ppt. The summary : Heaps And Tries, both are a data structures algorithm concept In this blog I will be summarizing : Heap Concept Min Heap  Max Heap Min-Max Heap Heap Applications Tries Concept Tries Applications Heap is a complete binary tree which impliments the "heap" property. Heap property ?  Min Heap and Max Heap Min Heap means every node's element is smaller than its children's Max Heap means every node's element is larger than its children's ...

Pertemuan III Mengenai pengaplikasian Linked List

Linked List, banyak hal dapat dilakukan dalam linked list, seperti push(insert), pop(delete). memahami konsep Singly Linked List dapat dibilang relatif mudah, namun pengaplikasian kodingnya perlu waktu untuk pemahaman. Saya Johanes Peter akan merangkum apa yang diajarkan di kelas besar pertemuan 3, yaitu mengenai koding linked list. Struct struct Data {     int value;     struct Data *next,*prev; }*head,*curr,*tail; Push tail (insert sebuah data di paling belakang) void push(int a) {     curr = (struct Data*)malloc(sizeof(struct Data));     curr->value = a;     if(head==NULL){         head = tail = curr;     }     else{         tail->next = curr;         curr->prev = tail;         tail = curr;     }     head->prev = tail->next = NULL; } Print isi dalam...

HALF SEMESTER SUMMARY

Data Structures Summary Dear reader, due to the recent outbreak of the COVID-19 pandemic, we've all been forced to study at home. It is a challenge to keep track of the subject matter, so in this blog, I will be putting all my summary about what I've learned in this half semester from Data Structures. I've been thinking about the final project of making a pacman game using linked list... I can make a pacman game with 2d arrays, but a Linked List as a map ? It still leaves me in confusion ... For now I'll be studying more too be closer on approaching the final project Linked List II,  a linear data structure where each element is a separate object. Materials : - Circular Linked List - Circular Single Linked List - Doubly Linked List - Circular Doubly Linked List Circular Linked List Circular Single Linked List is a variation of linked list which its first element points to the last element and the last element points to the first element. In this list, there is...