-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1077.cpp
More file actions
58 lines (54 loc) · 1.32 KB
/
1077.cpp
File metadata and controls
58 lines (54 loc) · 1.32 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
#include<bits/stdc++.h>
using namespace std;
int valor(char c)
{
if(c=='^')return 3;
if(c=='/' || c=='*')return 2;
if(c=='-' || c=='+')return 1;
if(c=='(')return 0;
}
int main()
{
int n;scanf("%d",&n);
while(n--)
{
char s[400];
scanf(" %[^\n]",s);
stack<char>c;
string output="";
for(int i=0;i<strlen(s);i++)
{
if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]=='(' || s[i]==')' || s[i]=='^')
{
if(!c.size() || s[i]=='(')
c.push(s[i]);
else if(s[i]==')')
{
while(c.size() && c.top() != '(')
{
output+=c.top();
c.pop();
}
c.pop();
}
else
{
while(c.size() && valor(s[i]) <= valor(c.top()))
{
output+=c.top();
c.pop();
}
c.push(s[i]);
}
}
else output+=s[i];
}
while(c.size())
{
output+=c.top();
c.pop();
}
cout << output << endl;
}
return 0;
}