-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path!--calculator.cpp
194 lines (186 loc) · 6.71 KB
/
!--calculator.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main()
{
map<string, int> variables;
while (true)
{
string in;
getline(cin, in);
if (in == "shutdown" || in == "sd")
{
return 0;
}
else if (in == "variables" || in == "v")
{
for (auto it = variables.begin(); it != variables.end(); ++it)
{
if (it == variables.begin())
{
cout << "[@] " << it->first << " = " << it->second << '\n';
}
else
{
cout << " " << it->first << " = " << it->second << '\n';
}
}
}
else if (in == "")
{
continue;
}
else
{
int size0 = in.size();
vector<string> store;
string temp = "";
for (int i = 0; i <= size0 - 1; ++i)
{
if (in[i] == ' ' || in[i] == ';')
{
store.push_back(temp);
temp = "";
}
else
{
temp += in[i];
}
}
if (temp != "")
{
store.push_back(temp);
}
if (store[0] == "out" || store[0] == "o")
{
if (variables.find(store[1]) == variables.end())
{
cout << "[@] There is no variable called " << '\'' << store[1] << "\' .\n";
}
else
{
cout << "[@] " << variables[store[1]] << '\n';
}
}
else if (store[0] == "create" || store[0] == "cr")
{
if (store.size() == 2)
{
if (variables.find(store[1]) == variables.end())
{
variables[store[1]] = 0;
cout << "[@] Variable " << '\'' << store[1] << "\' (0) has been created .\n";
}
else
{
cout << "[@] There is already a variable called " << '\'' << store[1] << "\' (" << variables[store[1]] << ") .\n";
}
}
else
{
if (variables.find(store[1]) == variables.end())
{
variables[store[1]] = stoi(store[3]);
cout << "[@] Variable " << '\'' << store[1] << "\' (" << store[3] << ") has been created .\n";
}
else
{
cout << "[@] There is already a variable called " << '\'' << store[1] << "\' (" << variables[store[1]] << ") .\n";
}
}
}
else if (store[0] == "change" || store[0] == "ch")
{
if (variables.find(store[1]) == variables.end())
{
cout << "[@] There is no variable called " << '\'' << store[1] << "\' .\n";
}
else
{
if (store.size() == 4)
{
if (store[3][0] >= '0' && store[3][0] <= '9')
{
variables[store[1]] = stoi(store[3]);
cout << "[@] Changed variable " << '\'' << store[1] << "\' to " << store[3] << " .\n";
}
else
{
if (variables.find(store[3]) == variables.end())
{
cout << "[@] There is no variable called " << '\'' << store[3] << "\' .\n";
continue;
}
variables[store[1]] = variables[store[3]];
cout << "[@] Set variable " << '\'' << store[1] << "\' as same as " << store[3] << '(' << variables[store[3]] << ") .\n";
}
}
else
{
int a, b, ans;
if (store[3][0] >= '0' && store[3][0] <= '9')
{
a = stoi(store[3]);
}
else
{
if (variables.find(store[3]) == variables.end())
{
cout << "[@] There is no variable called " << '\'' << store[3] << "\' .\n";
continue;
}
a = variables[store[3]];
}
if (store[5][0] >= '0' && store[5][0] <= '9')
{
b = stoi(store[5]);
}
else
{
if (variables.find(store[5]) == variables.end())
{
cout << "[@] There is no variable called " << '\'' << store[5] << "\' .\n";
continue;
}
b = variables[store[5]];
}
if (store[4] == "+")
{
ans = a + b;
}
else if (store[4] == "-")
{
ans = a - b;
}
else if (store[4] == "*")
{
ans = a * b;
}
else if (store[4] == "/")
{
ans = a / b;
}
else
{
ans = a % b;
}
variables[store[1]] = ans;
cout << "[@] Changed variable " << '\'' << store[1] << "\' to " << ans << " .\n";
}
}
}
else
{
cout << "Cannot understand the input .\n";
}
}
}
}