高精度代码(PSACAL)加·减·乘·除(高精度 div 2)

本文提供了高精度加法、减法、乘法及除法的实现方法,通过字符串操作完成大数运算,适用于超出常规整型或浮点型变量所能表示的数值范围。
//////////////////高精度加法///////////////////////

function jia(s1,s2:ansistring):ansistring;
var
        s0,s3:ansistring;
        l1,l2,i,l3:longint;
        a,b,c:array[0..10000]of longint;
begin
        fillchar(a,sizeof(a),0);
        fillchar(b,sizeof(b),0);
        fillchar(c,sizeof(c),0);
        if(length(s1)<length(s2))or(length(s1)=length(s2))and(s1<s2)then
        begin
                s0:=s1;
                s1:=s2;
                s2:=s0;
        end;
        l1:=length(s1);
        l2:=length(s2);
        for i:=1 to l1 do a[i]:=ord(s1[l1-i+1])-48;
        for i:=1 to l2 do b[i]:=ord(s2[l2-i+1])-48;
        for i:=1 to l1 do
        begin
                c[i]:=c[i]+a[i]+b[i];
                c[i+1]:=c[i+1]+(c[i]div 10);
                c[i]:=c[i]mod 10;
        end;
        l3:=i+1;
        s3:='';
        while (c[l3]=0) and (l3>1) do dec(l3);
        for i:=l3 downto 1 do s3:=s3+chr(c[i]+48);
        exit(s3);
end;

////////////////////高精度除法(高精度 div 2)///////////////////////

function chu(s5:string):string;
var
        s3:string;
        i,k:longint;
        a,b,c:array[0..10000]of longint;
begin
        fillchar(a,sizeof(a),0);
        fillchar(b,sizeof(b),0);
        fillchar(c,sizeof(c),0);
        a[0]:=length(s5);
        b[0]:=1;
        b[1]:=2;
        for i:=1 to a[0] do
                a[i]:=ord(s5[a[0]-i+1])-48;
        for i:=a[0] downto 1 do
        begin
                c[i]:=(k*10+a[i])div 2;
                k:=a[i] mod 2;
        end;
        while c[a[0]]=0 do dec(a[0]);
        for i:=a[0] downto 1 do s3:=s3+chr(48+c[i]);
        exit(s3);
end;

////////////////高精度乘法//////////////////////

function cheng(s5:ansistring):ansistring;
var
        s3:string;
        a,b,c:array[0..10000]of longint;
        i,x,j:longint;
begin
        fillchar(a,sizeof(a),0);
        fillchar(b,sizeof(b),0);
        fillchar(c,sizeof(c),0);
        a[0]:=length(s5);
        b[0]:=a[0];
        for i:=1 to a[0] do
        begin
                a[i]:=ord(s5[a[0]-i+1])-48;
                b[i]:=a[i];
        end;
        for i:=1 to a[0] do
        begin
                x:=0;
                for j:=1 to b[0] 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;
        s3:='';
        c[0]:=a[0]+b[0];
        while(c[c[0]]=0)and(c[0]>1)do c[0]:=c[0]-1;
        for i:=c[0] downto 1 do s3:=s3+chr(c[i]+48);
        exit(s3);
end;

/////////////////高精度减法////////////////////////////

function jian(str1,str2:string):string;
var
        t:string;
        a,b,c:array[1..300]of integer;
        l,l1,l2:integer;
        i:integer;
begin
        t:='';
        fillchar(a,sizeof(a),0);
        fillchar(b,sizeof(b),0);
        fillchar(c,sizeof(c),0);
        l1:=length(str1);
        for i:=1 to l1 do a[i]:=ord(str1[l1-i+1])-48;
        l2:=length(str2);
        for i:=1 to l2 do b[i]:=ord(str2[l2-i+1])-48;
        l:=l1;
        for i:=1 to l do
        begin
                if a[i]<b[i] then
                begin
                        a[i+1]:=a[i+1]-1;
                        a[i]:=a[i]+10 ;
                end;
                c[i]:=a[i]-b[i];
        end;
        while c[l]=0 do l:=l-1;
        for i:=l downto 1 do
                t:=t+chr(48+c[i]);
        exit(t);
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值