Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带链表的多项式乘法次数数学错误_Java_Loops_Math_Linked List_Logic - Fatal编程技术网

Java 带链表的多项式乘法次数数学错误

Java 带链表的多项式乘法次数数学错误,java,loops,math,linked-list,logic,Java,Loops,Math,Linked List,Logic,我的乘法逻辑不正确。当我必须把两个多项式相乘得到的相同程度的项相加时,我似乎没有考虑 public Polynomial multiply(Polynomial p) { if (p.poly == null || this.poly == null) { Polynomial zero = new Polynomial(); zero.poly = new Node (0, 0, null); return zero; } els

我的乘法逻辑不正确。当我必须把两个多项式相乘得到的相同程度的项相加时,我似乎没有考虑

public Polynomial multiply(Polynomial p) {
    if (p.poly == null || this.poly == null) {
        Polynomial zero = new Polynomial();
        zero.poly = new Node (0, 0, null);
        return zero;
    } else {

        Polynomial retPol = new Polynomial();
        retPol.poly = new Node(0, 0, null);

        Node front = retPol.poly;
        Node entered = p.poly;
        Node thisPol = this.poly;

        int high = Integer.MIN_VALUE;
        int low = Integer.MAX_VALUE;

        while (entered != null) {
            thisPol = this.poly; 

            while (thisPol != null) {
                if (thisPol.term.degree + entered.term.degree > high)
                    high = thisPol.term.degree + entered.term.degree;
                if (thisPol.term.degree + entered.term.degree < low)
                    low = thisPol.term.degree + entered.term.degree;

                thisPol = thisPol.next;
            }

            entered = entered.next;
        }

        entered = p.poly;

        Node create = front;
        for (int i = low; i <= high; i++) { 
            create.term.degree = i;
            create.term.coeff = 0;

            create.next = new Node (0, 0, null);
            create = create.next;
        }

        entered = p.poly;

        while (entered != null) {
            thisPol = this.poly; 

            while (thisPol != null) {
                int degree = entered.term.degree + thisPol.term.degree;
                create = front;

                while (create != null) {
                    if (create.term.degree == degree) {
                        create.term.coeff = entered.term.coeff * thisPol.term.coeff;
                    }

                    create = create.next;
                }

                thisPol = thisPol.next;
            }

            entered = entered.next;
        }

        create = front;

        while (create != null) {
            if (create.term.degree == high) {
                create.next = null;
                create = create.next;
            }
            else
                create = create.next;
        }

        retPol.poly = front;

        return retPol;
    }
}
公共多项式乘法(多项式p){
if(p.poly==null | | this.poly==null){
多项式零=新多项式();
zero.poly=新节点(0,0,null);
返回零;
}否则{
多项式retPol=新多项式();
retPol.poly=新节点(0,0,null);
节点前端=retPol.poly;
输入的节点=p.poly;
节点thisPol=this.poly;
int高=整数最小值;
int low=整数最大值;
while(输入!=null){
thisPol=this.poly;
while(thisPol!=null){
如果(thisPol.term.degree+entered.term.degree>高)
高=此pol.term.degree+输入的.term.degree;
如果(thisPol.term.degree+entered.term.degree<低)
低=此pol.term.degree+输入的.term.degree;
thisPol=thisPol.next;
}
entered=entered.next;
}
输入=p.poly;
节点创建=前端;

对于(int i=low;i我认为您应该总结所有对,而不是在这里选择最后一对(或随机):

                if (create.term.degree == degree) {
                    create.term.coeff += entered.term.coeff * thisPol.term.coeff;
                }
[更新]您的代码和我的更正对我非常有效:

public class PolynomialTest {
    public static void main(String[] args) {
        // 4x^5 - 2x^3 + 2x + 3
        Polynomial p1 = new Polynomial(new Node(4.0, 5, new Node(-2.0, 3, new Node(2.0, 1, new Node(3.0, 0, null)))));
        // 8x^4 + 4x^3 - 3x + 9
        Polynomial p2 = new Polynomial(new Node(8.0, 4, new Node(4.0, 3, new Node(-3.0, 1, new Node(9.0, 0, null)))));
        Polynomial p3 = p1.multiply(p2);
        System.out.println(p3.toString());
    }
}

class Term {
    int degree;
    double coeff;
}

class Node {
    Term term;
    Node next;
    public Node(double coeff, int degree, Node next) {
        this.term = new Term();
        this.term.degree = degree;
        this.term.coeff = coeff;
        this.next = next;
    }
}

class Polynomial {
    private Node poly;

    public Polynomial() {}

    public Polynomial(Node poly) {
        this.poly = poly;
    }

    public Polynomial multiply(Polynomial p) {
        if (p.poly == null || this.poly == null) {
            Polynomial zero = new Polynomial();
            zero.poly = new Node (0, 0, null);
            return zero;
        } else {

            Polynomial retPol = new Polynomial();
            retPol.poly = new Node(0, 0, null);

            Node front = retPol.poly;
            Node entered = p.poly;
            Node thisPol = this.poly;

            int high = Integer.MIN_VALUE;
            int low = Integer.MAX_VALUE;

            while (entered != null) {
                thisPol = this.poly; 

                while (thisPol != null) {
                    if (thisPol.term.degree + entered.term.degree > high)
                        high = thisPol.term.degree + entered.term.degree;
                    if (thisPol.term.degree + entered.term.degree < low)
                        low = thisPol.term.degree + entered.term.degree;

                    thisPol = thisPol.next;
                }

                entered = entered.next;
            }

            entered = p.poly;

            Node create = front;
            for (int i = low; i <= high; i++) { 
                create.term.degree = i;
                create.term.coeff = 0;

                create.next = new Node (0, 0, null);
                create = create.next;
            }

            entered = p.poly;

            while (entered != null) {
                thisPol = this.poly; 

                while (thisPol != null) {
                    int degree = entered.term.degree + thisPol.term.degree;
                    create = front;

                    while (create != null) {
                        if (create.term.degree == degree) {
                            create.term.coeff += entered.term.coeff * thisPol.term.coeff;
                        }

                        create = create.next;
                    }

                    thisPol = thisPol.next;
                }

                entered = entered.next;
            }

            create = front;

            while (create != null) {
                if (create.term.degree == high) {
                    create.next = null;
                    create = create.next;
                }
                else
                    create = create.next;
            }

            retPol.poly = front;

            return retPol;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Node n = poly;
        while (n != null) {
            if (sb.length() > 0)
                sb.append(" + ");
            sb.append(n.term.coeff);
            if (n.term.degree > 0)
                sb.append("x");
            if (n.term.degree > 1)
                sb.append("^").append(n.term.degree);
            n = n.next;
        }
        return sb.toString();
    }
}

我认为您应该总结所有对,而不是在这里选择最后(或随机)对:

                if (create.term.degree == degree) {
                    create.term.coeff += entered.term.coeff * thisPol.term.coeff;
                }
[更新]您的代码和我的更正对我非常有效:

public class PolynomialTest {
    public static void main(String[] args) {
        // 4x^5 - 2x^3 + 2x + 3
        Polynomial p1 = new Polynomial(new Node(4.0, 5, new Node(-2.0, 3, new Node(2.0, 1, new Node(3.0, 0, null)))));
        // 8x^4 + 4x^3 - 3x + 9
        Polynomial p2 = new Polynomial(new Node(8.0, 4, new Node(4.0, 3, new Node(-3.0, 1, new Node(9.0, 0, null)))));
        Polynomial p3 = p1.multiply(p2);
        System.out.println(p3.toString());
    }
}

class Term {
    int degree;
    double coeff;
}

class Node {
    Term term;
    Node next;
    public Node(double coeff, int degree, Node next) {
        this.term = new Term();
        this.term.degree = degree;
        this.term.coeff = coeff;
        this.next = next;
    }
}

class Polynomial {
    private Node poly;

    public Polynomial() {}

    public Polynomial(Node poly) {
        this.poly = poly;
    }

    public Polynomial multiply(Polynomial p) {
        if (p.poly == null || this.poly == null) {
            Polynomial zero = new Polynomial();
            zero.poly = new Node (0, 0, null);
            return zero;
        } else {

            Polynomial retPol = new Polynomial();
            retPol.poly = new Node(0, 0, null);

            Node front = retPol.poly;
            Node entered = p.poly;
            Node thisPol = this.poly;

            int high = Integer.MIN_VALUE;
            int low = Integer.MAX_VALUE;

            while (entered != null) {
                thisPol = this.poly; 

                while (thisPol != null) {
                    if (thisPol.term.degree + entered.term.degree > high)
                        high = thisPol.term.degree + entered.term.degree;
                    if (thisPol.term.degree + entered.term.degree < low)
                        low = thisPol.term.degree + entered.term.degree;

                    thisPol = thisPol.next;
                }

                entered = entered.next;
            }

            entered = p.poly;

            Node create = front;
            for (int i = low; i <= high; i++) { 
                create.term.degree = i;
                create.term.coeff = 0;

                create.next = new Node (0, 0, null);
                create = create.next;
            }

            entered = p.poly;

            while (entered != null) {
                thisPol = this.poly; 

                while (thisPol != null) {
                    int degree = entered.term.degree + thisPol.term.degree;
                    create = front;

                    while (create != null) {
                        if (create.term.degree == degree) {
                            create.term.coeff += entered.term.coeff * thisPol.term.coeff;
                        }

                        create = create.next;
                    }

                    thisPol = thisPol.next;
                }

                entered = entered.next;
            }

            create = front;

            while (create != null) {
                if (create.term.degree == high) {
                    create.next = null;
                    create = create.next;
                }
                else
                    create = create.next;
            }

            retPol.poly = front;

            return retPol;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Node n = poly;
        while (n != null) {
            if (sb.length() > 0)
                sb.append(" + ");
            sb.append(n.term.coeff);
            if (n.term.degree > 0)
                sb.append("x");
            if (n.term.degree > 1)
                sb.append("^").append(n.term.degree);
            n = n.next;
        }
        return sb.toString();
    }
}

你说不走运是什么意思?错误的数字?你想乘法的多项式是什么?你只显示了预期的结果多项式,没有输入。哎呀,对不起。
[4x^5-2x^3+2x+3]*[8x^4+4x^3-3x+9]
@qqxx,我刚刚检查过,一切似乎都很好。请参阅我的更新以获取完整的示例。你所说的不走运是什么意思?错误的数字?你想乘法的多项式是什么?你只显示了预期的结果多项式,没有输入。哎呀,对不起。
[4x^5-2x^3+2x+3]*[8x^4+4x^3-3x+9]
@qqxx,我刚刚检查过,一切似乎都很好。请参阅我的更新以获取完整示例。