Advertisement
Guest User

zan_nabil

a guest
Jun 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.04 KB | None | 0 0
  1. // Jubayer Al Nazi
  2. // Code with comments (documentation)
  3.  
  4. // include statements, header files
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11.  
  12.  
  13. //constants
  14. #define SIZ 1000  // maximum size of stack
  15.  
  16. int stack_len; // this variable contains the current
  17. //length of the stack, so if it's 0 means stack is empty
  18. //I have used 1 based indexing, meaning in my stack
  19. //stack[0] is empty, there's nothing
  20. //useful screwdriver price is kept from index 1
  21. //stack[1] <- price of screwdriver 1
  22.  
  23. struct data{
  24.  
  25. float cost; // this struct object contains the cost of sd
  26.  
  27. };
  28. struct data stack[SIZ+1]; // stack to contain prices of
  29. //individual screwdriver, maximum 1000 screwdrivers
  30.  
  31.  
  32. char command[100]; // a string, each time the command query will be saved
  33. // in here, input by the sales manager
  34. int max(int a, int b)
  35. {
  36.     if(a>=b)
  37.         return a;
  38.     return b;
  39.  
  40.  
  41. }
  42. void PUSH(float price) // take a price and
  43. {
  44.     // add it to the stack from behind
  45.     stack_len++; // a new item added to stack, so increase length by 1
  46.     stack[stack_len].cost = price; // keep the price in stack
  47.  
  48. }
  49. float POP()
  50. {
  51.     double ret = stack[stack_len].cost; //return the last
  52.     //element of stack
  53.     stack_len--; // decrease the stack size by 1
  54.     // so the last element is deleted now
  55.     return ret;
  56.  
  57. }
  58.  
  59.  
  60.  
  61. // Main program
  62. int main()
  63. {
  64.  
  65.  
  66.     //global variables are initialized as 0, so
  67.     //stack_len = 0, initially, meaning empty stack
  68.  
  69.     int exit_prog = 0; // when it's true, we
  70.     //will exit
  71.     printf("Start of Program\n");
  72.     for(;exit_prog==0;)
  73.     {
  74.         printf("\n\nSALE Format: s NUMBER_OF_SCREWDRIVER\n");
  75.         printf("Receipt format: r NUMBER_OF_SCREWDRIVER PRICE_OF_EACH_ITEM\n");
  76.         printf("Exit format: e\n\n\n");
  77.  
  78.         scanf("%s",command); // take input from console
  79.  
  80.         if(strcmp(command,"e")==0) // if input is equal to 'e'
  81.         {
  82.             exit_prog = 1;
  83.             //time to exit
  84.         }
  85.         else if(command[0]=='s' // sale report input
  86.                 || command[0]=='S')
  87.         {
  88.             float total_prc = 0.0;
  89.  
  90.             int num_sd = 0; //number of screwdrivers
  91.             scanf("%d",&num_sd);
  92.             float last_c; // last cost to check if two types of items are
  93.             // sold, to update the sale counter properly
  94.  
  95.             stack_len = max(stack_len,0);
  96.             //stack size can't be negative
  97.  
  98.             if(stack_len!=0) // not empty
  99.             {
  100.                 last_c = stack[stack_len].cost;
  101.                 //new type of item discovered
  102.             }
  103.             else
  104.             {
  105.                 //stack is empty
  106.                 printf("NOT AVAILABLE FOR SALE!\n");
  107.  
  108.             }
  109.  
  110.             //enough screwdrivers for sale
  111.             //first price is saved in last_c
  112.             //so, lets delete it, as already sold
  113.             int sold = 1;
  114.             POP(); //1 item sold, delete it from stack
  115.  
  116.             int num_prc = 1; // number of screwdriver of same cost
  117.  
  118.  
  119.  
  120.             while(sold<num_sd) //stack is not empty
  121.                 // and sold items < number of screwdriver --> means
  122.                 // we can sell more
  123.             {
  124.                 if(stack_len<=0)
  125.                     break;
  126.                 float cost = stack[stack_len].cost;
  127.                 if(cost!=last_c) // if both current cost
  128.                     // and last seen costs are not same, that means we are
  129.                     //dealing with different screwdrivers now of different cost
  130.                 {
  131.                     // so, print the updated sales
  132.                     printf("%d SCREWDRIVERS SOLD AT RM %f\n",num_prc,last_c);
  133.                     total_prc += num_prc*last_c;
  134.                     sold++;
  135.                     POP();
  136.                     last_c = cost;
  137.  
  138.                     num_prc = 1; // done updating
  139.                 }
  140.                 else
  141.                 {
  142.                     sold++; // 1 more sold
  143.                     POP(); //delete it from stack
  144.                     num_prc++; // same price
  145.  
  146.                 }
  147.  
  148.  
  149.  
  150.             }
  151.  
  152.             stack_len = max(stack_len,0);
  153.             total_prc += num_prc*last_c;
  154.             if(fabs(total_prc)>0.001) // if only we sold more than 0 RM, then print
  155.             {
  156.                 printf("%d SCREWDRIVERS SOLD AT RM %f\n\n",num_prc,last_c);
  157.  
  158.  
  159.                 printf("SALE -> %f\n", total_prc);
  160.             }
  161.         }
  162.  
  163.         else if(command[0]=='r'
  164.                 || command[0]=='R') //input receipt
  165.         {
  166.  
  167.  
  168.             float sd_price = 0; //number of screwdrivers
  169.  
  170.             int num_sd = 0; //number of screwdrivers
  171.  
  172.             scanf("%d %f",&num_sd,&sd_price);
  173.  
  174.             for(int sd_now=1; sd_now<=num_sd; sd_now++)
  175.             {
  176.                 PUSH(sd_price);
  177.             }
  178.  
  179.             printf("Receipt Received %d items at %f RM per item\n",num_sd,sd_price);
  180.  
  181.  
  182.             printf("SD in stock now : %d\n",stack_len);
  183.  
  184.         }
  185.  
  186.  
  187.  
  188.     }
  189.  
  190.  
  191.  
  192.     return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement