-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.c
664 lines (556 loc) · 12.9 KB
/
solver.c
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
/*
* calc.cpp
*
* the math expression solver.
*
* Author : Mohamad zbib
*
* THIS SOFTWARE UNDER THE BSD LICENSE.
* COPYRIGHT 2017.
*
*
*
* when you use this libary you should check from your program that
* the expression is true without any syntax error.
*
*
*
* NOTE:
* When we find bracket used for parameter of a function we will convert it to tilts and dots
* exmple:
* cos(2*12) <<===>> cos~.24.~
* this will make it easy to solve and less latency.
*
* this library built first time to be a part of private project,
* then it was an open source for all.
*
*
* project started on july 2017.
*
*/
#include "solver.h"
int main (){
//this main function just for test our libary
expression=(char *)malloc(256);//allocate the array
temp=(char *)malloc(256);//allocate the array
do{
printf("write an expression:\n");
scanf("%s",expression);
solve(expression); //we have the same global varaible but when we remove the main,
//we will have a different.
printf("ans=%s\n",expression);
}while(strcmp(expression,"q")!=0);
free(expression);//after the end destroy the location
free(temp);//after the end destroy the location
return 0;
}
void solve(const char*data){
//use the global variables
strcpy(expression,data);//with this main we have the same variable
//start first with high priority first
//level one
do{
//while we still found new operation.
opLen=strlen(expression);
bracket(0,opLen-1);
}while(missionDone==UPDATE);
missionDone=RESET;//reset the mission variable.
do{
Sign_Dot(0,opLen-1);
}while(missionDone==UPDATE);
missionDone=RESET;
//level two
do
{
opLen=strlen(expression);
functions(0,opLen-1);
}while(missionDone==UPDATE);
missionDone=RESET;//reset the mission variable.
/*
* we should skip the Sign_Dot here to show if we will include the sign under the power
*/
//level three
do
{
opLen=strlen(expression);
power(0,opLen-1);
}while(missionDone==UPDATE);
missionDone=RESET;
do{
Sign_Dot(0,opLen-1);
}while(missionDone==UPDATE);
missionDone=RESET;
//level four
do
{
opLen=strlen(expression);
Mul_Div_Mod(0,opLen-1);
}while(missionDone==UPDATE);
missionDone=RESET;
do{
Sign_Dot(0,opLen-1);
}while(missionDone==UPDATE);
missionDone=RESET;
//level five
do
{
opLen=strlen(expression);
Add_Sub(0,opLen-1);
}while(missionDone==UPDATE);
missionDone=RESET;
}
void bracket(int start, int end){
int i;
int brOp,brCl;//brOp for bracket open && brCl for bracket closed
double tmpNum;//to store the result number temporary.
again_bracket:
//reset the variable.
brOp=0;
brCl=0;
for(i=start;i<=end;i++)
{
if(expression[i]=='('){
brOp=i;//define new bracket
}
else if(expression[i]==')'){
brCl=i;
break;
}
}
if(brCl==0){
missionDone=DONE;
return;
}
else{
//the loop to end solving the bracket.
while(1){
if(checkSimple(brOp+1,brCl-1)==1)
{
//clone the simple number to variable.
tmpNum=CloneNumber(brOp+1,brCl-1);
//update the expression string and break the loop.
//check if is function parameter.
if(FuncFlag(brOp+1)!=0)
update_expression(tmpNum,brOp,brCl,BR_TO_FX);//update and change to tilts for function.
else
update_expression(tmpNum,brOp,brCl,NULL_F);
missionDone=UPDATE;//we still update the expression
return;
}
//the test of what we found!!
//we shold check it simple later
Sign_Dot(0,opLen-1);
if(missionDone=UPDATE){
missionDone=RESET;
goto again_bracket;
}
missionDone=RESET;
functions(brOp+1,brCl-1);//try the next step.
if(missionDone==UPDATE)
{
missionDone=RESET;//reset the mission variable.
goto again_bracket;
}
power(brOp+1,brCl-1);
if(missionDone==UPDATE){
missionDone=RESET;//reset the mission variable.
goto again_bracket;
}
Mul_Div_Mod(brOp+1, brCl-1);
if(missionDone==UPDATE){
missionDone=RESET;//reset the mission variable.
goto again_bracket;
}
Add_Sub(brOp+1, brCl-1);
if(missionDone==UPDATE){
missionDone=RESET;//reset the mission variable.
goto again_bracket;
}
}
}
}
void functions(int f,int l){
int i ;
int brOp=0,brCl=0;//open and closed bracket for functions.
double CNumber;//the number the we take from CloneNumber function.
int FName;//a flag to specify what this function will used.
//NOTE: Read the note above in description.
for(i=f;i<=l;i++){
if(expression[i]=='~' && expression[i+1]=='.'){
brOp=i+2;
i++;//the increment of the var should be 2 but the loop make the anather
}
if(expression[i]=='.' && expression[i+1]=='~'){
brCl=i-1;
break;
}
}
if(brCl==0){
missionDone=DONE;//send a signal to end the function
return;
}
else{
if(checkSimple(brOp,brCl)==1){
CNumber=CloneNumber(brOp,brCl);//get the parameter of the function.
FName=FuncFlag(brOp);
Calc_and_put_result(FName,CNumber,brOp,brCl);//calculate the function result and edit the main expression.
missionDone=UPDATE;//the mission here achived
}else
{
printf("will do it later!!\n");
/*
* we should calculate the number to be simple.
* will gonna done later.
*/
}
}
}
void power(int f,int l){
int i;
int pr=-1;//the index of the power operation symbol.
int opS=-1,opE=-1;//define opreration start and end.
double base=0,powerN=0;
double answer=0;
for(i=f;i<=l;i++)
{
if(expression[i]=='^')
{
pr=i;
break;
}
}
if(pr==-1){
missionDone=DONE;
return;
}
else{
//find the base number.
for(i=pr-1;i>=0;i--){
if(ifNum(expression[i])==1)
opS=i;
else
break;
}
//check if we have a sign for a number
if(expression[opS-1]=='-' || expression[opS-1]=='+')
if(expression[opS-2]=='-' || expression[opS-2]=='+')
opS--;//include the sign with the number.
base=CloneNumber(opS,pr-1);
//find the the power number.
for(i=pr+1;i<=l;i++){
if(ifNum(expression[i])==1)
opE=i;
else
break;
}
powerN=CloneNumber(pr+1,opE);
//get the answer now.
answer=pow(base,powerN);
//update the expression.
update_expression(answer,opS,opE,NULL_F);
missionDone=UPDATE;
}
}
void Mul_Div_Mod(int f, int l){
int i,pos=-1,Flag=0;//index , position of operation and flag to specify the operation.
double num1=0,num2=0,result=0;//for calculation
int opS=-1,opE=-1;//the start and the end of the equation.
int len;//the length of the expression.
for(i=f;i<=l;i++)
{
switch(expression[i])
{
case '*':
pos=i;
Flag=MUL;
break;
case '/':
pos=i;
Flag=DIV;
break;
case '%':
pos=i;
Flag=MOD;
break;
}
if(Flag!=0)
break;//if we break from the stwitch we should break from the loop
}
if(Flag==0){
missionDone=DONE;
return;
}
//find the first number
for(i=pos-1;i>=0;i--){
if(ifNum(expression[i])==1)
opS=i;
else
break;
}
num1=CloneNumber(opS,pos-1);
//find the second number
for(i=pos+1;i<=l;i++)
{
if(ifNum(expression[i])==1)
opE=i;
else
break;
}
num2=CloneNumber(pos+1,opE);
//get the result
switch(Flag){
case MUL:
result=num1*num2;
break;
case DIV:
result=num1/num2;
break;
case MOD:
result=(int)num1% (int)num2;
break;
}
update_expression(result,opS,opE,NULL_F);
missionDone=UPDATE;
}
void Add_Sub(int f, int l){
int i,pos=-1,Flag=0;//index , position of operation and flag to specify the operation.
double num1=0,num2=0,result=0;//for calculation
int opS=-1,opE=-1;//the start and the end of the equation.
int len;//the length of the expression.
for(i=f+1;i<=l;i++)
{
//if we start i=f so we test the sign of the number
//and this will crach the program.
switch(expression[i])
{
case '+':
pos=i;
Flag=ADD;
break;
case '-':
pos=i;
Flag=SUB;
break;
}
if(Flag!=0)
break;//if we break from the stwitch we should break from the loop
}
if(Flag==0){
missionDone=DONE;
return;
}
//find the first number
for(i=pos-1;i>=0;i--){
if(ifNum(expression[i])==1)
opS=i;
else
break;
}
num1=CloneNumber(opS,pos-1);
//find the second number
for(i=pos+1;i<=l;i++)
{
if(ifNum(expression[i])==1)
opE=i;
else
break;
}
num2=CloneNumber(pos+1,opE);
//get the result
switch(Flag){
case ADD:
result=num1+num2;
break;
case SUB:
result=num1-num2;
break;
}
update_expression(result,opS,opE,NULL_F);
missionDone=UPDATE;
}
int checkSimple(int f, int l){
int i;
if(expression[f]=='-' || expression[f]=='+')
f++;//if first if a sign character skip it.
for(i=f;i<=l;i++)
if((expression[i]<'0' || expression[i]>'9') && expression[i]!='.' )//if not belong numbers
return 0;
return 1;
}
int ifNum(char n){
if(n>='0' && n<='9')
return 1;
if(n=='.')
return 1;
return 0;
}
double CloneNumber(int f, int l){
int i,dotIndex=-1;
int afterDot=0;
double CNum=0;
char sign='+';
//check if found a sign for the number
if(expression[f]=='-'){
f++;
sign='-';
}else if(expression[f]=='+'){
f++;
sign='+';
}
//we will do it in best way to avoid double problems
for(i=f;i<=l;i++){
if(expression[i]>='0' && expression[i]<='9')
{
CNum*=10;
CNum+= (expression[i]-'0');
}
if(expression[i]=='.')
dotIndex=i;
if(expression[i]!='.' && dotIndex!=-1)//we have found a number after dot
afterDot++;
}
if(sign=='-')
CNum*=-1;//change the sign if it is negative.
return CNum / pow(10,afterDot);
}
int FuncFlag(int f){
//check first the kind of the brackets.
if(expression[f-1]=='.' &&expression[f-2]=='~')
f-=3;
else if(expression[f-1]=='(')
f-=2;
//if cosine
if(expression[f-2]=='c' && expression[f-1]=='o' && expression[f]=='s')
return COS_F;
//if sine
if(expression[f-2]=='s' && expression[f-1]=='i' && expression[f]=='n')
return SIN_F;
//if log
if(expression[f-2]=='l' && expression[f-1]=='o' && expression[f]=='g')
return LOG_F;
//if log10
if(expression[f-4]=='l' && expression[f-3]=='o' && expression[f-2]=='g' && expression[f-1]=='1' && expression[f]=='0')
return LOG10_F;
//will be continue if we need more..
return NULL_F;//if we dont find any function.
}
void Calc_and_put_result(int Flag,double Number,int brOp,int brCl){
int start,end=brCl+2;
double val;
switch(Flag){
case COS_F:
start=brOp-5;
val=cos(Number);
break;
case SIN_F:
start=brOp-5;
val=sin(Number);
break;
case TAN_F:
start=brOp-5;
val=tan(Number);
break;
case LOG_F:
start=brOp-5;
val=log(Number);
break;
case LOG10_F:
start=brOp-7;
val=log10(Number);
break;
//to be continue...
}
update_expression(val,start,end,NULL_F);//update the expression.
}
void update_expression(double num , int start, int end,int flag){
int lenOfStr;
int intPart=(int)num;
char strN[32],strN2[32];
if(num<0){
strcpy(strN2,"-");
num*=-1;//avoid problem of negative just.
}
else
strcpy(strN2,"");
if(num<1)
{
strcat(strN2,"0");//if should be 0 or 0.xx or -0.xx
}
intToStr(intPart,strN);//convert the integer to string.
strcat(strN2,strN);// here we complete the conversion of the integer part.
//now will work on float part.
num-=intPart;
lenOfStr=strlen(strN2);
if(num!=0)
{
strN2[lenOfStr++]='.';
while(num!=0 && lenOfStr<=10){
num=num*10;
intPart=(int)num;
strN2[lenOfStr++]=intPart+'0';
num-=intPart;
}
}
strN2[lenOfStr]='\0';//put the null termenator at the end of the string.
//now we have the string then we update.
strncpy(temp,expression,start);
temp[start]='\0';//to ensure the null.
//check the flag .
if(flag==BR_TO_FX)
strcat(temp,"~.\0");//if function insert the bracket of the function.
strcat(temp,strN2);//insert the number
//again the flag to end the bracket of the function.
if(flag==BR_TO_FX)
strcat(temp,".~\0");
strcat(temp,expression+end+1);
strcpy(expression,temp);
}
void intToStr(int n ,char *str){
int i=0;
int len;
char tmp[32];//to store the data temporary in reversed order
while(n!=0){
tmp[i++]=(n%10)+'0';//put the last digit directly at the beginning of the string.
n/=10;
}
tmp[i]='\0';//put the null tremenitor at the end of the string.
//second part reverse the string to give the correct answer.
len=strlen(tmp);
for(i=0;i<len;i++)
str[i]=tmp[len-1-i];
str[i]='\0';//Null termenator.
}
void Sign_Dot(int f,int l){
char result[2];//to use it in strcat as a string
int i,pos;
result[0]='\0';
result[1]='\0';
for(i=f;i<l;i++)
{
if(expression[i]=='+' || expression[i]=='-')
if(expression[i+1]=='+' || expression[i+1]=='-'){//then we have two sign to solve it
//now getting the result
if(expression[i]==expression[i+1])
result[0]='+';
else
result[0]='-';
pos=i;
}
}
if(result[0]=='\0'){
missionDone=DONE;
return;
}
strncpy(temp,expression,pos);
temp[pos]=result[0];
temp[pos+1]='\0';//make sure you put the NULL terminator.
strcat(temp,expression+pos+2);
strcpy(expression,temp);
missionDone=UPDATE;
}
void print(int f, int l){
int i;
for(i=f;i<=l;i++)
printf("%c",expression[i]);
printf("\n");
}