This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSciNum.cpp
1016 lines (963 loc) · 16.3 KB
/
SciNum.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
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "SciNum.h"
SciNum::SciNum():coefficient(), symbol(), error(), error_flag(), mantissa(), exponent(), num()
{}
SciNum::SciNum(const char *n):coefficient(), symbol(1), error(), error_flag(), mantissa(), exponent(), num()
{
int decimal_point_flag=0;//代表找到的小数点的数量
std::stack<char>int_part;
const char* p=n;
if(*p=='-')
{
symbol=-1;
p++;
}
while(*p>='0'&&*p<='9')
{
int_part.push(*p);
p++;
}
if(int_part.size()==0)//第一个数就不是数字 语法错误
{
error_flag=1;
error="语法错误(数首位不为数字)";
}
//当前输入的数字为整数部分
exponent=int_part.size()-1;//数字位数减一为其的指数项
char c;
double temp_num=1;//储存10的exponent-i次方
for(int i=int_part.size()-1;i>=0&&error_flag==0;i--)
{
c=int_part.top();
int_part.pop();
coefficient[100+exponent-i]=c-'0';
num+=temp_num*(c-'0');
temp_num*=10;
}
if(*p!='.'&&*p!='\0'&&error_flag==0)
{
error_flag=1;
error="语法错误(数结尾异常)";
}
else if(*p=='.'&&error_flag==0)//开始输入小数部分
{
p++;
std::queue<char>dec_part;
while(*p>='0'&&*p<='9')
{
dec_part.push(*p);
p++;
}
if(dec_part.size()==0)//第一个数就不是数字 语法错误
{
error_flag=1;
error="语法错误(小数首位不为数字)";
return;
}
if(*p!='\0')
{
error_flag=1;
error="语法错误(数结尾异常)";
return;
}
temp_num=0.1;
for(int i=1;dec_part.size()>0;i++)
{
c=dec_part.front();
dec_part.pop();
coefficient[100-i]=c-'0';
num+=temp_num*(c-'0');
temp_num/=10;
}
if(exponent==0&&coefficient[100]==0)
{
int i;
for(i=100;i>=0&&coefficient[i]==0;i--);//
if(i==-1)exponent=0;
else exponent=i-100;
}
}
//计算尾数
int i;
for(i=199;i>=0&&coefficient[i]==0;i--);//寻找第一个有数的位
if(i==-1)//说明该数为0
{
mantissa=0;
symbol=0;
}
else//找到了一个有数的位(此数不为0)
{
double temp_mant=1;
for(;i>=0;i--)
{
mantissa+=coefficient[i]*temp_mant;
temp_mant/=10;
}
}
if(symbol==-1)
{
mantissa*=-1;
num*=-1;
}
}
void SciNum::calculate_num()
{
if(error_flag)return;
mantissa=0;
num=0;
exponent=0;
int i;
for(i=199;i>=0&&coefficient[i]==0;i--);//找到第一个非0值
if(i==-1)
{
symbol=0;
return;
}
exponent=i-100;
double temp_mant=1;
for(;i>=0;i--)
{
mantissa+=coefficient[i]*temp_mant;
temp_mant/=10;
}
if(exponent>=0)
{
num=mantissa;
for(int j=0;j<exponent;j++)num*=10;
}
else
{
num=mantissa;
for(int j=0;j>exponent;j--)num/=10;
}
if(symbol==-1)
{
mantissa*=-1;
num*=-1;
}
}
void SciNum::calculate_sci_to_num()
{
if(error_flag)return;
std::memset(coefficient,0,sizeof(int)*200);
if(std::abs(mantissa)<1e-6)
{
symbol=0;
num=0;
return;
}
else if(mantissa>0)symbol=1;
else if(mantissa<0)symbol=-1;
double temp=mantissa;
temp=std::abs(temp);
for(int i=exponent+100;i>=0;i--)
{
coefficient[i]=(int)temp;
temp*=10;
while(temp>10-1e-14)temp-=10;
if(std::abs(temp)<1e-14)break;
}
this->calculate_num();
}
void SciNum::show_num()
{
if(error_flag)
{
std::cout << error << std::endl;
return;
}
std::cout << mantissa << "*10^" << exponent << std::endl;
std::cout << num << std::endl;
}
int SciNum::compare(const SciNum &n1, const SciNum &n2)
{
if(n1.symbol<n2.symbol)return -1;
if(n1.symbol>n2.symbol)return 1;
int cmp=0;
for(int i=199;i>=0;i--)
{
if(n1.coefficient[i]==n2.coefficient[i])
{
if(i!=0)continue;
else cmp=0;
}
else
{
cmp=n1.coefficient[i]-n2.coefficient[i];
break;
}
}
if(cmp>0&&n1.symbol>0)return 1;
else if(cmp>0&&n1.symbol<0)return -1;
else if(cmp==0)return 0;
else if(cmp<0&&n1.symbol>0)return -1;
else if(cmp<0&&n1.symbol<0)return 1;
return 0;
}
SciNum SciNum::operator+(const SciNum& n2) const
{
SciNum ans;
if(error_flag==1)
{
ans.error_flag=1;
ans.error=error;
return ans;
}
if(n2.error_flag==1)
{
ans.error_flag=1;
ans.error=n2.error;
return ans;
}
if(symbol==0)
{
return n2;
}
else if(n2.symbol==0)
{
return *this;
}
if(symbol==n2.symbol)//符号相同
{
ans.symbol=symbol;
int carry=0;
for(int i=0;i<200;i++)
{
int temp=coefficient[i]+n2.coefficient[i]+carry;
if(temp>=10)
{
carry=1;
temp-=10;
}
else carry=0;
ans.coefficient[i]=temp;
}
if(carry==1)
{
ans.error_flag=1;
ans.error="数据溢出";
return ans;
}
ans.calculate_num();
return ans;
}
else if(symbol==1)//正数加负数
{
SciNum temp=n2;
temp.symbol=1;
return *this-temp;
}
else//负数加正数
{
SciNum temp=*this;
temp.symbol=1;
return n2-temp;
}
}
SciNum SciNum::operator-(const SciNum& n2) const
{
SciNum ans;
if(error_flag==1)
{
ans.error_flag=1;
ans.error=error;
return ans;
}
if(n2.error_flag==1)
{
ans.error_flag=1;
ans.error=n2.error;
return ans;
}
if(symbol==0)
{
ans=n2;
ans.symbol*=-1;
return ans;
}
else if(n2.symbol==0)
{
return *this;
}
if(symbol==n2.symbol)
{
int abscmp=0;//比较绝对值大小
if(symbol>0)
{
abscmp=compare(*this, n2);
}
else abscmp=-(compare(*this, n2));
if(abscmp<0)
{
ans=n2-*this;
ans.symbol=-symbol;
ans.calculate_num();
return ans;
}
else if(abscmp==0)
{
return ans;
}
else
{
ans.symbol=symbol;
int borrow=0;//代表是否借位
for(int i=0;i<=199;i++)
{
int temp=coefficient[i]-borrow;
if(temp<n2.coefficient[i])
{
temp+=10;
borrow=1;
}
else borrow=0;
ans.coefficient[i]=temp-n2.coefficient[i];
}
ans.calculate_num();
return ans;
}
}
else
{
SciNum temp=n2;
temp.symbol*=-1;
return *this+temp;
}
}
SciNum SciNum::operator*(const SciNum& n2) const
{
SciNum ans;
if(error_flag==1)
{
ans.error_flag=1;
ans.error=error;
return ans;
}
if(n2.error_flag==1)
{
ans.error_flag=1;
ans.error=n2.error;
return ans;
}
if(symbol==0||n2.symbol==0)//0乘任何数都为0
{
return ans;
}
for(int i=0;i<200;i++)
{
if(coefficient[i]==0)continue;
SciNum multi_temp;//用于储存每一位乘n2的值
int carry=0;//进位
for(int j=0;j<200;j++)
{
int temp=coefficient[i]*n2.coefficient[j]+carry;
carry=temp/10;
temp%=10;
if(temp!=0)
{
if(i+j-100>=0&&i+j-100<=199)multi_temp.coefficient[i+j-100]=temp;
else if(i+j-100>199)
{
ans.error_flag=1;
ans.error="数据溢出";
}
}
}
multi_temp.symbol=1;
multi_temp.calculate_num();
ans=ans+multi_temp;
}
if(symbol==n2.symbol)ans.symbol=1;
else ans.symbol=-1;
ans.calculate_num();
return ans;
}
SciNum SciNum::operator/(const SciNum& n2) const
{
SciNum ans;
if(error_flag==1)
{
ans.error_flag=1;
ans.error=error;
return ans;
}
if(n2.error_flag==1)
{
ans.error_flag=1;
ans.error=n2.error;
return ans;
}
if(n2.symbol==0)
{
ans.error_flag=1;
ans.error="除数不能为0";
return ans;
}
else if(symbol==0)
{
return ans;
}
SciNum dividend=*this;//被除数
SciNum num10("10");
SciNum n2ei=n2;
dividend.symbol=1;
n2ei.symbol=1;
int i=100;
while(compare(dividend, n2ei*num10)==1)
{
n2ei=n2ei*num10;
i++;
}
while(i>=0)
{
while(compare(dividend, n2ei)>=0)
{
dividend=dividend-n2ei;
ans.coefficient[i]++;
}
for(int j=0;j<199;j++)
{
n2ei.coefficient[j]=n2ei.coefficient[j+1];
}
n2ei.coefficient[199]=0;
i--;
}
if(symbol==n2.symbol)ans.symbol=1;
else ans.symbol=-1;
ans.calculate_num();
return ans;
}
SciNum SciNum::operator^(const SciNum& n2) const
{
SciNum ans;
if(error_flag==1)
{
ans.error_flag=1;
ans.error=error;
return ans;
}
if(n2.error_flag==1)
{
ans.error_flag=1;
ans.error=n2.error;
return ans;
}
if(symbol==0)
{
return ans;
}
if(n2.symbol==0)
{
SciNum temp("1");
return temp;
}
double new_mantissa=pow(mantissa,n2.num);//计算尾数的n2次方,exp为指数部分
int exp=0;
while(std::abs(new_mantissa)>10-1e-6)
{
new_mantissa/=10;
exp++;
}
while(std::abs(new_mantissa)<1-1e-6)
{
new_mantissa*=10;
exp--;
}
double exppart_mantissa=pow(10,n2.num*exponent);
if(exppart_mantissa<pow(10,-200))
{
return ans;
}
else if(exppart_mantissa>pow(10,200))
{
ans.setError("数据溢出");
return ans;
}
int exp2=0;
while(exppart_mantissa>10-1e-6)
{
exppart_mantissa/=10;
exp2++;
}
while(exppart_mantissa<1-1e-6)
{
exppart_mantissa*=10;
exp2--;
}
ans.mantissa=new_mantissa*exppart_mantissa;
ans.exponent=exp+exp2;
if(ans.mantissa>10-1e-6)
{
ans.mantissa/=10;
ans.exponent++;
}
if(ans.exponent>=100)
{
ans.error_flag=1;
ans.error="数据溢出";
return ans;
}
ans.calculate_sci_to_num();
return ans;
}
SciNum SciNum::sin(SciNum n)
{
SciNum ans;
if(n.error_flag)return n;
if(n.num>1e10)
{
n.error_flag=1;
n.error="数据溢出";
return n;
}
if(std::abs(n.num)<1e-6)return n;
double temp=std::sin(n.num);
int exp=0;
while(temp>10-1e-6)
{
temp/=10;
exp++;
}
while(std::abs(temp)<1-1e-6)
{
temp*=10;
exp--;
}
ans.num=std::sin(n.num);
ans.exponent=exp;
ans.mantissa=temp;
ans.calculate_sci_to_num();
return ans;
}
SciNum SciNum::cos(SciNum n)
{
SciNum ans;
if(n.error_flag)return n;
if(n.num>1e10)
{
n.error_flag=1;
n.error="数据溢出";
return n;
}
if(std::abs(n.num)<1e-6)
{
SciNum b("1");
ans=b-n;
return ans;
}
double temp=std::cos(n.num);
int exp=0;
while(temp>10-1e-6)
{
temp/=10;
exp++;
}
while(std::abs(temp)<1-1e-6)
{
temp*=10;
exp--;
}
ans.num=std::cos(n.num);
ans.exponent=exp;
ans.mantissa=temp;
ans.calculate_sci_to_num();
return ans;
}
SciNum SciNum:: calculateExpression(const char *formula, const SciNum& Ans)
{
char s[100]={};
SciNum result;
int count=0;
if(*formula=='\0')
{
result.setError("括号不匹配!");
return result;
}
{//此程序块用于补右括号,并把算式赋值进s
int i;
for(i=0;*(formula+i)!='\0';i++)
{
s[i]=*(formula+i);
if(*(formula+i)=='(')
{
count++;
}
else if(*(formula+i)==')')
{
count--;
}
if(i==99)
{
result.error_flag=1;
result.error="输入过长!";
return result;
}
}
if(count==0)
{
s[i]='\0';
}
else if(count<0)
{
result.setError("语法错误");
return result;
}
else//count>0
{
while(count--)
{
if(i>=99)
{
result.error_flag=1;
result.error="输入过长!";
return result;
}
s[i++]=')';
}
s[i]='\0';
}
}
std::stack<SciNum> NumStack;
std::stack<char> OperatorStack;
int flag=0;//flag代表当前的状态,0代表下一个是数,1代表下一个是符号
char *p=s;
while(*p!='\0')
{
if(flag==0)
{
if(*p=='(')//左括号,找到则计算该括号内部的值,使用递归
{
int count=1;//已经数到的左括号-右括号数量
char newnumber[100]={};
int pnew=0;
p++;
while(1)
{
if(*p=='(')count++;
else if(*p==')')
{
if(count==1)
{
p++;
break;
}
else count--;
}
newnumber[pnew++]=*p++;
}
newnumber[pnew]='\0';
SciNum n;
n=calculateExpression(newnumber, Ans);
NumStack.push(n);
flag=1;
}
else if(*p=='A')
{
p++;
if(*p!='n')
{
result.setError("语法错误");
return result;
}
p++;
if(*p!='s')
{
result.setError("语法错误");
return result;
}
p++;
NumStack.push(Ans);
flag=1;
}
else if(*p=='c')
{
SciNum n;//cos后接的数
p++;
if(*p!='o')
{
result.setError("语法错误");
return result;
}
p++;
if(*p!='s')
{
result.setError("语法错误");
return result;
}
p++;
if(*p!='(')
{
result.setError("语法错误");
return result;
}
else
{
int count=1;//已经数到的左括号-右括号数量
char newnumber[100]={};
int pnew=0;
p++;
while(1)
{
if(*p=='(')count++;
else if(*p==')')
{
if(count==1)
{
p++;
break;
}
else count--;
}
newnumber[pnew++]=*p++;
}
newnumber[pnew]='\0';
n=calculateExpression(newnumber, Ans);
}
n=SciNum::cos(n);
NumStack.push(n);
flag=1;
}
else if(*p=='s')
{
SciNum n;//cos后接的数
p++;
if(*p!='i')
{
result.setError("语法错误");
return result;
}
p++;
if(*p!='n')
{
result.setError("语法错误");
return result;
}
p++;
if(*p!='(')
{
result.setError("语法错误");
return result;
}
else
{
int count=1;//已经数到的左括号-右括号数量
char newnumber[100]={};
int pnew=0;
p++;
while(1)
{
if(*p=='(')count++;
else if(*p==')')
{
if(count==1)
{
p++;
break;
}
else count--;
}
newnumber[pnew++]=*p++;
}
newnumber[pnew]='\0';
n=calculateExpression(newnumber, Ans);
}
n=SciNum::sin(n);
NumStack.push(n);
flag=1;
}
else
{
char newnumber[100]={};
int pnew=0;
int pointflag=0;
newnumber[pnew++]=*p++;
while((*p<='9'&&*p>='0')||*p=='.')
{
if(*p=='.')
{
pointflag++;
}
newnumber[pnew++]=*p++;
}
newnumber[pnew]='\0';
SciNum n(newnumber);
NumStack.push(n);
flag=1;
}
}
else
{
if(*p=='+'||*p=='-')
{
while(!(OperatorStack.empty()))
{
SciNum n2=NumStack.top();
NumStack.pop();
SciNum n1=NumStack.top();
NumStack.pop();
char opera=OperatorStack.top();
OperatorStack.pop();
if(opera=='+')
{
n1=n1+n2;
NumStack.push(n1);
}
else if(opera=='-')
{
n1=n1-n2;
NumStack.push(n1);
}
else if(opera=='*')
{
n1=n1*n2;
NumStack.push(n1);
}
else if(opera=='/')
{
n1=n1/n2;
NumStack.push(n1);
}
else if(opera=='^')
{
n1=n1^n2;
NumStack.push(n1);
}
else
{
printf("特殊符号\n");
exit(0);
}
}
OperatorStack.push(*p);
p++;
}
else if(*p=='*'||*p=='/')
{
while(!OperatorStack.empty()&&(OperatorStack.top()=='*'||OperatorStack.top()=='/'||OperatorStack.top()=='^'))//计算前面所有的乘、除、乘方
{
SciNum n2=NumStack.top();
NumStack.pop();
SciNum n1=NumStack.top();
NumStack.pop();
char opera=OperatorStack.top();
OperatorStack.pop();
if(opera=='*')
{
n1=n1*n2;
NumStack.push(n1);
}
else if(opera=='/')
{
n1=n1/n2;
NumStack.push(n1);
}
else if(opera=='^')
{
n1=n1^n2;
NumStack.push(n1);
}
}
OperatorStack.push(*p);
p++;
}
else if(*p=='^')
{
if(!OperatorStack.empty()&&OperatorStack.top()=='^')//计算前面所有乘方(按理来讲顶多只有一个)
{
SciNum n2=NumStack.top();
NumStack.pop();
SciNum n1=NumStack.top();
NumStack.pop();
char opera=OperatorStack.top();
OperatorStack.pop();
n1=n1^n2;
NumStack.push(n1);
}
OperatorStack.push(*p);
p++;
}
else
{
result.error_flag=1;
result.error="输入错误";
return result;
}
flag=0;
}
}
while(!(OperatorStack.empty()))//计算剩下的
{
if(NumStack.size()==1)
{
SciNum temp;
temp.error_flag=1;
temp.error="输入错误";
NumStack.push(temp);
break;
}
SciNum n2=NumStack.top();
NumStack.pop();
SciNum n1=NumStack.top();
NumStack.pop();
char opera=OperatorStack.top();
OperatorStack.pop();
if(opera=='+')
{
n1=n1+n2;
NumStack.push(n1);
}
else if(opera=='-')
{
n1=n1-n2;
NumStack.push(n1);
}
else if(opera=='*')
{
n1=n1*n2;
NumStack.push(n1);
}
else if(opera=='/')
{
n1=n1/n2;
NumStack.push(n1);
}
else if(opera=='^')
{
n1=n1^n2;
NumStack.push(n1);
}
else
{
printf("特殊符号\n");
exit(0);
}
}
result=NumStack.top();
NumStack.pop();
return result;
}
double SciNum::getNum()
{
return num;
}
int SciNum::getExp()
{
return exponent;
}
double SciNum::getMantisaa()
{
return mantissa;
}