陈凯斌吧 关注:9贴子:84
  • 0回复贴,共1

多项式相乘

收藏回复

  • 202.101.104.*
uses math;
var s1,s2:string;
    a1,a2,a3:array[0..10000]of longint;
    i,j,n,m,o,x,y,q,x1,x2,y1,code:longint;
begin
    readln(s1); readln(s2);
    if (s1[1]<>'+') and (s1[1]<>'-') then s1:='+'+s1;
      x:=pos('+',s1); x2:=pos('-',s1);
      if (x=0) and (x2<>0) then x:=x2 else
      if (x2<>0) and (x2<x) then x:=x2;
    while x<>0 do
     begin
      q:=0;
      if s1[1]='+' then o:=1 else o:=-1;
      delete(s1,1,1);
      x:=pos('+',s1); x2:=pos('-',s1);
      if (x=0) and (x2<>0) then x:=x2 else
      if (x2<>0) and (x2<x) then x:=x2;
      y:=pos('x',copy(s1,1,x-1));
      if y=0 then
       begin
        val(copy(s1,1,x-1),q,code);
        a1[0]:=a1[0]+q*o;
       end
      else
       begin
        if s1[y+1]='^' then
        begin
         val(copy(s1,1,y-1),q,code);
         val(copy(s1,y+2,x-y-2),x1,code);
         if q=0 then q:=1;
         a1[x1]:=a1[x1]+o*q;
        end
        else
        begin
         val(copy(s1,1,y-1),q,code);
         if q=0 then q:=1;
         a1[1]:=a1[1]+o*q;
        end;
       end;
       delete(s1,1,x-1);
      end;
      y:=pos('x',s1);
      if y=0 then
       begin
        val(copy(s1,1,length(s1)),q,code);
        a1[0]:=a1[0]+q*o;
       end
      else
       begin
        if s1[y+1]='^' then
        begin
         val(copy(s1,1,y-1),q,code);
         val(copy(s1,y+2,length(s1)-y-2),x1,code);
         if q=0 then q:=1;
         a1[x1]:=a1[x1]+o*q;
        end
        else
        begin
         val(copy(s1,1,length(s1)-1),q,code);
         if q=0 then q:=1;
         a1[1]:=a1[1]+o*q;
        end;
       end;
   if (s2[1]<>'+') and (s2[1]<>'-') then s2:='+'+s2;
    x:=pos('+',s2); x2:=pos('-',s2);
      if (x=0) and (x2<>0) then x:=x2 else
      if (x2<>0) and (x2<x) then x:=x2;
    while x<>0 do
     begin
      q:=0;
      if s2[1]='+' then o:=1 else o:=-1;
      delete(s2,1,1);
      x:=pos('+',s2); x2:=pos('-',s2);
      if (x=0) and (x2<>0) then x:=x2 else
      if (x2<>0) and (x2<x) then x:=x2;
      y:=pos('x',copy(s2,1,x-1));
      if y=0 then
       begin
        val(copy(s2,1,x-1),q,code);
        a2[0]:=a2[0]+q*o;
       end
      else
       begin
        if s2[y+1]='^' then
        begin
         val(copy(s2,1,y-1),q,code);
         val(copy(s2,y+2,x-y-2),x1,code);
         if q=0 then q:=1;
         a2[x1]:=a2[x1]+o*q;
        end
        else
        begin
         val(copy(s2,1,y-1),q,code);
         if q=0 then q:=1;
         a2[1]:=a2[1]+o*q;
        end;
       end;
       delete(s2,1,x-1);
      end;
      y:=pos('x',s2);
      if y=0 then
       begin
        val(copy(s2,1,length(s2)),q,code);
        a2[0]:=a2[0]+q*o;
       end
      else
       begin
        if s2[y+1]='^' then
        begin
         val(copy(s2,1,y-1),q,code);
         val(copy(s2,y+2,length(s2)-y-2),x1,code);
         if q=0 then q:=1;
         a2[x1]:=a2[x1]+o*q;
        end
        else
        begin
         val(copy(s2,1,length(s2)-1),q,code);
         if q=0 then q:=1;
         a2[1]:=a2[1]+o*q;
        end;
       end;
    {for i:=0 to 6 do write(a1[i]);
    writeln;
    for i:=0 to 6 do write(a2[i]);}
    q:=0;
    for i:=0 to 1000 do
     for j:=0 to 1000 do
      if (a1[i]<>0) and (a2[j]<>0) then
       begin
        a3[i+j]:=a3[i+j]+a1[i]*a2[j];
        q:=1;
       end;
    for i:=10000 downto 2 do
     if a3[i]<>0 then if a3[i]>0 then write('+',a3[i],'x^',i) else
      write(a3[i],'x^',i);
    if a3[1]<>0 then if a3[1]>0 then write('+',a3[1],'x') else
     write(a3[1],'x');
    if a3[0]<>0 then if a3[0]>0 then write('+',a3[0]) else write(a3[0]);
    if q=0 then write(0);
end.



1楼2007-11-15 10:47回复