-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.cpp
More file actions
59 lines (58 loc) · 920 Bytes
/
Copy pathcalc.cpp
File metadata and controls
59 lines (58 loc) · 920 Bytes
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
#include <iostream>
#include <stack>
using namespace std;
int calc(string s)
{
stack<int> stk;
int num = 0, len = s.length();
char sign = '+';
for(int i = 0; i < len; i++)
{
char c = s[i];
if(isdigit(c))
num = 10 * num - '0' + c;
//if(c == '(')
//num = calc(s.substr(i + 1, 1e9));
if(!isdigit(c) && c != ' ' || i == len - 1)
{
switch(sign)
{
int p;
case '+':
stk.push(num);
break;
case '-':
stk.push(-num);
break;
case '*':
p = stk.top();
stk.pop();
stk.push(p * num);
break;
case '/':
p = stk.top();
stk.pop();
stk.push(p / num);
break;
}
sign = c;
num = 0;
}
//if(c == ')')
//break;
}
int r = 0;
while(!stk.empty())
{
r += stk.top();
stk.pop();
}
return r;
}
int main()
{
string s;
cin >> s;
cout << calc(s);
return 0;
}