需要帮助编写一个添加2个多项式的方法(v2) [英] Need help writing a method that adds 2 polynomials together (v2)

查看:137
本文介绍了需要帮助编写一个添加2个多项式的方法(v2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
我正在编写一个方法,它将两个多项式(由2个文本文件给出)组合在一起。所以例如:

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

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

& ;

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

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

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

would result in: 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循环的顺序,您可以在下面看到。我需要这些术语。

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



}

谢谢。

推荐答案

您可以执行合并排序方式或散列/会员资格查询。

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

合并排序应该看起来像

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

这篇关于需要帮助编写一个添加2个多项式的方法(v2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