课件编号709615

高精度运算 microsoft word 文档

日期:2024-05-16 科目:信息技术 类型:高中素材 查看:81次 大小:11127Byte 来源:二一课件通
预览图 1/2
高精度,运算,microsoft,word,文档
  • cover
高精度加法 program exam1; const max=200; var a,b,c:array[1..max] of 0..9; n:string; lena,lenb,lenc,i,x:integer; begin write('Input augend:'); readln(n); lena:=length(n); {加数放入a数组} for i:=1 to lena do a[lena-i+1]:=ord(n[i])-ord('0'); write('Input addend:'); readln(n); lenb:=length(n); {被加数放入b数组} for i:=1 to lenb do b[lenb-i+1]:=ord(n[i])-ord('0'); i:=1; while (i<=lena) or(i<=lenb) do begin x := a[i] + b[i] + x div 10; {两数相加,然后加前次进位} c[i] := x mod 10; {保存第i位的值} i := i + 1 end; if x>=10 then {处理最高进位} begin lenc:=i;c[i]:=1 end else lenc:=i-1; for i:=lenc downto 1 do write(c[i]); {输出结果} writeln end. 例2 高精度减法。 从键盘读入两个正整数,求它们的差。 分析:类似加法,可以用竖式求减法。在做减法运算时,需要注意的是:被减数必须比减数大,同时需要处理借位。 因此,可以写出如下关系式 if a[i]1) do dec(lenc); {最高位的0不输出} for i:=lenc downto 1 do write(c[i]); writeln end. 例3 高精度乘法。 从键盘读入两个正整数,求它们的积。 分析:类似加法,可以用竖式求乘法。在做乘法运算时,同样也有进位,同时对每一位进乘法运算时,必须进行错位相加,如图3, 图4。 分析C数组下标的变化规律,可以写出如下关系式 C i = C’ i +C ”i +… 由此可见,C i跟A[i]*B[j]乘积有关,跟上次的进位有关,还跟原C i的值有关,分析下标规律,有 x:= A[i]*B[j]+ x DIV 10+ C[i+j-1]; C[i+j-1] := x mod 10; 类似,高精度乘法的参考程序: program exam3; 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 {对乘数的每一位进行处理} x := a[i]*b[j] + x div 10 + c[i+j-1]; {当前乘积+上次乘积进位+原数} c[i+j-1] := x mod 10; end; c[i+j]:= x div 10; {进位} 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. 例4 高精度除法。 从键盘读入两个正整数,求它们的商(做整除)。 分析:做除法时,每一次上商的值都在0~9,每次求得的余数连接以后的若干位得到新的被除数,继续做除法。因此,在做高精度除法时,要涉及到乘法运算和减法运 ... ...

~~ 您好,已阅读到文档的结尾了 ~~