forked from beet-aizu/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.cpp
88 lines (81 loc) · 1.29 KB
/
parse.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
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
int expression(string,int&);
int term(string,int&);
int factor(string,int&);
int number(string,int&);
bool f;
int expression(string s,int& p){
int res=term(s,p);
while(p<(int)s.size()){
if(s[p]=='+'){
p++;
res+=term(s,p);
continue;
}
if(s[p]=='-'){
p++;
res-=term(s,p);
continue;
}
break;
}
return res;
}
int term(string s,int& p){
int res=factor(s,p);
while(p<(int)s.size()){
if(s[p]=='*'){
p++;
res*=factor(s,p);
continue;
}
if(s[p]=='/'){
p++;
int tmp=factor(s,p);
if(tmp==0){
f=1;
break;
}
res/=tmp;
continue;
}
break;
}
return res;
}
int factor(string s,int& p){
int res;
if(s[p]=='('){
p++;
res=expression(s,p);
p++;
}else{
res=number(s,p);
}
return res;
}
int number(string s,int& p){
int res=0;
while(p<(int)s.size()&&isdigit(s[p]))
res=res*10+s[p++]-'0';
return res;
}
//END CUT HERE
signed main(){
int n;
cin>>n;
while(n--){
string s;
int p=0;
cin>>s;s.pop_back();
cout<<expression(s,p)<<endl;
}
return 0;
}
/*
verified on 2017/11/20
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?lang=jp&id=0109
*/