Advertisement
Guest User

Untitled

a guest
Oct 19th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #pragma warning(disable: 4996)
  6.  
  7. struct Item{                       // a node to hold personal details
  8.     char name[255];
  9.     float price;  // Price for each item.
  10.     int quantity;
  11.     struct Item *next;
  12. }*head=NULL;
  13.  
  14. void flush();                         // forward declaration of functions
  15. void branching(char c);            
  16. int insert();
  17. void print();
  18. struct search();
  19. void delete();
  20.  
  21. int main() {  // print a menu for selection
  22.     char ch = 'i';
  23.  
  24.     ungetc('\n', stdin); // inject input buffer with a return character
  25.  
  26.     do {
  27.         printf("Enter your selection\n");
  28.         printf("\ti: insert a new item\n");
  29.         printf("\td: delete an item\n");
  30.         printf("\tp: print all items\n");
  31.         printf("\tq: quit \n");
  32.        
  33.         flush();                    // flush input buffer
  34.         ch = tolower(getchar());           
  35.         branching(ch);
  36.     } while (ch != 113);
  37.  
  38.     return 0;
  39. }
  40.  
  41. void flush() {
  42.     int c;
  43.     do {
  44.         c = getchar();
  45.     } while (c != '\n' && c != EOF);
  46. }
  47.  
  48. void branching(char c) {    // branch to different tasks
  49.     switch (c) {
  50.     case 'i':
  51.         insert();
  52.         break;
  53.     case 'd':
  54.         delete();
  55.         break;
  56.     case 'p':
  57.         print();
  58.         break;
  59.     case 'q':
  60.         break;
  61.     default:
  62.         printf("Invalid input\n");
  63.     }
  64. }
  65.  
  66. int insert() {    // insert a new entry
  67.     struct Item *p;
  68.     p = malloc(sizeof(struct Item));
  69.     if (p == 0) {
  70.         printf("out of memory\n");  return -1;
  71.     }
  72.     printf("Enter name, price, quantity: \n");
  73.     scanf("%s", p->name);   // p->name is array
  74.     scanf("%f", &p->price);
  75.     scanf("%d", &p->quantity);
  76.     p->next = head;
  77.     head = p;
  78.     return 0;
  79. }
  80.  
  81. struct Item* search() {    // search for position to delete
  82.     char sname[32];
  83.     struct Item *p = head, *b = p;
  84.     printf("please enter the name to be searched for:\n");
  85.     scanf("%s", sname);
  86.     while (p != 0)
  87.         if (stricmp(sname, p->name)== 0) {
  88.             return p;
  89.         }
  90.         else {b = p; p = p->next;}
  91.     printf("The name does not exist.\n");
  92.     return 0;
  93. }
  94.  
  95. void delete() {  
  96.     struct Item *b;
  97.     if (head == 0)  return;     // nothing to delete
  98.     b = search();       // find the position
  99.     if (b == 0) {   // free head        // case 1
  100.         free(head); // C garbage collection
  101.         head = head->next;
  102.         return;
  103.     }
  104.     if (b->next->next == 0) {   // case 2
  105.         free(b->next);
  106.         b->next = 0;
  107.         return;
  108.     } else {
  109.         free(b->next);
  110.         b->next = b->next->next;    // case 3
  111.         return;
  112.     }
  113. }
  114.  
  115. void print(){
  116.     float total = 0;
  117.     // .. element to L
  118.  
  119.     // Iterate each node and print
  120.      struct Item* n = &ShoppingList;
  121.  
  122.     do{
  123.         printf("Item: %s\n", n->name);
  124.         printf("Quantity: %d\n", &n->quantity);
  125.         printf("Price: %f\n", &n->price);
  126.         total += (&n->quantity)*(&n->price);
  127.         n = n->next;
  128.     }
  129.         while(n != NULL)
  130.     printf("Grand Total: %f\n", total);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement