-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfix_to_infix.cpp
101 lines (100 loc) · 1.65 KB
/
postfix_to_infix.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>
#include<conio.h>
#define max 20
#include<string.h>
#include<stdlib.h>
using namespace std;
char stack[max][10];
int top=-1;
void push(char x[])
{
if(top==(max-1) )
cout<<"Overflow";
else
{
stack[++top]=x;
}
}
char pop()
{
if(top==-1)
{ cout<<"Invalid expression";
return NULL;
}
else
return (stack[top--]);
}
int main()
{
char a[]={"abc*+d-"};
int i,j,p1,p2;
char e1[10],e2[10],sum[5];
int s=sizeof(a)/sizeof(a[0]);
s=s-1;
for(i=0;i<s;i++)
{
if(a[i]>='a' && a[i]<='z')
{
push(a[i]);
}
else if(a[i]=='*')
{
e2=pop();
e1=pop();
sum[0]='(';
sum[1]=e1;
sum[2]='*';
sum[3]=e2;
sum[4]=')';
push(sum);
}
else if(a[i]=='+')
{
e2=pop();
e1=pop();
sum[0]='(';
sum[1]=e1;
sum[2]='+';
sum[3]=e2;
sum[4]=')';
push(sum);
}
else if(a[i]=='-')
{ e2=pop();
e1=pop();
sum[0]='(';
sum[1]=e1;
sum[2]='-';
sum[3]=e2;
sum[4]=')';
push(sum);
}
else if(a[i]=='/')
{
e2=pop();
e1=pop();
sum[0]='(';
sum[1]=e1;
sum[2]='/';
sum[3]=e2;
sum[4]=')';
push(sum);
}
else if(a[i]=='^')
{
e2=pop();
e1=pop();
sum[0]='(';
sum[1]=e1;
sum[2]='^';
sum[3]=e2;
sum[4]=')';
push(sum);
}
else
cout<<"Error in expression\n";
}
cout<<"Value="<<stack[top];
getch();
return 0;
}