分开读字符串s或闹磨铅许更方便
var
ch
:char;
a,b
:longint;
c
:string;
begin
{一个一个字符去读}
read(ch);
c:='';
while
ch<>'
'
do
begin
c:=c+ch;
{读一个,往c里面写一个}
read(ch);
end;
readln(a,b);
{最后就可以放心地读数啦}
end.
非要先读一个字符串再游岩分开的话,会麻烦些
var
s
:string;
a,b
:longint;
c
:string;
begin
readln(s);
while
s[1]='
'
do
delete(s,1,1);
{如果前面有空格通通干掉}
c:='';
while
s[1]<>'
'
do
begin
c:=c+s[1];
delete(s,1,1);
{写一个液好,删一个}
end;
delete(s,1,1);
{最后把名字后的空格删掉}
a:=0;
while
s[1]<>'
'
do
begin
a:=a*10+ord(s[1])-ord('0');
{这样多位数也不怕}
delete(s,1,1);
end;
delete(s,1,1);
b:=0;
{b和a是一个思路}
while
s<>''
do
begin
b:=b*10+ord(s[1])-ord('0');
delete(s,1,1);
end;
end.
用STR函数,格式大概是这样STR(NUM,STRING)
readln(s1);
l1:=length(s1);
for i:=1 to l1 do
val(s1[i],a[l1+1-i]);
a[0]:=l1;
计算
for i:=a[0] downto 1 do
write(a[i]);
(这键轮样输轿陪出和字符串效稿帆信果相同)
附 高精加var
a:array[1..100] of integer;
b:array[1..100] of integer;
c:array[0..101] of integer;
i,l1,l2,len:integer;
s1,s2:string;
begin
readln(s1);
readln(s2);
l1:=length(s1);
l2:=length(s2);
for i:=1 to l1 do
val(s1[i],a[l1+1-i]);
for i:=1 to l2 do
val(s2[i],b[l2+1-i]);
fillchar(c,sizeof(c),0);
len:=0;
if l1>len then len:=l1;
if l2>len then len:=l2;
for i:=1 to len do
begin
c[i]:=a[i]+b[i]+c[i-1] div 10;
c[i-1]:=c[i-1] mod 10;
end;
inc(len);
while c[len]>9 do
begin
inc(len);
c[len]:=c[len] div 10;
c[len-1]:=c[len-1] mod 10;
end;
while c[len]=0 do
dec(len);
for i:=len downto 1 do
write(c[i]);
writeln;
end.
Num -> String
STR函数 STR(NUM,STRING)配蚂颂
String -> Num
val(string,num,p) p=1 OR 0 判断是否能字符串物胡转成数字培郑
例如a+b;
const max=250;
var s1,s2:string;
a,b,c:array[1..max] of byte;
l1,l2,l,i:integer;
begin
writeln('input two large integer:');
readln(s1);
readln(s2); {用字符串方式读入两陵陪个高精度数}
l1:=length(s1);
l2:=length(s2);
for i:=1 to max do begin a[i]:=0;b[i]:=0;c[i]:=0;end; {注意一定要初始化}
for i:=1 to l1 do
a[i]:=ord(s1[l1+1-i])-48;
for i:=1 to l2 do
b[i]:=ord(s2[l2+1-i])-48; {以上是把两个高精度数逐位处理并转存到a、b两个数组中}
if l1>l2 then l:=l1 else l:=l2;
for i:=1 to l do c[i]:=a[i]+b[i]; {对应位相加}
for i:=1 to l do {从低位到高位,统一处理进位}
if c[i]>=10 then
begin
c[i]:=c[i]-10;
c[i+1]:=c[i+1]+1;
end;
if c[l+1]>0 then l:=l+1;
write('档启add='); {输尺蠢蠢出}
for i:=l downto 1 do write(c[i]);
readln;
end.