• 全部
• 语言基础/算法/系统设计
• 数据库相关
• 图形处理/多媒体
• 网络通信/分布式开发
• VCL组件开发及应用
• Windows SDK/API
• 问答

# "无限"位四则运算完整代码!

ehom 2002-10-08 06:13:16
N天前zswang (伴水)兄贴了段实现无限位加法,乘法的代码!可惜少了两种!

uses StrUtils, Math;

function StrLeft(const mStr: string; mDelimiter: string): string;
begin
Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1);
end; { StrLeft }

function StrRight(const mStr: string; mDelimiter: string): string;
begin
if Pos(mDelimiter, mStr) <= 0 then
Result := ''
else Result := Copy(mStr, Pos(mDelimiter, mStr) + Length(mDelimiter), MaxInt);
end; { StrRight }

function formatnum(mNumber: string):string;
var
m:integer;
TemStr:string;
begin
Result:='';
for m:=1 to Length(mNumber) do
begin
if mNumber[m]='.' then
Result:=Result+'.'
else
Result:=Result+IntToStr(StrToIntDef(mNumber[m], 0));
end;
while Pos('0', Result) = 1 do Delete(Result, 1, 1); //排除整数前无效的0
if Pos('.', Result )<= 0 then Result := Result + '.'; //没有有小数点补小数点
TemStr:=StrRight(Result,'.');
while Copy(TemStr, Length(TemStr), 1) = '0' do Delete(TemStr, Length(TemStr), 1); //排除小数后无效的0
Result:=StrLeft(Result,'.')+'.'+TemStr;
if Copy(Result, Length(Result), 1) = '.' then Delete(Result, Length(Result), 1); //排除无效小数点
if Result[1]='.' then Result:='0'+Result;
if (Result = '') then Result := '0';
end;

function InfiniteAdd(mNumberA, mNumberB: string): string; { 无限位数加法 }
var
I: Integer;
T: Integer;
begin
Result := '';
if Pos('.', mNumberA) <= 0 then mNumberA := mNumberA + '.'; //没有有小数点补小数点
if Pos('.', mNumberB) <= 0 then mNumberB := mNumberB + '.'; //没有有小数点补小数点
I := Max(Length(StrLeft(mNumberA, '.')), Length(StrLeft(mNumberB, '.'))); //整数部分最大长度
mNumberA := DupeString('0', I - Length(StrLeft(mNumberA, '.'))) + mNumberA; //整数前补0
mNumberB := DupeString('0', I - Length(StrLeft(mNumberB, '.'))) + mNumberB; //整数前补0
T := Max(Length(StrRight(mNumberA, '.')), Length(StrRight(mNumberB, '.'))); //小数部分最大长度
mNumberA := mNumberA + DupeString('0', T - Length(StrRight(mNumberA, '.'))); //小数后补0
mNumberB := mNumberB + DupeString('0', T - Length(StrRight(mNumberB, '.'))); //小数后补0
I := I + T + 1; //计算总长度//小数长度和整数长度加上小数点长度
T := 0; //进位数初始化
for I := I downto 1 do //从后向前扫描
if [mNumberA[I], mNumberB[I]] <> ['.'] then begin //不是小数点时
T := StrToIntDef(mNumberA[I], 0) + T; //累加当前数位
T := StrToIntDef(mNumberB[I], 0) + T; //累加当前数位
Result := IntToStr(T mod 10) + Result; //计算当前数位上的数字
T := T div 10; //计算进位数
end else Result := '.' + Result; //加上小数点
if T <> 0 then Result := IntToStr(T mod 10) + Result; //处理进位数
while Pos('0', Result) = 1 do Delete(Result, 1, 1); //排除整数前无效的0
while Copy(Result, Length(Result), 1) = '0' do
Delete(Result, Length(Result), 1); //排除小数后无效的0
if Copy(Result, Length(Result), 1) = '.' then
Delete(Result, Length(Result), 1); //排除无效小数点
if Copy(Result, 1, 1) = '.' then Result := '0' + Result; //处理无0小数情况
if (Result = '') then Result := '0'; //处理空字符情况

function InfiniteMult(mNumberA, mNumberB: string): string; { 无限位数乘法 }

function fMult(mNumber: string; mByte: Byte): string; { 无限位数乘法子函数 }
var
I: Integer;
T: Integer;
begin
Result := '';
T := 0;
for I := Length(mNumber) downto 1 do begin //从后向前扫描
T := StrToIntDef(mNumber[I], 0) * mByte + T; //累加当前数位
Result := IntToStr(T mod 10) + Result; //计算当前数位上的数字
T := T div 10; //计算进位数
end;
if T <> 0 then Result := IntToStr(T mod 10) + Result; //处理进位数
end; { fMult }

