C Program for Evaluation of Postfix Expression 34*5+

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++]) != '
#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]);
}
'
) { 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]); }
Ansten Lobo

One thought on “C Program for Evaluation of Postfix Expression 34*5+

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in