p(x)=2x3+3.2x5-6x+10polycoef3.2-6.02.010.0...3510exp...6/38
coef 2.0 3 3.2 5 -6.0 1 10.0 0 . . . exp . poly p(x)=2x 3+3.2x 5-6x+10 6/38
2.设计Polyclass的基本运算算法(1)在顺序表末尾添加元素Add(p)/末尾添加一个多项式项public void Add(PolyElemp)Tpoly.add(p);7/38
2. 设计PolyClass的基本运算算法 public void Add(PolyElem p) //末尾添加一个多项式项 { poly.add(p); } 7/38
(2)创建多项式顺序表CreatePoly(a,b,n)public voidCreatePoly(double[] a,int[] b,int n)1/建立多项式顺序表for ((int i=o;i<n;i++)poly.add(new PolyElem(a[i],b[i]));8/38
public void CreatePoly(double[] a,int[] b,int n) //建立多项式顺序表 { for (int i=0;i<n;i++) poly.add(new PolyElem(a[i],b[i])); } 8/38
(3)按exp成员递减排序Sort()public void Sort()//对多项式顺序表按exp成员递减排序poly.sort(Comparator.comparing(PolyElem::getexp).reversed());9/38
public void Sort() //对多项式顺序表按exp成员递减排序 { poly.sort(Comparator.comparing(PolyElem::getexp).reversed()); } 9/38
(4)输出多项式顺序表DispPoly()//输出多项式顺序表public void DispPoly()//first为true表示是第一项boolean first=true;一int i=o;while (i<poly.size())fPolyElemp=(PolyElem)poly.get(i);if (first)first=false;else if (p.coef>o) System.out.print("+");//指数为0时不输出xif (p.exp==0)System.out.print(p.coef);//指数为1时不输出指数else if (p.exp==1)System.out.print(p.coef+"x");elseSystem.out.print(p.coef+"x^"+p.exp);i++;System.out.println();10/38
public void DispPoly() //输出多项式顺序表 { boolean first=true; //first为true表示是第一项 int i=0; while (i<poly.size()) { PolyElem p=(PolyElem)poly.get(i); if (first) first=false; else if (p.coef>0) System.out.print("+"); if (p.exp==0) //指数为0时不输出'x' System.out.print(p.coef); else if (p.exp==1) //指数为1时不输出指数 System.out.print(p.coef+"x"); else System.out.print(p.coef+"x^"+p.exp); i++; } System.out.println(); } 10/38