## java 实现多项式相加相乘

1.定义一个类，get set 方法，toString 方法，指数和项数

4.使用java 中的scanner类交互，提示输入，输出

0
``````import java.util.LinkedList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

class Term {
private int exp; //指数 exponent
private int coef;//系数 coefficient
private String symbol ="X";
public Term(int coef, int exp){
setExp(exp);
setCoef(coef);
}
public void setExp(int exp){
this.exp = exp;
}
public void setCoef(int coef){
this.coef = coef;
}
public int getExp(){
return exp;
}
public int getCoef(){
return coef;
}
public String toString(){
String s = coef+"";
s += symbol;
s += "^" + exp;
return s;
}
}

class ExponentCompare implements Comparator<Term>{
public int compare(Term node1, Term node2){
if (node1.getExp()<node2.getExp())
return  1;
if (node1.getExp()>node2.getExp())
return -1;
return 0;
}
}

class Polynomial{
public Polynomial(){ //构造方法
}

// 多项式 p1, p2 相加 ,方法 返回相加的结果
public Polynomial add(Polynomial p1, Polynomial p2){
Polynomial p = new Polynomial();
for (int i=0;i<p1.polynomial.size();i++){
Term node = p2.search(p1.polynomial.get(i).getExp());
if (node==null)
else{
int n = node.getCoef() + p1.polynomial.get(i).getCoef();
}

}
return p;
}

// 多项式 p1, p2 相乘 ,方法 返回相乘的结果
public Polynomial multiply(Polynomial p1, Polynomial p2){
Polynomial p = new Polynomial();
for (int i=0;i<p1.polynomial.size();i++)
for (int j=0;j<p2.polynomial.size();j++)
p1.polynomial.get(i).getExp() + p2.polynomial.get(j).getExp() );
return p;
}

public Term search(int n){ //查询指数为n的元素。查询不到，返回null
int size = polynomial.size();
for (int i=0;i<size;i++)
if ( polynomial.get(i).getExp() == n)
return polynomial.get(i);
return null;
}
public void addTerm(int coefficient, int exponent){
Term node = search(exponent);
if (node==null){
Term term = new Term(coefficient,exponent);
Collections.sort(polynomial, new ExponentCompare() );
} else{
node.setCoef(node.getCoef()  + coefficient)	;
}
}
public String toString(){
String s = polynomial.get(0).toString();
for (int i=1;i<polynomial.size();i++)
s += "+" + polynomial.get(i);
return s;
}
}

public class Test{

public static void main(String args[]){

Polynomial poly1 = new Polynomial();
Polynomial poly2 = new Polynomial();
Scanner input = new Scanner(System.in);
int i =1, co=0,ex=0;
System.out.println("注意：每一个多项式的数据输入完毕时，将系数项 输入0值。");
System.out.println("输入第一个多项式的数据");
while (true) {
System.out.print("第 " + i + " 项系数 (若键入 0，则终止这个多项式的数据输入)：");
co = input.nextInt();
if (co==0) break;
System.out.print("第 " + i++ + " 项指数：");
ex = input.nextInt();
}
System.out.println("输入的第一个多项式：" + poly1);

System.out.println("输入第二个多项式的数据");
i=1;
while (true) {
System.out.print("第 " + i + " 项系数 (若键入 0，则终止这个多项式的数据输入)：");
co = input.nextInt();
if (co==0) break;
System.out.print("第 " + i++ + " 项指数：");
ex = input.nextInt();
}
System.out.println("输入的第二个多项式：" + poly2);

System.out.println("这两个多项式的乘积：" + poly1.multiply(poly1,poly2));
}

}``````

``````注意：每一个多项式的数据输入完毕时，将系数项 输入0值。

0
f

0

``````public String toString(){

return StringUtil.join(polynomial,"+");
}``````