G2A Many GEOs
SHARE
TWEET

Untitled

a guest Mar 31st, 2020 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. struct inventory_item
  7. {
  8.     char productName[50];
  9.     int quantity;
  10.     float price;
  11.     char id[9];
  12.     char date[15];
  13. };
  14.  
  15.  
  16. int readFile(char filename[], struct inventory_item inventory_list[]);
  17. int getTotalItems(struct inventory_item inventory_list[], int count);
  18. double getTotalVal(struct inventory_item inventory_list[], int count);
  19.  
  20. void printItem(struct inventory_item item, struct inventory_item inventory_list[], int count)
  21. {
  22.         printf("Items that need to be ordered:\n");
  23.     for(int j = 0; j < count; j++)
  24.     {
  25.         if (inventory_list[j].quantity < 10)
  26.         {
  27.             num = 10 - inventory_list[j].quantity;
  28.             printf("%d: %s\n", inventory_list[j].id, inventory_list[j].productName);
  29.         }
  30.         else
  31.         {
  32.             break;
  33.         }
  34.     }
  35. }
  36.  
  37. int main(int argc, char *argv[])
  38. {
  39.     if (argv[1] == NULL)
  40.     {
  41.         printf("Error: file not specified\n Format: ./a.out <input-file>\n");
  42.         return 0;
  43.     }
  44.     if (argc == 2)
  45.     {
  46.         struct inventory_item inventory_list[1000];
  47.         int count = readFile(argv[1], inventory_list);
  48.         int totalNumItem = getTotalItems(inventory_list, count);
  49.         float totalVal = getTotalVal(inventory_list, count);
  50.         printf("Total inventory value: $%.2f\n", totalVal);
  51.         printf("Total inventory quantity: %d\n", totalNumItem);
  52.     }
  53.     if (argc != 2)
  54.     {
  55.         printf("Error: Unable to open file %s/n", argv[1]);
  56.     }
  57.     return 0;
  58. }
  59.  
  60. int readFile(char filename[], struct inventory_item inventory_list[])
  61. {
  62.     int i = 0;
  63.     int numberItems = 0;
  64.     FILE * ifp = fopen(filename, "r");
  65.  
  66.     fscanf(ifp, "%d", &numberItems);
  67.     printf("%d\n", numberItems);
  68.  
  69.     for(i = 0; i < numberItems; i++)
  70.     {
  71.         fscanf(ifp, "%s", &inventory_list[i].productName);
  72.         printf("%s\n", inventory_list[i].productName);
  73.  
  74.         fscanf(ifp, "%d", &inventory_list[i].quantity);
  75.         printf("%d\n", inventory_list[i].quantity);
  76.  
  77.         fscanf(ifp, "%f", &inventory_list[i].price);
  78.         printf("%.2f\n", inventory_list[i].price);
  79.  
  80.         fscanf(ifp, "%s", &inventory_list[i].id);
  81.         printf("%s\n", inventory_list[i].id);
  82.  
  83.         fscanf(ifp, "%s", &inventory_list[i].date);
  84.         printf("%s\n", inventory_list[i].date);
  85.     }
  86.  
  87.     fclose(ifp);
  88.     return numberItems;
  89. }
  90.  
  91. int getTotalItems(struct inventory_item inventory_list[], int count)
  92. {
  93.     int totalproduct = 0;
  94.     for(int i = 0; i < count; i++)
  95.     {
  96.         printf("%d\n", inventory_list[i].quantity);
  97.         totalproduct += inventory_list[i].quantity;
  98.     }
  99.     return totalproduct;
  100. }
  101.  
  102. double getTotalVal(struct inventory_item inventory_list[], int count)
  103. {
  104.     int quantityTemp;
  105.     int num;
  106.     float priceTemp;
  107.     float priceTotal;
  108.     float temp;
  109.  
  110.     for(int i = 0; i < count; i++)
  111.     {
  112.         quantityTemp = inventory_list[i].quantity;
  113.         priceTemp = inventory_list[i].price;
  114.         temp = quantityTemp * priceTemp;
  115.         priceTotal += temp;
  116.     }
  117.  
  118.     return priceTotal;
  119. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top