var
I: Integer;
vDecimal: Integer; //小数位数
T: string;
begin
Result := '';
///////Begin 处理小数
if Pos('.', mNumberA) <= 0 then mNumberA := mNumberA + '.'; //没有有小数点补小数点
if Pos('.', mNumberB) <= 0 then mNumberB := mNumberB + '.'; //没有有小数点补小数点
vDecimal := Length(StrRight(mNumberA, '.')) + Length(StrRight(mNumberB, '.')); //计算小数位数
mNumberA := StrLeft(mNumberA, '.') + StrRight(mNumberA, '.'); //删除小数点
mNumberB := StrLeft(mNumberB, '.') + StrRight(mNumberB, '.'); //删除小数点
///////End 处理小数
T := '';
for I := Length(mNumberB) downto 1 do begin
Result := InfiniteAdd(Result, fMult(mNumberA, StrToIntDef(mNumberB[I], 0)) + T);
T := T + '0';
end;
Insert('.', Result, Length(Result) - vDecimal + 1);
while Pos('0', Result) = 1 do Delete(Result, 1, 1); //排除整数前无效的0
while Copy(Result, Length(Result), 1) = '0' do
Delete(Result, Length(Result), 1); //排除小数后无效的0
if Copy(Result, Length(Result), 1) = '.' then
Delete(Result, Length(Result), 1); //排除无效小数点
if Copy(Result, 1, 1) = '.' then Result := '0' + Result; //处理无0小数情况
if (Result = '') then Result := '0'; //处理空字符情况
end; { InfiniteMult }

function InfiniteSub(mNumberA, mNumberB: string): string; { 无限位数减法 }
var
I: Integer;
T: Integer;
TemNumA:String;
minus:Boolean;
begin
Result := '';
mNumberA:=formatnum(mNumberA);
mNumberB:=formatnum(mNumberB);
if Pos('.', mNumberA) <= 0 then mNumberA := mNumberA + '.'; //没有有小数点补小数点
if Pos('.', mNumberB) <= 0 then mNumberB := mNumberB + '.'; //没有有小数点补小数点
I := Max(Length(StrLeft(mNumberA, '.')), Length(StrLeft(mNumberB, '.'))); //整数部分最大长度
mNumberA := DupeString('0', I - Length(StrLeft(mNumberA, '.'))) + mNumberA; //整数前补0
mNumberB := DupeString('0', I - Length(StrLeft(mNumberB, '.'))) + mNumberB; //整数前补0
T := Max(Length(StrRight(mNumberA, '.')), Length(StrRight(mNumberB, '.'))); //小数部分最大长度
if ((Length(StrLeft(mNumberA, '.'))) > (Length(StrLeft(mNumberB, '.')))) or(((Length(StrLeft(mNumberA, '.'))) = (Length(StrLeft(mNumberB, '.'))))and(mNumberB>mNumberA))then
begin
TemNumA := mNumberA;
mNumberA := mNumberB + DupeString('0', T - Length(StrRight(mNumberB, '.'))); //小数后补0
mNumberB := TemNumA + DupeString('0', T - Length(StrRight(TemNumA, '.'))); //小数后补0
minus:=True;
end
else
begin
mNumberA := mNumberA + DupeString('0', T - Length(StrRight(mNumberA, '.'))); //小数后补0
mNumberB := mNumberB + DupeString('0', T - Length(StrRight(mNumberB, '.'))); //小数后补0
minus:=False;
end;
I := I + T + 1; //计算总长度//小数长度和整数长度加上小数点长度
T := 0; //进位数初始化
for I := I downto 1 do //从后向前扫描
if [mNumberA[I], mNumberB[I]] <> ['.'] then begin //不是小数点时
T := StrToIntDef(mNumberB[I], 0) - T; //累加当前数位
T := StrToIntDef(mNumberA[I], 0) - T; //累加当前数位
if (T<0) and (I<>1) then
begin
T:=T+10;
Result := IntToStr(T mod 10) + Result; //计算当前数位上的数字
T := -1; //计算进位数
end
else
begin
Result := IntToStr(T mod 10) + Result; //计算当前数位上的数字
T := T div 10; //计算进位数
end;
end else Result := '.' + Result; //加上小数点
if T <> 0 then Result := IntToStr(T mod 10) + Result; //处理进位数
while Pos('0', Result) = 1 do Delete(Result, 1, 1); //排除整数前无效的0
while Copy(Result, Length(Result), 1) = '0' do
Delete(Result, Length(Result), 1); //排除小数后无效的0
if Copy(Result, Length(Result), 1) = '.' then
Delete(Result, Length(Result), 1); //排除无效小数点
if Copy(Result, 1, 1) = '.' then Result := '0' + Result; //处理无0小数情况
if (Result = '') then Result := '0'; //处理空字符情况
if minus then Result:='-'+Result;
end; { InfiniteSub}
...全文
43 点赞 收藏 40

40 条回复

http://expert.csdn.net/Expert/topic/1355/1355122.xml?temp=9.316653E-02

magietao 2003-01-24
mark

chun2000 2002-10-23

stanely 2002-10-22
roge that

qiqi97 2002-10-22

viscky 2002-10-22

findcsdn 2002-10-10
sc

ly_liuyang 2002-10-10
:)收下

daniel007 2002-10-10

SnowFisher 2002-10-10
sc

blazingfire 2002-10-09

tangyong_delphi 2002-10-09

stdcall 2002-10-09

neilwq 2002-10-09

hongqi162 2002-10-09

ddvboy 2002-10-09
sou

forgot 2002-10-09

chao_jian 2002-10-09

xiaokang520 2002-10-09

lht96sd2 2002-10-09

Delphi

4869

Delphi 开发及应用

2002-10-08 06:13