-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfixtopostfix.cpp
More file actions
149 lines (147 loc) · 2.01 KB
/
Copy pathinfixtopostfix.cpp
File metadata and controls
149 lines (147 loc) · 2.01 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
#include<iostream>
#include<cstring>
using namespace std;
class stack
{
public:
char st[20],source[20]="\0",top=-1,max=20;
void push(char);
int pop();
int priority(char);
int isoperator(char);
int isoperand(char);
void insertion(char);
};
void stack::push(char x)
{
if(top==max)
{
cout<<"overflow condition\n";
}
else
{
top++;
st[top]=x;
}
}
int stack::pop()
{
int item;
if(top==-1)
{
cout<<"underflow condition\n";
}
else
{
item=st[top];
top--;
return(item);
}
}
int stack::isoperand(char x)
{
if((int(x)>=65 && int(x)<=90)||(int(x)>=97 && int(x)<=122))
{
return 1;
}
else
{
return 0;
}
}
int stack::isoperator(char x)
{
if(x=='+'||x=='-'||x=='%'||x=='/'||x=='*'||x=='^')
{
return 1;
}
else
{
return 0;
}
}
int stack::priority(char x)
{
if(x=='^')
{
return 3;
}
else if(x=='*'||x=='/')
{
return 2;
}
else if(x=='+'||x=='-')
{
return 1;
}
else
{
return 0;
}
}
void stack::insertion(char x)
{
int length=strlen(source);
if(!length)
{
source[0]=x;
}
else
{
for(int i=0;i<=length+1;i++)
{
if(i==length)
{
source[i]=x;
}
}
}
length++;
}
int main()
{
stack s;
string arr;
int i=0;
cout<<"enter string\n";
getline(cin,arr);
while(arr[i]!='\0')
{
if(s.isoperand(arr[i])!=0)
{
s.insertion(arr[i]);
}
else if(arr[i]==40)
{
s.push(arr[i]);
}
else if(s.isoperator(arr[i])!=0)
{
while(s.priority(arr[i])<=s.priority(s.st[s.top]))
{
cout<<"high priority element in stack\n";
char z=char(s.pop());
s.insertion(z);
cout<<"element popped "<<z<<endl;
}
s.push(arr[i]);
}
else if(arr[i]==41)
{
char z=char(s.pop());
while(s.isoperator(z)!=0)
{
s.insertion(z);
cout<<"element popped "<<z<<endl;
z=s.pop();
}
}
i++;
}
while(s.top!=-1)
{
s.insertion(s.pop());
}
cout<<s.source<<endl;
return 0;
}