forked from XiaoxiZheng/SHUNT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber_Classification.cpp
More file actions
372 lines (364 loc) · 9.05 KB
/
Number_Classification.cpp
File metadata and controls
372 lines (364 loc) · 9.05 KB
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
#include <iostream>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <vector>
#include <cstring>
#include <cmath>
#include <string>
#include <sstream>
#include <fstream>
#include <exception>
#include "Number_Classification.h"
#define PI 3.141592653589793
#define e 2.71828182846
using namespace std;
void Number_Classification::enterExpression(char z){
z == 'y' || z == 'Y' ? this->frac_float = 1 : this->frac_float = 0;
cout << "Enter Expression: " << endl;
getline(cin, this->expression);
this->exptoToken(z);
}
bool Number_Classification::is_number(const std::string& s){
std::string nu = s;
int number = 0;
std::stringstream m(nu);
if (m >> number){
return true;
}
else{
return false;
}
}
int Number_Classification::precedence(string s){
switch(s.c_str()[0]){
case '^':
return 2;
case '*':
case '/':
return 1;
case '+':
case '-':
return 0;
default:
return -1;
}
}
bool Number_Classification::is_operator(const std::string& s){
if(s.compare("+") == 0 || s.compare("-") == 0 ||
s.compare("*") == 0 || s.compare("/") == 0 ||
s.compare("^") == 0 || s.compare(")") == 0 ||
s.compare("(") == 0){
return true;
}
return false;
}
bool Number_Classification::is_function(const std::string& s){
if (s.find("sqrt:") != std::string::npos) {
return true;
}
if (s.find("rt:") != std::string::npos) {
return true;
}
if (s.find("log_") != std::string::npos) {
return true;
}
return false;
}
bool Number_Classification::is_sqrt(const std::string& s){
if (s.find("sqrt:") != std::string::npos) {
return true;
}
return false;
}
bool Number_Classification::is_log(const std::string& s){
if (s.find("log_") != std::string::npos) {
return true;
}
return false;
}
bool Number_Classification::is_rt(const std::string& s){
if (s.find("rt:") != std::string::npos) {
return true;
}
return false;
}
string Number_Classification::getPrevExp(int a){
if( a > this->prevExpressions.size()){
return "Does not exist";
}
return this->prevExpressions.at(this->prevExpressions.size() - a);
}
void Number_Classification::replace_all(string& input, string& find, string& rep){
size_t p = 0;
while((p = input.find(find,p)) != std::string::npos){
input.replace(p, find.length(), rep);
p += rep.length();
}
}
void Number_Classification::exptoToken(char z){
shunt:
if (this->expression.find(" ") == std::string::npos) {
cout<<"Please put a space between every input for system readibilty.";
}
if(this->expression.find("ans") != std::string::npos && this->prevExpressions.size() == 0){
cout << "\nNo Previous Answer(s). Re-enter an expression.\n" << endl;
this->enterExpression('y');
return;
}
if(this->expression.find("ans") != std::string::npos && this->prevExpressions.size() != 0){
string ans = "ans";
replace_all(this->expression, ans, this->prevExpressions.back());
}
stringstream p(this->expression);
string t;
char* q;
while(getline(p,t,' ')){
this->expToken.push_back(t);
}
bool passed = false;
for(int i=0; i < expToken.size(); i++){
if(is_function(expToken.at(i))){
if(is_sqrt(expToken.at(i))){
Sqrt q(expToken.at(i));
expToken.at(i) = q.simRoots();
passed = true;
}
/*if(is_log()){
}
if(is_rt()){
}*/
}
if(expToken.at(i).compare("pi") == 0){
expToken.at(i) = "pi";
passed = true;
}
if(expToken.at(i).compare("e") == 0){
expToken.at(i) = "e";
passed = true;
}
}
if(passed == true && (z == 'Y' || z == 'y')){
string t;
cout << "\n";
for(int i=0; i < expToken.size(); i++){
t+=expToken.at(i) + " ";
}
prevExpressions.push_back("( "+ t +" )");
expressions_b.push_back(this->expression);
cout << "\n\n";
if(!is_function(t) && t.find("e") == std::string::npos && t.find("pi") == std::string::npos){
this->expression = t;
expToken.clear();
goto shunt;
}
cout << t << endl;
expToken.clear();
return;
}
this->shunting_Yard();
}
bool Number_Classification::is_left(char op){
switch(op){
case '+':
case '-':
case '*':
case '/':
return true;
default:
return false;
}
}
void Number_Classification::evalulate(){
stack<string> eval;
stack<float> decimal_eval;
string o;
while(!numbers.empty()){
o = numbers.front();
numbers.pop();
if(is_number(o)){
eval.push(o);
decimal_eval.push(atof(o.c_str()));
}
/*else if(is_function(o)){
}*/
else{
if(decimal_eval.size() > 1){
float t1 = decimal_eval.top();
decimal_eval.pop();
float t2 = decimal_eval.top();
decimal_eval.pop();
decimal_eval.push(operate(t2,t1, o[0]));
}
switch(o[0]){
case '*':
multiply(eval);
break;
case '/':
divide(eval);
break;
case '+':
add(eval);
break;
case '-':
subtract(eval);
break;
/*case 's':
eval.push(o);
square_root(eval);
break;*/
case '^':
exponent(eval);
break;
}
}
}
if(this->frac_float == 1){
cout << "\n" << eval.top() << " - Fractional\n" << endl;
prevExpressions.push_back("( "+eval.top()+" )");
}
if(this->frac_float == 0){
cout << "\n" << decimal_eval.top() << " - Float Representation\n" << endl;
stringstream ss;
ss << decimal_eval.top();
prevExpressions.push_back("( "+ss.str()+" )");
}
expressions_b.push_back(this->expression);
expToken.clear();
}
void Number_Classification::writeFile(){
ofstream myfile;
myfile.open("Previous_Expressions.txt");
cout << "\n\nSaving evaluated expressions to text file...\n" << endl;
for(int i=0;i<prevExpressions.size();i++){
myfile << expressions_b.at(i) + " = " + prevExpressions.at(i) + '\n';
}
cout << "Saved.\n\n" << endl;
myfile.close();
}
string Number_Classification::square_root(stack<string>& stack){
Sqrt t(stack.top());
stack.pop();
stack.push(t.simRoots());
cout << t.simRoots() << endl;
return t.simRoots();
}
string Number_Classification::multiply(stack<string>& stack){
Fraction t1(stack.top());
stack.pop();
Fraction t2(stack.top());
stack.pop();
Fraction t3 = t1*t2;
stack.push(t3.toString());
return t3.toString();
}
string Number_Classification::divide(stack<string>& stack){
Fraction t1(stack.top());
stack.pop();
Fraction t2(stack.top());
stack.pop();
Fraction t3 = t1/t2;
stack.push(t3.toString());
return t3.toString();
}
string Number_Classification::add(stack<string>& stack){
Fraction t1(stack.top());
stack.pop();
Fraction t2(stack.top());
stack.pop();
Fraction t3 = t1+t2;
stack.push(t3.toString());
return t3.toString();
}
string Number_Classification::subtract(stack<string>& stack){
Fraction t1(stack.top());
stack.pop();
Fraction t2(stack.top());
stack.pop();
Fraction t3 = t2-t1;
stack.push(t3.toString());
return t3.toString();
}
string Number_Classification::exponent(stack<string>& stack){
Fraction t1(stack.top());
stack.pop();
Fraction t2(stack.top());
stack.pop();
Fraction t3 = t2^t1;
stack.push(t3.toString());
return t3.toString();
}
float Number_Classification::operate(float num, string func){
/*if(func == "sin")
return sin(num);
else if(func == "cos")
return cos(num);
else if(func == "log")
return log10(num); */
return 0;
}
float Number_Classification::operate(float n1, float n2, char z){
switch(z)
{
case '+':
return n1 + n2;
case '-':
return n1 - n2;
case '*':
return n1 * n2;
case '/':
return n1 / n2;
case '^':
return pow(n1, n2);
default:
return 0;
}
}
bool Number_Classification::shunting_Yard(){
for(int i=0;i<this->expToken.size();i++){
if(is_number(this->expToken.at(i))){
this->numbers.push(this->expToken.at(i));
}
else if(is_function(this->expToken.at(i)) && !is_operator(this->expToken.at(i))){
this->operators.push(this->expToken.at(i));
}
else{
bool left = is_left(this->expToken.at(i)[0]);
int prec = this->precedence(this->expToken.at(i));
if(expToken.at(i) != "(" && expToken.at(i) != ")"){
// ENCASED OR IN OWN PARANTHESIS
while(!this->operators.empty() && (( left && prec <= this->precedence(this->operators.top())) || (!left && prec < this->precedence(this->operators.top())))){
this->numbers.push(this->operators.top());
this->operators.pop();
}
this->operators.push(this->expToken.at(i));
}
if(this->operators.size() == 0 || this->expToken.at(i).compare("(") == 0){
this->operators.push(this->expToken.at(i));
}
if(this->expToken.at(i).compare(")") == 0){
while(this->operators.top().compare("(") != 0){
this->numbers.push(operators.top());
this->operators.pop();
}
if(this->operators.top().compare("(") == 0){
operators.pop();
}
if(!operators.empty() && is_function(this->operators.top())){
this->numbers.push(this->operators.top());
this->operators.pop();
}
}
}
}
while(!this->operators.empty()){
if(this->operators.top().compare("(") == 0 || this->operators.top().compare(")") == 0 ){
cout << "MisMatched Paranthesis\n" << endl;
return false;
}
this->numbers.push(this->operators.top());
this->operators.pop();
}
this->evalulate();
return true;
}