Pascal語言高精度計算資料_第1頁
Pascal語言高精度計算資料_第2頁
Pascal語言高精度計算資料_第3頁
Pascal語言高精度計算資料_第4頁
Pascal語言高精度計算資料_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、高精度計算2.1高精度加法2.2高精度減法2.3高精度乘法2.4高精度除法2.1高精度加法高精度加法程序如下:program HighPrecision1_Plus;constfn_inp=hp1.inp;fn_out=hp1.out;maxlen=100; max length of the number typehp=recordlen:integer; length of the number s:array1.maxlen of integer s1 is the lowest position slen is the highest position end;varx:array1.

2、2 of hp;y:hp; x:input ; y:output procedure PrintHP(const p:hp);var i:integer;beginfor i:=p.len downto 1 do write(p.si);end;procedure init;varst:string;j,i:integer;beginassign(input,fn_inp);reset(input);for j:=1 to 2 dobeginreadln(st);xj.len:=length(st);for i:=1 to xj.len do change string to HP xj.si

3、:=ord(stxj.len+1-i)-ord(0);end;close(input);end;procedure Plus(a,b:hp;var c:hp); c:=a+b var i,len:integer;beginfillchar(c,sizeof(c),0);if a.lenb.len then len:=a.len get the bigger length of a,b else len:=b.len;for i:=1 to len do plus from low to high begininc(c.si,a.si+b.si);if c.si=10 thenbegindec(

4、c.si,10);inc(c.si+1); add 1 to a higher position end;end;if c.slen+10 then inc(len);c.len:=len;end;procedure main;beginPlus(x1,x2,y);end;procedure out;beginassign(output,fn_out);rewrite(output);PrintHP(y);writeln;close(output);end;begininit;main;out;end.2. 2高精度減法高精度減法程序如下: program HighPrecision2_Sub

5、tract;constfn_inp=hp2.inp;fn_out=hp2.out;maxlen=100; max length of the number typehp=recordlen:integer; length of the number s:array1.maxlen of integer s1 is the lowest positionslen is the highest position end;varx:array1.2 of hp;y:hp; x:input ; y:output positive:boolean;procedure PrintHP(const p:hp

6、);var i:integer;beginfor i:=p.len downto 1 do write(p.si);end;procedure init;varst:string;j,i:integer;beginassign(input,fn_inp);reset(input);for j:=1 to 2 dobeginreadln(st);xj.len:=length(st);for i:=1 to xj.len do change string to HP xj.si:=ord(stxj.len+1-i)-ord(0);end;close(input);end;procedure Sub

7、tract(a,b:hp;var c:hp); c:=a-b, suppose a=b var i,len:integer;beginfillchar(c,sizeof(c),0);if a.lenb.len then len:=a.len get the bigger length of a,b else len:=b.len;for i:=1 to len do ( subtract from low to high begininc(c.si,a.si-b.si);if c.si1) and (c.slen=0) do dec(len);c.len:=len;end;function C

