-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.cpp
354 lines (314 loc) · 7.88 KB
/
calc.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "calc.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <pthread.h>
#include <map>
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
using std::map;
using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::stringstream;
using std::stoi;
using std::to_string;
struct Calc {
private:
// fields
map<string, int> variableCollection;
bool valid = true;
public:
//lock for concurrence
pthread_mutex_t lock;
// public member functions
Calc();
~Calc();
int evalExpr(const std::string &expr, int &result);
private:
// private member functions
vector<string> tokenize(const string &expr);
int parseOperand(string operand);
int assignVariable(string variable, string value);
int calculate(string operand1, string op, string operand2);
bool checkVar(string input);
bool checkInt(string input);
bool checkOperand(string input);
bool checkOp(string input);
bool checkVarExistence(string varName);
string singleCharToString(char character);
};
/*
* Evalutes expression given by user input
*
* Parameters:
* expr - string with user input
* result - pointer to an integer to store expression result
*
* Returns:
* 1 if valid expression, 0 if not
*/
int Calc::evalExpr(const std::string &expr, int &result) {
vector<string> input = tokenize(expr); //splits user input into tokoens
int inputSize = input.size();
valid = true; //initializes valid to true before any error checking
if (inputSize > 5 || inputSize < 1 || inputSize == 2 || inputSize == 4) { //first check for valid input
valid = false;
}
if (inputSize == 1 && checkOperand(input.at(0))) { //evaluate expression: operand
result = parseOperand(input.at(0));
return valid;
}
if (inputSize == 3) {
if (singleCharToString('=').compare(input.at(1)) == 0 && checkVar(input.at(0)) && checkOperand(input.at(2))) { //evaluate expression: var = operand
result = assignVariable(input.at(0), input.at(2));
return valid;
}
if (checkOperand(input.at(0)) && checkOperand(input.at(2)) && checkOp(input.at(1))) { //evaluate expression: operand op operand
result = calculate(input.at(0), input.at(1), input.at(2));
return valid;
}
}
if (inputSize == 5) { //evaluate expression: var = operand op operand
if (singleCharToString('=').compare(input.at(1)) == 0 && checkVar(input.at(0)) && checkOperand(input.at(2)) && checkOp(input.at(3)) && checkOperand(input.at(4))) {
result = assignVariable(input.at(0), to_string(calculate(input.at(2), input.at(3), input.at(4))));
return valid;
}
}
return false; //return false if none of the if statements execute
}
Calc::Calc(){
//constructor must initialize mutex when data structure is initialized
pthread_mutex_init(&lock, NULL);
}
Calc::~Calc() {
//constructor must destroy mutex when data structure is deallocated
pthread_mutex_destroy(&lock);
}
/*
* Splits user input into tokens
*
* Parameters:
* expr - string with user input
*
* Returns:
* vector with tokens
*/
vector<string> Calc::tokenize(const string &expr) {
vector<string> vec;
stringstream s(expr);
string tok;
while (s >> tok) {
vec.push_back(tok);
}
return vec;
}
/*
* Returns operand value
*
* Parameters:
* operand - string with inputted operand
*
* Returns:
* integer with operand value
*/
int Calc::parseOperand(string operand) {
if (checkVar(operand)) { //finds value if operand is a value
if (checkVarExistence(operand)) {
return variableCollection.find(operand)->second;
}
else { //flags expression as invalid
valid = false;
return 0;
}
}
else { //returns operand as integer
return stoi(operand);
}
}
/*
* Adds variable to map
*
* Parameters:
* variable - string with variable name
* value - string with variable value
*
* Returns:
* integer with value
*/
int Calc::assignVariable(string variable, string value) {
int intValue = parseOperand(value);
variableCollection.erase(variable); //removes previous key if exists
variableCollection.insert(std::pair<string,int>(variable,intValue));
return intValue;
}
/*
* Calculates arithmetic for expression
*
* Parameters:
* operand1 - string with first operand
* op - string with operator
* operand2 - string with second operand
*
* Returns:
* integer with calculated value
*/
int Calc::calculate(string operand1, string op, string operand2) {
int value1 = parseOperand(operand1);
int value2 = parseOperand(operand2);
if (op == "+") {
return value1 + value2;
}
if (op == "-") {
return value1 - value2;
}
if (op == "*") {
return value1 * value2;
}
if (op == "/") {
if (value2 == 0) { //ensures no divide by 0
valid = false;
return 0;
}
return value1 / value2;
}
valid = false; //bad expression if no if statement entered, should never reach
return 0;
}
/*
* Checks if string is a valid variable
*
* Parameters:
* input - string to check
*
* Returns:
* true if valid, false if not
*/
bool Calc::checkVar(string input) {
int i = 0;
bool validVar = true;
//checks if each character is an alpha
while (input[i]) {
if (!isalpha(input[i])) {
validVar = false;
}
i++;
}
return validVar;
}
/*
* Checks if string is a valid integer
*
* Parameters:
* input - string to check
*
* Returns:
* true if valid, false if not
*/
bool Calc::checkInt(string input) {
bool validInt;
//checks if first character is either a negative or a digit
validInt = (isdigit(input[0]) || '-' == input[0]) ? true : false;
int i = 1;
//checks if each character is a digit
while (input[i]) {
if (!isdigit(input[i])) {
validInt = false;
}
i++;
}
return validInt;
}
/*
* Checks if string is a valid operand
*
* Parameters:
* input - string to check
*
* Returns:
* true if valid, false if not
*/
bool Calc::checkOperand(string input) {
return checkVar(input) || checkInt(input);
}
/*
* Checks if string is a valid operator
*
* Parameters:
* input - string to check
*
* Returns:
* true if valid, false if not
*/
bool Calc::checkOp(string input) {
return singleCharToString('+').compare(input) == 0|| singleCharToString('-').compare(input) == 0|| singleCharToString('*').compare(input) == 0|| singleCharToString('/').compare(input) ==0;
}
/*
* Checks if variable exists in dictionary
*
* Parameters:
* varName - variable name
*
* Returns:
* true if exists, false if not
*/
bool Calc::checkVarExistence(string varName) {
if (variableCollection.count(varName) == 1) {
return true;
}
return false;
}
/*
* Converts single character to string
*
* Parameters:
* character - character to convert
*
* Returns:
* character as string
*/
string Calc::singleCharToString(char character) {
string s(1, character);
return s;
}
/*
* Creates an instance of the struct Calc data type
*
* Returns:
* pointer to new Calculator struct
*/
extern "C" struct Calc *calc_create(void) {
return new Calc();
}
/*
* Deletes an instance of the struct Calc data type
*
* Parameters:
* calc - pointer to Calculator struct
*/
extern "C" void calc_destroy(struct Calc *calc) {
delete calc;
}
/*
* Evalutes expression given by user input
*
* Parameters:
* calc - pointer to Calculator struct
* expr - string with user input
* result - pointer to an integer to store expression result
*
* Returns:
* return value from evalExpr()
*/
extern "C" int calc_eval(struct Calc *calc, const char *expr, int *result) {
//critical section, mutex used to lock data before starting to accept user input and evaluate
pthread_mutex_lock(&calc->lock);
int i = calc->evalExpr(expr, *result);
//once evaluation is done, data is no longer critical, data unlocked
pthread_mutex_unlock(&calc->lock);
return i;
}