Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- struct Product
- {
- char name[50];
- int type;
- double weight;
- double price;
- int quantity;
- };
- enum type
- {
- food,
- drink
- };
- int main()
- {
- const int maxProductPerCell = 3;
- printf("Enter X:");
- int x;
- scanf ("%d",&x);
- printf("Enter Y:");
- int y;
- scanf ("%d",&y);
- printf("\nVending Machine size is %dx%d\n\n", x,y);
- struct Product machine[x][y];
- for(int i = 0; i < x; i++)
- {
- for(int j = 0; j < y; j++)
- {
- struct Product temp;
- printf("Enter product name:");
- scanf("%s", temp.name);
- printf("Enter product type (food or drink):");
- char type[20];
- scanf("%s", type);
- while (strcmp(type, "food") != 0 && strcmp(type, "drink") != 0)
- {
- printf("Error try again (type food or drink):");
- scanf("%s", type);
- }
- if(strcmp(type, "food") == 0)
- {
- temp.type = food;
- }
- if(strcmp(type, "drink") == 0)
- {
- temp.type = drink;
- }
- printf("Enter product weight:");
- scanf("%lf", &temp.weight);
- printf("Enter product price:");
- scanf("%lf", &temp.price);
- printf("Enter product quantity (max 3):");
- scanf("%d", &temp.quantity);
- while (temp.quantity > maxProductPerCell)
- {
- printf("Error max 3 element per Cell try again:");
- scanf("%d", &temp.quantity);
- }
- machine[i][j] = temp;
- if(j < y - 1)
- {
- printf("\nNext Product\n");
- }
- }
- if(i < x - 1)
- {
- printf("\nNext Product\n");
- }
- }
- //Sorting Algorithm
- for(int i = 0; i < x * y; i++)
- {
- for(int j = i + 1; j < x * y; j++)
- {
- if(machine[i / x][i % x].type > machine[j / x][j % x].type ||
- (machine[i / x][i % x].type == machine[j / x][j % x].type &&
- machine[i / x][i % x].weight > machine[j / x][j % x].weight)){
- struct Product temp = machine[j / x][j % x];
- machine[j / x][j % x] = machine[i / x][i % x];
- machine[i / x][i % x] = temp;
- i--;
- break;
- }
- }
- }
- //PRINT
- printf("\n\n\n|----VENDING MACHINE----|\n");
- for(int i = 0; i < x; i++)
- {
- for(int j = 0; j < y; j++)
- {
- printf("|%s(x%d)| ", machine[i][j].name, machine[i][j].quantity);
- }
- printf("\n");
- }
- printf("|-----------------------|\n\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement