package Phlonomial;
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;
}
}
上面的是结点类
下面的是链表类
package Phlonomial;
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
指数 1 3
x
指数 2 3
Exception in thread "main" java.lang.NullPointerException
at Phlonomial.Polynomial.insertTerm(Polynomial.java:15)
at Phlonomial.Polynomial.add(Polynomial.java:39)
at Phlonomial.Polynomial.main(Polynomial.java:107)
不懂为什么报错啊,我觉得和模拟实现的过程几乎一样,为何这个会报错?
求解答,感激不尽~
------解决思路----------------------
// 指数不等时候
else if (current.getExp() < p.current.getExp()) {
result.insertTerm(current.getCoe(), current.getExp());
current = current.next;
}
else {
result.insertTerm(p.current.getCoe(), p.current.getExp());
p.current = p.current.next;
}
result.print();
}
把代码的指数小于和指数大于的时候改一下 加2个else就可以了
原因在于: 当指数相等时 程序会改变current和p.current的值 然后可能会满足指数不等的情况 应该直接让程序进入下一循环即可