-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostFixToInfixPractice2.cpp
83 lines (75 loc) · 1.12 KB
/
PostFixToInfixPractice2.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream> // @author Zaid_Aly
#include<string>
using namespace std;
int top = -1;
int const size = 20;
char Stack[size];
int oper1,oper2,res;
int Pop()
{
int temp;
if(top ==-1)
{
return -1;
}
else
{
temp = Stack[top];
top--;
return temp;
}
}
void Push(int val)
{
if (top==size-1)
{
cout<<"Size Full\n";
}
else
{
top++;
Stack[top] = val;
}
}
void PostFixCoverter(string Exp)
{
int value,result;
for(int i=0; i<Exp.length(); i++)
{
if(Exp[i]>='0' && Exp[i]<='9')
{
value = (int)(Exp[i] - 48);
Push(value);
}
else
{
oper1 = Pop();
oper2 = Pop();
switch(Exp[i])
{
case '+':
result = oper2+oper1;
break;
case '-':
result = oper2-oper1;
break;
case '*':
result = oper2*oper1;
break;
case '/':
result = oper2/oper1;
break;
}
Push(result);
}
}
cout<<"The Result in InFix are : "<<Pop();
}
int main(int argc, char** argv)
{
string expression;
cout<<"Enter the PostFix Expression\n";
cin>>expression;
PostFixCoverter(expression);
return 0;
}