求pascal程序:高精度乘法过程(高精度*单精度)
全局变量:s:string;{高精度数}m:integer;{单精度数}t:string;{结果}...
全局变量:s:string; {高精度数}
m:integer; {单精度数}
t:string; {结果} 展开
m:integer; {单精度数}
t:string; {结果} 展开
2个回答
展开全部
Const
maxlen=1500;
numbers=['0'..'9'];
Type
gjd=record
d:array[0..maxlen] of longint;
len:longint;
zf:boolean;
end;
{……}
Procedure initgjd(var p:gjd;k:longint);//初始化gjd置为k
var
i:longint;
s:string;
begin
str(abs(k),s);
p.len:=length(s);
fillchar(p.d,sizeof(p.d),0);
if k>=0 then p.zf:=true else p.zf:=false;
for i:=1 to length(s) do
p.d[i]:=ord(s[length(s)-i+1])-ord('0');
end;
Function initgjd(var p:gjd):gjd;//无参数默认为0
begin
initgjd(p,0);
exit(p);
end;
Function zero(P:gjd):boolean;//判断是否为0
begin
if (p.len=1) and (p.d[1]=0) then exit(true);
exit(false);
end;
Function RealCheng(p1,p2:gjd):gjd;//绝对值乘
var
i,j:longint;
ans:gjd;
begin
initgjd(ans);
ans.len:=p1.len+p2.len;
for i:=1 to p1.len do
for j:=1 to p2.len do
inc(ans.d[i+j-1],p1.d[i]*p2.d[j]);
for i:=1 to ans.len do
begin
inc(ans.d[i+1],ans.d[i] div 10);
ans.d[i]:=ans.d[i] mod 10;
end;
while (ans.len>0) and (ans.d[ans.len]=0) do dec(ans.len);
exit(ans);
end;
Function cheng(p1,p2:gjd):gjd;//高精度乘
begin
if zero(p1) or zero(p2) then begin initgjd(cheng);exit;end;
Cheng:=RealCheng(p1,p2);
if p1.zf xor p2.zf then cheng.zf:=false else cheng.zf:=true;
end;
Function cheng(p1:gjd;k:integer):gjd;//高精度乘整数
begin
exit(cheng(p1,initgjd(k));
end;
Procedure readgjd(var p:gjd);//读入一行高精度
var
tmp:gjd;
i:longint;
c:char;
begin
initgjd(tmp);
tmp.len:=0;
while not eoln do
begin
read(c);
if c='-' then tmp.zf:=not tmp.zf;
if c in numbers then
begin
inc(tmp.len);
tmp.d[tmp.len]:=ord(c)-ord('0');
end;
end;
readln;
p:=tmp;
for i:=1 to p.len do
p.d[i]:=tmp.d[p.len-i+1];
end;
Procedure print(p:gjd);//输出一行高精度
var
i:longint;
begin
if not p.zf then write('-');
for i:=p.len downto 1 do
write(p.d[i]);
end;
maxlen=1500;
numbers=['0'..'9'];
Type
gjd=record
d:array[0..maxlen] of longint;
len:longint;
zf:boolean;
end;
{……}
Procedure initgjd(var p:gjd;k:longint);//初始化gjd置为k
var
i:longint;
s:string;
begin
str(abs(k),s);
p.len:=length(s);
fillchar(p.d,sizeof(p.d),0);
if k>=0 then p.zf:=true else p.zf:=false;
for i:=1 to length(s) do
p.d[i]:=ord(s[length(s)-i+1])-ord('0');
end;
Function initgjd(var p:gjd):gjd;//无参数默认为0
begin
initgjd(p,0);
exit(p);
end;
Function zero(P:gjd):boolean;//判断是否为0
begin
if (p.len=1) and (p.d[1]=0) then exit(true);
exit(false);
end;
Function RealCheng(p1,p2:gjd):gjd;//绝对值乘
var
i,j:longint;
ans:gjd;
begin
initgjd(ans);
ans.len:=p1.len+p2.len;
for i:=1 to p1.len do
for j:=1 to p2.len do
inc(ans.d[i+j-1],p1.d[i]*p2.d[j]);
for i:=1 to ans.len do
begin
inc(ans.d[i+1],ans.d[i] div 10);
ans.d[i]:=ans.d[i] mod 10;
end;
while (ans.len>0) and (ans.d[ans.len]=0) do dec(ans.len);
exit(ans);
end;
Function cheng(p1,p2:gjd):gjd;//高精度乘
begin
if zero(p1) or zero(p2) then begin initgjd(cheng);exit;end;
Cheng:=RealCheng(p1,p2);
if p1.zf xor p2.zf then cheng.zf:=false else cheng.zf:=true;
end;
Function cheng(p1:gjd;k:integer):gjd;//高精度乘整数
begin
exit(cheng(p1,initgjd(k));
end;
Procedure readgjd(var p:gjd);//读入一行高精度
var
tmp:gjd;
i:longint;
c:char;
begin
initgjd(tmp);
tmp.len:=0;
while not eoln do
begin
read(c);
if c='-' then tmp.zf:=not tmp.zf;
if c in numbers then
begin
inc(tmp.len);
tmp.d[tmp.len]:=ord(c)-ord('0');
end;
end;
readln;
p:=tmp;
for i:=1 to p.len do
p.d[i]:=tmp.d[p.len-i+1];
end;
Procedure print(p:gjd);//输出一行高精度
var
i:longint;
begin
if not p.zf then write('-');
for i:=p.len downto 1 do
write(p.d[i]);
end;
追问
我说的是高精度*单精度呀,你的哪个才是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
program zjh;
var
a:array[1..1000]of longint;
i,m,l,x:longint;
s:string;
begin
readln(s);
for i:=1 to length(s) do
a[i]:=ord(s[length(s)-i+1])-48;
readln(m);
l:=length(s);
x:=0;
for i:=1 to l do begin
a[i]:=a[i]*m+x;
x:=a[i] div 10;
a[i]:=a[i] mod 10;
end;
if x>0 then begin l:=l+1;a[l]:=x; end;
while a[l]>10 do begin l:=l+1;a[l]:=a[l-1] div 10;a[l-1]:=a[l-1] mod 10; end;
for i:=l downto 1 do
write(a[i]);
end.
var
a:array[1..1000]of longint;
i,m,l,x:longint;
s:string;
begin
readln(s);
for i:=1 to length(s) do
a[i]:=ord(s[length(s)-i+1])-48;
readln(m);
l:=length(s);
x:=0;
for i:=1 to l do begin
a[i]:=a[i]*m+x;
x:=a[i] div 10;
a[i]:=a[i] mod 10;
end;
if x>0 then begin l:=l+1;a[l]:=x; end;
while a[l]>10 do begin l:=l+1;a[l]:=a[l-1] div 10;a[l-1]:=a[l-1] mod 10; end;
for i:=l downto 1 do
write(a[i]);
end.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询