主题:高精度
超时了……
看下
var s,t:string;{两串数字} ch:char;{运算符}
e,f,g,h,i,j:integer;a,b,c:packed array[1..100] of 0..9;{转换数组}
procedure jisuan;
begin
e:=length(s);f:=length(t);{长度}
for i:=1 to e do a[e+1-i]:=ord(s[i])-ord('0');{将字符串放入数组}
for i:=1 to f do b[f+1-i]:=ord(t[i])-ord('0');
if e<f then begin g:=f;f:=e;e:=f;g:=-1 end else g:=1;
{交换ef得结果最多可能的位数,g用于减法少位减多位时反减}
case ch of
'+':begin for i:=1 to e do
begin c[i]:=c[i]+a[i]+b[i];
if c[i]>9 then
begin c[i]:=c[i]-10;c[i+1]:=c[i+1]+1{进位} end;
end; end;
'-':begin
for i:=1 to e-1 do
begin c[i]:=c[i]+(a[i]-b[i])*g;{少位减多位反减}
if c[i]<0 then
begin c[i]:=c[i]+10;c[i+1]:=c[i+1]-1{借位} end;
end;
c[e]:=c[e]+a[e]-b[e];{最后一步直接减,保留符号}
if g=-1 then c[e]:=-c[e];{少位减多位变符号}
end;
'*':begin
for i:=1 to length(s) do
for j:=1 to length(t) do
begin c[i+j-1]:=c[i+j-1]+a[i]*b[j];
if c[i+j-1]>9 then
begin c[i+j]:=c[i+j]+c[i+j-1] div 10;
c[i+j-1]:=c[i+j-1] mod 10{进位} end;
end;
end;
end;
for i:=e*2 downto 1 do
if c[i]<>0 then
begin h:=i;exit{统计长度} end;
for i:=1 to h do write(c[h+1-i]);{输出}
end;
begin
readln(s);readln(ch);readln(t);{输入}
jisuan;
readln
end.
还有个问题就是如果输入的是负数………………
看下
var s,t:string;{两串数字} ch:char;{运算符}
e,f,g,h,i,j:integer;a,b,c:packed array[1..100] of 0..9;{转换数组}
procedure jisuan;
begin
e:=length(s);f:=length(t);{长度}
for i:=1 to e do a[e+1-i]:=ord(s[i])-ord('0');{将字符串放入数组}
for i:=1 to f do b[f+1-i]:=ord(t[i])-ord('0');
if e<f then begin g:=f;f:=e;e:=f;g:=-1 end else g:=1;
{交换ef得结果最多可能的位数,g用于减法少位减多位时反减}
case ch of
'+':begin for i:=1 to e do
begin c[i]:=c[i]+a[i]+b[i];
if c[i]>9 then
begin c[i]:=c[i]-10;c[i+1]:=c[i+1]+1{进位} end;
end; end;
'-':begin
for i:=1 to e-1 do
begin c[i]:=c[i]+(a[i]-b[i])*g;{少位减多位反减}
if c[i]<0 then
begin c[i]:=c[i]+10;c[i+1]:=c[i+1]-1{借位} end;
end;
c[e]:=c[e]+a[e]-b[e];{最后一步直接减,保留符号}
if g=-1 then c[e]:=-c[e];{少位减多位变符号}
end;
'*':begin
for i:=1 to length(s) do
for j:=1 to length(t) do
begin c[i+j-1]:=c[i+j-1]+a[i]*b[j];
if c[i+j-1]>9 then
begin c[i+j]:=c[i+j]+c[i+j-1] div 10;
c[i+j-1]:=c[i+j-1] mod 10{进位} end;
end;
end;
end;
for i:=e*2 downto 1 do
if c[i]<>0 then
begin h:=i;exit{统计长度} end;
for i:=1 to h do write(c[h+1-i]);{输出}
end;
begin
readln(s);readln(ch);readln(t);{输入}
jisuan;
readln
end.
还有个问题就是如果输入的是负数………………