-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.cpp
379 lines (351 loc) · 8.61 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
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
#include <iostream>
#include <tr1/unordered_map>
#include <cmath>
#include <stack>
#include <queue>
using namespace std;
//used to determine type of function to perform
string substring(string input, int start, int end){
string returnString = "";
if(input.length() < (unsigned int)end-start+1){
return input;
}
for(int i = 0;i<(end-start+1);i++){
returnString.append(" ");
}
for(int i = start;i<=end;i++){
returnString.at(i-start)=input.at(i);
}
return returnString;
}
//removes spaces
string noSpaces(string input){
string returnString;
for(unsigned int i = 0,k = 0; i<input.length() ;i++){
if(input.at(i)!=' '){
returnString.append(" ");
returnString.at(k)=input.at(i);
k++;
}
}
return returnString;
}
//converts string to double
double stoi(string input){
double returnValue;
for(unsigned int i = 0, j=0; i<input.length();i++){
if(input.at(i)=='1'){
returnValue += 1*pow(10,input.length()-1-i+j);
}else if(input.at(i)=='2'){
returnValue += 2*pow(10,input.length()-1-i+j);
}else if(input.at(i)=='3'){
returnValue += 3*pow(10,input.length()-1-i+j);
}else if(input.at(i)=='4'){
returnValue += 4*pow(10,input.length()-1-i+j);
}else if(input.at(i)=='5'){
returnValue += 5*pow(10,input.length()-1-i+j);
}else if(input.at(i)=='6'){
returnValue += 6*pow(10,input.length()-1-i+j);
}else if(input.at(i)=='7'){
returnValue += 7*pow(10,input.length()-1-i+j);
}else if(input.at(i)=='8'){
returnValue += 8*pow(10,input.length()-1-i+j);
}else if(input.at(i)=='9'){
returnValue += 9*pow(10,input.length()-1-i+j);
}else if(input.at(i)=='.'){
j=1;
}
}
return returnValue;
}
//determines if input string is a number
bool isNum(string input){
bool returnValue = true;
for(unsigned int i = 0; i<input.length();i++){
if(input.at(i)=='1'){
}else if(input.at(i)=='2'){
}else if(input.at(i)=='3'){
}else if(input.at(i)=='4'){
}else if(input.at(i)=='5'){
}else if(input.at(i)=='6'){
}else if(input.at(i)=='7'){
}else if(input.at(i)=='8'){
}else if(input.at(i)=='9'){
}else if(input.at(i)=='.'){
}else if(input.at(i)=='0'){
}else{
return false;
}
}
return returnValue;
}
//determines if input is an operator
bool isOperator(string input){
if(input == "^"){
return true;
}else if(input == "*"){
return true;
}else if(input == "/"){
return true;
}else if(input == "+"){
return true;
}else if(input == "-"){
return true;
}else if(input == "("){
return true;
}else if(input == ")"){
return true;
}else{
return false;
}
}
//determines if input is a string(var name)
bool isString(string input){
for(unsigned int i = 0; i<input.length();i++){
if(((input.at(i) >=65 && input.at(i) <=90) || (input.at(i) >=97 && input.at(i) <=122))){
}else{
return false;
}
}return true;
}
//determines if input is a string(function)
bool isFunction(string input){
if(input == "sin("){
return true;
}else if(input == "cos("){
return true;
}else if(input == "log("){
return true;
}else if(input == "sin"){
return true;
}else if(input == "cos"){
return true;
}else if(input == "log"){
return true;
}else{
return false;
}
}
double evaluate(tr1::unordered_map <string,double> map, tr1::unordered_map <string,bool> mappedValues,queue<string> input){
double returnValue = 0;
queue<string> output;
stack<string> operators;
stack<double> evaluateStack;
bool doPop = true;
//shunting-yard to postfix
while(!input.empty()){
doPop = true;
if(isNum(input.front())){
output.push(input.front());
}
if(isString(input.front())){
output.push(input.front());
}
if(isOperator(input.front()) && input.front() != " "){
if(input.front()=="^"){
operators.push(input.front());
}
if(input.front()=="/" || input.front()=="*"){
if(operators.size()!=0){
if(operators.top()=="/" || operators.top()=="*" || operators.top()== "^"){
output.push(operators.top());
operators.pop();
doPop = false;
}else{
operators.push(input.front());
}
}else{
operators.push(input.front());
}
}
if(input.front()=="-" || input.front()=="+"){
if(operators.size()!=0){
if(operators.top()=="-" || operators.top()=="+" || operators.top()=="/" || operators.top()=="*" || operators.top()== "^"){
output.push(operators.top());
operators.pop();
doPop = false;
}else{
operators.push(input.front());
}
}else{
operators.push(input.front());
}
}
if(input.front()=="("){
operators.push(input.front());
}
if(input.front()==")"){
while(true){
if(operators.top() == "("){
break;
}
output.push(operators.top());
operators.pop();
}
operators.pop();
}
}
if(isFunction(input.front())){
if(input.front() == "sin("){
operators.push("sin");
operators.push("(");
}
if(input.front() == "cos("){
operators.push("cos");
operators.push("(");
}
if(input.front() == "log("){
operators.push("log");
operators.push("(");
}
}
if(doPop){
input.pop();
}
}
//empty operator stack to output queue
while(!operators.empty()){
if(operators.top() != "(" && operators.top() != ")"){
output.push(operators.top());
}
operators.pop();
}
/*queue<string> temp = output;
while(!temp.empty()){
cout << temp.front();
temp.pop();
}
cout << endl;*/
//evaluate postfix
while(!output.empty()){
if(isNum(output.front())){
evaluateStack.push(stoi(output.front()));
}else if(isString(output.front()) && !isFunction(output.front())){
if(mappedValues[output.front()]==false){
throw 2;
}else{
evaluateStack.push(map[output.front()]);
}
}else if(isOperator(output.front()) && !isFunction(output.front())){
double x = evaluateStack.top();
evaluateStack.pop();
double y = evaluateStack.top();
evaluateStack.pop();
if(output.front() == "+"){
evaluateStack.push(y + x);
}
if(output.front() == "-"){
evaluateStack.push(y - x);
}
if(output.front() == "/"){
if(x == 0){
throw 1;
return 0;
}else{
evaluateStack.push(y/x);
}
}
if(output.front() == "*"){
evaluateStack.push(y * x);
}
if(output.front() == "^"){
evaluateStack.push(pow(y,x));
}
}else if(isFunction(output.front())){
double x = evaluateStack.top();
evaluateStack.pop();
if(output.front() == "sin"){
evaluateStack.push(sin(x));
}
if(output.front() == "cos"){
evaluateStack.push(cos(x));
}
if(output.front() == "log"){
evaluateStack.push(log(x));
}
}
output.pop();
}
//return final value
returnValue = evaluateStack.top();
return returnValue;
}
int main(){
string inputString;
string typeOfFunction;
string tempString;
string name;
string data;
string quit;
double value = 1;
bool operatedOn = true;
tr1::unordered_map <string, double> map;
tr1::unordered_map <string, bool> mappedVars;
queue<string> input;
map["Pi"]=3.14169;
map["e"]=2.718;
mappedVars["Pi"]=true;
mappedVars["e"]=true;
while(quit != "quit"){
//take in input
getline(cin,inputString);
inputString.append(" ");
for(unsigned int startIndex=0;startIndex<inputString.length();startIndex++){
if(inputString.at(startIndex) != ' '){
for(unsigned int endIndex=startIndex;endIndex<inputString.length();endIndex++){
if(inputString.at(endIndex) == ' ' && noSpaces(substring(inputString,startIndex,endIndex-1)) != ""){
input.push(noSpaces(substring(inputString,startIndex,endIndex-1)));
startIndex = endIndex;
}else if(endIndex == inputString.length()-1){
input.push(noSpaces(substring(inputString,startIndex,endIndex-1)));
}
}
}
}
if(input.front() == "quit"){
break;
}
//evaluating expression
if(input.front() != "let" && input.size()!=1){
try{
cout << evaluate(map, mappedVars,input) << endl;
}catch(int e){
if(e == 1){
cout << "ERROR: Division by zero" << endl;
}else if (e == 2){
cout << "ERROR: Undeclared Variable" << endl;
}
}
operatedOn = false;
}
//setting var
if(input.front()=="let"){
input.pop();
name = noSpaces(input.front());
input.pop();
try{
value = evaluate(map, mappedVars, input);
}catch(int e){
if(e == 1){
cout << "ERROR: Division by zero" << endl;
}else if (e == 2){
cout << "ERROR: Undeclared Variable" << endl;
}
}
map[name]= value;
mappedVars[name]= true;
operatedOn=false;
}
//print var
if(mappedVars[input.front()] && operatedOn){
cout << map[input.front()] << endl;
}else if(mappedVars[input.front()]==false && operatedOn){
cout << "Error: Undeclared Variable" << endl;
}
operatedOn=true;
while(!input.empty()){
input.pop();
}
}
return 0;
}