-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExperiment 6.cpp
More file actions
157 lines (152 loc) · 3.04 KB
/
Copy pathExperiment 6.cpp
File metadata and controls
157 lines (152 loc) · 3.04 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include<iostream>
using namespace std;
class convert
{
char a[20];
int top;
public:
convert()
{
top=-1;
}
void push(int x)
{
a[++top]=x;
}
char pop()
{
return a[top--];
}
int priority(char p)
{
if(p=='(' )
return 0;
else if(p=='+'||p=='-')
return 1;
else if(p=='*'|| p=='/')
return 2;
else if(p=='^')
return 3;
}
void in_po(char exp[20])
{
char *e;
char x;
e=exp;
while(*e!='\0')
{
if(isalnum(*e))
{
cout<<*e;
}
else if(*e=='(')
{
push(*e);
}
else if(*e==')')
{
while((x=pop())!='(')
cout<<x;
}
else
{
while(priority(a[top])>=priority(*e))
cout<<pop();
push(*e);
}
e++;
}
while(top!=-1)
{
x=pop();
cout<<x;
}
}
};
class s1
{
public:
char a[20];
int top;
s1()
{
top=-1;
}
void push(int x)
{
a[++top]=x;
}
int pop()
{
return a[top--];
}
void in_po(char exp[20])
{
char *e;
char x;
int num;
e=exp;
while(*e!='\0')
{
if(isalnum(*e))
{
num=*e-48;
push(num);
}
else
{
int a=pop();
int b=pop();
int c;
switch(*e)
{
case '+':
c=a+b;
push(c);
break;
case '-':
c=a-b;
push(c);
break;
case '/':
c=a/b;
push(c);
break;
case '*':
c=a*b;
push(c);
break;
case '^':
c=a^b;
push(c);
break;
}
}
e++;
}
while(top!=-1)
{
cout<<pop();
}
}
};
int main()
{
int ch;
cout<<"Enter choice:\n1 for Infix to postfix conversion\n2 for Postfix Evaluation\n";
cin>>ch;
if(ch==1)
{
convert r;
char ss[20];
cin>>ss;
r.in_po(ss);
}
else if(ch==2)
{
s1 r;
char ss[20];
cin>>ss;
r.in_po(ss);
}
}