-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddingwords.cpp
More file actions
71 lines (69 loc) · 1.81 KB
/
Copy pathaddingwords.cpp
File metadata and controls
71 lines (69 loc) · 1.81 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
#include <iostream>
#include <string>
#include <sstream>
#include <unordered_map>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string line;
unordered_map<string, int> dictbyword;
unordered_map<int, string> dictbynum;
while (getline(cin, line)) {
stringstream ss(line);
string cmd; ss >> cmd;
if (cmd == "clear") {
dictbyword.clear();
dictbynum.clear();
}
else if (cmd == "def") {
string word, value_str; int value_int;
ss >> word; ss >> value_str; value_int = stoi(value_str);
if (dictbyword.find(word) != dictbyword.end()) { //wrd defined before
dictbynum.erase(dictbyword[word]);
dictbyword.erase(word);
}
dictbyword[word] = value_int;
dictbynum[value_int] = word;
}
else if (cmd == "calc") {
string word; ss >> word;
bool error = false;
int sum = 0;
if (dictbyword.find(word) != dictbyword.end())
sum += dictbyword[word]; //initialise sum
else
error = true;
cout << word;
while (ss >> word) {
if (word == "+") {
cout << " " << word;
ss >> word; //read in next word
if (dictbyword.find(word) == dictbyword.end()) //word was not defined
error = true;
else
sum += dictbyword[word];
cout << " " << word;
}
else if (word == "-") {
cout << " " << word;
ss >> word; //read in next word
if (dictbyword.find(word) == dictbyword.end()) //word was not defined
error = true;
else
sum -= dictbyword[word];
cout << " " << word;
}
else { //word == "="
cout << " " << word;
if (dictbynum.find(sum) == dictbynum.end())
error = true;
if (error)
cout << " " << "unknown" << '\n';
else
cout << " " << dictbynum[sum] << '\n';
}
}
}
}
}