0
한 개 또는 여러 개의 서로 다른 용어로 구성된 다항식을 나타내는 LinkedList를 사용하여 프로그램을 작성하라는 메시지가 표시되었습니다. 대부분의 모든 것이 작동하는 것처럼 보이지만 결과 다항식을 인쇄 할 때 원하는 형식으로 가져 오는 데 문제가 있습니다.다항식 LinkedList 서식
내 다항식은 내림차순으로 형식이 지정되지만 오름차순으로 인쇄됩니다. 또한 다항식이 인쇄 될 때, 어떻게 든 각 다항 사이의 나머지 "+"기호에 문제를 일으키지 않고 전체 다항식 앞에있는 첫 번째 "+"부호를 제거해야합니다.
용어 클래스
public class Term {
private DecimalFormat formatHelper = new DecimalFormat("#.####");
int coeff;
private int exp;
private Term next;
public Term(int exp, int coeff, Term next) {
this.setExp(exp);
this.coeff = coeff;
this.setNext(next);
}
public String toString() {
String format = formatHelper.format(Math.abs(coeff));
if (getExp() == 0)
return format;
else
if (getExp() == 1)
return format + "x";
else
return format + "x^" + getExp();
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
public Term getNext() {
return next;
}
public void setNext(Term next) {
this.next = next;
}
다항식 클래스
public class Polynomial {
private double test = 0.0005;
private Term head;
public Polynomial() {
head = null;
}
/**
* Adds a term to the current polynomial with the specified coefficient and exponent
*/
public void addTerm(int exp, int coeff) {
if (Math.abs(coeff) < test)
return;
if (head == null || exp < head.getExp()) {
head = new Term(exp, coeff, head);
return;
}
Term last = null;
Term current = head;
while (current != null && exp > current.getExp()) {
last = current;
current = current.getNext();
}
if (current == null || exp != current.getExp())
last.setNext(new Term(exp, coeff, current));
else {
current.coeff += coeff;
if (Math.abs(current.coeff) < test)
if (last != null)
last.setNext(current.getNext());
else
head = head.getNext();
}
}
/**
* Formats the polynomial
*/
public String toString() {
StringBuffer buffer = new StringBuffer();
for (Term term = head; term != null; term = term.getNext())
if (term.coeff < 0)
buffer.append(" - " + term.toString());
else
buffer.append(" + " + term.toString());
return buffer.toString();
}
/**
* EXTRA CREDIT - Adds two polynomials
*/
public Polynomial add(Polynomial p2) {
Polynomial answer = clone();
for (Term term = p2.head; term != null; term = term.getNext())
answer.addTerm(term.getExp(), term.coeff);
return answer;
}
/**
* Special method used only for extra credit, aids in adding of polynomials
*/
public Polynomial clone() {
Polynomial answer = new Polynomial();
for (Term term = head; term != null; term = term.getNext())
answer.addTerm(term.getExp(), term.coeff);
return answer;
}
Tester Class
public class Prog7 {
public static void main(String[] args)
{
Polynomial p1 = new Polynomial();
Polynomial p2 = new Polynomial();
Scanner keyboard = new Scanner(System.in);
int coeffChoice;
int expChoice;
String userInput = "";
String userInput2 = "";
while(!userInput.equalsIgnoreCase("no")){
System.out.println("Please enter the coefficient of the current term: ");
coeffChoice = keyboard.nextInt();
System.out.println("Please enter the exponent of the current term: ");
expChoice = keyboard.nextInt();
p1.addTerm(expChoice, coeffChoice);
System.out.println("Would you like to add a term to the polynomial?");
userInput = keyboard.next();
}
System.out.println("Time to start building the second polynomial!");
while(!userInput2.equalsIgnoreCase("no")){
System.out.println("Please enter the coefficient of the current term: ");
coeffChoice = keyboard.nextInt();
System.out.println("Please enter the exponent of the current term: ");
expChoice = keyboard.nextInt();
p2.addTerm(expChoice, coeffChoice);
System.out.println("Would you like to add a term to the polynomial?");
userInput2 = keyboard.next();
}
System.out.println("Polynomial 1");
System.out.println(p1.toString());
System.out.println();
System.out.println("Polynomial 2");
System.out.println(p2.toString());
System.out.println();
System.out.println("Polynomial Addition.");
System.out.println(p1.add(p2));
}
예제 출력
전류 출력 : + 배^2 + 4X^5 + 9 배^6
원하는 출력 : 9x^6 + 4x^5 + 5x^2