【求助】求高精度乘法和减法的程序
pascal吧
全部回复
仅看楼主
level 10
暗夜羽花 楼主
RT,麻烦大神帮忙编一下
2016年07月05日 12点07分 1
level 1
program exam2;
const max=200;
var a,b,c:array[1..max] of 0..9;
n,n1,n2:string;
lena,lenb,lenc,i,x:integer;
begin
write('Input minuend:'); readln(n1); //输入被减数
write('Input subtrahend:');readln(n2); //输入减数
if (length(n1)<length(n2)) or (length(n1)=length(n2)) and (n1<n2) then
begin //处理被减数和减数
n:=n1;
n1:=n2;
n2:=n;    //交换减数和被减数
write('-') //交换了减数和被减数,结果为负数
end;
lena:=length(n1);
lenb:=length(n2);
for i:=1 to lena do
a[lena-i+1]:=ord(n1[i])-ord('0');
for i:=1 to lenb do
b[lenb-i+1]:=ord(n2[i])-ord('0');
i:=1;
x:=0;
while (i<=lena) or(i<=lenb) do
begin
if a[i]<b[i] then //不够减,那么向高位借1当10
begin
a[i]:=a[i]+10;
a[i+1]:=a[i+1]-1;
end;
c[i] := a[i]-b[i] ; //对应位相减
i := i + 1
end;
lenc:=i;
while (c[lenc]=0) and (lenc>1) do dec(lenc); //最高位的0不输出
for i:=lenc downto 1 do
write(c[i]);
end.
减法
2016年07月09日 11点07分 2
level 1
const max=200;
var a,b,c:array[1..max] of 0..9;
n1,n2:string;
lena,lenb,lenc,i,j,x:integer;
begin
write('Input multiplier:'); readln(n1); write('Input multiplicand:'); readln(n2);
lena:=length(n1); lenb:=length(n2);
for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-ord('0');
for i:=1 to lenb do b[lenb-i+1]:=ord(n2[i])-ord('0');
for i:=1 to lena do
begin
x:=0; //用于存放进位
for j:=1 to lenb do
begin //对乘数的每一位进行处理
c[i+j-1] := a[i]*b[j] + x + c[i+j-1]; //当前乘积+上次乘积进位+原数
x:=c[i+j-1] div 10; c[i+j-1] := c[i+j-1] mod 10;
end;
c[i+j]:= x;     //进位
end;
lenc:=i+j;
while (c[lenc]=0) and (lenc>1) do dec(lenc);
for i:=lenc downto 1 do write(c[i]);
writeln
end.
乘法
2016年07月09日 11点07分 3
level 1
因懒得写从课件Ctrl v过来的
2016年07月09日 11点07分 4
非常感谢
2016年07月09日 14点07分
@暗夜羽花 不客气
2016年07月10日 11点07分
1