Advertisement
Guest User

Untitled

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