-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.cpp
368 lines (336 loc) · 8.84 KB
/
expression.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
#include "expression.h"
//PUBLIC
//..CONSTRUCTOR
expression::expression()
{
//initialization done in class variable declarations
}
//..MAIN PROGRAM
void expression::run(int argc, char *argv[]) {
bool record = false;
switch (argc) {
case 1:
break;
case 2: { //TESTED
if(string(argv[1]) == "/h" ||
string(argv[1]) == "/?") {
help();
} else {
string filename(argv[1]);
load(filename);
}
break;
}
case 3: {
if(toUpper(string(argv[1]))=="EXECUTE") { //TESTED
execute(string(argv[2]));
}
if(toUpper(string(argv[1]))=="RECORD") {
record = true;
cout<<"recording to "<<argv[2]<<endl;
}
break;
}
default:
cout<<"argument error"<<endl;
exit(1);
//throw error
break;
}
while(1) {
prompt();
string line = getCommand(cin);
execCommand(line);
if(record)
recordToFile(argv[2], line);
}
}
//..TESTING
void expression::test() {
saveStringToFile("test.txt", "the fist line");
saveStringToFile("test.txt", "the second line", true);
}
//PRIVATE
//..MAIN FUNCTIONS
void expression::prompt() { //TESTED
cout<<"INPUT: ";
}
void expression::help() {
cout<<"Examples for INPUT: \n"
<< "LET F = 2X + 4 \n"
<< "EVAL F(1/4) \n"
<< "PRINT F \n"
<< "LOAD filename.exp \n"
<< "SAVE filename.exp \n"
<< "NEWTON F 5 \n"
<< "F = G + H \n"
<< "\n"
<< "Examples for command line arguments: \n"
<< "test1.exp \n"
<< "EXECUTE script1.spt \n"
<< "RECORD record1.spt \n"
<< "/h \n"
<< endl;
}
string expression::getCommand(istream& in) { //TESTED: cout<<getCommand(cin)<<endl;
if(in.peek()=='\n')
exit(1);
string temp;
getline(in, temp);
return temp;
}
void expression::execCommand(string command) {
/* sample argm:
* LET F = 2X + 4
* EVAL F(1/4)
* PRINT F
* LOAD filename.exp
* SAVE filename.exp
* NEWTON F 5
* F = G + H
*/
string noSpaceCommand = delSpace(command),
upperCommand = toUpper(noSpaceCommand);
for(unsigned int i = 0; i < 6; ++i) {
size_t pos=0;
if((pos = upperCommand.find(commandArr[i])) != string::npos) {
string exp = noSpaceCommand.substr(pos+commandArr[i].length()) ;
switch (i) {
case 0: //LET, exp = "F=2X+4", TESTED
{
char funcName = exp[0];
exp.erase(0,2); //"2X+4"
let(funcName, exp);
break;
}
case 1: //EVAL, exp = "F(1/4)", TESTED
{
char funcName = exp[0];
fraction value(exp.substr(2,exp.length()-1-2));
eval(funcName, value); //value = 1/4
break;
}
case 2: //PRINT, exp = "F", TESTED
{
char funcName = exp[0];
print(funcName);
break;
}
case 3: //LOAD, exp = "FILENAME.EXP", TESTED
{
load(exp);
break;
}
case 4: //SAVE, exp = "FILENAME.EXP", TESTED
{
save(exp);
break;
}
case 5: //NEWTON, exp = "F10.5"
{
char funcName = exp[0];
fraction initGuess(exp.substr(1));
newton(funcName, initGuess);
}
default:
break;
}
return;
}
}
algebra(noSpaceCommand); //"F=G+H", TESTED
}
void expression::let(char funcName, string exp) { //NOT FULLY WORKING - setExp...
/* sample argm:
* 2X+4
*/
exps[index(funcName)].setExp(exp);
print(funcName);
}
void expression::eval(char funcName, fraction value) { //PARTIALLY TESTED
stringstream ss;
string temp;
ss<<funcName<<'('<<value<<") = "
<<exps[index(funcName)].evaluate(value);
getline(ss,temp);
cout<<temp<<endl;
}
void expression::print(char funcName) { //TESTED
stringstream ss;
string temp;
ss<<funcName<<" = "<<exps[index(funcName)];
getline(ss,temp);
cout<<temp<<endl;
}
void expression::load(string& filename) {
ifstream ifs;
string line;
if(filename.empty()) {
cout<<"invalid filename"<<endl;
return;
//throw error
}
if(filename.find(".exp")>filename.size())
filename+=".exp";
ifs.open(filename);
if(ifs.fail()) {
cout<<"file does not exist"<<endl;
return;
//throw error
}
cout<<"loaded "<<filename<<endl;
while(!ifs.eof())
{
stringstream ss;
getline(ifs, line);
if(!line.empty()){
ss<<"LET "<<line;
getline(ss, line);
// cout<<"##line: "<<line<<endl;
execCommand(line);
}
}
}
void expression::save(string& filename) {
ifstream ifs;
ofstream ofs;
string line;
if(filename.empty()) {
cout<<"invalid filename"<<endl;
return;
//throw error
}
if(filename.find(".exp")>filename.size())
filename+=".exp";
ifs.open(filename);
if(!ifs.fail()) {
cout<<"file already exists, overwrite?"<<endl;
getline(cin, line);
if(toupper(line[0])!='Y')
return;
}
ofs.open(filename);
for(char i = 'A'; i <= 'Z'; ++i) {
ofs<<i<<" = "<<exps[index(i)]<<endl;
}
cout<<"saved to "<<filename<<endl;
}
void expression::algebra(string algebraExp) { //TESTED
/* sample argm:
* F=G+H
* F=G-H
* F=G*H
* F=G'
* F=G'''
*/
size_t pos;
if((pos = algebraExp.find_first_of("+-*"))!= string::npos) {
char fn1 = algebraExp[0], //funcName
fn2 = algebraExp[2],
fn3 = algebraExp[4];
switch (algebraExp[pos]) {
case '+': //TESTED
exps[index(fn1)] = exps[index(fn2)] + exps[index(fn3)];
break;
case '-': //TESTED
exps[index(fn1)] = exps[index(fn2)] - exps[index(fn3)];
break;
case '*': //TESTED
exps[index(fn1)] = exps[index(fn2)] * exps[index(fn3)];
break;
default:
break;
}
print(fn1);
return;
}
if((pos = algebraExp.find('\''))!= string::npos) { //TESTED
char fn1 = algebraExp[0],
fn2 = algebraExp[2];
int derCount = 1;
while((pos = algebraExp.find('\'', pos+1))!= string::npos)
derCount++;
exps[index(fn1)]= nthDerivative(exps[index(fn2)], derCount);
print(fn1);
return;
}
//throw error here
}
void expression::execute(string filename) {
ifstream ifs;
string line;
if(filename.empty()) {
cout<<"invalid filename"<<endl;
return;
//throw error
}
if(filename.find(".spt")>filename.size())
filename+=".spt";
ifs.open(filename);
if(ifs.fail()) {
cout<<"file does not exist"<<endl;
return;
//throw error
}
cout<<"loaded "<<filename<<endl;
while(!ifs.eof())
{
stringstream ss;
getline(ifs, line);
if(!line.empty()){
ss<<line;
getline(ss, line);
cout<<"##line: "<<line<<endl;
execCommand(line);
}
}
}
void expression::recordToFile(string filename, string line) {
if(filename.find(".spt")>filename.size())
filename+=".spt";
saveStringToFile(filename, line, true);
}
void expression::newton(char funcName, fraction initGuess) {
polynomial exp1(exps[index(funcName)]),
exp2 = firstDerivative(exp1);
double x = initGuess.evaluate();
double epsilon = 0.1;
double h = exp1.evaluate(x) / exp2.evaluate(x);
while(abs(h)>= epsilon) {
h = exp1.evaluate(x) / exp2.evaluate(x);
x = x - h;
}
cout<<"root of "<<funcName<<" is "<<x<<endl;
}
//..ADDITIONAL FUNCTIONS
string expression::toUpper(string str) { //TESTED
string temp;
for(string::iterator it=str.begin(); it!=str.end(); ++it){
temp.push_back(toupper(*it));
}
return temp;
}
string expression::delSpace(string str) { //TESTED
string temp=str;
size_t pos=0;
while((pos=temp.find(" ", pos+1))!=string::npos) {
temp.erase(pos,1);
}
return temp;
}
int expression::index(char funcName) { //TESTED
return toupper(funcName)-65;
}
void expression::saveStringToFile(string filename, string line, bool append) {
ofstream ofs;
if(append)
ofs.open(filename, ios_base::app);
else
ofs.open(filename);
ofs<<line<<endl;
//check if there is line endings
}
void expression::askString(string prompt, string& theString) {
cout<<prompt;
cin>>theString;
}