-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddingwords.cpp
More file actions
141 lines (125 loc) · 4.22 KB
/
addingwords.cpp
File metadata and controls
141 lines (125 loc) · 4.22 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
// Get the initial command
string cmd;
cin >> cmd;
// Create maps to map words to numbers and numbers to words
map<string, int> wordToNum;
map<int, string> numToWord;
// While there is still something to read;
while(!cin.eof())
{
// If the command is clear
if(cmd == "clear")
{
// Set clear both maps
wordToNum.clear();
numToWord.clear();
}
// If the command is def
else if(cmd == "def")
{
// Get the word and number to set that word to
string word;
int num;
cin >> word >> num;
// Remove the word if it's already in the maps
if(wordToNum.count(word) > 0)
{
numToWord.erase(wordToNum[word]);
wordToNum.erase(word);
}
// Update the maps
wordToNum[word] = num;
numToWord[num] = word;
}
// If the command is calc
else
{
vector<string> out; // Create a vector to store the parts of the output
int total = 0; // Store the total of the calculation
bool unknown = false; // Store whether a word with an unknown number has been seen
// Get the word and the operator
string word, op;
cin >> word >> op;
// Add the word and operator to the output vector
out.push_back(word);
out.push_back(op);
// If the word has a mapping
if(wordToNum.count(word) > 0)
{
// Set the starting total to that word's value
total = wordToNum[word];
}
// If the word doesn't have a mapping
else
{
// Set the total to be unknown
unknown = true;
}
// While there are extra parts of the calculation left
while(op != "=")
{
cin >> word; // Get the word to be added or subtracted
out.push_back(word); // Add the word to the output vector
// If it's an addition operation to perform
if(op == "+")
{
// If the word has a mapping
if(wordToNum.count(word) > 0)
{
// Add the word's value to the total
total += wordToNum[word];
}
// If the word doesn't have a mapping
else
{
// Set the total to be unknown
unknown = true;
}
}
// If it's a subtraction operation to perform
else
{
// If the word has a mapping
if(wordToNum.count(word) > 0)
{
// Subtract that word's value from the total
total -= wordToNum[word];
}
// If the word doesn't have a mapping
else
{
// Set the total to be unknown
unknown = true;
}
}
cin >> op; // Get the next operator
out.push_back(op); // Add the operator to the output vector
}
// Print each part of the output in the output vector
for(int i = 0; i < out.size(); i++)
{
cout << out[i] << " ";
}
// If the total is unknown or there is no mapping for the total value
if(unknown || numToWord.count(total) <= 0)
{
cout << "unknown" << endl;
}
// If the total is known and there is a mapping for the total value
else
{
cout << numToWord[total] << endl;
}
}
// Get the next command to perform or EOF
cin >> cmd;
}
return 0;
}