C Program for Evaluation of Postfix Expression 34*5+

Saturday, June 9th, 2012

Source: Dr. G T Raju, Professor & Head, Dept. of CSE, RNSIT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#define SIZE 50            /* Size of Stack */
#include <ctype.h>
int s[SIZE];
int top=-1;       /* Global declarations */
 
push(int elem)
{                       /* Function for PUSH operation */
    s[++top]=elem;
}
 
int pop()
{                      /* Function for POP operation */
    return(s[top--]);
}
 
main()
{                         /* Main Program */
    char pofx[50],ch;
    int i=0,op1,op2;
    printf("\n\nRead the Postfix Expression ? ");
    scanf("%s",pofx);
    while( (ch=pofx[i++]) != '\0')
    {
        if(isdigit(ch)) push(ch-'0'); /* Push the operand */
        else
        {        /* Operator,pop two  operands */
            op2=pop();
            op1=pop();
            switch(ch)
            {
            case '+':push(op1+op2);break;
            case '-':push(op1-op2);break;
            case '*':push(op1*op2);break;
            case '/':push(op1/op2);break;
            }
        }
    }
    printf("\n Given Postfix Expn: %s\n",pofx);
    printf("\n Result after Evaluation: %d\n",s[top]);
}

Author Name :
Ansten Lobo

Total : 1 Comment


One Response to “C Program for Evaluation of Postfix Expression 34*5+”

  1. Nitish says:

    its not working

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: