Advertisement
Guest User

Untitled

a guest
May 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.86 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.   #include <stdio.h>
  3.   #include <string.h>
  4.   #include <stdlib.h>
  5.  
  6.   struct product {
  7.      unsigned long long id;
  8.      char name[51];
  9.      double price;
  10.      int quantity;
  11.      int day;
  12.      int month;
  13.      int year;
  14.   };
  15.  
  16.   struct products {
  17.       struct product val;    
  18.       struct products *next;
  19.   };
  20.   int comparedate(int day1, int month1, int year1, int day2, int month2, int year2);
  21.   struct products* find_by_id(struct products*, unsigned long long id);
  22.   int isUniqueID(struct products *products, unsigned long long id);
  23.   void delete_by_id(struct products*, unsigned long long id);
  24.   int change_quantity_of_product(struct products*);
  25.   int print_all_expired_products(struct products*);
  26.   int print_product_with_id(struct products*);
  27.   int add_new_product(struct products*);
  28.   int load_products(struct products*);
  29.   int save_products(struct products*);
  30.   int showproducts(struct products*);
  31.   void free_list(struct products*);
  32.  
  33.   int main()
  34.    {
  35.     int option = 0,exit_flag = 1;
  36.     struct products *products = NULL;
  37.     //products = malloc(sizeof(struct products));
  38.   // if (products == NULL)
  39.     //{
  40.     //   printf("Memory allocation error");
  41.     //  return 1;
  42.    // }                        
  43.    // products->next;
  44.     if (load_products(products) == 1)
  45.     {
  46.       printf("Memory allocation error");
  47.       return 1;
  48.     }
  49.  
  50.        while(exit_flag)
  51.     {
  52.         system("@cls||clear");
  53.     printf(" *****************/MENU\\*****************\n");
  54.     printf("*                                        *\n");
  55.     printf("*   1.Add new item in the warehouse      *\n");
  56.     printf("*   2.Change the quantity                *\n");
  57.     printf("*   3.Display the expired items          *\n");
  58.     printf("*   4.Display information for item       *\n");
  59.     printf("*   5.Show products in the warehouse     *\n");
  60.     printf("*   6.Exit                               *\n");
  61.     printf("*                                        *\n");
  62.     printf("******************************************\n");
  63.         printf("Insert option: ");
  64.         if (scanf("%d", &option) == EOF)
  65.         {
  66.             break;
  67.         }
  68.          fflush(stdin);
  69.         switch (option) {
  70.             case 1:
  71.                 if (add_new_product(products)) {
  72.                     return -1;
  73.                 }
  74.                 break;
  75.             case 2:
  76.                 if (change_quantity_of_product(products)) {
  77.                     return -1;
  78.                 }
  79.                 break;
  80.             case 3:
  81.                 if (print_all_expired_products(products)) {
  82.                     return -1;
  83.                 }
  84.                 break;
  85.             case 4:
  86.                 if (print_product_with_id(products)) {
  87.                     return -1;
  88.                 }
  89.                 break;
  90.               case 5:                            
  91.                 if (showproducts(products)) {
  92.                     return ;
  93.                 }
  94.                 break;    
  95.             case 6:
  96.                 exit_flag = 0;
  97.                 break;
  98.             default:
  99.                 break;
  100.         }
  101.         fflush(stdin);
  102.  
  103.       system("pause");
  104.     }
  105.  
  106.    if (save_products(products))
  107.     {
  108.         return 1;
  109.     }
  110.     free_list(products);
  111.     return 0;
  112.  }
  113.     void free_list(struct products *products)
  114.     {
  115.         struct products *curr_item=products;
  116.         while(products!=NULL)
  117.         {
  118.             products=products->next;
  119.             free(curr_item);
  120.             curr_item=products;
  121.         }
  122.     }
  123.    
  124.     int comparedate(int day1, int month1, int year1, int day2, int month2, int year2)
  125. {
  126.     if(year2>year1)
  127.     {
  128.         return 1;
  129.     }
  130.     else if(year2==year1 && month2>month1)
  131.     {
  132.         return 1;
  133.     }
  134.     else if(year2==year1 && month2==month1 && day2>=day1)
  135.     {
  136.         return 1;
  137.     }
  138.     return 0;
  139. }
  140. int print_all_expired_products(struct products *products)
  141.   {
  142.      if(products==NULL)
  143.     {
  144.         printf("List is empty!\n");
  145.         return;
  146.     }
  147.     struct products* current_product;
  148.  
  149.     int day,month,year;
  150.     printf("Enter date(DD/MM/YYYY): ");
  151.     scanf("%d/%d/%d",&day,&month,&year);
  152.     //struct products *iterate_products = products->next;
  153.     for(current_product=products;current_product!=NULL;current_product=current_product->next)
  154.     {
  155.         if(comparedate(current_product->val.day,current_product->val.month,current_product->val.year,day,month,year))
  156.         {
  157.             printf("id: %llu\n", current_product->val.id);
  158.             printf("name: %s\n", current_product->val.name);
  159.             printf("price: %.2f\n",current_product->val.price);
  160.             printf("quantity: %d\n", current_product->val.quantity);
  161.             //printf("date: %s\n", iterate_products->val.date);
  162.         }
  163.     }
  164.         //iterate_products = iterate_products->next;
  165.    
  166.     return 0;
  167.   }
  168.    int isUniqueID(struct products *products, unsigned long long id)
  169.   {  
  170.     while(products)
  171.     {
  172.         if(products->val.id == id)
  173.         {
  174.             printf("ID already exist!\n");
  175.             return 0;
  176.         }    
  177.     products = products->next;
  178.         }
  179.   return 1;
  180.  }
  181.  
  182.    int load_products(struct products *products)
  183.    {
  184.       FILE *fp;
  185.       if ((fp = fopen("data", "rb")) == NULL)
  186.         {
  187.           printf("The file cannot be opened");
  188.           exit(1);
  189.         }
  190.  
  191.       struct products *prev_products = products;
  192.       struct products *curr_products = NULL;
  193.       while (1)
  194.       {
  195.           struct product product;
  196.           if (fread(&product, sizeof(struct product), 1, fp) != 1)
  197.            {
  198.               if (feof(fp))
  199.               {
  200.                   break;
  201.               }
  202.              printf("reading error");
  203.              break;
  204.            }
  205.  
  206.         curr_products = malloc(sizeof(struct products));
  207.         if (curr_products == NULL)
  208.         {
  209.             printf("Memory allocation error");
  210.             return 1;
  211.         }
  212.         curr_products->val = product;
  213.         curr_products->next = NULL;
  214.  
  215.         prev_products->next = curr_products;
  216.         prev_products = curr_products;
  217.     }
  218.       fclose(fp);
  219.       return 0;
  220.   }
  221. int showproducts(struct products* products)
  222. {
  223.     if(products==NULL)
  224.     {
  225.         printf("List is empty!\n");
  226.         return 1;
  227.     }
  228.     struct products* current_product;
  229.     for(current_product=products;current_product!=NULL;current_product=current_product->next)
  230.     {
  231.             printf("id: %llu\n", current_product->val.id);
  232.             printf("name: %s\n", current_product->val.name);
  233.             printf("price: %.2f\n", current_product->val.price);
  234.             printf("quantity: %d\n", current_product->val.quantity);
  235.             printf("date: %d.%d.%d\n", current_product->val.day,current_product->val.month,current_product->val.year);
  236.     }
  237.     return 0;
  238. }
  239.     int save_products(struct products *products)
  240.     {
  241.       FILE *fp  = NULL;
  242.       if ((fp = fopen("data", "ab")) == NULL)
  243.       {
  244.         printf("The file cannot be opened");
  245.        return 2;
  246.       }
  247.        while (1)
  248.       {
  249.          products = products->next;
  250.          if (products == NULL)
  251.          {
  252.             break;
  253.          }
  254.  
  255.         if (fwrite(&products->val, sizeof(struct product), 1, fp) != 1)
  256.         {
  257.             printf("writening error");
  258.             break;
  259.         }
  260.      }
  261.      fclose(fp);
  262.      return 0;
  263.     }
  264.  
  265.     struct products* find_by_id(struct products *products,unsigned long long id)
  266.   {
  267.     struct products *iterate_products = products->next;
  268.     while(1)
  269.     {
  270.         if (iterate_products == NULL)
  271.         {    
  272.             printf("The warehouse is empty.");
  273.             exit(212);
  274.         }
  275.         if (iterate_products->val.id == id)
  276.         {
  277.             return iterate_products;
  278.         }
  279.        iterate_products = iterate_products->next;
  280.     }
  281.     return NULL;
  282.   }
  283.  
  284.    void delete_by_id(struct products *products, unsigned long long id)
  285.   {
  286.       struct products *prev_products = products;
  287.       struct products *iterate_products = products->next;
  288.       while(1)
  289.       {
  290.         if (iterate_products == NULL)
  291.         {
  292.             printf("The warehouse is empty.");
  293.             break;
  294.         }
  295.         if (iterate_products->val.id == id)
  296.         {
  297.             prev_products->next = iterate_products->next;
  298.             break;
  299.         }
  300.         iterate_products = iterate_products->next;
  301.         prev_products = prev_products->next;
  302.       }
  303.   }
  304.  
  305.  
  306.  
  307. int add_new_product(struct products *products)
  308. {  
  309.    struct product product;
  310.    unsigned long long id=0;
  311.    do{
  312.         printf("Insert id: ");
  313.         fflush(stdin);
  314.         scanf("%llu", &product.id);
  315.     }while(!(isUniqueID(products, product.id)) || product.id > 999999999999|| product.id<100000000000);
  316.    
  317.     printf("Insert name: ");
  318.     scanf("%50s",product.name);
  319.     printf("Insert price: ");
  320.     scanf("%lf", &product.price);
  321.     printf("Insert quantity: ");
  322.     scanf("%d", &product.quantity);;
  323.     printf("Insert date /in dd.mm.yyyy format/: ");
  324.     scanf("%d/%d/%d",&product.day,&product.month,&product.year);
  325.     if(product.month<1 || product.month>12 || product.day<1 || product.day>31 || product.year<0)
  326.     {
  327.         printf("The date is incorrect.");
  328.         return 1;
  329.     }
  330.                    
  331.     struct products *last_products = products;
  332.     while(last_products->next)
  333.     {
  334.         last_products = last_products->next;
  335.     }
  336.    
  337.     struct products *next_products = NULL;
  338.     next_products = malloc(sizeof(struct products));
  339.     if (next_products==NULL)
  340.     {
  341.         printf("Memory allocation error.");
  342.         return 1;
  343.     }
  344.     next_products->val = product;
  345.     next_products->next = NULL;
  346.     last_products->next = next_products;
  347.     return 0;
  348. }
  349.  
  350. int print_product_with_id(struct products *products)
  351.  {
  352.     unsigned long long id=0;
  353.     printf("Insert id: ");
  354.     scanf("%llu", &id);
  355.  
  356.     struct products *products_with_id = find_by_id(products, id);
  357.     if (products_with_id == NULL)
  358.         printf("Missing product!\n");
  359.     else
  360.     {
  361.         printf("id: %llu\n", products_with_id->val.id);
  362.         printf("name: %s\n", products_with_id->val.name);
  363.         printf("price: %.2f\n", products_with_id->val.price);
  364.         printf("quantity: %d\n", products_with_id->val.quantity);
  365.         printf("date: %.2d/%.2d/%.4d;\n", products_with_id->val.day,products_with_id->val.month,products_with_id->val.year);
  366.     }
  367.     return 0;
  368.    }
  369.  
  370.    int change_quantity_of_product(struct products *products)
  371. {
  372.     unsigned long long id=0;int quantity=0;
  373.     printf("Insert id: ");
  374.     scanf(" %llu", &id);
  375.     printf("Insert quantity to change[(+)for adding and (-)for subtraction]:");
  376.     scanf("%d", &quantity);
  377.     struct products *products_with_id = find_by_id(products, id);
  378.     if (products_with_id == NULL)
  379.     printf("Missing product!\n");
  380.     else
  381.     {
  382.        int new_quantity = products_with_id->val.quantity + quantity;
  383.        if (new_quantity < 0)
  384.              printf("Quantity to change is bigger than product quantity!\n");
  385.        else if (new_quantity > 0)
  386.                products_with_id->val.quantity = new_quantity;
  387.        else if (new_quantity == 0)
  388.                delete_by_id(products, id);
  389.     }
  390.  return 0;
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement