Advertisement
Guest User

zan_nabil

a guest
Jun 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.32 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 DATE TIME NUMBER_OF_SCREWDRIVER\n");
  75.         printf("Receipt format: r DATE TIME 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.             char date[100];
  89.             char time[100];
  90.             scanf("%s",date); //date
  91.             scanf("%s",time); //time
  92.  
  93.             float total_prc = 0.0;
  94.  
  95.             int num_sd = 0; //number of screwdrivers
  96.             scanf("%d",&num_sd);
  97.             float last_c; // last cost to check if two types of items are
  98.             // sold, to update the sale counter properly
  99.  
  100.             stack_len = max(stack_len,0);
  101.             //stack size can't be negative
  102.  
  103.             if(stack_len!=0) // not empty
  104.             {
  105.                 last_c = stack[stack_len].cost;
  106.                 //new type of item discovered
  107.             }
  108.             else
  109.             {
  110.                 //stack is empty
  111.                 printf("NOT AVAILABLE FOR SALE!\n");
  112.  
  113.             }
  114.  
  115.             //enough screwdrivers for sale
  116.             //first price is saved in last_c
  117.             //so, lets delete it, as already sold
  118.             int sold = 1;
  119.             POP(); //1 item sold, delete it from stack
  120.  
  121.             int num_prc = 1; // number of screwdriver of same cost
  122.  
  123.  
  124.  
  125.             while(sold<num_sd) //stack is not empty
  126.                 // and sold items < number of screwdriver --> means
  127.                 // we can sell more
  128.             {
  129.                 if(stack_len<=0)
  130.                     break;
  131.                 float cost = stack[stack_len].cost;
  132.                 if(cost!=last_c) // if both current cost
  133.                     // and last seen costs are not same, that means we are
  134.                     //dealing with different screwdrivers now of different cost
  135.                 {
  136.                     // so, print the updated sales
  137.                     printf("%d SCREWDRIVERS SOLD AT RM %f\n",num_prc,last_c);
  138.                     total_prc += num_prc*last_c;
  139.                     sold++;
  140.                     POP();
  141.                     last_c = cost;
  142.  
  143.                     num_prc = 1; // done updating
  144.                 }
  145.                 else
  146.                 {
  147.                     sold++; // 1 more sold
  148.                     POP(); //delete it from stack
  149.                     num_prc++; // same price
  150.  
  151.                 }
  152.  
  153.  
  154.  
  155.             }
  156.  
  157.             stack_len = max(stack_len,0);
  158.             total_prc += num_prc*last_c;
  159.             if(fabs(total_prc)>0.001) // if only we sold more than 0 RM, then print
  160.             {
  161.                 printf("%d SCREWDRIVERS SOLD AT RM %f\n\n",num_prc,last_c);
  162.  
  163.  
  164.                 printf("SALE -> %f\n", total_prc);
  165.             }
  166.         }
  167.  
  168.         else if(command[0]=='r'
  169.                 || command[0]=='R') //input receipt
  170.         {
  171.             char date[100];
  172.             char time[100];
  173.             scanf("%s",date); //date
  174.             scanf("%s",time); //time
  175.  
  176.  
  177.             float sd_price = 0; //number of screwdrivers
  178.  
  179.             int num_sd = 0; //number of screwdrivers
  180.  
  181.             scanf("%d %f",&num_sd,&sd_price);
  182.  
  183.             for(int sd_now=1; sd_now<=num_sd; sd_now++)
  184.             {
  185.                 PUSH(sd_price);
  186.             }
  187.  
  188.             printf("Receipt Received %d items at %f RM per item\n",num_sd,sd_price);
  189.  
  190.  
  191.             printf("SD in stock now : %d\n",stack_len);
  192.  
  193.         }
  194.  
  195.  
  196.  
  197.     }
  198.  
  199.  
  200.  
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement