Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct Product
  5. {
  6.    char  name[50];
  7.    int  type;
  8.    double  weight;
  9.    double  price;
  10.    int quantity;
  11. };
  12.  
  13. enum type
  14. {
  15.     food,
  16.     drink
  17. };
  18.  
  19. int main()
  20. {
  21.     const int maxProductPerCell = 3;
  22.  
  23.     printf("Enter X:");
  24.     int x;
  25.     scanf ("%d",&x);
  26.  
  27.     printf("Enter Y:");
  28.     int y;
  29.     scanf ("%d",&y);
  30.  
  31.     printf("\nVending Machine size is %dx%d\n\n", x,y);
  32.  
  33.     struct Product machine[x][y];
  34.  
  35.     for(int i = 0; i < x; i++)
  36.     {
  37.         for(int j = 0; j < y; j++)
  38.         {
  39.             struct Product temp;
  40.             printf("Enter product name:");
  41.             scanf("%s", temp.name);
  42.  
  43.             printf("Enter product type (food or drink):");
  44.             char type[20];
  45.             scanf("%s", type);
  46.  
  47.             while (strcmp(type, "food") != 0 && strcmp(type, "drink") != 0)
  48.             {
  49.                 printf("Error try again (type food or drink):");
  50.                 scanf("%s", type);
  51.             }
  52.  
  53.             if(strcmp(type, "food") == 0)
  54.             {
  55.                 temp.type = food;
  56.             }
  57.             if(strcmp(type, "drink") == 0)
  58.             {
  59.                 temp.type = drink;
  60.             }
  61.  
  62.             printf("Enter product weight:");
  63.             scanf("%lf", &temp.weight);
  64.  
  65.             printf("Enter product price:");
  66.             scanf("%lf", &temp.price);
  67.  
  68.             printf("Enter product quantity (max 3):");
  69.             scanf("%d", &temp.quantity);
  70.  
  71.             while (temp.quantity > maxProductPerCell)
  72.             {
  73.                 printf("Error max 3 element per Cell try again:");
  74.                 scanf("%d", &temp.quantity);
  75.             }
  76.  
  77.             machine[i][j] = temp;
  78.  
  79.             if(j < y - 1)
  80.             {
  81.                 printf("\nNext Product\n");
  82.             }
  83.         }
  84.  
  85.         if(i < x - 1)
  86.         {
  87.             printf("\nNext Product\n");
  88.         }
  89.     }
  90.  
  91.     //Sorting Algorithm
  92.     for(int i = 0; i < x * y; i++)
  93.     {
  94.         for(int j = i + 1; j < x * y; j++)
  95.         {
  96.             if(machine[i / x][i % x].type > machine[j / x][j % x].type ||
  97.                 (machine[i / x][i % x].type == machine[j / x][j % x].type &&
  98.                  machine[i / x][i % x].weight > machine[j / x][j % x].weight)){
  99.                 struct Product temp = machine[j / x][j % x];
  100.                 machine[j / x][j % x] = machine[i / x][i % x];
  101.                 machine[i / x][i % x] = temp;
  102.                 i--;
  103.                 break;
  104.             }
  105.         }
  106.     }
  107.  
  108.  
  109.     //PRINT
  110.     printf("\n\n\n|----VENDING MACHINE----|\n");
  111.     for(int i = 0; i < x; i++)
  112.     {
  113.         for(int j = 0; j < y; j++)
  114.         {
  115.             printf("|%s(x%d)| ", machine[i][j].name, machine[i][j].quantity);
  116.         }
  117.         printf("\n");
  118.     }
  119.     printf("|-----------------------|\n\n\n");
  120.  
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement