在 Java 中使用 LinkedList 添加两个多项式
Addition of two Polynomials using LinkedList in Java
我的任务是编写一个使用 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的构造函数是这样的。
Term(int numberCoefficent, char literalCoefficent, int exponentialCoefficient) {
//Codes here
}
感谢@GhostCat,我最终审视了我的问题并尝试改进它,结果我找到了问题的答案。
@MordechayS 问题出在我显示多项式的那一行。
@matt 我会发布我的答案,所以我想不再需要帮助了。
问题是这样的:
System.out.println((resultPolyOne.addPolys(resultPolyTwo)).toString());
我替换为:
resultPolyThree.toStringRep();
这是我在 Polynomial Class 中创建的方法,我没有重写 Object 的 toString 方法,因为我想完善它。
我的任务是编写一个使用 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的构造函数是这样的。
Term(int numberCoefficent, char literalCoefficent, int exponentialCoefficient) {
//Codes here
}
感谢@GhostCat,我最终审视了我的问题并尝试改进它,结果我找到了问题的答案。
@MordechayS 问题出在我显示多项式的那一行。 @matt 我会发布我的答案,所以我想不再需要帮助了。
问题是这样的:
System.out.println((resultPolyOne.addPolys(resultPolyTwo)).toString());
我替换为:
resultPolyThree.toStringRep();
这是我在 Polynomial Class 中创建的方法,我没有重写 Object 的 toString 方法,因为我想完善它。