Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #define LENGTH 80
  7. #define TRUE
  8.  
  9. struct produceItem
  10. {
  11.     char produce[20];
  12.     char type[20];
  13.     char soldBy[20];
  14.     float price;
  15.     int quantityInStock;
  16.     struct produceItem *next;
  17. };
  18.  
  19. void menu();
  20.  
  21. int main() {
  22.     menu();
  23.     return 0;
  24. }
  25.  
  26. void addProduceItem(struct produceItem **, char *, char *, char *, float, int);
  27. void count(void);
  28. void display(struct produceItem *);
  29. void readDataFile(struct produceItem **);
  30. void recursiveReverse(struct produceItem **);
  31. char * trim(char *c);
  32. void writeDataFile(struct produceItem *);
  33.  
  34.  
  35. void addProduceItem (struct produceItem **headRef, char *produce, char *type, char *soldBy, float price, int quantity)
  36. {
  37.    
  38.     struct produceItem* temp = (struct produceItem *) malloc(sizeof(struct produceItem));
  39.     strcpy(temp->produce,produce);
  40.     strcpy(temp->type, type);
  41.     strcpy(temp->soldBy, soldBy);
  42.     temp->price = price;
  43.     temp ->quantityInStock = quantity;
  44.     temp->next = (*headRef);
  45.     (*headRef) = temp;
  46. }
  47.  
  48.  
  49. void display(struct produceItem *headRef)
  50. {
  51.     struct produceItem *helper = headRef;
  52.    
  53.     if (helper ==NULL)
  54.     {
  55.         return;
  56.        
  57.     }
  58.     else
  59.     {
  60.         printf("=======================================================\n");
  61.         printf(" Item #    Produce    Type   Sold By    Price  In Stock\n");
  62.         printf("=======================================================\n");
  63.     }
  64.     int counter = 1;
  65.     while(helper !=NULL)
  66.     {
  67.         printf("%5d %3s %-13s %-16s %-13s %6.2f %8d \n",
  68.                counter++, " ", helper->produce, helper->type,
  69.                helper->soldBy, helper->price, helper->quantityInStock);
  70.         helper = helper->next;
  71.        
  72.     }
  73.     printf("\n");
  74.    
  75. }
  76.  
  77.  
  78. void readDataFile(struct produceItem **headRef)
  79. {
  80.    
  81.     const char comma[2] = ",";
  82.     char dataLine[LENGTH];
  83.     char *produce;
  84.     char *type;
  85.     char *soldBy;
  86.     float price;
  87.     int quantityInStock;
  88.     char *fileName = "Assignment8Input.txt";
  89.    
  90.     printf("Trying to open file %s\n", fileName);
  91.     FILE *filePointer = fopen("Assignment8Input.txt", "r");
  92.     if (filePointer == 0) {
  93.         perror("fopen");
  94.         exit(1);
  95.     }
  96.     while (fgets(dataLine, LENGTH, filePointer) !=NULL)
  97.     {
  98.         produce = trim(strtok(dataLine, ","));
  99.         type = trim(strtok(NULL, ","));
  100.         soldBy = trim(strtok(NULL,","));
  101.         price = atof(trim(strtok(NULL,",")));
  102.         quantityInStock = atoi (trim(strtok(NULL,",")));
  103.        
  104.        
  105.         addProduceItem(headRef, produce, type, soldBy, price, quantityInStock);
  106.        
  107.     }
  108.     fclose(filePointer);
  109. }
  110.  
  111.  
  112. void recursiveReverse(struct produceItem** head_ref)
  113. {
  114.     struct produceItem* first;
  115.     struct produceItem* rest;
  116. }
  117.  
  118.  
  119. char * trim(char *c)
  120. {
  121.     char * e = c + strlen(c) - 1;
  122.    
  123.     while(*c && isspace(*c))
  124.         c++;
  125.    
  126.     while(e > c && isspace(*e))
  127.         *e-- = '\0';
  128.    
  129.     return c;
  130. }
  131.  
  132. void menu()
  133. {
  134.     int menu_selection = 0;
  135.     printf("List Operations\n");
  136.     printf("===============\n");
  137.     printf("1. Stock Produce Department\n");
  138.     printf("2. Display Produce Inventory ~ Stack\n");
  139.     printf("3. Display Produce Inventory ~ Queue\n");
  140.     printf("4. Pop 10 Elements Off Stack and Output\n");
  141.     printf("5. Dequeue 15 Elements Off Queue and Output\n");
  142.     printf("6. Exit Program\n");
  143.     printf("Enter your choice:");
  144.     scanf("%d",&menu_selection);
  145.     switch (menu_selection) {
  146.         case 1:
  147.             readDataFile(0);
  148.             break;
  149.         default:
  150.             printf("Invalid Option");
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement