Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. /*Menu Macros:*/
  7. #define OPTION_ADD 1
  8. #define OPTION_LIST 2
  9. #define OPTION_DISPLAY_TOTAL_VALUE 3
  10. #define OPTION_SEARCH_ID 4
  11. #define OPTION_SEARCH_NAME 5
  12. #define OPTION_DISPLAY_CHEAPEST 6
  13. #define OPTION_DISPLAY_NEED_SUPPLY 7
  14. #define OPTION_MODIFY_PRICE 8
  15. #define OPTION_DELETE 9
  16. #define OPTION_EXIT 10
  17.  
  18. /*Max buffer macro*/
  19. #define MAX_BUFFER 256
  20.  
  21. /*File name*/
  22. #define FILE_NAME "input.txt"
  23.  
  24. typedef struct{
  25.     int id;
  26.     char name[32];
  27.     char features[32];
  28.     int quantity;
  29.     int price;
  30. }product;
  31.  
  32.  
  33.  
  34. /*Header area*/
  35. void menu();
  36. void add();
  37.  
  38. /*Main function*/
  39. int main(){
  40.     menu();
  41.     return 0;
  42. }
  43.  
  44. /*Implementation area*/
  45. void add(){
  46.     char buffer[32];
  47.     product p;
  48.  
  49.     printf("Please add an id:\n");
  50.     fseek(stdin, 0, SEEK_END);
  51.     scanf("%d",&(p.id));
  52.  
  53.     printf("Please add a name:\n");
  54.     fseek(stdin, 0, SEEK_END);
  55.     fgets(buffer,32,stdin);
  56.     buffer[strlen(buffer)-1]='\0';
  57.     strncpy(p.name,buffer,32);
  58.  
  59.     printf("Please add the features of the product:\n");
  60.     fseek(stdin, 0, SEEK_END);
  61.     fgets(buffer,32,stdin);
  62.     buffer[strlen(buffer)-1]='\0';
  63.     strncpy(p.features,buffer,32);
  64.  
  65.     printf("Please add a quantity:\n");
  66.     fseek(stdin, 0, SEEK_END);
  67.     scanf("%d",&(p.quantity));
  68.  
  69.     printf("Please add a price:\n");
  70.     fseek(stdin, 0, SEEK_END);
  71.     scanf("%d",&(p.price));
  72.  
  73.  
  74.     FILE *out=fopen(FILE_NAME,"a");
  75.     fprintf(out,"%d,%s,%s,%d,%d\n",p.id,p.name,p.features,p.quantity,p.price);
  76.     fclose(out);
  77. }
  78.  
  79. void list(){
  80.     FILE *in=fopen(FILE_NAME,"r");
  81.     char buffer[256];
  82.     char *ptr;
  83.     while(fgets(buffer,256,in)!=NULL){
  84.         ptr=strtok(buffer,",");
  85.         printf("ID:%s\n",ptr);
  86.         ptr=strtok(NULL,",");
  87.         printf("Name:%s\n",ptr);
  88.         ptr=strtok(NULL,",");
  89.         printf("Features:%s\n",ptr);
  90.         ptr=strtok(NULL,",");
  91.         printf("Quantity:%s\n",ptr);
  92.         ptr=strtok(NULL,",");
  93.         printf("Price:$%s\n\n",ptr);
  94.     }
  95.     fclose(in);
  96. }
  97.  
  98. void total_value(){
  99.     FILE *in=fopen(FILE_NAME,"r");
  100.     char buffer[256];
  101.     char *ptr;
  102.     int total=0;
  103.     int aux_q,aux_p;
  104.     while(fgets(buffer,256,in)!=NULL){
  105.         ptr=strtok(buffer,",");
  106.         ptr=strtok(NULL,",");
  107.         ptr=strtok(NULL,",");
  108.         ptr=strtok(NULL,",");
  109.         aux_q=atoi(ptr);
  110.         ptr=strtok(NULL,",");
  111.         aux_p=atoi(ptr);
  112.         total+=aux_p*aux_q;
  113.     }
  114.     printf("Total price:%d\n",total);
  115.     fclose(in);
  116. }
  117.  
  118. void menu(){
  119.     char *buffer=malloc(MAX_BUFFER*sizeof(char));
  120.     int option=-1;
  121.     do{
  122.         printf("%d. Add a product\n",OPTION_ADD);
  123.         printf("%d. List products\n",OPTION_LIST);
  124.         printf("%d. Display total value\n",OPTION_DISPLAY_TOTAL_VALUE);
  125.         printf("%d. Search by id\n",OPTION_SEARCH_ID);
  126.         printf("%d. Search by name\n",OPTION_SEARCH_NAME);
  127.         printf("%d. Display the cheapest\n",OPTION_DISPLAY_CHEAPEST);
  128.         printf("%d. Display the ones that need to be resupplied\n",OPTION_DISPLAY_NEED_SUPPLY);
  129.         printf("%d. Modify the price/quantity\n",OPTION_MODIFY_PRICE);
  130.         printf("%d. Delete product\n",OPTION_DELETE);
  131.         printf("%d. Exit\n",OPTION_EXIT);
  132.  
  133.         fseek(stdin, 0, SEEK_END);
  134.         fgets(buffer,MAX_BUFFER,stdin);
  135.         option=atoi(buffer);
  136.         switch(option){
  137.             case OPTION_ADD:
  138.                 printf("Adding a new product...\n");
  139.                 add();
  140.                 break;
  141.             case OPTION_LIST:
  142.                 printf("Listing products...\n");
  143.                 list();
  144.                 break;
  145.             case OPTION_DISPLAY_TOTAL_VALUE:
  146.                 printf("Displaying the total value...\n");
  147.                 total_value();
  148.                 break;
  149.             case OPTION_SEARCH_ID:
  150.                 printf("Searching by id...\n");
  151.                 break;
  152.             case OPTION_SEARCH_NAME:
  153.                 printf("Searching by name...\n");
  154.                 break;
  155.             case OPTION_DISPLAY_CHEAPEST:
  156.                 printf("Displaying the cheapest product...\n");
  157.                 break;
  158.             case OPTION_DISPLAY_NEED_SUPPLY:
  159.                 printf("Displaying the needed supplies...\n");
  160.                 break;
  161.             case OPTION_MODIFY_PRICE:
  162.                 printf("Modifying the price...\n");
  163.                 break;
  164.             case OPTION_DELETE:
  165.                 printf("Deleting a product...\n");
  166.                 break;
  167.             case OPTION_EXIT:
  168.                 printf("Exiting...\n");
  169.                 break;
  170.             default:
  171.                 printf("Please Enter a valid option!\n");
  172.         }
  173.     }while(option!=OPTION_EXIT);
  174.     free(buffer);
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement