-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1918.cpp
63 lines (59 loc) · 1.36 KB
/
1918.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
#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;
string input;
void solving(int start, int end);
int whereBracket(int start);
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> input;
solving(0, (int)input.size());
return 0;
}
void solving(int start, int end){
stack<char>s;
for(int i = start; i < end; i++){
if(input[i]=='('){
int tmp = whereBracket(i+1);
solving(i+1, tmp);
i = tmp-1;
}
if(input[i] >= 'A' && input[i] <= 'Z')
cout << input[i];
else if((input[i] == '+' || input[i] == '-')){
while(!s.empty()){
cout << s.top();
s.pop();
}
s.push(input[i]);
}
else if(input[i] == '*' || input[i] == '/'){
if(!s.empty()){
if(s.top() == '*' || s.top() == '/'){
cout << s.top();
s.pop();
}
}
s.push(input[i]);
}
}
while(!s.empty()){
cout << s.top();
s.pop();
}
}
int whereBracket(int start){
int a = 1;
for(int i = start; i < input.size(); i++){
if(input[i] == ')'){
a--;
if(a==0) return i+1;
}
if(input[i] == '(')
a++;
}
}