-
Notifications
You must be signed in to change notification settings - Fork 12
/
ONP.cpp
99 lines (79 loc) · 1.68 KB
/
ONP.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
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
int priority(char ch)
{
if(ch=='^')return 3;
if(ch=='/' || ch == '*')return 2;
if(ch=='+' || ch =='-')return 1;
return 0;
}
bool is_op(char ch)
{
if(ch =='+' || ch=='-'||ch =='/' || ch=='*'||ch =='^')
{
return true;
}
else
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int l = s.length();
vector<char> ans;
int i=0;
char ch;
stack<char> st;
while(l--)
{
ch = s[i++];
if(isalnum(ch))
{
ans.pb(ch);
}
else if(ch=='(')
{
st.push(ch);
}
else if(ch == ')')
{
while(!(st.empty()) && st.top() !='(')
{
ans.pb(st.top());
st.pop();
}
st.pop();
}
else
{
while(!(st.empty()) && (is_op(st.top())==true) && priority(ch)>=priority(st.top()))
{
cout<<is_op(st.top())<<endl;
ans.pb(st.top());
st.pop();
}
st.push(ch);
}
}
while(!(st.empty()))
{
ans.pb(st.top());
st.pop();
}
for(int i=0;i<ans.size();i++)
{
cout<<ans[i];
}
ans.clear();
cout<<endl;
}
}