-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser2.h
827 lines (664 loc) · 26.8 KB
/
parser2.h
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
#include <iostream>
#include <string>
#include "math.h"
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <fstream>
#include "matrix.h"
#include "functions.h"
using namespace std;
extern int numberofmatrixVariables;
extern std::string* matrixNames;
extern asu::CMatrix* matrixVariables;
/**parns_are_incomplete function it takes string and return true if open brackets is more than closed brackets
and returns false otherwise */
bool parens_are_incomplete(std::string matrix_string)
{
unsigned int open_counter = 0 , close_counter=0 ;
for (unsigned int i=0 ; i<matrix_string.length() ; i++)
{
if(matrix_string[i] == '[')
open_counter++;
if(matrix_string[i] == ']')
close_counter++;
}
if (open_counter > close_counter )
return true ;
else return false ;
}
std::string removeSpaces(std::string input) //remove spaces from the beginning to '[' or the end of the line
{
//int length = input.length();
if (input == "\n" || input == "" ) return "";
for (int i = 0; !( (input[i]=='[') || (input[i]=='\0') ); i++)
{
if(input[i] == ' ' )
{input.erase(i, 1);i--;}
}
return input;
}
asu::CMatrix stringtomatrix (std::string s, int k) //return matrix value of a string name
{
int i;
for (i =k+1 ; i>=0; i--)
{
if(matrixNames[i]==s) return matrixVariables[i];
}
if(i<=0) throw std::invalid_argument("Impossible variable name");
asu::CMatrix nomatrix; return nomatrix ;
}
std::string to_string(double operand){
char buffer_test[50];
sprintf(buffer_test,"%lf",operand);
return(std::string)buffer_test;
}
double to_double(std::string operand){
char * buffer=new char[operand.length()+1];
strcpy(buffer,operand.c_str());
double result=atof(buffer);
delete [] buffer;
return result;
}
/*this function is responsable for calcualating any expression containing any basic operations
like power,mul,div,add,sub,neg on numbers or matrices using piorities.
arguments:the expression that needs to be calculated and it is passed by reference to append in it
return:void
*/
void basicOperationCalculation(std::string& expression){
int count=0;//number of temporary matrix variables
//searching for any operation contains power operator.
while(expression.find('^')!=std::string::npos){
unsigned int operatorPosition=0;//position of operator
//determining the position of the operator
for(unsigned int i=0;i<expression.length();i++){
if(expression[i]=='^'){
operatorPosition=i;
break;
}
}
//if the operator at the end of the expression ,it throws expection
if(operatorPosition==expression.length()-1){
throw std::invalid_argument
("Syntax Error");
}
int startPosition=0;//the position of the first char in the operation
int endPosition=expression.length()-1;//the position of the last char in the operation
//determining the position of the first char in the operation
for(int i=operatorPosition-1;i>=0;i--){
if(!((expression[i]>='.'&&expression[i]<='9')&&expression[i]!='/')&&(!(expression[i]>='a'&&expression[i]<='z'))&&(!(expression[i]>='A'&&expression[i]<='Z')))
{
startPosition=i+1;
break;
}
}
//determining the position of the last char in the operation
for(unsigned int j=operatorPosition+1;j<expression.length();j++){
if(!((expression[j]>='.'&&expression[j]<='9')&&expression[j]!='/')&&(!(expression[j]>='a'&&expression[j]<='z'))&&(!(expression[j]>='A'&&expression[j]<='Z'))&&(expression[operatorPosition+1]!='-'||j!=operatorPosition+1)){
endPosition=j-1;
//
break;
}
}
std::string operand1;//the first operand in the operation
std::string operand2;//the second operand in the operation
if(expression[operatorPosition-1]=='.'){
//extract the first operand in case of elementwise operation
operand1=expression.substr(startPosition,operatorPosition-startPosition-1);
}
else{
//extract the first operand
operand1=expression.substr(startPosition,operatorPosition-startPosition);
}
operand2=expression.substr(operatorPosition+1,endPosition-operatorPosition);//extract the second operand
//incase of the operands are matrices
if((!isdigit(operand1[0])||!isdigit(operand2[0]))&&operand1[0]!='-'&&operand2[0]!='-'){
double powerValue=to_double(operand2);
asu::CMatrix resultMatrix ;
if(expression[operatorPosition-1]=='.'){
//elementwise operation
resultMatrix =power_modified_elementwise(stringtomatrix(operand1,numberofmatrixVariables),powerValue);
}
else{//normal operation
resultMatrix =power_modified(stringtomatrix(operand1,numberofmatrixVariables),powerValue);}
count++;
numberofmatrixVariables++;
matrixVariables[numberofmatrixVariables]=resultMatrix;//saving the result in the array of matrices
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);//saving the name of resulting matrices
expression.replace(startPosition,endPosition-startPosition+1,matrixNames[numberofmatrixVariables]);//appending the expression with the result
}
else{
double operand_double=to_double(expression.substr(startPosition,operatorPosition-startPosition));
double power=to_double(expression.substr(operatorPosition+1,endPosition-operatorPosition));
double result=pow(operand_double,power);
std::string replacement=to_string(result);
expression.replace(startPosition,endPosition-startPosition+1,replacement);
}
}
while(expression.find('*')!=std::string::npos||expression.find('/')!=std::string::npos){
unsigned int operatorPosition=0;
for(unsigned int i=0;i<expression.length();i++){
if(expression[i]=='*'||expression[i]=='/'){
operatorPosition=i;
break;
}
}
if(operatorPosition==expression.length()-1){
throw std::invalid_argument
("Syntax Error");
}
int startPosition=0;
int endPosition=expression.length()-1;
//&&expression[0]!='-'
for(int i=operatorPosition-1;i>=0;i--){
if(!((expression[i]>='.'&&expression[i]<='9')&&expression[i]!='/')&&(!(expression[i]>='a'&&expression[i]<='z'))&&(!(expression[i]>='A'&&expression[i]<='Z')))
{
startPosition=i+1;
break;
}
}
if(startPosition==1&&expression[0]=='-')
{
startPosition=0;
}
for(unsigned int j=operatorPosition+1;j<expression.length();j++){
if(!((expression[j]>='.'&&expression[j]<='9')&&expression[j]!='/')&&(!(expression[j]>='a'&&expression[j]<='z'))&&(!(expression[j]>='A'&&expression[j]<='Z'))&&(expression[operatorPosition+1]!='-'||j!=operatorPosition+1)){
endPosition=j-1;
//
break;
}
}
std::string operand1;
std::string operand2;
if(expression[operatorPosition-1]=='.'){
operand1=expression.substr(startPosition,operatorPosition-startPosition-1);
}
else{ operand1=expression.substr(startPosition,operatorPosition-startPosition);}
if(expression[endPosition]=='.'){
endPosition-=1;
}
operand2=expression.substr(operatorPosition+1,endPosition-operatorPosition);
if(operand1[0]=='-'&&(!isdigit(operand1[1]))){
double negative=-1;
numberofmatrixVariables++;
matrixVariables[numberofmatrixVariables].CopyMatrix(amul(stringtomatrix(operand1.substr(1,operand1.length()-1),numberofmatrixVariables),negative));
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
operand1=matrixNames[numberofmatrixVariables];
}
if(operand2[0]=='-'&&(!isdigit(operand2[1]))){
double negative=-1;
numberofmatrixVariables++;
matrixVariables[numberofmatrixVariables].CopyMatrix(amul(stringtomatrix(operand2.substr(1,operand2.length()-1),numberofmatrixVariables),negative));
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
operand2=matrixNames[numberofmatrixVariables];
}
if(operand2=="0"&&expression[operatorPosition]=='/')
throw std::invalid_argument
("Division by Zero");
if((!isdigit(operand1[0])||!isdigit(operand2[0]))&&operand1[0]!='-'&&operand2[0]!='-'){
asu::CMatrix firstmatrix;
asu::CMatrix secondmatrix;
double operand_double;
if(!isdigit(operand1[0])&&!isdigit(operand2[0])){
firstmatrix=stringtomatrix(operand1,numberofmatrixVariables);
secondmatrix=stringtomatrix(operand2,numberofmatrixVariables);
if(expression[operatorPosition-1]=='.'){
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '*': matrixVariables[numberofmatrixVariables].CopyMatrix(amul(firstmatrix,secondmatrix)); break;
case '/': matrixVariables[numberofmatrixVariables].CopyMatrix(adiv(firstmatrix,secondmatrix)); break;
}
}
else{
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '*': matrixVariables[numberofmatrixVariables].CopyMatrix(mul(firstmatrix,secondmatrix)); break;
case '/': matrixVariables[numberofmatrixVariables].CopyMatrix(div(firstmatrix,secondmatrix)); break;
}
}
}
if(!isdigit(operand1[0])&&isdigit(operand2[0])){
firstmatrix=stringtomatrix(operand1,numberofmatrixVariables);
operand_double=to_double(operand2);
if(expression[operatorPosition-1]=='.'){
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '*': matrixVariables[numberofmatrixVariables].CopyMatrix(amul(firstmatrix,operand_double)); break;
case '/': matrixVariables[numberofmatrixVariables].CopyMatrix(adiv(firstmatrix,operand_double)); break;
}
}
else{
secondmatrix=operand_double;
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '*': matrixVariables[numberofmatrixVariables].CopyMatrix(mul(firstmatrix,secondmatrix)); break;
case '/': matrixVariables[numberofmatrixVariables].CopyMatrix(div(firstmatrix,secondmatrix)); break;
}
}
}
///
if(isdigit(operand1[0])&&!isdigit(operand2[0])){
operand_double=to_double(operand1);
secondmatrix=stringtomatrix(operand2,numberofmatrixVariables);
if(expression[operatorPosition-1]=='.'){
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '*': matrixVariables[numberofmatrixVariables].CopyMatrix(amul(operand_double,secondmatrix)); break;
case '/': matrixVariables[numberofmatrixVariables].CopyMatrix(adiv(operand_double,secondmatrix)); break;
}
}
else{
firstmatrix=operand_double;
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '*': matrixVariables[numberofmatrixVariables].CopyMatrix(mul(firstmatrix,secondmatrix)); break;
case '/': matrixVariables[numberofmatrixVariables].CopyMatrix(div(firstmatrix,secondmatrix)); break;
}
}
}
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
expression.replace(startPosition,endPosition-startPosition+1,matrixNames[numberofmatrixVariables]);
}
else{
double operand1_double=to_double(expression.substr(startPosition,operatorPosition-startPosition));
double operand2_double=to_double(expression.substr(operatorPosition+1,endPosition-operatorPosition));
double result;
switch(expression[operatorPosition]){
case '*': result=operand1_double*operand2_double; break;
case '/': result=operand1_double/operand2_double; break;
}
std::string replacement=to_string(result);
expression.replace(startPosition,endPosition-startPosition+1,replacement);
}
}
int flag=0;
while((expression.find('+')!=std::string::npos||expression.find('-',flag+1)!=std::string::npos)/*&&(expression[0]!='-'||expression.length()>10)*/){
unsigned int operatorPosition=0;
for(unsigned int i=flag;i<expression.length();i++){
if((expression[i]=='+'||expression[i]=='-')&&i!=0){
operatorPosition=i;
break;
}
}
if(operatorPosition==expression.length()-1){
throw std::invalid_argument
("Syntax Error");
}
int startPosition=0;
int endPosition=expression.length()-1;
for(int i=operatorPosition-1;i>=0;i--){
if(!((expression[i]>='.'&&expression[i]<='9')&&expression[i]!='/')&&(!(expression[i]>='a'&&expression[i]<='z'))&&(!(expression[i]>='A'&&expression[i]<='Z')))
{
startPosition=i+1;
break;
}
}
if(startPosition==1&&expression[0]=='-')
{
startPosition=0;
}
for(unsigned int j=operatorPosition+1;j<expression.length();j++){
if(!((expression[j]>='.'&&expression[j]<='9')&&expression[j]!='/')&&(!(expression[j]>='a'&&expression[j]<='z'))&&(!(expression[j]>='A'&&expression[j]<='Z'))&&(expression[operatorPosition+1]!='-'||j!=operatorPosition+1)){
endPosition=j-1;
//
break;
}
}
std::string operand1;
std::string operand2;
if(expression[operatorPosition-1]=='.'){
operand1=expression.substr(startPosition,operatorPosition-startPosition-1);
}
else{ operand1=expression.substr(startPosition,operatorPosition-startPosition);}
if(expression[endPosition]=='.'){
endPosition-=1;
}
if(expression[operatorPosition-1]==' '||expression[operatorPosition-1]==';'||expression[operatorPosition-1]=='['){
int index=0;
for(unsigned int i=operatorPosition;i<expression.length();i++){
if(expression[i]==' '||expression[i]==']' ||expression[i]==';'){index=i-1; break;}
}
std::string matrixElement=expression.substr(operatorPosition,index-operatorPosition+1);
basicOperationCalculation(matrixElement);
expression.replace(operatorPosition,expression.substr(operatorPosition,index-operatorPosition+1).length(),matrixElement);
flag=operatorPosition+1;
continue;
}
operand2=expression.substr(operatorPosition+1,endPosition-operatorPosition);
if(operand1[0]=='-'&&(!isdigit(operand1[1]))){
double negative=-1;
numberofmatrixVariables++;
matrixVariables[numberofmatrixVariables].CopyMatrix(amul(stringtomatrix(operand1.substr(1,operand1.length()-1),numberofmatrixVariables),negative));
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
operand1=matrixNames[numberofmatrixVariables];
}
if(operand2[0]=='-'&&(!isdigit(operand2[1]))){
double negative=-1;
numberofmatrixVariables++;
matrixVariables[numberofmatrixVariables].CopyMatrix(amul(stringtomatrix(operand2.substr(1,operand2.length()-1),numberofmatrixVariables),negative));
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
operand2=matrixNames[numberofmatrixVariables];
}
if((!isdigit(operand1[0])||!isdigit(operand2[0]))&&operand1[0]!='-'&&operand2[0]!='-'){
asu::CMatrix firstmatrix;
asu::CMatrix secondmatrix;
double operand_double;
if(!isdigit(operand1[0])&&!isdigit(operand2[0])){
firstmatrix=stringtomatrix(operand1,numberofmatrixVariables);
secondmatrix=stringtomatrix(operand2,numberofmatrixVariables);
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '+': matrixVariables[numberofmatrixVariables].CopyMatrix(add(firstmatrix,secondmatrix)); break;
case '-': matrixVariables[numberofmatrixVariables].CopyMatrix(sub(firstmatrix,secondmatrix)); break;
}
}
if((!isdigit(operand1[0]))&&((isdigit(operand2[0]))||(isdigit(operand2[1])&&operand2[0]=='-'))){
firstmatrix=stringtomatrix(operand1,numberofmatrixVariables);
operand_double=to_double(operand2);
if(expression[operatorPosition-1]=='.'){
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '+': matrixVariables[numberofmatrixVariables].CopyMatrix(add(firstmatrix,operand_double)); break;
case '-': matrixVariables[numberofmatrixVariables].CopyMatrix(sub(firstmatrix,operand_double)); break;
}
}
else{
secondmatrix=operand_double;
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '+': matrixVariables[numberofmatrixVariables].CopyMatrix(add(firstmatrix,secondmatrix)); break;
case '-': matrixVariables[numberofmatrixVariables].CopyMatrix(sub(firstmatrix,secondmatrix)); break;
}
}
}
///
if((isdigit(operand1[0])||(isdigit(operand1[1])&&operand1[0]=='-'))&&!isdigit(operand2[0])){
operand_double=to_double(operand1);
secondmatrix=stringtomatrix(operand2,numberofmatrixVariables);
if(expression[operatorPosition-1]=='.'){
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '+': matrixVariables[numberofmatrixVariables].CopyMatrix(add(operand_double,secondmatrix)); break;
case '-': matrixVariables[numberofmatrixVariables].CopyMatrix(sub(operand_double,secondmatrix)); break;
}
}
else{
firstmatrix=operand_double;
numberofmatrixVariables++;
switch(expression[operatorPosition]){
case '+': matrixVariables[numberofmatrixVariables].CopyMatrix(add(firstmatrix,secondmatrix)); break;
case '-': matrixVariables[numberofmatrixVariables].CopyMatrix(sub(firstmatrix,secondmatrix)); break;
}
}
}
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
expression.replace(startPosition,endPosition-startPosition+1,matrixNames[numberofmatrixVariables]);
}
else{
double operand1_double=to_double(expression.substr(startPosition,operatorPosition-startPosition));
double operand2_double=to_double(expression.substr(operatorPosition+1,endPosition-operatorPosition));
double result;
switch(expression[operatorPosition]){
case '+': result=operand1_double+operand2_double; break;
case '-': result=operand1_double-operand2_double; break;
}
std::string replacement=to_string(result);
expression.replace(startPosition,endPosition-startPosition+1,replacement);
}
}
if(expression[0]=='-'&&(!isdigit(expression[1]))){
double negative=-1;
numberofmatrixVariables++;
matrixVariables[numberofmatrixVariables].CopyMatrix(amul(stringtomatrix(expression.substr(1,expression.length()-1),numberofmatrixVariables),negative));
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
expression=matrixNames[numberofmatrixVariables];
}
}
/*
this function is responsable for determining the parthensis and calculating the expressions inside it
it takes care of piorities
arguments:the expression and it is passed by reference to append in it
*/
void parthen_analysis(std::string& expression){
//searching for opening bracket until it finds no brackets
while(expression.find('(')!=std::string::npos)
{
//checking that there is no syntax errors by comparing the number of opening brackets and closing brackets
unsigned int openBracket_counter = 0 , closeBracket_counter=0 ;
for (unsigned int i=0 ; i<expression.length() ; i++)
{
if(expression[i] == '(')
openBracket_counter ++;
if(expression[i] == ')')
closeBracket_counter++;
}
if (openBracket_counter > closeBracket_counter )
throw std::invalid_argument
("Syntax Error");
unsigned int openingBracket=0;//position of first bracket
int closingBracket=expression.length()-1;//position of closing bracket
//finding the positions of opening and closing brackets
for(unsigned int i=0;i<expression.length();i++){
if(expression[i]=='(')
openingBracket=i;
}
for(unsigned int i=0;i<expression.length();i++){
if(expression[i]==')'&&i>openingBracket){
closingBracket=i;
break;
}
}
std::string insideBrackets=expression.substr(openingBracket+1,closingBracket-openingBracket-1);//expression inside brackets
basicOperationCalculation(insideBrackets);//calculation the expression inside brackets
expression.replace(openingBracket,closingBracket-openingBracket+1,insideBrackets);//replacing the result of the expression in the original expression
}
}/*
this function is resposable for searching for trignometric and sqrt expression and calculating it and append the result in the expression
arguments:the expression and it is passed by reference to append in it
*/
void functionsCalculation(std::string& expression){
//if the expression is sin or cos,... only it throws exception
if(expression=="sin"||expression=="cos"||expression=="tan"||expression=="sqrt")
throw std::invalid_argument("error using " + expression + "\n not enough input arguments");
//searching for sin in the expression
while(expression.find("sin")!=std::string::npos){
int first_pos=expression.find("sin");//position of first occurence of sin in the string
int end_pos=expression.find(')',first_pos);//position of the end of sin expression
std::string function=expression.substr(first_pos,end_pos-first_pos+1);//extraction the function that needed to be calculated
std::string expressionInsideFunction=function.substr(4,expression.length()-4-1);//extracting the expression inside the function
//erasing any brackets found in the expression
if(expressionInsideFunction.find(')')!=std::string::npos){
expressionInsideFunction.erase(expressionInsideFunction.find(')'),1);
}
basicOperationCalculation(expressionInsideFunction);//calculating the expression inside the function
//if the expression was a matrix expression
if(!isdigit(expressionInsideFunction[0])){
asu::CMatrix firstmatrix=stringtomatrix(expressionInsideFunction,numberofmatrixVariables);//getting the matrix variable inside the function
numberofmatrixVariables++;//increasing the number of matrix varibales to save the result
matrixVariables[numberofmatrixVariables]=sin(firstmatrix);
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
expression.replace(first_pos,end_pos-first_pos+1,matrixNames[numberofmatrixVariables]);//replacing the name of resulting matrix in the original matrix
}
//if the expression was a number
else{
char * buffer=new char[expressionInsideFunction.length()+1];//creating a char array to have the number
strcpy(buffer,expressionInsideFunction.c_str());//copying the number to the char array
double operand=atof(buffer);//getting the operand by converting it into double to be able to calculate the required function
double result;//result of the required function
result=sin(operand);
char buffer_test[50];//char array to save the result
sprintf(buffer_test,"%g",result);
expression.replace(first_pos,end_pos-first_pos+1,(std::string)buffer_test);//replacing the function with the result
delete [] buffer;
}
}
/////////////////////////the same steps was explainded prevouisly in the part of finding sin //////////////////////////////////////
while(expression.find("cos")!=std::string::npos){
int first_pos=expression.find("cos");
int end_pos=expression.find(')',first_pos);
std::string function=expression.substr(first_pos,end_pos-first_pos+1);
std::string expressionInsideFunction=function.substr(4,expression.length()-4-1);
if(expressionInsideFunction.find(')')!=std::string::npos){
expressionInsideFunction.erase(expressionInsideFunction.find(')'),1);
}
basicOperationCalculation(expressionInsideFunction);
if(!isdigit(expressionInsideFunction[0])){
asu::CMatrix firstmatrix=stringtomatrix(expressionInsideFunction,numberofmatrixVariables);
numberofmatrixVariables++;
matrixVariables[numberofmatrixVariables]=cos(firstmatrix);
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
expression.replace(first_pos,end_pos-first_pos+1,matrixNames[numberofmatrixVariables]);
}
else{
char * buffer=new char[expressionInsideFunction.length()+1];
strcpy(buffer,expressionInsideFunction.c_str());
double operand=atof(buffer);
double result=cos(operand);
char buffer_test[50];
sprintf(buffer_test,"%g",result);
expression.replace(first_pos,end_pos-first_pos+1,(std::string)buffer_test);
delete [] buffer;
}
}
while(expression.find("tan")!=std::string::npos){
int first_pos=expression.find("tan");
int end_pos=expression.find(')',first_pos);
std::string function=expression.substr(first_pos,end_pos-first_pos+1);
std::string expressionInsideFunction=function.substr(4,expression.length()-4-1);
if(expressionInsideFunction.find(')')!=std::string::npos){
expressionInsideFunction.erase(expressionInsideFunction.find(')'),1);
}
basicOperationCalculation(expressionInsideFunction);
if(!isdigit(expressionInsideFunction[0])){
asu::CMatrix firstmatrix=stringtomatrix(expressionInsideFunction,numberofmatrixVariables);
numberofmatrixVariables++;
matrixVariables[numberofmatrixVariables]=tan(firstmatrix);
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
expression.replace(first_pos,end_pos-first_pos+1,matrixNames[numberofmatrixVariables]);
}
else{
char * buffer=new char[expressionInsideFunction.length()+1];
strcpy(buffer,expressionInsideFunction.c_str());
double operand=atof(buffer);
double result=tan(operand);
char buffer_test[50];
sprintf(buffer_test,"%g",result);
expression.replace(first_pos,end_pos-first_pos+1,(std::string)buffer_test);
delete [] buffer;
}
}
while(expression.find("sqrt")!=std::string::npos){
int first_pos=expression.find("sqrt");
int end_pos=expression.find(')',first_pos);
std::string function=expression.substr(first_pos,end_pos-first_pos+1);
std::string expressionInsideFunction=function.substr(5,expression.length()-5-1);
if(expressionInsideFunction.find(')')!=std::string::npos){
expressionInsideFunction.erase(expressionInsideFunction.find(')'),1);
}
basicOperationCalculation(expressionInsideFunction);
if(!isdigit(expressionInsideFunction[0])){
asu::CMatrix firstmatrix=stringtomatrix(expressionInsideFunction,numberofmatrixVariables);
numberofmatrixVariables++;
matrixVariables[numberofmatrixVariables]=sqrt(firstmatrix);
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
expression.replace(first_pos,end_pos-first_pos+1,matrixNames[numberofmatrixVariables]);
}
else{
char * buffer=new char[expressionInsideFunction.length()+1];
strcpy(buffer,expressionInsideFunction.c_str());
double operand=atof(buffer);
double result=sqrt(operand);
char buffer_test[50];
sprintf(buffer_test,"%g",result);
expression.replace(first_pos,end_pos-first_pos+1,(std::string)buffer_test);
delete [] buffer;
}
}
}
/*this function is resposable for stroring a matrix line in a CMatrix variable
arguments:matrix line with concatination brackets
return type:CMatrix varibale
*/
asu::CMatrix concatinationCalculation(std::string s ){
asu::CMatrix r;
char* buffer = new char[s.length()+1];
strcpy(buffer, s.c_str());
char* lineContext;
char lineSeparators[3];
lineSeparators[0] = ';';
lineSeparators[1] = '\r';
lineSeparators[2] = '\n';
char* line = strtok_r(buffer, lineSeparators, &lineContext);
while(line)
{
asu::CMatrix row;
char* context;
char separators[4];
separators[0] = ' ';
separators[1] = '[';
separators[2] = ']';
separators[3] = ',';
char* token = strtok_r(line, separators, &context);
while(token)
{
if(!isdigit(token[0])&&token[0]!='-'){
row.addColumn(stringtomatrix(token,numberofmatrixVariables));
}
else{
asu::CMatrix item = atof(token);
row.addColumn(item);}
token = strtok_r(NULL, separators, &context);
}
if (row.getnColumns() > 0 && (row.getnColumns() == r.getnColumns() || r.getnRows() == 0)){
r.addRow(row);
}
else{
throw std::invalid_argument
("Invalid matrix dimensions");
}
line = strtok_r(NULL, lineSeparators, &lineContext);
}
delete[] buffer;
return r;
}
/*
this function responsable for handling concatination process and replacing any matrix brackets with
a n
*/
void concatinationAnalysis(std::string& expression){
//checking that there is no syntax errors by comparing the number of opening brackets and closing concatination brackets
while(expression.find('[',1)!=std::string::npos)
{
unsigned int startPosition=0;//postion of opening bracket
int endPosition=expression.length()-1;//postion of closing bracket
//searching for opening and closing brackets
for(unsigned int i=0;i<expression.length();i++){
if(expression[i]=='[')
startPosition=i;
}
for(unsigned int i=0;i<expression.length()-1;i++){
if(expression[i]==']'&&i>startPosition){
endPosition=i;
break;
}
}
std::string insideBrackets=expression.substr(startPosition+1,endPosition-startPosition-1);//expression inside brackets
numberofmatrixVariables++;
matrixVariables[numberofmatrixVariables]=concatinationCalculation(insideBrackets);//getting a matrix variable of the expression inside brackets
std::string s="result";
matrixNames[numberofmatrixVariables]=s+to_string(numberofmatrixVariables);
expression.replace(startPosition,endPosition-startPosition+1,matrixNames[numberofmatrixVariables]);//replacing these brackets by a name refered to the resulting matrix
}
}