线性表的应用2.5求解两个多项式相加问题描述2.5.1多项式p()=cx1+C,x2+..+cmxm求两个多项式相加的程序例如,p(x)=2x3+3.2x5-6x+10,q(×)=6x+1.8x5-2x3+x2-2.5x4-5r(x)=p(x)+q(x)r(x)=5x5-2.5x4+x2+51/38
多项式 求两个多项式相加的程序 例如,p(x)=2x 3+3.2x 5-6x+10,q(x)=6x+1.8x 5-2x 3+x 2-2.5x 4-5 r(x)=5x 5-2.5x 4+x 2+5 r(x)=p(x)+q(x) 1/38
p(x) = Cixe1+c,xe2 +...+cmxem拉(c2,e2) ..(C1,e1)(cm,em)多项式线性表多项式项2/38
(c1,e1) (c2,e2) . (cm,em) 多项式项 多项式线性表 2/38
ADT PolyClass//多项式抽象数据类型【 数据对象:PolyElem={(ci,ei) 1≤i≤n,c,Edouble,e,Eint);数据关系:r={<xi,yi>I Xi, yiEPolyElem,i=1,., n-1]基本运算:voidAdd(PolyElemp):将多项式项p添加到末尾。void CreatePoly(double[] a,int [] b,int n):由数组a和b创建多项式顺序表。void Sort():对多项式顺序表按指数递减排序。voidDispPoly():输出多项式顺序表。//ADT PolyClass在PolyClaSs的基础上实现以下求两个多项式L1和L2相加的运算算法:PolyClass Add(PolyClass L1,PolyclassL2);3/38
ADT PolyClass //多项式抽象数据类型 { 数据对象: PolyElem={(ci,ei) | 1≤i≤n,ci∈double,ei∈int}; 数据关系: r={<xi,yi> | xi,yi∈PolyElem,i=1,.,n-1} 基本运算: void Add(PolyElem p):将多项式项p添加到末尾。 void CreatePoly(double [] a,int [] b,int n): 由数组a和b创建多项式顺序表。 void Sort():对多项式顺序表按指数递减排序。 void DispPoly():输出多项式顺序表。 } //ADT PolyClass 在PolyClass的基础上实现以下求两个多项式L1和L2相加的运算算法: PolyClass Add(PolyClass L1,PolyClass L2); 3/38
采用顺序存储结构求解2.5.21.设计顺序存储结构多项式项类型PolyElem//多项式顺序表元素类型class PolyElem//系数double coef;1 /指数int exp;1/构造方法PolyElem(double c,int e) coef=c;exp=e;11 /返回exppublic int getexp()return exp;}4/38
1. 设计顺序存储结构 多项式项类型PolyElem class PolyElem //多项式顺序表元素类型 { double coef; //系数 int exp; //指数 PolyElem(double c,int e) //构造方法 { coef=c; exp=e; } public int getexp() //返回exp { return exp; } } 4/38
多项式顺序表Polyclass类//多项式顺序表类class Polyclass//存放多项式顺序表ArrayList<PolyElem>poly//构造方法public Polyclass(){//分配顺序表的data空间poly=new ArrayList<PolyElem>();7//包含Add、CreatePoly、Sort和DispPoly方法75/38
多项式顺序表PolyClass类 class PolyClass //多项式顺序表类 { ArrayList<PolyElem> poly; //存放多项式顺序表 public PolyClass() //构造方法 { poly=new ArrayList<PolyElem>(); //分配顺序表的data空间 } //包含Add、CreatePoly、Sort和DispPoly方法 } 5/38