daily pastebin goal
5%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. #define STACK_SIZE 20
  7.  
  8. void push(int* stack, int* top, int element);
  9. int pop(int* stack, int* top);
  10. bool is_empty(int* top);
  11. bool is_full(int* top);
  12. void stack_overflow(void);
  13. void stack_underflow(void);
  14. int evaluate(int i, int j, int k);
  15.  
  16. int main(void){
  17.     int top1=0,top2=0;
  18.   int *top_operators=&top1;
  19.   int *top_operands=&top2;
  20.   int stack_operands[STACK_SIZE];
  21.   int stack_operators[STACK_SIZE];
  22.   int *operands = &stack_operands[0];
  23.   int *operators = &stack_operators[0];
  24.   char next;
  25.   int temp, temp1, result;
  26.  
  27.   scanf("%c", &next);
  28.   while(next!='='){
  29.     if(next=='+' || next=='-' || next=='*' || next=='/')
  30.     {
  31.         push(operators,top_operators,next);
  32.         printf("first");
  33.     }
  34.  
  35.     else if(isdigit(next)){
  36.         printf("a");
  37.       //temp=next-'0';
  38.       push(operands,top_operands,next);
  39.       printf("%d\n", pop(operands,top_operands));
  40.       push(operands,top_operands,next);
  41.     }
  42.     else if(next==')'){
  43.         printf("b");
  44.       temp=pop(operands,top_operands);
  45.       temp1=pop(operands,top_operands);
  46.       result=evaluate(temp,temp1,pop(operators,top_operators));
  47.       push(operands,top_operands,result);
  48.  
  49.     }
  50.     else if(next!='('){
  51.         printf("c");
  52.       printf("Invalid input.");
  53.       exit(EXIT_FAILURE);
  54.     }
  55.     scanf("%c",&next);
  56.   }
  57.   printf("hi");
  58.   //result=pop(operands,top_operands);
  59.   if(!is_empty(top_operands) || !is_empty(top_operators)){
  60.       printf("d");
  61.     printf("Invalid input.");
  62.     exit(EXIT_FAILURE);
  63.  
  64.   }
  65.   else{
  66.       printf("boo");
  67.     printf("%d\n",result);
  68.     printf("e");
  69.     return(0);
  70.   }
  71. }
  72.  
  73. void push(int* stack, int* top, int element){
  74.   if(is_full(top))
  75.     stack_overflow();
  76.   else
  77.   {
  78.       *(stack+(*top++))=element;
  79.       printf("q");
  80.   }
  81.  
  82. }
  83.  
  84. int pop(int* stack, int* top){
  85.   if(is_empty(top))
  86.      stack_underflow();
  87.   else
  88.     return *(stack+(--*top));
  89. }
  90.  
  91. bool is_empty(int* top){
  92.   return *top==0;
  93. }
  94.  
  95. bool is_full(int* top){
  96.   return *top == STACK_SIZE;
  97. }
  98.  
  99. void stack_overflow(void){
  100.   printf("Error. Stack overflow.\n");
  101.   exit(EXIT_FAILURE);
  102. }
  103.  
  104. void stack_underflow(void){
  105.   printf("Error. Stack underflow.\n");
  106.   exit(EXIT_FAILURE);
  107. }
  108.  
  109.  int evaluate(int i, int j, int k){
  110.    int result;
  111.    if(k=='+')
  112.      result=(i+j);
  113.    else if(k=='-')
  114.      result=(i-j);
  115.    else if(k=='*')
  116.      result=(i*j);
  117.    else
  118.      result=(i/j);
  119.    return result;
  120.  }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top