6
6
#include < sstream>
7
7
#include < condition_variable>
8
8
using namespace std ;
9
- #define _VERSION_ " 1.10 "
9
+ #define _VERSION_ " 1.11 "
10
10
#define _DEFAULT_BUFFER_SIZE_ 1024
11
11
string identifiers[] = {
12
12
" output" ,
13
13
" wrap" ,
14
14
" new" ,
15
- " set"
15
+ " set" ,
16
+ " input" ,
17
+ " addition" ,
18
+ " subtraction" ,
19
+ " multiplication" ,
20
+ " division"
16
21
};
17
22
struct Variable {
18
- int value;
23
+ long double value;
19
24
string name;
20
25
};
21
26
@@ -40,6 +45,7 @@ struct UndeclaredVariable : public exception {
40
45
return " Undeclared variable." ;
41
46
}
42
47
};
48
+
43
49
vector<string> split (string str, char delim) {
44
50
stringstream ss (str);
45
51
string item;
@@ -51,7 +57,17 @@ vector<string> split(string str, char delim) {
51
57
}
52
58
return result;
53
59
}
54
- int main (int argc, char * argv[]) {
60
+ bool isInteger (string x) {
61
+ bool integer = true ;
62
+ for (int i = 0 ; i < strlen (x.c_str ()); i++) {
63
+ if (x[i] < ' 0' || x[i] > ' 9' ) {
64
+ integer = false ;
65
+ break ;
66
+ }
67
+ }
68
+ return integer;
69
+ }
70
+ int main (int argc, char * argv[]) {
55
71
try {
56
72
string fileName = string (argv[1 ]);
57
73
int bufferSize;
@@ -128,6 +144,126 @@ int main (int argc, char* argv[]) {
128
144
}
129
145
break ;
130
146
}
147
+ if (*it == identifiers[4 ]) {
148
+ bool undeclared = true ;
149
+ for (int i = 0 ; i <= variableCount; i++) {
150
+ if (variables[i].name == *(it + 1 )) {
151
+ int temp;
152
+ cin >> temp;
153
+ variables[i].value = temp;
154
+ undeclared = false ;
155
+ break ;
156
+ }
157
+ }
158
+ if (undeclared) {
159
+ throw UndeclaredVariable ();
160
+ }
161
+ break ;
162
+ }
163
+ if (*it == identifiers[5 ]) {
164
+ bool undeclared = true ;
165
+ for (int i = 0 ; i <= variableCount; i++) {
166
+ if (variables[i].name == *(it + 1 )) {
167
+ if (isInteger (*(it + 2 ))) {
168
+ variables[i].value += atoi ((*(it + 2 )).c_str ());
169
+ undeclared = false ;
170
+ break ;
171
+ }
172
+ else {
173
+ undeclared = true ;
174
+ for (int j = 0 ; j <= variableCount; j++) {
175
+ if (variables[j].name == *(it + 2 )) {
176
+ variables[i].value += variables[j].value ;
177
+ undeclared = false ;
178
+ break ;
179
+ }
180
+ }
181
+ }
182
+ }
183
+ }
184
+ if (undeclared) {
185
+ throw UndeclaredVariable ();
186
+ }
187
+ break ;
188
+ }
189
+ if (*it == identifiers[6 ]) {
190
+ bool undeclared = true ;
191
+ for (int i = 0 ; i <= variableCount; i++) {
192
+ if (variables[i].name == *(it + 1 )) {
193
+ if (isInteger (*(it + 2 ))) {
194
+ variables[i].value -= atoi ((*(it + 2 )).c_str ());
195
+ undeclared = false ;
196
+ break ;
197
+ }
198
+ else {
199
+ undeclared = true ;
200
+ for (int j = 0 ; j <= variableCount; j++) {
201
+ if (variables[j].name == *(it + 2 )) {
202
+ variables[i].value -= variables[j].value ;
203
+ undeclared = false ;
204
+ break ;
205
+ }
206
+ }
207
+ }
208
+ }
209
+ }
210
+ if (undeclared) {
211
+ throw UndeclaredVariable ();
212
+ }
213
+ break ;
214
+ }
215
+ if (*it == identifiers[7 ]) {
216
+ bool undeclared = true ;
217
+ for (int i = 0 ; i <= variableCount; i++) {
218
+ if (variables[i].name == *(it + 1 )) {
219
+ if (isInteger (*(it + 2 ))) {
220
+ variables[i].value *= atoi ((*(it + 2 )).c_str ());
221
+ undeclared = false ;
222
+ break ;
223
+ }
224
+ else {
225
+ undeclared = true ;
226
+ for (int j = 0 ; j <= variableCount; j++) {
227
+ if (variables[j].name == *(it + 2 )) {
228
+ variables[i].value *= variables[j].value ;
229
+ undeclared = false ;
230
+ break ;
231
+ }
232
+ }
233
+ }
234
+ }
235
+ }
236
+ if (undeclared) {
237
+ throw UndeclaredVariable ();
238
+ }
239
+ break ;
240
+ }
241
+ if (*it == identifiers[8 ]) {
242
+ bool undeclared = true ;
243
+ for (int i = 0 ; i <= variableCount; i++) {
244
+ if (variables[i].name == *(it + 1 )) {
245
+ if (isInteger (*(it + 2 ))) {
246
+ variables[i].value /= atoi ((*(it + 2 )).c_str ());
247
+ undeclared = false ;
248
+ break ;
249
+ }
250
+ else {
251
+ undeclared = true ;
252
+ for (int j = 0 ; j <= variableCount; j++) {
253
+ if (variables[j].name == *(it + 2 )) {
254
+ variables[i].value /= variables[j].value ;
255
+ undeclared = false ;
256
+ break ;
257
+ }
258
+ }
259
+ }
260
+ }
261
+ }
262
+ if (undeclared) {
263
+ throw UndeclaredVariable ();
264
+ }
265
+ break ;
266
+ }
131
267
}
132
268
else {
133
269
throw UnknowIdentifier ();
0 commit comments