-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_evaluation.cpp
More file actions
78 lines (72 loc) · 1.11 KB
/
Copy pathexpression_evaluation.cpp
File metadata and controls
78 lines (72 loc) · 1.11 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/// https://infoarena.ro/problema/evaluare
#include<fstream>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
char s[100002];
int i;
int termen();
int factor();
int calc()
{
int x=termen();
while(s[i]=='+'||s[i]=='-')
{
if(s[i]=='+')
{
i++;
x+=termen();
}
else
if(s[i]=='-')
{
i++;
x-=termen();
}
}
i++;
return x;
}
int termen()
{
int x=factor();
while(s[i]=='*'||s[i]=='/')
{
if(s[i]=='*')
{
i++;
x*=factor();
}
else
if(s[i]=='/')
{
i++;
x/=factor();
}
}
return x;
}
int factor()
{
int x=0;
if(s[i]=='(')
{
i++;
return calc();
}
else
{
while(s[i]>='0'&&s[i]<='9')
x=x*10+s[i]-48,i++;
return x;
}
}
int main()
{
int sol;
f.getline(s+1,100001);
i=1;
sol=calc();
g<<sol;
return 0;
}