forked from KISHOREMUTHU/Data-Structures-And-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostfix_evaluation.cpp
More file actions
69 lines (53 loc) · 1.02 KB
/
Copy pathpostfix_evaluation.cpp
File metadata and controls
69 lines (53 loc) · 1.02 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// # Data-Structures-And-Algorithms
// Here I will post my regular DSA problems
// Postfix Expression Evaluation Program
#include<iostream.h>
#include<conio.h>
#define SIZE 50
#include <ctype.h>
int s[SIZE]; // Global variables
int top=-1;
int push( int elem )
{
s[ ++top ] = elem;
return( s [top] );
}
int pop()
{
return( s [top--] );
}
int main()
{
char pofx[50],ch;
int i=0,op1,op2;
cout << "\nEnter the postfix expression : ";
cin >> pofx;
while( ( ch = pofx [i++] ) != '\0' )
{
if( isdigit ( ch ) )
push( ch - '0' );
else
{
op2 = pop();
op1 = pop();
// Using switch case to evaluate the expression
switch( ch )
{
case '+' :
push( op1 + op2 );
break;
case '-' :
push( op1 - op2 );
break;
case '*' :
push( op1 * op2 );
break;
case '/' :
push( op1 / op2 );
break;
}
}
}
cout << endl;
cout << "Result after Evaluation: " << s[top];
}