-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCTDL097.cpp
75 lines (74 loc) · 1.56 KB
/
SCTDL097.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
/**
* @file SCTDL097.cpp
* @author long ([email protected])
* @brief Postfix to Prefix
* @version 0.1
* @date 2023-06-10
*
* @copyright Copyright (c) 2023
*
*/
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
string operators = "+-*/^";
bool check(char c)
{
for (int i = 0; i < operators.size(); i++)
if (operators[i] == c)
return 1;
return 0;
}
bool Var(char c)
{
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
string s, k, ans;
cin >> s;
stack<string> st;
for (int i = 0; i < s.size(); i++)
{
if (Var(s[i]))
st.push(string(1, s[i]));
if (check(s[i]))
{
k = st.top();
st.pop();
k = s[i] + k;
k = st.top() + k;
st.pop();
k = "(" + k + ")";
st.push(k);
}
}
s = st.top();
while (!st.empty())
st.pop();
for (int i = s.size() - 1; i >= 0; i--)
{
if (Var(s[i]))
ans = s[i] + ans;
if (check(s[i]))
st.push(string(1, s[i]));
if (s[i] == '(')
{
ans = st.top() + ans;
st.pop();
}
}
while (!st.empty())
{
ans = st.top() + ans;
st.pop();
}
cout << ans << endl;
}
}