pascal的多多项式相加的代码:
var sia:array [1..200,1..2] of integer;
sc:char; sii,si,si1,si2:integer; s,s1:byte;
ss1,ss2,ss3:string;
ft:text;
begin
fillchar(sia,sizeof(sia),#0);
s:=0;
assign(ft,'lineb.in');
{$I-}reset(ft);
if IOResult<>0 then halt;
while not eof(ft) do begin
readln(ft,ss1);
repeat
s1:=pos(#32,ss1);
if s1=0 then break;
delete(ss1,s1,1);
until false;
if ss1>'' then ss1:=ss1+'+';
ss2:=''; ss3:=''; s1:=0;
while ss1>'' do begin
sc:=ss1[1];
delete(ss1,1,1);
case sc of
'^':begin
sc:=ss1[1];
if (sc='-')or(sc='+') then begin
ss3:=sc;
delete(ss1,1,1);
end;
end;
'x':s1:=1;
'+','-':if (ss3>'')or(ss2>'')or(s1=1) then begin
if (ss2='')or(ss2='+') then si1:=1
else if ss2='-' then si1:=-1
else val(ss2,si1,si);
if ss3>'' then val(ss3,si2,si)
else si2:=s1;
si:=0;
for s1:=1 to s do begin
if sia[s1,2]=si2 then begin
si:=s1;
break;
end;
end;
if si=0 then begin
inc(s);
sia[s,2]:=si2;
si:=s;
end;
inc(sia[si,1],si1);
ss2:=sc;
ss3:='';
s1:=0;
end;
'0'..'9':begin
if s1=0 then ss2:=ss2+sc
else ss3:=ss3+sc
end;
end;
end;
end;
close(ft);
for si:=1 to s-1 do begin
si1:=si;
for si2:=si+1 to s do begin
if sia[si1,2]>sia[si2,2] then si1:=si2;
end;
if si1<>si then for s1:=1 to 2 do begin
si2:=sia[si1,s1];
sia[si1,s1]:=sia[si,s1];
sia[si,s1]:=si2;
end;
end;
ss1:='';
assign(ft,'lineb.out');
{$I-} rewrite(ft);
if IOResult<>0 then halt;
for s1:=1 to s do begin
ss2:='';
if sia[s1,1]<>0 then begin
if (sia[s1,1]=-1) then ss1:=ss1+'-'
else begin
if (ss1>'')and(sia[s1,1]>0) then ss1:=ss1+'+';
if (sia[s1,1]<>1)or(sia[s1,2]=0) then begin
str(sia[s1,1],ss2);
ss1:=ss1+ss2;
end;
end;
if sia[s1,2]<>0 then begin
ss1:=ss1+'x';
if sia[s1,2]<>1 then begin
str(sia[s1,2],ss2);
ss1:=ss1+'^'+ss2;
end;
end;
end;
if Length(ss1)>100 then begin
write(ft,copy(ss1,1,90));
delete(ss1,1,90);
end;
end;
writeln(ft,ss1);
close(ft);
end.
若转载请注明出处: Spirit's Home
本文地址: http://www.7788sky.cn/post/pascalduoxs.html
0 Response to “pascal多项式相加”