有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中使用LinkedList列出两个多项式的加法

我的任务是使用LinkedList数据结构制作一个添加两个多项式的程序,到目前为止,我已经编写了添加和接受两个多项式的代码。我的问题是,在用户输入多项式后,应该进行加法,但是返回的多项式的值总是0x^0

这是程序的主要代码。(注意:这是一个开关盒,所以我没有把整个方法)

System.out.println("Literal Coefficient of the two Polynomials: \'x\'");
System.out.println("Degree of the first Polynomial?");
System.out.print("Input: ");
    eV = readInteger();
    nC = new int[eV];
        for(int x=0; x<=nC.length-1; x++) {
            System.out.println("Numerical Coefficient of the term with Degree "+eV+"?");
            System.out.print("Input: ");
            nC[x] = readInteger();

            firstPoly.add(new Term(nC[x], eV, 'x'));

            eV--;
        }
resultPolyOne.setTerms(firstPoly);
System.out.println();   
System.out.println("The first Polynomial entered: ");
stringRepresentation(firstPoly);
System.out.println();
System.out.println();
System.out.println("Degree of the second Polynomial?");
System.out.print("Input: ");
    eV = readInteger();
    nC = new int[eV];
        for(int x=0; x<=nC.length-1; x++) {
            System.out.println("Numerical Coefficient of the term with Degree "+eV+"?");
            System.out.print("Input: ");
            nC[x] = readInteger();

            secondPoly.add(new Term(nC[x], eV, 'x'));

            eV--;
        }
resultPolyTwo.setTerms(firstPoly);    
System.out.println();   
System.out.println("The second Polynomial entered: ");
stringRepresentation(secondPoly);
System.out.println();
System.out.println();  
System.out.println("Result of the Addition: ");
System.out.println((resultPolyOne.addPolys(resultPolyTwo)).toString());

firstPoly.clear();
secondPoly.clear();

多项式的加法

public Polynomial addPolys(Polynomial otherPoly) throws Exception {
    LinkedList<Term> resultTerms = new LinkedList<Term>();
    Polynomial resultPoly = new Polynomial();
        for(int x=0; x<this.getTerms().size(); x++) {
            Term currentTerm = this.getTerms().get(x);
            resultTerms.add(new Term(currentTerm.getNumC(), currentTerm.getExpC(), currentTerm.getLitC()));
        }
        resultPoly.setTerms(resultTerms);
        for(int y=0; y<otherPoly.getTerms().size(); y++) {
            resultPoly.addTerm(otherPoly.getTerms().get(y));
        } 
        if(resultPoly.getTerms().size()==0) {
            resultPoly.addTerm(new Term(0,0,'x'));
        }
return resultPoly;
}//addPolys

向多项式中添加项的方法

public void addTerm(Term newTerm) throws Exception {
    int listIndex = 00;
    boolean foundFlag = false;
    Term currentTerm = null;

    for(listIndex=0; listIndex<polyTerm.size(); listIndex++) {
        currentTerm = polyTerm.get(listIndex);
            if(currentTerm.getExpC()<=newTerm.getExpC()) {
                foundFlag = true;
                break;
            }
    }
    if(!foundFlag) {
        polyTerm.add(newTerm);
    } else {
        if(currentTerm.getExpC()<newTerm.getExpC()) {
            polyTerm.add(listIndex, newTerm);
        } else {
            currentTerm.setNumC(currentTerm.getNumC()+newTerm.getNumC());
            if(currentTerm.getNumC()==0) {
                polyTerm.remove(listIndex);
            }
        }
    }
}//addTerm

我要补充的是,我的术语的构造器是这样的

Term(int numberCoefficent, char literalCoefficent, int exponentialCoefficient) {
    //Codes here
}

共 (1) 个答案

  1. # 1 楼答案

    多亏了@GhostCat,我终于复习了我的问题,并试图改进它,结果我找到了问题的答案

    @MordechayS问题就在我显示多项式的那一行内。 @马特,我会发布我的答案,所以我想不再需要帮助了

    问题是:

    System.out.println((resultPolyOne.addPolys(resultPolyTwo)).toString());
    

    我将其替换为:

    resultPolyThree.toStringRep();
    

    这是我在多项式类中创建的一个方法,我没有重写Object的toString方法,因为我想进一步完善它