回 帖 发 新 帖 刷新版面

主题:跪求程序  一元多相式的表示和加减运算

一元多相式的表示和加减运算   
各位大哥大姐帮帮忙啦  谢谢

回复列表 (共2个回复)

沙发

可以用纪录数组
type rode=record
            n:integer
            y:integer
          end;
var data:array[1..100]of rode;
n存贮系数 y存储指数
相加时把指数相同的系数相加就行了

板凳

我编过一个一元多项式的加法,你可以去试一下哦!
由于本人也是出学者,可能会有一些错误,那就请楼主自己改一下了哦!
program dxsh;
  type
    linktp=^nodetp;
    nodetp=record
    coef:real;
    exp:integer;
    next:linktp
    end;
    polytp=linktp;
  var
    pa,pb:polytp;
    kc:integer;
  procedure inpoly(var v:polytp);
    var
      p,r:linktp;
      x:real;
    begin
      new(v); v^.next:=nil;
      r:=v;
      writeln('please enter x!');
      readln(x);
      while x<>0 do
        begin
          new(p); p^.coef:=x;
          writeln('please enter p^.exp!');
          readln(p^.exp);
          p^.next:=r^.next;
          r^.next:=p;
          r:=p;
          writeln('please enter x!');
          readln(x)
        end
    end;
  procedure outpoly(v:polytp);
    var
      p,r:linktp;
      x:real;
    begin
      p:=v^.next;
      while p<>nil do
        begin
          write(p^.coef); write('x^');
          write(p^.exp);
          p:=p^.next
        end;
        writeln
    end;
  procedure add_poly(var pa:polytp;pb:polytp);
    var
      pre,u,p,q,pc:linktp;
      x:real;
    begin
      p:=pa^.next;
      q:=pb^.next;
      pre:=pa;
      pc:=pa;
      while (p<>nil) and (q<>nil) do
      if p^.exp<q^.exp
        then
          begin
            pre:=p;
            p:=p^.next
          end
        else
          begin
            if p^.exp=q^.exp
              then
                begin
                  x:=p^.coef+q^.coef;
                  if x<>0
                    then
                      begin
                        p^.coef:=x;
                        pre:=p
                      end
                    else
                      begin
                        pre^.next:=p^.next;
                        dispose(p)
                      end;
                  p:=pre^.next;
                  u:=q;
                  q:=q^.next;
                  dispose(u)
                end
              else
                begin
                  u:=q^.next;
                  q^.next:=p;
                  pre^.next:=q;
                  pre:=q;
                  q:=u
                end
          end;
      if q<>nil
        then
         begin
           pre^.next:=q;
           dispose(pb)
         end
    end;
  begin
    repeat
      writeln('1: enter dxsh pa');
      writeln('2: enter dxsh pb');
      writeln('3: pa+pb');
      writeln('please enter your choose!');
      readln(kc);
      case kc of
        1: inpoly(pa);
        2: inpoly(pb);
        3: begin
             add_poly(pa,pb);
             outpoly(pa)
           end
      end;
    until kc=0
  end.

我来回复

您尚未登录,请登录后再回复。点此登录或注册