Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<math.h>
  5.  
  6. typedef struct {
  7.     int cost;
  8.     int weight;
  9.     char description[19];
  10. }Bag;
  11.  
  12. typedef struct Node {
  13.     Bag bag;
  14.     Node *link;
  15. }Node;
  16.  
  17. Node *myStack[1000];
  18.  
  19. void push(Bag inBag, int location) {
  20.     Node *ptr = new Node;
  21.     ptr->bag = inBag;
  22.    
  23.     if (myStack[location] != NULL) {
  24.         ptr->link = myStack[location];
  25.         myStack[location] = ptr;
  26.     }
  27.     else {
  28.         myStack[location] = ptr;
  29.     }
  30. }
  31.  
  32. void pop(int location) {
  33.     if (myStack[location] == NULL) {
  34.         printf("Stack is empty at location %d", location);
  35.     }
  36.     else {
  37.         Node *ptr = myStack[location];
  38.         myStack[location] = myStack[location]->link;
  39.         free(ptr);
  40.         ptr = NULL;
  41.     }
  42. }
  43.  
  44. int main() {
  45.     while (true) {
  46.         //-1 = add a bag
  47.         //0 = exit
  48.         //1 = buy a bag
  49.         int updateType;
  50.         scanf("%d", &updateType);
  51.  
  52.         if (updateType == -1) {
  53.             // if -1, customer is buying
  54.             // Take 3 positive ints k, m, c
  55.             // k = location
  56.             // m = cost
  57.             // c = carrying capacity
  58.             int k, m, c;
  59.             scanf("%d %d %d", &k, &m, &c);
  60.  
  61.             // Look at the top of the stack and see if
  62.             // customer can carry and afford top bag
  63.             // if so, take the top bag
  64.  
  65.             int p = 0;
  66.             while (myStack[k] != NULL) {
  67.                 Bag myBag = myStack[k]->bag;
  68.                 if (myBag.cost < m && myBag.weight < c) {
  69.                     push(myBag, -1); //use -1 for temp storage
  70.                     p += myBag.cost;
  71.                     m -= myBag.cost;
  72.                     c -= myBag.weight;
  73.                     pop(k);
  74.                 }
  75.                 else {
  76.                     break;
  77.                 }
  78.             }
  79.  
  80.             printf("%d ", p);
  81.             while (myStack[-1] != NULL) {
  82.                 printf("%s ", myStack[-1]->bag.description);
  83.                 pop(-1);
  84.             }
  85.         }
  86.         else if (updateType == 1) {
  87.             //if 1, customer is dropping off
  88.            // Take 2 positive ints k and n
  89.            // k = location
  90.            // n = # of bags
  91.             int k, n;
  92.             scanf("%d %d", &k, &n);
  93.  
  94.             // for n # of times, get ints m and w and string description
  95.             // m = cost
  96.             // w = weight
  97.             // for each bag, add to stack
  98.             for (int i = 0; i < n; i++) {
  99.                 int m = 0, w = 0;
  100.                 char desc[19];
  101.  
  102.                 scanf("%d %d %s", &m, &w, &desc);
  103.  
  104.                 Bag newBag;
  105.                 newBag.cost = m;
  106.                 newBag.weight = w;
  107.                 strcpy(newBag.description, desc);
  108.  
  109.                 push(newBag, k);
  110.             }
  111.         }
  112.         else if (updateType == 0) {
  113.             break;
  114.         }
  115.  
  116.         else {
  117.             printf("Input can only be -1,0,1");
  118.         }
  119.     }
  120.     return EXIT_SUCCESS;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement