資源簡(jiǎn)介 高精度加法program exam1;constmax=200;vara,b,c:array[1..max] of 0..9;n:string;lena,lenb,lenc,i,x:integer;beginwrite('Input augend:'); readln(n);lena:=length(n); {加數(shù)放入a數(shù)組}for i:=1 to lena do a[lena-i+1]:=ord(n[i])-ord('0');write('Input addend:'); readln(n);lenb:=length(n); {被加數(shù)放入b數(shù)組}for i:=1 to lenb do b[lenb-i+1]:=ord(n[i])-ord('0');i:=1;while (i<=lena) or(i<=lenb) do beginx := a[i] + b[i] + x div 10; {兩數(shù)相加,然后加前次進(jìn)位}c[i] := x mod 10; {保存第i位的值}i := i + 1end;if x>=10 then {處理最高進(jìn)位}begin lenc:=i;c[i]:=1 endelse lenc:=i-1;for i:=lenc downto 1 do write(c[i]); {輸出結(jié)果}writelnend.例2 高精度減法。從鍵盤(pán)讀入兩個(gè)正整數(shù),求它們的差。分析:類似加法,可以用豎式求減法。在做減法運(yùn)算時(shí),需要注意的是:被減數(shù)必須比減數(shù)大,同時(shí)需要處理借位。因此,可以寫(xiě)出如下關(guān)系式if a[i]c[i]:=a[i]-b[i]類似,高精度減法的參考程序:program exam2;constmax=200;vara,b,c:array[1..max] of 0..9;n,n1,n2:string;lena,lenb,lenc,i,x:integer;beginwrite('Input minuend:'); readln(n1);write('Input subtrahend:'); readln(n2);{處理被減數(shù)和減數(shù)}if (length(n1)beginn:=n1;n1:=n2;n2:=n;write('-') {n1end;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;while (i<=lena) or(i<=lenb) do beginx := a[i] - b[i] + 10 + x; {不考慮大小問(wèn)題,先往高位借10}c[i] := x mod 10 ; {保存第i位的值}x := x div 10 - 1; {將高位借掉的1減去}i := i + 1end;lenc:=i;while (c[lenc]=0) and (lenc>1) do dec(lenc); {最高位的0不輸出}for i:=lenc downto 1 do write(c[i]);writelnend.例3 高精度乘法。從鍵盤(pán)讀入兩個(gè)正整數(shù),求它們的積。分析:類似加法,可以用豎式求乘法。在做乘法運(yùn)算時(shí),同樣也有進(jìn)位,同時(shí)對(duì)每一位進(jìn)乘法運(yùn)算時(shí),必須進(jìn)行錯(cuò)位相加,如圖3, 圖4。分析C數(shù)組下標(biāo)的變化規(guī)律,可以寫(xiě)出如下關(guān)系式C i = C’ i +C ”i +…由此可見(jiàn),C i跟A[i]*B[j]乘積有關(guān),跟上次的進(jìn)位有關(guān),還跟原C i的值有關(guān),分析下標(biāo)規(guī)律,有x:= A[i]*B[j]+ x DIV 10+ C[i+j-1];C[i+j-1] := x mod 10;類似,高精度乘法的參考程序:program exam3;constmax=200;vara,b,c:array[1..max] of 0..9;n1,n2:string;lena,lenb,lenc,i,j,x:integer;beginwrite('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 beginx:=0;for j:=1 to lenb do begin {對(duì)乘數(shù)的每一位進(jìn)行處理}x := a[i]*b[j] + x div 10 + c[i+j-1]; {當(dāng)前乘積+上次乘積進(jìn)位+原數(shù)}c[i+j-1] := x mod 10;end;c[i+j]:= x div 10; {進(jìn)位}end;lenc:=i+j;while (c[lenc]=0) and (lenc>1) do dec(lenc);for i:=lenc downto 1 do write(c[i]);writelnend.例4 高精度除法。從鍵盤(pán)讀入兩個(gè)正整數(shù),求它們的商(做整除)。分析:做除法時(shí),每一次上商的值都在0~9,每次求得的余數(shù)連接以后的若干位得到新的被除數(shù),繼續(xù)做除法。因此,在做高精度除法時(shí),要涉及到乘法運(yùn)算和減法運(yùn)算,還有移位處理。當(dāng)然,為了程序簡(jiǎn)潔,可以避免高精度乘法,用0~9次循環(huán)減法取代得到商的值。這里,我們討論一下高精度數(shù)除以單精度數(shù)的結(jié)果,采取的方法是按位相除法。參考程序:program exam4;constmax=200;vara,c:array[1..max] of 0..9;x,b:longint;n1,n2:string;lena:integer;code,i,j:integer;beginwrite('Input dividend:'); readln(n1);write('Input divisor:'); readln(n2);lena:=length(n1);for i:=1 to lena do a[i] := ord(n1[i]) - ord('0');val(n2,b,code);{按位相除}x:=0;for i:=1 to lena do beginc[i]:=(x*10+a[i]) div b;x:=(x*10+a[i]) mod b;end;{顯示商}j:=1;while (c[j]=0) and (jfor i:=j to lena do write(c[i]) ;writelnend.實(shí)質(zhì)上,在做兩個(gè)高精度運(yùn)算時(shí)候,存儲(chǔ)高精度數(shù)的數(shù)組元素可以不僅僅只保留一個(gè)數(shù)字,而采取保留多位數(shù)(例如一個(gè)整型或長(zhǎng)整型數(shù)據(jù)等),這樣,在做運(yùn)算(特別是乘法運(yùn)算)時(shí),可以減少很多操作次數(shù)。 展開(kāi)更多...... 收起↑ 資源預(yù)覽 縮略圖、資源來(lái)源于二一教育資源庫(kù)