【问题标题】:Need help writing a method that adds 2 polynomials together (v2)需要帮助编写将 2 个多项式相加的方法 (v2)
【发布时间】:2015-09-29 16:44:13
【问题描述】:

背景: 我目前正在编写一种将两个多项式(由 2 个文本文件给出)相加的方法。比如:

4.0x^5 + -2.0x^3 + 2.0x + 3.0

&

8.0x^4 + 4.0x^3 + -3.0x + 9.0

将导致:4.0x^5 + 8.0x^4 + 2.0x^3 - 1.0x + 12

目前,我的输出仅产生:8.0 x^4 + 4.0x^5 + 2.0x^3 - 1.0x + 12 - 这是因为您可以在下面看到的 for 循环的顺序。我需要条款井井有条。

Polynomial answer = new Polynomial();


    //p = addZeroes(p);



    for (Node firstPoly = poly; firstPoly != null; firstPoly = firstPoly.next){
        boolean polyAdded = false;
        for (Node secondPoly = p.poly; secondPoly != null; secondPoly = secondPoly.next){

            if (firstPoly.term.degree == secondPoly.term.degree){

            answer = addToRear(answer, (firstPoly.term.coeff + secondPoly.term.coeff), firstPoly.term.degree, null);
                    if (answer.poly.term.coeff == 0){
                        answer.poly = null;
                    }
                    polyAdded = true;


            }


        }
        if (polyAdded == false){
        answer = addToRear(answer, firstPoly.term.coeff, firstPoly.term.degree, null);
        if (answer.poly.term.coeff == 0){
            answer.poly = null;
        }
        }

    }

    for (Node secondPoly = p.poly; secondPoly != null; secondPoly = secondPoly.next){
        boolean match = false;
        for (Node answerPoly = answer.poly; answerPoly != null; answerPoly = answerPoly.next){
            if (secondPoly.term.degree == answerPoly.term.degree){
                match = true;
                break;
            }


        }
        if (match == false){
        answer = addToRear(answer, secondPoly.term.coeff, secondPoly.term.degree, null);
        }
    }



    return answer;

    //alt + shift + r



}

谢谢。

【问题讨论】:

    标签: java data-structures linked-list


    【解决方案1】:

    您可以执行合并排序之类的方法,或哈希/成员查找。

    合并排序应该是这样的

    Node firstPoly = poly;
    Node secondPoly = p.poly;
    
    while (firstPoly != null && secondPoly != null) {
        if (firstPoly.term.degree == secondPoly.term.degree) {
            firstPoly.term.coeff += secondPoly.term.coeff;
    
            /* move both of them. */
            firstPoly = firstPoly.next;
            secondPoly = secondPoly.next;
        }   
        else if (firstPoly.term.degree > secondPoly.term.degree) {
            /* move the firstPoly */
            firstPoly = firstPoly.next;
        }   
        else /* if (firstPoly.term.degree < secondPoly.term.degree) */ {
            /* add secondPoly before firstPoly, and move the secondPoly */
    
            addBefore(firstPoly, secondPoly);
            secondPoly = secondPoly.next;
        }   
    }   
    
    /* flush secondPoly */
    while (secondPoly != null) {
        addRear(secondPoly);
        secondPoly = secondPoly.next;
    }   
    

    如果你喜欢基于查找的方法。

    /* This returns a node with given degree. */
    Node lookup(int degree);
    /* Adds a new term. */
    void addTerm(int degree, int coeff);
    
    for (secondPoly = p.poly; secondPoly != null; secondPoly = secondPoly.next) {
        Node node = lookup(secondPoly.term.degree);
        if (node != null)
            node.coeff += secondPoly.term.coeff;
        else
            addTerm(secondPoly.term.degree, secondPoly.term.coeff);
    }
    

    【讨论】:

    • 我不明白你的 while 循环条件。它的陈述方式,你是说 while firstPoly == true && secondPoly = true;
    • 本来应该是(firstPoly != null &amp;&amp; secondPoly != null)
    • 我明白了,但无论如何,这可以用我当前的 for 循环结构来完成吗?
    • 嵌套循环不起作用。另一种方法是使用查找方法。我更新了答案。
    • 我的解决方案现在成功地添加了每个多项式!但是..现在从第二个多项式添加的任何项都是乱序的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多