Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Jubayer Al Nazi
- // Code with comments (documentation)
- // include statements, header files
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- //constants
- #define SIZ 1000 // maximum size of stack
- int stack_len; // this variable contains the current
- //length of the stack, so if it's 0 means stack is empty
- //I have used 1 based indexing, meaning in my stack
- //stack[0] is empty, there's nothing
- //useful screwdriver price is kept from index 1
- //stack[1] <- price of screwdriver 1
- struct data{
- float cost; // this struct object contains the cost of sd
- };
- struct data stack[SIZ+1]; // stack to contain prices of
- //individual screwdriver, maximum 1000 screwdrivers
- char command[100]; // a string, each time the command query will be saved
- // in here, input by the sales manager
- int max(int a, int b)
- {
- if(a>=b)
- return a;
- return b;
- }
- void PUSH(float price) // take a price and
- {
- // add it to the stack from behind
- stack_len++; // a new item added to stack, so increase length by 1
- stack[stack_len].cost = price; // keep the price in stack
- }
- float POP()
- {
- double ret = stack[stack_len].cost; //return the last
- //element of stack
- stack_len--; // decrease the stack size by 1
- // so the last element is deleted now
- return ret;
- }
- // Main program
- int main()
- {
- //global variables are initialized as 0, so
- //stack_len = 0, initially, meaning empty stack
- int exit_prog = 0; // when it's true, we
- //will exit
- printf("Start of Program\n");
- for(;exit_prog==0;)
- {
- printf("\n\nSALE Format: s NUMBER_OF_SCREWDRIVER\n");
- printf("Receipt format: r NUMBER_OF_SCREWDRIVER PRICE_OF_EACH_ITEM\n");
- printf("Exit format: e\n\n\n");
- scanf("%s",command); // take input from console
- if(strcmp(command,"e")==0) // if input is equal to 'e'
- {
- exit_prog = 1;
- //time to exit
- }
- else if(command[0]=='s' // sale report input
- || command[0]=='S')
- {
- float total_prc = 0.0;
- int num_sd = 0; //number of screwdrivers
- scanf("%d",&num_sd);
- float last_c; // last cost to check if two types of items are
- // sold, to update the sale counter properly
- stack_len = max(stack_len,0);
- //stack size can't be negative
- if(stack_len!=0) // not empty
- {
- last_c = stack[stack_len].cost;
- //new type of item discovered
- }
- else
- {
- //stack is empty
- printf("NOT AVAILABLE FOR SALE!\n");
- }
- //enough screwdrivers for sale
- //first price is saved in last_c
- //so, lets delete it, as already sold
- int sold = 1;
- POP(); //1 item sold, delete it from stack
- int num_prc = 1; // number of screwdriver of same cost
- while(sold<num_sd) //stack is not empty
- // and sold items < number of screwdriver --> means
- // we can sell more
- {
- if(stack_len<=0)
- break;
- float cost = stack[stack_len].cost;
- if(cost!=last_c) // if both current cost
- // and last seen costs are not same, that means we are
- //dealing with different screwdrivers now of different cost
- {
- // so, print the updated sales
- printf("%d SCREWDRIVERS SOLD AT RM %f\n",num_prc,last_c);
- total_prc += num_prc*last_c;
- sold++;
- POP();
- last_c = cost;
- num_prc = 1; // done updating
- }
- else
- {
- sold++; // 1 more sold
- POP(); //delete it from stack
- num_prc++; // same price
- }
- }
- stack_len = max(stack_len,0);
- total_prc += num_prc*last_c;
- if(fabs(total_prc)>0.001) // if only we sold more than 0 RM, then print
- {
- printf("%d SCREWDRIVERS SOLD AT RM %f\n\n",num_prc,last_c);
- printf("SALE -> %f\n", total_prc);
- }
- }
- else if(command[0]=='r'
- || command[0]=='R') //input receipt
- {
- float sd_price = 0; //number of screwdrivers
- int num_sd = 0; //number of screwdrivers
- scanf("%d %f",&num_sd,&sd_price);
- for(int sd_now=1; sd_now<=num_sd; sd_now++)
- {
- PUSH(sd_price);
- }
- printf("Receipt Received %d items at %f RM per item\n",num_sd,sd_price);
- printf("SD in stock now : %d\n",stack_len);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement