-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfix to Posfix.cpp
More file actions
79 lines (68 loc) · 1.59 KB
/
Infix to Posfix.cpp
File metadata and controls
79 lines (68 loc) · 1.59 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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
//Check the priority of operators function
int priority(char symbol)
{
switch(symbol)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default :
return 0;
}
}
int main()
{
string ara;
stack <char> mystack;
vector <char> output;
int tc;
cin>>tc;
while(tc--){
cin>>ara;
for(int i=0;i<ara.length();i++){
if( ara[i] == '+' || ara[i] == '-' || ara[i] == '*' || ara[i] == '/' || ara[i] == '^'){
while( !mystack.empty() && priority(mystack.top()) >= priority(ara[i]) ){
output.push_back(mystack.top());
mystack.pop();
}
mystack.push(ara[i]);
}
else if(ara[i] == '('){
mystack.push(ara[i]);
}
else if( ara[i]==')' ){
while( mystack.top() !='(' ){
output.push_back(mystack.top());
mystack.pop();
}
mystack.pop();
}
else {
output.push_back(ara[i]);
}
}
while( !mystack.empty() ){
output.push_back(mystack.top());
mystack.pop();
}
for(int i=0;i<output.size();i++){
cout<<output[i];
}
cout<<endl;
while(!output.empty()){
output.pop_back();
}
}
return 0;
}