lllllt95 于 2014.10.31 18:01 提问

public class Literal {
private int coefficient;
private int exponent;
Literal next = null;// 默认是null

``````public Literal() {
this(0, 0);
}

public Literal(int coefficient, int exponent) {
setCoe(coefficient);
setExp(exponent);
this.next=null;
}

public int getCoe() {
return coefficient;
}

public int getExp() {
return exponent;
}

public void setCoe(int coefficient) {
this.coefficient = coefficient;
}

public void setExp(int exponent) {
this.exponent = exponent;
}
``````

}

public class Polynomial {
private Literal head;
private Literal current;

``````public Polynomial() {
head = new Literal();
current = head;
}

public void insertTerm(int coefficient, int exponent) {
Literal node = new Literal(coefficient, exponent);
current.next = node;// 在现有结点后添加新结点
current = node;
}

public Polynomial add(Polynomial p) {
Polynomial result = new Polynomial();
current = head.next;
p.current = p.head.next;// 都指向第一个元素

// 只有都不为空才能比较指数大小
while (current != null && p.current != null) {
System.out.println("指数 "+current.getExp()+" "+p.current.getExp());
// 指数相等时候
if (current.getExp() == p.current.getExp()) {
int a = current.getCoe() + p.current.getCoe();
if (a != 0) {
result.insertTerm(a, current.getExp());
current = current.next;
p.current = p.current.next;
}
}

// 指数不等时候
if (current.getExp() < p.current.getExp()) {
result.insertTerm(current.getCoe(), current.getExp());
current = current.next;
}
if (current.getExp() > p.current.getExp()) {
result.insertTerm(p.current.getCoe(), p.current.getExp());
p.current = p.current.next;
}
result.print();
}

// 处理还有剩余项的情况
while (current != null) {
result.insertTerm(current.getCoe(), current.getExp());
current = current.next;
}
while (p.current != null) {
result.insertTerm(p.current.getCoe(), p.current.getExp());
p.current = p.current.next;
}

return result;
}

public void print() {
String result = "";

current = head.next;// 指向第一个
while (current != null) {
String coe = "";
String str = "";// 先清空

if (current.getCoe() != 1)// 系数
coe += current.getCoe();

if (current.getExp() == 1) // 指数为1
str = coe + "x";
else
str = coe + "x^" + current.getExp();

if (current == head.next)
result += str;
else
result += "+" + str;

current = current.next;
}
System.out.println(result);
}

public static void main(String[] args) {
Polynomial p1 = new Polynomial();
p1.insertTerm(1, 1);
p1.insertTerm(2, 2);
p1.insertTerm(3, 3);
p1.insertTerm(4, 4);

Polynomial p2 = new Polynomial();
p2.insertTerm(2, 3);
p2.insertTerm(4, 5);

//模拟实现下面p1,p2相加的过程
Polynomial polynomial=new Polynomial();
p2.current=p2.head.next;
polynomial.insertTerm(p2.current.getCoe(), p2.current.getExp());
p2.current=p2.current.next;
polynomial.insertTerm(p2.current.getCoe(), p2.current.getExp());
polynomial.print();

Polynomial p = p1.add(p2);
System.out.print("p1+p2=");
p.print();
}
``````

}

2x^3+4x^5

x

Exception in thread "main" java.lang.NullPointerException
at Phlonomial.Polynomial.insertTerm(Polynomial.java:17)
at Phlonomial.Polynomial.add(Polynomial.java:41)
at Phlonomial.Polynomial.main(Polynomial.java:108)

Polynomial result = new Polynomial();
current = head.next;
if (current.getExp() < p.current.getExp()) {
result.insertTerm(current.getCoe(), current.getExp());
current = current.next;
}

1个回答

ChinMint   2016.10.25 11:05