-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1539.cc
89 lines (79 loc) · 2.51 KB
/
1539.cc
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
89
//Name: Evaluating Simple C Expressions
//Level: 2
//Category: 文字列,構文解析
//Note:
/*
* 曖昧な演算がないので,前か後ろに変数のある++は必ずインクリメント演算子になる.
* これらを見つけ次第記録して消去していくと,最終的には2項演算のみが残るので簡単に計算できる.
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
while(true) {
string str;
getline(cin, str);
if(str == "") break;
vector<int> used(26, 0), pre(26, 0), post(26, 0);
string org = str;
size_t pos;
while((pos = str.find("++")) != string::npos) {
int cur = pos-1;
while(cur >= 0 && str[cur] == ' ') --cur;
if(cur >= 0 && isalpha(str[cur])) {
post[str[cur]-'a']++;
str.erase(pos, 2);
continue;
}
cur = pos+2;
while(cur < str.size() && str[cur] == ' ') ++cur;
if(cur < str.size() && isalpha(str[cur])) {
pre[str[cur]-'a']++;
str.erase(pos, 2);
}
}
while((pos = str.find("--")) != string::npos) {
int cur = pos-1;
while(cur >= 0 && str[cur] == ' ') --cur;
if(cur >= 0 && isalpha(str[cur])) {
post[str[cur]-'a']--;
str.erase(pos, 2);
continue;
}
cur = pos+2;
while(cur < str.size() && str[cur] == ' ') ++cur;
if(cur < str.size() && isalpha(str[cur])) {
pre[str[cur]-'a']--;
str.erase(pos, 2);
}
}
int acc = 0;
char op = '+';
for(int i = 0; i < (int)str.size(); ++i) {
if(str[i] == ' ') continue;
if(isalpha(str[i])) {
int idx = str[i] - 'a';
int val = idx+1 + pre[idx];
used[idx] = true;
if(op == '+') acc += val;
else if(op == '-') acc -= val;
}
else {
op = str[i];
}
}
cout << "Expression: " << org << endl;
cout << " value = " << acc << endl;
for(int i = 0; i < 26; ++i) {
if(used[i]) {
cout << " " << char(i+'a') << " = " << (i+1+pre[i]+post[i]) << endl;
}
}
}
return 0;
}