8、ompare(const a,b:hp):integer;(1 if ab0 if a=b-1 if ab.len then len:=a.len ( get the bigger length of a,b else len:=b.len;while(len0) and (a.slen=b.slen) do dec(len);( find a position which have a different digit if len=0 then compare:=0 ( no difference else compare:=a.slen-b.slen;end;procedure main;

9、beginif Compare(x1,x2)=10) dobegininc(c.slen+1,c.slen div 10);c.slen=c.slen mod 10;inc(len);end;while(len1) and (c.slen=0) do dec(len);c.len:=len;end;procedure main;beginMultiply(x,z,y);end;procedure out;beginassign(output,fn_out);rewrite(output);PrintHP(y);writeln;close(output);end;begininit;main;o

10、ut;end.2.高精度乘一個整型數(shù)據(jù)(integer)只需要將上述程序的hp類型定義如下即可:typehp=recordlen:integer ( length of the number s:array1.maxlen of longint( s1 is the lowest position slen is the highest position end;3.高精度乘高精度程序如下:program HighPrecision4_Multiply2;constfn_inp=hp4.inp;fn_out=hp4.out;maxlen=100; ( max length of the num

11、ber typehp=recordlen:integer; ( length of the number s:array1.maxlen of integer( s1 is the lowest position slen is the highest position end;varx:array1.2 of hp;y:hp; ( x:input ; y:output procedure PrintHP(const p:hp);var i:integer;beginfor i:=p.len downto 1 do write(p.si);end;procedure init;varst:st

12、ring;j,i:integer;beginassign(input,fn_inp);reset(input);for j:=1 to 2 dobeginreadln(st);xj.len:=length(st);for i:=1 to xj.len do ( change string to HP 2.4高精度除法xj.si:=ord(stxj.len+1-i)-ord(0);end;close(input);end;procedure Multiply(a,b:hp;var c:hp); c:=a+b var i,j,len:integer;beginfillchar(c,sizeof(c

13、),0);for i:=1 to a.len dofor j:=1 to b.len dobegininc(c.si+j-1,a.si*b.sj);inc(c.si+j,c.si+j-1 div 10);c.si+j-1:=c.si+j-1 mod 10;end;len:=a.len+b.len+1;the product of a number with i digits and a number with j digitscan only have at most i+j+1 digitswhile(len1)and(c.slen=0) do dec(len);c.len:=len;end

14、;procedure main;beginMultiply(x1,x2,y);end;procedure out;beginassign(output,fn_out);rewrite(output);PrintHP(y);writeln;close(output);end;begininit;main;out;end.1.高精度除以整型數(shù)據(jù)(integer);程序如下:program HighPrecision3_Multiply1;constfn_inp=hp5.inp;fn_out=hp5.out;maxlen=100; max length of the number typehp=re

15、cordlen:integer; length of the number s:array1.maxlen of integer s1 is the lowest position slen is the highest position end;varx,y:hp;z,w:integer;procedure PrintHP(const p:hp);var i:integer;beginfor i:=p.len downto 1 do write(p.si);end;procedure init;varst:string;i:integer;beginassign(input,fn_inp);

16、reset(input);readln(st);x.len:=length(st);for i:=1 to x.len do change string to HP x.si:=ord(stx.len+1-i)-ord(0);readln(z);close(input);end;procedure Divide(a:hp;b:integer;var c:hp;var d:integer); c:=a div b ; d:=a mod b var i,len:integer;beginfillchar(c,sizeof(c),0);len:=a.len;d:=0;for i:=len downt

17、o 1 do ( from high to low begind:=d*10+a.si;c.si:=d div b;d:=d mod b;end;while(len1) and (c.slen=0) do dec(len);c.len:=len;end;procedure main;beginDivide(x,z,y,w);end;procedure out;beginassign(output,fn_out);rewrite(output);PrintHP(y);writeln;writeln(w);close(output);end;begininit;main;out;end.2.高精度

18、除以高精度程序如下:program HighPrecision4_Multiply2;constfn_inp=hp6.inp;fn_out=hp6.out;maxlen=100; ( max length of the number typehp=recordlen:integer; ( length of the number s:array1.maxlen of integer( s1 is the lowest positionslen is the highest position end;varx:array1.2 of hp;y,w:hp; ( x:input ; y:output

19、 procedure PrintHP(const p:hp);var i:integer;beginfor i:=p.len downto 1 do write(p.si);end;procedure init;varst:string;j,i:integer;beginassign(input,fn_inp);reset(input);for j:=1 to 2 dobeginreadln(st);xj.len:=length(st);for i:=1 to xj.len do ( change string to HP xj.si:=ord(stxj.len+1-i)-ord(0);end

20、;close(input);end;procedure Subtract(a,b:hp;var c:hp); ( c:=a-b, suppose a=b var i,len:integer;beginfillchar(c,sizeof(c),0);if a.lenb.len then len:=a.len ( get the bigger length of a,b else len:=b.len;for i:=1 to len do ( subtract from low to high begininc(c.si,a.si-b.si);if c.si1) and (c.slen=0) do

21、 dec(len);c.len:=len;end;function Compare(const a,b:hp):integer;(1 if ab0 if a=b-1 if ab.len then len:=a.len ( get the bigger length of a,b else len:=b.len;while(len0) and (a.slen=b.slen) do dec(len);( find a position which have a different digit if len=0 then compare:=0 ( no difference else compare:=a.slen-b.slen;end;procedure Multiply10(var a:hp); ( a:=a*10 var i:I

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論