高精度加减乘法小程序
复习高精度玩,写了个非常直观的加减乘程序.
1 Uses Math; 2 Var 3 a,b:AnsiSTring; 4 DC,la,lb:longint; 5 c:Array[-2555555..2555555] of longint; 6 Procedure Plus(a,b:AnsiSTring); 7 Var 8 Tt,i:longint; 9 Begin 10 if la>lb Then 11 Begin 12 For i:=1 to la-lb do 13 b:='0'+b; 14 lb:=la; 15 End 16 Else 17 Begin 18 For i:=1 to lb-la do 19 a:='0'+a; 20 la:=lb; 21 End; 22 For i:=1 to la do 23 c[i]:=Ord(a[i])+Ord(b[i])-96; 24 For i:=la Downto 1 do 25 if (c[i]+DC)>=10 Then 26 Begin 27 Tt:=(c[i]+DC) Mod 10; 28 DC:=(c[i]+DC) Div 10; 29 c[i]:=Tt; 30 End 31 Else 32 Begin 33 c[i]:=c[i]+DC; 34 DC:=0; 35 End; 36 c[0]:=-1; 37 if DC>0 Then c[0]:=DC; 38 For i:=0 to la do 39 if c[i]<>-1 Then Write(c[i]); 40 Writeln; 41 End; 42 Procedure Minus(A,b:AnsiSTring); 43 Var 44 t:AnsiSTring; 45 Tt,i:longint; 46 Begin 47 if la>=lb Then 48 Begin 49 For i:=1 to la-lb do 50 b:='0'+b; 51 lb:=la; 52 End 53 Else 54 Begin 55 For i:=1 to lb-la do 56 a:='0'+a; 57 la:=lb; 58 Write('-'); 59 t:=a; 60 a:=b; 61 b:=t; 62 End; 63 For i:=1 to la do 64 c[i]:=Ord(a[i])-Ord(b[i]); 65 For i:=la Downto 1 do 66 if (c[i])<0 Then 67 Begin 68 c[i]:=c[i]+10; 69 c[i-1]:=c[i-1]-1; 70 End; 71 lb:=1; 72 While (c[lb]=0) And (lb<la) do Inc(lb); 73 For i:=lb to la do 74 if c[i]<>-1 Then Write(c[i]); 75 Writeln; 76 End; 77 Procedure Multiply(a,b:AnsiString); 78 Var 79 Tt,xx,i,j:longint; 80 Begin 81 xx:=0; Tt:=Maxlongint; 82 For i:=La Downto 1 do 83 For j:=lb Downto 1 do 84 Begin 85 c[i+j-1]:=((Ord(a[i])-48)*(Ord(b[j])-48)+c[i+j-1]); 86 Tt:=Min(Tt,i+j-1); 87 xx:=Max(xx,i+j-1); 88 End; 89 While c[tt]=0 Do Inc(tt); 90 DC:=0; 91 For i:=xx Downto Tt Do 92 Begin 93 if c[i]+DC>=10 Then 94 Begin 95 lb:=c[i]; 96 c[i]:=(c[i]+DC) Mod 10; 97 DC:=(lb+DC) Div 10; 98 End 99 Else //17956 100 Begin 101 c[i]:=c[i]+Dc; 102 DC:=0; 103 End; 104 End; 105 if DC>0 Then Write(DC); 106 For i:=Tt To xx do Write(c[i]); Writeln; 107 End; 108 Begin 109 While True Do 110 Begin 111 Writeln('高精度数字计算器 Ver 0.9 Done By Catch-22.S.In'); 112 Writeln('本计算器暂时只支持加减乘三则运算,其他运算更新中...'); 113 Writeln('请不要拿无聊的超多位数来BS本程序,谢谢合作.'); 114 Writeln('请输入要计算的两个高精度数字:'); 115 Write('请输入第一个数字,并换行:'); 116 Readln(a); 117 Write('请输入第二个数字,并换行:'); 118 Readln(b); 119 la:=length(a); 120 lb:=Length(b); 121 Writeln('计算中..'); 122 FillChar(c,Sizeof(c),0); DC:=0; 123 Write(a,'+',b,'='); 124 Plus(a,b); 125 FillChar(c,Sizeof(c),0); DC:=0; 126 Write(a,'-',b,'='); 127 Minus(a,b); 128 FillChar(c,Sizeof(c),0); DC:=0; 129 Write(a,'*',b,'='); 130 Multiply(a,b); 131 Writeln('谢谢使用,如需继续计算请输入11,如不需要请输入任意字符'); 132 Readln(a); 133 if a<>'11' Then 134 Begin 135 Writeln('Thank You.'); 136 For la:=1 to Maxlongint Shr 4 do lb:=1; 137 Exit; 138 End; 139 End; 140 End.
EXE:http://yunpan.cn/lk/sVMDU5H9aYjgJ ,欢迎使用.