Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define STACK_SIZE 20
- void push(int* stack, int* top, int element);
- int pop(int* stack, int* top);
- bool is_empty(int* top);
- bool is_full(int* top);
- void stack_overflow(void);
- void stack_underflow(void);
- int evaluate(int i, int j, int k);
- int main(void){
- int top1=0,top2=0;
- int *top_operators=&top1;
- int *top_operands=&top2;
- int stack_operands[STACK_SIZE];
- int stack_operators[STACK_SIZE];
- int *operands = &stack_operands[0];
- int *operators = &stack_operators[0];
- char next;
- int temp, temp1, result;
- scanf("%c", &next);
- while(next!='='){
- if(next=='+' || next=='-' || next=='*' || next=='/')
- {
- push(operators,top_operators,next);
- printf("first");
- }
- else if(isdigit(next)){
- printf("a");
- //temp=next-'0';
- push(operands,top_operands,next);
- printf("%d\n", pop(operands,top_operands));
- push(operands,top_operands,next);
- }
- else if(next==')'){
- printf("b");
- temp=pop(operands,top_operands);
- temp1=pop(operands,top_operands);
- result=evaluate(temp,temp1,pop(operators,top_operators));
- push(operands,top_operands,result);
- }
- else if(next!='('){
- printf("c");
- printf("Invalid input.");
- exit(EXIT_FAILURE);
- }
- scanf("%c",&next);
- }
- printf("hi");
- //result=pop(operands,top_operands);
- if(!is_empty(top_operands) || !is_empty(top_operators)){
- printf("d");
- printf("Invalid input.");
- exit(EXIT_FAILURE);
- }
- else{
- printf("boo");
- printf("%d\n",result);
- printf("e");
- return(0);
- }
- }
- void push(int* stack, int* top, int element){
- if(is_full(top))
- stack_overflow();
- else
- {
- *(stack+(*top++))=element;
- printf("q");
- }
- }
- int pop(int* stack, int* top){
- if(is_empty(top))
- stack_underflow();
- else
- return *(stack+(--*top));
- }
- bool is_empty(int* top){
- return *top==0;
- }
- bool is_full(int* top){
- return *top == STACK_SIZE;
- }
- void stack_overflow(void){
- printf("Error. Stack overflow.\n");
- exit(EXIT_FAILURE);
- }
- void stack_underflow(void){
- printf("Error. Stack underflow.\n");
- exit(EXIT_FAILURE);
- }
- int evaluate(int i, int j, int k){
- int result;
- if(k=='+')
- result=(i+j);
- else if(k=='-')
- result=(i-j);
- else if(k=='*')
- result=(i*j);
- else
- result=(i/j);
- return result;
- }
Add Comment
Please, Sign In to add comment