Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include<stdio.h>     //standard input output functions
  2.        //console functions
  3. #include<string.h>       //string functions
  4. #define MAX 50              //max size defined
  5. int stack[MAX];             //a global stack
  6. char post[MAX];             //a global postfix stack
  7. int top=-1;                  //initializing top to -1
  8. void pushstack(char a);       //push function
  9. void pushstack2(char a);  
  10. void evaluate(char c);          //calculate function
  11. int main()
  12. {
  13.     int i,l;
  14.     //clrscr();
  15.     printf("Insert a postfix notation :: ");
  16.     gets(post);                    //getting a postfix expression
  17.     l=strlen(post);               //string length
  18.     for(i=0;i<l;i++)
  19.     {
  20.         if(post[i] == ' '){
  21.             continue;
  22.         }
  23.         if (post[i] == '-' && post[i+1]>='0' && post[i+1]<='9'){
  24.             pushstack2 (post [i+1]);
  25.             i++;
  26.         }
  27.         else if(post[i]>='0' && post[i]<='9')
  28.         {
  29.             pushstack(post[i]);             //if the element is a number push it
  30.         }
  31.         else if(post[i]=='+' || post[i]=='-' || post[i]=='*' ||
  32.         post[i]=='/' || post[i]=='^')       //if element is an operator
  33.         {
  34.             evaluate(post[i]);             //pass it to the evaluate
  35.         }
  36.  
  37.     }                      //print the result from the top
  38.     printf("\n\nResult :: %d",stack[top]);
  39.    return 0;
  40. }
  41.  
  42. void pushstack2(char a)          //definiton for push
  43. {
  44.    top++;                              //incrementing top
  45.    stack[top]=(int) a - 48;    //type casting the string to its integer value
  46.    stack[top]=-stack[top];
  47. }
  48.  
  49. void pushstack(char a)          //definiton for push
  50. {
  51.    top++;                              //incrementing top
  52.    stack[top]=(int) a - 48;    //type casting the string to its integer value
  53. }
  54.  
  55. void evaluate(char c)       //evaluate function
  56. {
  57.    int a,b,ans;        //variables used
  58.    a=stack[top];       //a takes the value stored in the top
  59.    stack[top]='\0';     //make the stack top NULL as its a string
  60.    top--;                //decrement top's value
  61.    b=stack[top];         //put the value at new top to b
  62.    stack[top]='\0';      //make it NULL
  63.    top--;                //decrement top
  64.    switch(c)     //check operator been passed to evaluate
  65.    {
  66.       case '+':          //addition
  67.           ans=b+a;
  68.           break;
  69.       case '-':           //subtraction
  70.           ans=b-a;
  71.           break;
  72.       case '*':            //multiplication
  73.           ans=b*a;
  74.           break;
  75.       case '/':           //division
  76.           ans=b/a;
  77.           break;
  78.       case '^':      //power
  79.           ans=b^a;
  80.           break;
  81.       default:
  82.           ans=0;      //else 0
  83.    }
  84.    top++;             //increment top
  85.    stack[top]=ans;        //store the answer at top
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement