Advertisement
Shailrshah

Infix to Postfix conversion and evaluation

Aug 30th, 2013
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. #include <stdio.h>
  2. char stack[100];
  3. int top = -1;
  4. char ch;
  5. void push(char elem){
  6.     stack[++top] = elem;
  7. }
  8. char pop(){
  9.    return (stack[top--]);
  10. }
  11. void evalutation(char postfix[50]){
  12.     int j=0, operand1, operand2;
  13.     while((ch=postfix[j++]) != '\0'){//jth character of postfix string will be stored in ch
  14.         if(ch >= '0' && ch <= '9') push(ch - '0');//if ch is a number, number will be pushed on top of stack
  15.         else{//if ch is an operator
  16.             operand2=pop();//2 operands will be popped
  17.             operand1=pop();
  18.             switch(ch){//the operation will be done and pushed on top of the stack
  19.                 case '+':   push(operand1 + operand2); break;
  20.                 case '-':   push(operand1 - operand2); break;
  21.                 case '*':   push(operand1 * operand2); break;
  22.                 case '/':   push(operand1 / operand2); break;
  23.             }
  24.         }
  25.     }
  26. }
  27. int precidence(char elem){
  28.     switch (elem){ // | ( | < | +,- | < | *,/ |
  29.         case '(':   return 1;
  30.         case '+':
  31.         case '-':   return 2;
  32.         case '*':
  33.         case '/':   return 3;
  34.         default:    return 0;
  35.     }
  36. }
  37. void convert(char infix[50], char postfix[50]){
  38.     int i = 0, j = 0; //i and j will be indexes for the infix & postfix strings
  39.     while ((ch = infix[i++]) != '\0'){//ith character will be stored in ch
  40.         if (ch == '(')  push(ch);//if ch is (, it will be on top of the stack
  41.         else if (ch >= '0' && ch <= '9')   postfix[j++] = ch;//if ch is 0-9, add to postfix string
  42.         else if (ch == ')'){//if ch is ),
  43.             while (stack[top] != '(')//Until stack's top is not (,
  44.                 postfix[j++] = pop();//pop elements from stack, add to postfix string
  45.             pop();//pop the (
  46.         } else{// if ch is an operator
  47.             while (precidence(stack[top]) >= precidence(ch))//if top's character's precidence is >= ch's,
  48.                 postfix[j++] = pop();//pop the character on top of the stack and add it to the postfix string
  49.             push(ch);//push operator on top of the stack
  50.         }
  51.     }
  52.     while (top != -1)   postfix[j++] = pop();//Until the stack doesn't become emty, pop the top element and add to postfix string
  53.     postfix[j] = '\0';//add null character at the end of postfix string to indicate end of string
  54. }
  55. int main(){
  56.     char infix[50], postfix[50];//infix and postfix strings created
  57.     printf("\nInfix Expression:\t");
  58.     gets(infix);//infix string is inputted
  59.     convert(infix, postfix);//call for converting infix string to postfix
  60.     printf("\nPostfix Expression:\t%s\n", postfix);//print the postfix string
  61.     evalutation(postfix);//call for evaluating postfix string
  62.     printf("\nPostfix Evaluation:\t%d\n",stack[top]);//the value on top will be the answer
  63.     return 0;
  64. }
  65. //Output:-
  66. //>C:\programs\"in2post"
  67.  
  68. //Infix Expression:       7-(2*3+5)*(8-4/2)
  69.  
  70. //Postfix Expression:     723*5+842/-*-
  71.  
  72. //Postfix Evaluation:     -59
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement