-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathHandler.cpp
More file actions
292 lines (271 loc) · 8.9 KB
/
Copy pathMathHandler.cpp
File metadata and controls
292 lines (271 loc) · 8.9 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
#include "MathHandler.h"
int domath(std::string thestring, std::map<std::string, int> intmap, std::map<std::string, std::string> stringmap, std::map<std::string, float> floatmap)
{
//thestring = removespaces(thestring);
thestring = turnvarstovals(thestring, intmap, stringmap, floatmap);
return mathhandler(thestring);
}
std::string turnvarstovals(std::string thestring, std::map<std::string, int> intmap, std::map<std::string, std::string> stringmap, std::map<std::string, float> floatmap)
{
if(DEBUG == true)
{
std::cout << "____VARSTOVALS_____" << std::endl;
}
std::string letters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
std::string variablestring;
int firstpos = 0;
int secondpos = -1;
for(int i = 0; i < thestring.size(); i++) //loops through the whole string
{
//reset the position values
firstpos = 0;
secondpos = -1;
//TODO: STEP ONE, GET THE VARIABLE
//std::cout << i << std::endl;
if(isalpha(thestring[i])) //trigger on the first letter
{
if(DEBUG == true)
std::cout << "found alpha " << thestring[i] << " at" << i << std::endl;
firstpos = i;
secondpos = thestring.find_first_not_of(letters); //find the first non-character
if(DEBUG == true)
{
std::cout << "firstposition " << firstpos << std::endl;
std::cout << "secondposition " << secondpos << std::endl;
}
}
if(secondpos >= 0 && secondpos <= thestring.size()) //check if the string found any letters
{
if(DEBUG == true)
std::cout << "Running code to convert variable to number" << std::endl;
//I should have the first string of characters. I now want to make two strings.
//one with the normal numbers, one with the string.
//TODO: now I need to use this to find the number I need
variablestring = thestring.substr(firstpos, secondpos-firstpos);
thestring = thestring.substr(0, firstpos) + thestring.substr(secondpos);
if(DEBUG == true)
std::cout << "Found variable: " << variablestring << std::endl;
//TODO:
//now I need the number I want,
//then I want to insert that number into the string at firstpos
//I am currently passing stringmap, intmap, and floatmap to this function.
int repvar;
float frepvar;
std::string srepvar;
if(intmap.find(variablestring) != intmap.end())
{
repvar = intmap[variablestring];
std::stringstream constr;
constr << repvar;
std::string therepvar = constr.str();
thestring = thestring.substr(0, i) + therepvar + thestring.substr(i);
}
else if(stringmap.find(variablestring) != stringmap.end())
{
srepvar = stringmap[variablestring];
thestring = thestring.substr(0, i) + srepvar + thestring.substr(i + srepvar.size());
}
else if(floatmap.find(variablestring) != floatmap.end())
{
std::stringstream fconstr;
fconstr >> frepvar;
std::string thefrepvar = fconstr.str();
frepvar = floatmap[variablestring];
thestring = thestring.substr(0, i) + thefrepvar + thestring.substr(i + thefrepvar.size());
}
if(DEBUG == true)
std::cout << "Completed string: " << thestring << std::endl;
}
//STEP TWO: FIND EXPONENTS
if(thestring[i] == '^')
{
//DONE: find and replicate areas of (2x+5)^5
int power = thestring[i + 1] - '0'; //store the power as an integer
thestring = thestring.substr(0, i) + thestring.substr(i+2); //remove the ^x from the string
std::string replicatestring;
//i = i - 2; //adjust i back to account for the removed two characters
if(thestring[i-1] == ')')
{
//find the opening parens
int locofcloseparens = i;
int locofopenparens = -1;
for(int j = i; j > 0; j--)
{
if(thestring[j] == '(')
{
locofopenparens = j;
break;
}
}
if(locofopenparens == -1)
{
std::cerr << "Failed to find Opening Parens for exponential operator" <<std::endl;
}
//Begin process of replicating what is in the parens
//std::cout << locofopenparens << " " << locofcloseparens << std::endl;
replicatestring = thestring.substr(locofopenparens, locofcloseparens - locofopenparens);
//std::cout << replicatestring <<std::endl;
}
else
{
replicatestring = std::string(1, thestring[i - 1]); //store the thing to be exponential
}
for(int j = 0; j < power - 1; j++) //loop one less than the power, to add the number of times X has to be multiplied
{
thestring = thestring.substr(0, i) + "*" + replicatestring + thestring.substr(i); //add in the power and the multiple
i = i + 2; // adjust I for the new string size
//std::cout << thestring << std::endl; //debug
}
}
}
if(DEBUG == true)
{
std::cout << "returning " << thestring <<std::endl;
std::cout << "____ENDVARSTOVALS_____" << std::endl;
}
return thestring;
}
//TODO: should be working, honestly, I hope.
//TODO: make the function work with things other than int
//
//how to do this?
//Methods: use templates
//Return a double by default, make all vars doubles, remove strings
//return an int by default, make all vars ints, remove strings
//I honestly am not sure
//
//TODO: devide this massive fucking thing into sub-parts.
int mathhandler(std::string thestring)
{
//std::cout << "--------------" << std::endl;
//std::cout << thestring << std::endl;
//find if there is a ) that is not the last character in the string
for(int i = 0; i < thestring.size(); i++)
{
if((thestring[i] == ')' || thestring[i] == '(') && (i != thestring.size() - 1))
break;
else if(thestring[i] == ')' && (i == thestring.size() - 1))
{ //if there is not remove the () from the string
thestring = thestring.substr(1, thestring.size() - 2);
//std::cout << thestring << std::endl;
}
}
//find the place to split at
int ignorelines = 0;
std::string highestfound = "";
int splitlocation = 0; // the first value should never be an operator
for(int i = 0; i < thestring.size(); ++i)
{
if(ignorelines == 0)
{
if(thestring[i] == '(')
{
ignorelines++;
//std::cout << "ignorelines " << ignorelines << std::endl;
}
else if(thestring[i] == '+')
{
highestfound = "+";
splitlocation = i;
}
else if(thestring[i] == '-')
{
if(highestfound != "+")
{
highestfound = "-";
splitlocation = i;
}
}
else if(thestring[i] == '/')
{
if(highestfound != "+" && highestfound != "-")
{
highestfound = "/";
splitlocation = i;
}
}
else if(thestring[i] == '*')
{
if(highestfound != "+" && highestfound != "-" && highestfound != "/")
{
highestfound = "*";
splitlocation = i;
}
}
}
else
{
if(thestring[i] == ')')
{
ignorelines--;
//std::cout << "ignorelines " << ignorelines << std::endl;
}
if(thestring[i] == '(')
ignorelines++;
/*if((thestring[i] == '=' || thestring[i] == '/' || thestring[i] == '|' || thestring[i] == '&') && splitlocation == 0)
{
if(thestring[i+1] == '(')
splitlocation = i;
}*/
}
}
//std::cout << splitlocation << std::endl;
if(splitlocation == 0) //if no operators were found in the string
{
//if no ops are found, it could be two cases
//1 (35+34)
//2 24
//case 1, continue recursion
//case 2, return the int
//if case one
if(thestring[0] == '(' && thestring[thestring.size()-1] == ')')
return mathhandler(thestring.substr(1, thestring.size()-2)); //the string minus the ()
else //if case 2
{
//std::cout << "Returning " << std::atoi(thestring.c_str()) << std::endl;
return std::atoi(thestring.c_str());
}
return 0;
}
else //if operators were found in the string
{
char theoperator = thestring[splitlocation];
//std::cout << "operator = " << theoperator << std::endl;
//I need to switch this and go through every possible operator, returning what is needed
//'=' leads to return function(left) = function(right)
switch(theoperator){
case '*':
//std::cout << "calculating *" << std::endl;
return (mathhandler(thestring.substr(0, splitlocation)) * mathhandler(thestring.substr(splitlocation + 1)));
break;
case '/':
//std::cout << "calculating /" << std::endl;
return ((mathhandler(thestring.substr(0, splitlocation))) / mathhandler(thestring.substr(splitlocation + 1)));
break;
case '+':
//std::cout << "calculating +" << std::endl;
//std::cout << thestring.substr(splitlocation + 1) << std::endl;
return (mathhandler(thestring.substr(0, splitlocation)) + mathhandler(thestring.substr(splitlocation + 1)));
break;
case '-':
//std::cout << "calculating -" << std::endl;
return ( (mathhandler(thestring.substr(0, splitlocation))) - (mathhandler(thestring.substr(splitlocation + 1))) );
break;
}
}
}
std::string removespaces(std::string thestring)
{
if(DEBUG == true)
std::cout << "Before removal: " << thestring << std::endl;
for(int i = 0; i < thestring.size(); i++)
{
if(thestring[i] == ' ')
{
thestring = thestring.substr(0, i) + thestring.substr(i+1);
}
}
if(DEBUG == true)
std::cout << "Removed spaces: " << thestring << std::endl;
return thestring;
}