-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhanwuji.cpp
71 lines (65 loc) · 1.18 KB
/
hanwuji.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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define ERREXIT(i) do { cout << "ERROR line:" << i; exit(1); } while (0)
bool IsInt(const string& s)
{
int asint = std::stoi(s);
string tmp;
while (asint)
{
int digit = asint % 10;
tmp.push_back('0' + digit);
asint /= 10;
}
for (int i = 0; i != (int)s.size(); ++i)
{
if (s[i] != tmp[s.size()-i-1])
return false;
}
return true;
}
int main()
{
vector<string> expr;
string val = "";
for (int i = 1; val != "="; ++i)
{
cin >> val;
if (i & 1) // expect a integer
{
if (!IsInt(val))
ERREXIT(i);
expr.emplace_back(val);
}
else // expect an operator
{
if (val == "+" || val == "-" || val == "=")
expr.emplace_back(val);
else
ERREXIT(i);
}
}
int ret = 0;
char op = '+';
for (size_t i = 1; i <= expr.size(); ++i)
{
if (i & 1)
{
switch (op)
{
case '+': ret += std::stoi(expr[i-1]);
break;
case '-': ret -= std::stoi(expr[i-1]);
break;
}
}
else
{
op = expr[i-1][0];
}
}
cout << ret;
return 0;
}