-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBODMASS_Calculator.cpp
403 lines (383 loc) · 16.2 KB
/
BODMASS_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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "BODMASS_Calculator.h"
#include <cmath>
#include <iostream>
#include <sstream>
#include <stack>
/**
+ : Addition
- : Subtraction or Unary Minus
* : Multiplication
/ : Division
% : Modulo or remainder operator
** : Power
& : Bitwise AND
| : Bitwise OR
^ : Bitwise XOR
<< : Bitwise left shift operator
>> : Bitwise right shift operator
*/
// Utility function to check unwanted operator or operand
bool BODMASS_Calculator::checkUnwantedOperatorOperand() {
bool result{true};
for (const auto &c : expr) {
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
result = false;
break;
} else if (c == '`' || c == '~' || c == '!' || c == '@' || c == '#' || c == '$' || c == '_' || c == '\\' || c == ';' || c == ':' || c == '\"' || c == '\'' || c == ',' || c == '?' || c == '=') {
result = false;
break;
}
}
if (expr.find("++") != std::string::npos || expr.find("--") != std::string::npos || expr.find("&&") != std::string::npos || expr.find("||") != std::string::npos) {
result = false;
}
return result;
}
// Utitlity Function to check whether the operators are used correctly or not
bool BODMASS_Calculator::checkCorrectOperatorUse() {
bool result{true};
for (size_t i{}; i < expr.length(); i++) {
// Checking that before and after an operator either a digit should come or the corresponding bracket should come
if ((expr.at(i) == '*' && expr.at(i + 1) == '*') || (expr.at(i) == '<' && expr.at(i + 1) == '<') || (expr.at(i) == '>' && expr.at(i + 1) == '>')) {
if (!(expr.at(i - 1) == ')' || expr.at(i - 1) == '}' || expr.at(i - 1) == ']' || (expr.at(i - 1) <= '9' && expr.at(i - 1) >= '0'))) {
result = false;
break;
}
if (!(expr.at(i + 2) == '(' || expr.at(i + 2) == '{' || expr.at(i + 2) == '[' || (expr.at(i + 2) <= '9' && expr.at(i + 2) >= '0'))) {
result = false;
break;
}
} else if ((expr.at(i) == '*' && expr.at(i - 1) != '*' && expr.at(i + 1) != '*') || (expr.at(i) == '/') || (expr.at(i) == '%') || (expr.at(i) == '+') || (expr.at(i) == '&') || (expr.at(i) == '^') || (expr.at(i) == '|')) {
if (!(expr.at(i - 1) == ')' || expr.at(i - 1) == '}' || expr.at(i - 1) == ']' || (expr.at(i - 1) <= '9' && expr.at(i - 1) >= '0'))) {
result = false;
break;
}
if (!(expr.at(i + 1) == '(' || expr.at(i + 1) == '{' || expr.at(i + 1) == '[' || (expr.at(i + 1) <= '9' && expr.at(i + 1) >= '0'))) {
result = false;
break;
}
} else if (expr.at(i) == '-' && i != 0) {
if (expr.at(i - 1) == '(' || expr.at(i - 1) == '{' || expr.at(i - 1) == '[') {
result = true;
} else {
if (!(expr.at(i - 1) == ')' || expr.at(i - 1) == '}' || expr.at(i - 1) == ']' || (expr.at(i - 1) <= '9' && expr.at(i - 1) >= '0'))) {
result = false;
break;
}
if (!(expr.at(i + 1) == '(' || expr.at(i + 1) == '{' || expr.at(i + 1) == '[' || (expr.at(i + 1) <= '9' && expr.at(i + 1) >= '0'))) {
result = false;
break;
}
}
}
}
return result;
}
// Utility Function to modify the given expression to the proper mathematical expression
void BODMASS_Calculator::modifyExpression() {
// Modifying decimal point cases as : .5 => 0.5 and 5. => 5.0
for (size_t i{}; i < expr.length(); i++) {
if (expr.at(i) == '.') {
if (i == 0) {
expr.insert(i, "0");
i++;
} else if (expr.at(i - 1) < '0' || expr.at(i - 1) > '9') {
expr.insert(i, "0");
i++;
}
if (i + 1 == expr.length()) {
expr.insert(i + 1, "0");
} else if (expr.at(i + 1) < '0' || expr.at(i + 1) > '9') {
expr.insert(i + 1, "0");
}
}
}
// Modifying multiplication cases as : 8(9) => 8*(9) and (8)(9) => (8)*(9) and (8)9 => (8)*9
for (size_t i{}; i < expr.length(); i++) {
if (i != 0 && (expr.at(i) == '(' || expr.at(i) == '{' || expr.at(i) == '[')) {
if ((expr.at(i - 1) <= '9' && expr.at(i - 1) >= '0') || expr.at(i - 1) == ')' || expr.at(i - 1) == '}' || expr.at(i - 1) == ']') {
expr.insert(i, "*");
}
} else if (expr.at(i) == ')' || expr.at(i) == '}' || expr.at(i) == ']') {
if (i + 1 < expr.length()) {
if ((expr.at(i + 1) <= '9' && expr.at(i + 1) >= '0') || expr.at(i + 1) == '(' || expr.at(i + 1) == '{' || expr.at(i + 1) == '[') {
expr.insert(i + 1, "*");
}
}
}
}
// Modifying unary minus cases as : -8*9 => M8*9 and 9*(-8) => 9*(M8)
for (size_t i{}; i < expr.length(); i++) {
if (i == 0 && expr.at(i) == '-') {
expr.at(i) = 'M';
} else if (expr.at(i) == '-' && (expr.at(i - 1) == '(' || expr.at(i - 1) == '{' || expr.at(i - 1) == '[')) {
expr.at(i) = 'M';
}
}
// Modifying power operator cases as : 8**9 => 8P9
for (size_t i{}; i < expr.length(); i++) {
if (expr.at(i) == '*' && expr.at(i + 1) == '*') {
expr.at(i) = 'P';
expr.erase(i + 1, 1);
}
}
}
// Utility Function to create the priority table for operators and operands
void BODMASS_Calculator::createPriorityTable() {
priorityTable["1"] = std::pair(18, 18);
priorityTable["2"] = std::pair(18, 18);
priorityTable["3"] = std::pair(18, 18);
priorityTable["4"] = std::pair(18, 18);
priorityTable["5"] = std::pair(18, 18);
priorityTable["6"] = std::pair(18, 18);
priorityTable["7"] = std::pair(18, 18);
priorityTable["8"] = std::pair(18, 18);
priorityTable["9"] = std::pair(18, 18);
priorityTable["0"] = std::pair(18, 18);
priorityTable["("] = std::pair(17, 0);
priorityTable["["] = std::pair(17, 0);
priorityTable["{"] = std::pair(17, 0);
priorityTable[")"] = std::pair(0, -1);
priorityTable["]"] = std::pair(0, -1);
priorityTable["}"] = std::pair(0, -1);
priorityTable["M"] = std::pair(16, 15);
priorityTable["P"] = std::pair(14, 13);
priorityTable["*"] = std::pair(11, 12);
priorityTable["/"] = std::pair(11, 12);
priorityTable["%"] = std::pair(11, 12);
priorityTable["+"] = std::pair(9, 10);
priorityTable["-"] = std::pair(9, 10);
priorityTable["<<"] = std::pair(7, 8);
priorityTable[">>"] = std::pair(7, 8);
priorityTable["&"] = std::pair(5, 6);
priorityTable["^"] = std::pair(3, 4);
priorityTable["|"] = std::pair(1, 2);
}
// Utility Function to create the maping of the operands and operators to interpret them as single unit
// To handle the case of numbers with multiple digits and floating point numbers also
void BODMASS_Calculator::createSingleStringMap() {
size_t i{};
int key{};
while (i < expr.length()) {
if (expr.at(i) <= '9' && expr.at(i) >= '0') {
std::string num{};
int countDecimal{};
while (i < expr.length() && ((expr.at(i) <= '9' && expr.at(i) >= '0') || expr.at(i) == '.')) {
num += expr.at(i);
if (expr.at(i) == '.') {
countDecimal++;
if (countDecimal > 1) {
throw 0;
}
}
i++;
}
singleStringMap[key] = num;
key++;
} else if (expr.at(i) == '<' && expr.at(i + 1) == '<') {
singleStringMap[key] = "<<";
key++;
i += 2;
} else if (expr.at(i) == '>' && expr.at(i + 1) == '>') {
singleStringMap[key] = ">>";
key++;
i += 2;
} else {
singleStringMap[key] = expr.at(i);
key++;
i++;
}
}
}
// Utility Function to return the in stack precedence of the operator/operand
int BODMASS_Calculator::inStackPrecedence(std::string op) {
if (op.at(0) <= '9' && op.at(0) >= '0') {
op = op.at(0);
}
return priorityTable[op].second;
}
// Utility Function to return the out stack precedence of the operator/operand
int BODMASS_Calculator::outStackPrecedence(std::string op) {
if (op.at(0) <= '9' && op.at(0) >= '0') {
op = op.at(0);
}
return priorityTable[op].first;
}
// Constructor
BODMASS_Calculator::BODMASS_Calculator(std::string expr)
: expr{expr}, priorityTable{}, singleStringMap{}, postfixMap{} {
createPriorityTable();
}
// Destructor
BODMASS_Calculator::~BODMASS_Calculator() {
}
// Funtion to check the correct brackets matching pairs
bool BODMASS_Calculator::areBracketsMatching() {
std::stack<char> bracketStack;
bool result{true};
for (const auto &c : expr) {
if (c == '{' || c == '[' || c == '(') {
bracketStack.push(c);
} else if (c == '}') {
if (!bracketStack.empty()) {
if (bracketStack.top() == '{') {
bracketStack.pop();
} else {
result = false;
break;
}
} else {
result = false;
break;
}
} else if (c == ']') {
if (!bracketStack.empty()) {
if (bracketStack.top() == '[') {
bracketStack.pop();
} else {
result = false;
break;
}
} else {
result = false;
break;
}
} else if (c == ')') {
if (!bracketStack.empty()) {
if (bracketStack.top() == '(') {
bracketStack.pop();
} else {
result = false;
break;
}
} else {
result = false;
break;
}
}
}
if (!bracketStack.empty()) {
result = false;
}
return result;
}
// Function to get the postfix expression of the input expression
std::string BODMASS_Calculator::postfix() {
std::string result{};
if (areBracketsMatching()) {
try {
if (checkUnwantedOperatorOperand() && checkCorrectOperatorUse()) {
modifyExpression();
createSingleStringMap();
std::stack<std::string> operatorStack;
int key{};
for (size_t i{}; i < singleStringMap.size();) {
if (operatorStack.empty() && (singleStringMap[i] != ")" || singleStringMap[i] != "}" || singleStringMap[i] != "]")) {
operatorStack.push(singleStringMap[i]);
i++;
} else {
if (inStackPrecedence(operatorStack.top()) < outStackPrecedence(singleStringMap[i])) {
operatorStack.push(singleStringMap[i]);
i++;
} else if (inStackPrecedence(operatorStack.top()) > outStackPrecedence(singleStringMap[i])) {
result += operatorStack.top();
postfixMap[key] = operatorStack.top();
key++;
operatorStack.pop();
} else {
operatorStack.pop();
i++;
}
}
}
while (!operatorStack.empty()) {
result += operatorStack.top();
postfixMap[key] = operatorStack.top();
key++;
operatorStack.pop();
}
} else {
std::cout << std::endl
<< std::endl;
std::cout << "*************************************************************************************************************" << std::endl;
std::cout << "An unexpected error occured." << std::endl
<< "Please try again & Make sure you have entered a correct Mathematical Expression with proper paranthesization" << std::endl;
std::cout << "*************************************************************************************************************" << std::endl;
std::cout << std::endl;
}
} catch (...) {
std::cout << std::endl
<< std::endl;
std::cout << "*************************************************************************************************************" << std::endl;
std::cout << "An unexpected error occured." << std::endl
<< "Please try again & Make sure you have entered a correct Mathematical Epression with proper paranthesization" << std::endl;
std::cout << "*************************************************************************************************************" << std::endl;
std::cout << std::endl;
}
} else {
std::cout << std::endl
<< std::endl;
std::cout << "*************************************************************************************************************" << std::endl;
std::cout << "Error : Brackets Not Matching In The Expression..." << std::endl;
std::cout << "*************************************************************************************************************" << std::endl;
std::cout << std::endl;
}
return result;
}
// Function to calculate the value of the entered expression
double BODMASS_Calculator::value() {
if (postfixMap.size() == 0) {
postfix();
}
std::stack<double> operandStack;
for (size_t i{}; i < postfixMap.size(); i++) {
if (postfixMap[i].at(0) <= '9' && postfixMap[i].at(0) >= '0') {
std::stringstream ss{postfixMap[i]};
double value{};
ss >> value;
operandStack.push(value);
} else {
double newValue{};
if (postfixMap[i] == "M") { // Unary Minus
double temp{operandStack.top()};
operandStack.pop();
newValue = -temp;
operandStack.push(newValue);
} else {
double val1{operandStack.top()};
operandStack.pop();
double val2{operandStack.top()};
operandStack.pop();
if (postfixMap[i] == "P") { // Power
newValue = std::pow(val2, val1);
} else if (postfixMap[i] == "*") {
newValue = static_cast<double>(val2 * val1);
} else if (postfixMap[i] == "/") {
newValue = static_cast<double>(val2 / val1);
} else if (postfixMap[i] == "%") {
newValue = static_cast<double>(static_cast<long long>(val2) % static_cast<long long>(val1));
} else if (postfixMap[i] == "+") {
newValue = static_cast<double>(val2 + val1);
} else if (postfixMap[i] == "-") {
newValue = static_cast<double>(val2 - val1);
} else if (postfixMap[i] == "<<") {
newValue = static_cast<double>(static_cast<long long>(val2) << static_cast<long long>(val1));
} else if (postfixMap[i] == ">>") {
newValue = static_cast<double>(static_cast<long long>(val2) >> static_cast<long long>(val1));
} else if (postfixMap[i] == "&") {
newValue = static_cast<double>(static_cast<long long>(val2) & static_cast<long long>(val1));
} else if (postfixMap[i] == "^") {
newValue = static_cast<double>(static_cast<long long>(val2) ^ static_cast<long long>(val1));
} else if (postfixMap[i] == "|") {
newValue = static_cast<double>(static_cast<long long>(val2) | static_cast<long long>(val1));
}
operandStack.push(newValue);
}
}
}
if (!operandStack.empty()) {
return operandStack.top();
} else {
return 0.0;
}
}