高精度加減乘法小程式 Ver 0 9 5 beta

2022-08-30 11:12:16 字數 4567 閱讀 9916

第一版見:

ver 0.9.5 beta

本版修正了乘積為0時無輸出的bug(感謝luogu.org的資料)

1

uses math;

2var

3a,b:ansistring;

4dc,la,lb:longint;

5 c:array[-2555555..2555555] of

longint;

6procedure plus(a,b:ansistring);

7var

8tt,i:longint;

9begin

10if la>lb then

11begin

12 for i:=1

to la-lb do

13 b:='

0'+b;

14 lb:=la;

15end

16else

17begin

18 for i:=1

to lb-la do

19 a:='

0'+a;

20 la:=lb;

21end;

22 for i:=1

to la do

23 c[i]:=ord(a[i])+ord(b[i])-96

;24 for i:=la downto 1

do25

if (c[i]+dc)>=10

then

26begin

27 tt:=(c[i]+dc) mod 10

;28 dc:=(c[i]+dc) div 10

;29 c[i]:=tt;

30end

31else

32begin

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

39if c[i]<>-1

then write(c[i]);

40writeln;

41end;

42procedure minus(a,b:ansistring);

43var

44t:ansistring;

45tt,i:longint;

46begin

47if la>=lb then

48begin

49 for i:=1

to la-lb do

50 b:='

0'+b;

51 lb:=la;

52end

53else

54begin

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;

62end;

63 for i:=1

to la do

64 c[i]:=ord(a[i])-ord(b[i]);

65 for i:=la downto 1

do66

if (c[i])<0

then

67begin

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 (lbdo

inc(lb);

73 for i:=lb to la do

74if c[i]<>-1

then write(c[i]);

75writeln;

76end;

77procedure multiply(a,b:ansistring);

78var

79tt,xx,i,j:longint;

80begin

81 xx:=0; tt:=maxlongint;

82 for i:=la downto 1

do83 for j:=lb downto 1

do84

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

92begin

93if c[i]+dc>=10

then

94begin

95 lb:=c[i];

96 c[i]:=(c[i]+dc) mod 10

;97 dc:=(lb+dc) div 10;98

end99

else

100begin

101 c[i]:=c[i]+dc;

102 dc:=0

;103

end;

104end;

105if dc>0

then write(dc);

106 for i:=tt to xx do write(c[i]); //writeln;

107if tt>=xx then writeln(0) else writeln;//thanks for luogu.org '

s data.

108end;

109begin

110while true do

111begin

112 writeln('

高精度數字計算器 ver 0.9.5 beta done by catch-22.s.iris');

113 writeln('

本計算器暫時只支援加減乘三則運算,其他運算更新中...');

114 writeln('

請不要拿無聊的超多位數來bs本程式,謝謝合作.');

115 writeln('

請輸入要計算的兩個高精度數字:');

116 write('

請輸入第乙個數字,並換行:');

117readln(a);

118 write('

請輸入第二個數字,並換行:');

119readln(b);

120 la:=length(a);

121 lb:=length(b);

122 writeln('

計算中..');

123 fillchar(c,sizeof(c),0); dc:=0

;124 write(a,'

+',b,'='

);125

plus(a,b);

126 fillchar(c,sizeof(c),0); dc:=0

;127 write(a,'

-',b,'='

);128

minus(a,b);

129 fillchar(c,sizeof(c),0); dc:=0

;130 write(a,'

*',b,'='

);131

multiply(a,b);

132 writeln('

謝謝使用,如需繼續計算請輸入11,如不需要請輸入任意字元');

133readln(a);

134if a<>'11'

then

135begin

136 writeln('

thank you.');

137 for la:=1

to maxlongint shr 4

do lb:=1

;138

exit;

139end;

140end;

141 end.

exe:

高精度加減乘法小程式

複習高精度玩,寫了個非常直觀的加減乘程式.1 uses math 2var 3a,b ansistring 4dc,la,lb longint 5 c array 2555555.2555555 of longint 6procedure plus a,b ansistring 7var 8tt,i...

高精度加減乘法運算

高精度,就是利用陣列存放數字,每個元素存放乙個數字,這樣就可以實現對一些較大的數字進行運算 加法 進製 include include include using namespace std intmain c lenc x if c lenc 0 lenc 處理最高進製 for i lenc i ...

高精度減法,高精度乘法

高精度減法 oj資料偏弱如果新增乙個101 2就錯了,下面這一步是為了防止錯誤的 if a aa 0 可能出現第一位的1被借走的的情況,所以加乙個while找第乙個不是0的 while c i 0 i include include include include include include u...