2 lllllt95 lllllt95 于 2014.10.31 18:01 提问

一个简单java程序,用链表实现多项式相加

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
指数 1 3
x
指数 2 3
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)

就是说“current.next = node;// 在现有结点后添加新结点”这句空指针报错,可是我看不出来有什么问题啊QAQ
Polynomial result = new Polynomial();
current = head.next;
if (current.getExp() < p.current.getExp()) {
result.insertTerm(current.getCoe(), current.getExp());
current = current.next;
}
和上面的模拟过程应该是类似,求解答!小女子感激不尽~调试好久就是不知道为何报错 T T

1个回答

ChinMint
ChinMint   2016.10.25 11:05

私以为代码有点错误,在那个判断指数相等那里,你只讨论了 a!=0的情况,如果a=0,程序就进行不下去了。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!