1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 /* 6 * Item struct for holding item information 7 */ 8 typedef struct _Item 9 { 10 char name[25]; 11 size_t quantity; 12 double price; 13 struct _Item *next; 14 }Item; 15 16 /* 17 * ResizeArray will be called when the number of 18 * items to be added to cart exceed the current bounds of the array 19 */ 20 void ResizeArray(Item **ShoppingCart,int num) 21 { 22 Item *temp = *ShoppingCart; 23 *ShoppingCart = malloc(num*2*sizeof(Item)); 24 if(*ShoppingCart == NULL) 25 { 26 printf("Cart can't add more items. \n"); 27 return; 28 } 29 memmove(*ShoppingCart,temp,num); 30 } 31 32 /* 33 * Print the current list of items 34 */ 35 void print_items(Item *head) 36 { 37 Item *temp = head; 38 while( temp != NULL) 39 { 40 printf("Item Name:%s \n",temp->name); 41 printf("Item quantity:%lu \n",temp->quantity); 42 printf("Item price:%.2f\n\n",temp->price); 43 temp = temp->next; 44 } 45 } 46 47 /* 48 * Add an item to ShoppingCart 49 */ 50 void add_item(Item **head,Item *newItem) 51 { 52 if(*head == NULL) 53 { 54 *head = newItem; 55 } 56 else 57 { 58 Item *temp = *head; 59 while(temp -> next != NULL) 60 { 61 temp = temp->next; 62 } 63 newItem->next = temp->next; 64 temp->next = newItem; 65 } 66 } 67 68 /* 69 * Provide a menu that allows the user to choose among 3 options. 70 */ 71 void menu(char* choice) 72 { 73 char Buffer[50]; 74 printf("Enter a or A: Add an item to ShoppingCart\n"); 75 printf("Enter p or P: Print the current list of items\n"); 76 printf("Enter q or Q: Quit the program\n"); 77 printf("Enter your choice:"); 78 fgets(Buffer,50,stdin); 79 sscanf(Buffer,"%c",choice); 80 } 81 82 int main() 83 { 84 int num; 85 int num_in_cart = 0; 86 Item *ShoppingCart; 87 Item *head = NULL; 88 char Buffer[50]; 89 char choice; 90 91 /* 92 * Ask the user for the number of items, 93 * and use this number as tbe initial size of cart 94 */ 95 printf("Enter the number of items you want to buy:"); 96 fgets(Buffer,50,stdin); 97 sscanf(Buffer,"%d",&num); 98 99 ShoppingCart = malloc( num*sizeof(Item) ); 100 if(ShoppingCart == NULL) 101 { 102 printf("ShoppingCart is NULL!\n"); 103 exit(1); 104 } 105 106 /* 107 * Option A: Add am item to cart 108 * Option P: Print the current list of items 109 * Option Q: Quit the program 110 */ 111 menu(&choice); 112 while(choice != 'q' && choice !='Q') 113 { 114 if(choice == 'A' || choice == 'a') 115 { 116 char Buffer[50]; 117 Item *newItem; 118 if(num_in_cart == num) 119 { 120 ResizeArray(&ShoppingCart,num); 121 num = num*2; 122 } 123 124 newItem = &ShoppingCart[num_in_cart]; 125 newItem->next = NULL; 126 127 printf("Enter name:"); 128 fgets(Buffer,50,stdin); 129 sscanf(Buffer," %s",newItem->name); 130 131 printf("Enter quantity:"); 132 fgets(Buffer,50,stdin); 133 sscanf(Buffer,"%lu",&(newItem->quantity) ); 134 135 printf("Enter price:"); 136 fgets(Buffer,50,stdin); 137 sscanf(Buffer,"%lf",&(newItem->price)); 138 139 printf("name = %s \n", newItem->name); 140 printf("quantity = %lu \n", newItem->quantity); 141 printf("price = %f \n", newItem->price); 142 143 add_item(&head,newItem); 144 num_in_cart++; 145 146 menu(&choice); 147 } 148 else if(choice == 'p' || choice == 'P') 149 { 150 print_items(head); 151 menu(&choice); 152 } 153 else 154 { 155 printf("Invalid input! \n"); 156 menu(&choice); 157 } 158 } 159 160 free(ShoppingCart); 161 printf("Program Exited.\n"); 162 return 0; 163 }