Need help writing a method that adds 2 polynomials together (v2)

北战南征 提交于 2019-12-12 02:41:36

问题


Background: I am currently working on writing a method that adds two polynomials (given by 2 text files) together. So for example:

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

&

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

would result in: 4.0x^5 + 8.0x^4 + 2.0x^3 - 1.0x + 12

Currently, my output only produces: 8.0 x^4 + 4.0x^5 + 2.0x^3 - 1.0x + 12 -- this is because of the order of my for loops which you can see below. I need the terms to be in order.

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



}

Thank you.


回答1:


You can do either merge sort like method, or hash / membership lookup.

merge sort like should look like

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;
}   

If you like lookup based method.

/* 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);
}


来源:https://stackoverflow.com/questions/32849486/need-help-writing-a-method-that-adds-2-polynomials-together-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!