在添加多项式时遇到问题

时间:2019-03-05 01:11:34

标签: java

public Polynomial add(Polynomial P2) {
    PolynomialImp poly = new PolynomialImp();
    Iterator<Term> iter = this.iterator();
    Iterator<Term> iter2 = P2.iterator();
    Term t1 = iter.next();
    Term t2 = iter2.next();
    while(iter.hasNext()&&iter2.hasNext()){
        if(t1.getExponent()==t2.getExponent()){
            poly.termList.add(new 
         TermImp(t1.getCoefficient()+t2.getCoefficient(),t1.getExponent()));
            t1 = iter.next();
            t2 = iter2.next();
        } else if(t1.getExponent()>t2.getExponent()){
            poly.termList.add(new TermImp(t1.getCoefficient(), 
            t1.getExponent()));
            t1 = iter.next();
        } else {
            poly.termList.add(new TermImp(t2.getCoefficient(), 
            t2.getExponent()));
            t2 = iter2.next();
        }
    }
    while(iter.hasNext()){
        poly.termList.add(new TermImp(t1.getCoefficient(), t1.getExponent()));
        t1 = iter.next();
    }
    while(iter2.hasNext()){
        poly.termList.add(new TermImp(t2.getCoefficient(), t2.getExponent()));
        t2 = iter2.next();
    }
    return poly;
}

我一直在使这种方法无法正常工作,它应该添加两个多项式,但是我无法使其添加最后一部分。它返回一个缺少最后一项的多项式,几乎是正数,这是由于while循环仅检查下一项是否为空,我如何更改我的代码以使其起作用

0 个答案:

没有答案