首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多项式除法过载算子

多项式除法过载算子
EN

Stack Overflow用户
提问于 2010-03-12 14:56:17
回答 3查看 6.9K关注 0票数 2

好的。到目前为止,我成功地编写了以下操作,感谢您的帮助:

补充:

代码语言:javascript
复制
polinom operator+(const polinom& P) const
{
    polinom Result;
    constIter i = poly.begin(), j = P.poly.begin();

    while (i != poly.end() && j != P.poly.end()) { //logic while both iterators are valid
           if (i->pow > j->pow) { //if the current term's degree of the first polynomial is bigger
               Result.insert(i->coef, i->pow);
               i++;    
            }
            else if (j->pow > i->pow) { // if the other polynomial's term degree is bigger
               Result.insert(j->coef, j->pow);
               j++;
            }

            else { // if both are equal
                Result.insert(i->coef + j->coef, i->pow);
                i++; 
                j++; 
            }
    }

//handle the remaining items in each list
//note: at least one will be equal to end(), but that loop will simply be skipped

    while (i != poly.end()) {
        Result.insert(i->coef, i->pow);
        ++i;
    }

    while (j != P.poly.end()) {
        Result.insert(j->coef, j->pow);
        ++j;
    }
    return Result;
}

减法:

代码语言:javascript
复制
polinom operator-(const polinom& P) const //fixed prototype re. const-correctness
{
    polinom Result;
    constIter i = poly.begin(), j = P.poly.begin();

    while (i != poly.end() && j != P.poly.end()) { //logic while both iterators are valid
           if (i->pow > j->pow) { //if the current term's degree of the first polynomial is bigger
               Result.insert(-(i->coef), i->pow);
               i++;    
            }

            else if (j->pow > i->pow) { // if the other polynomial's term degree is bigger
               Result.insert(-(j->coef), j->pow);
               j++;
            }

            else { // if both are equal
                Result.insert(i->coef - j->coef, i->pow);
                i++; 
                j++; 
            }
    }

//handle the remaining items in each list
//note: at least one will be equal to end(), but that loop will simply be skipped

    while (i != poly.end()) {
        Result.insert(i->coef, i->pow);
        ++i;
    }

    while (j != P.poly.end()) {
        Result.insert(j->coef, j->pow);
        ++j;
    }
    return Result;
} 

乘法:

代码语言:javascript
复制
polinom operator*(const polinom& P) const
{
    polinom Result;
    constIter i, j, lastItem = Result.poly.end();
    Iter it1, it2, first, last;
    int nr_matches;

    for (i = poly.begin() ; i != poly.end(); i++) {
         for (j = P.poly.begin(); j != P.poly.end(); j++)
              Result.insert(i->coef * j->coef, i->pow + j->pow);
    }

    Result.poly.sort(SortDescending());

    lastItem--;

    while (true) {
        nr_matches = 0;

        for (it1 = Result.poly.begin(); it1 != lastItem; it1++) {
             first = it1;
             last = it1;
             first++;
             for (it2 = first; it2 != Result.poly.end(); it2++) { 
                  if (it2->pow == it1->pow) {
                      it1->coef += it2->coef;
                      nr_matches++;
                  }
             }

             nr_matches++;
             do {
                last++;
                nr_matches--;
             } while (nr_matches != 0);

             Result.poly.erase(first, last);
        }   
        if (nr_matches == 0)
            break;
    }     

    return Result;
}

Division(Edited):

代码语言:javascript
复制
polinom operator/(const polinom& P) const
{
    polinom Result, temp2;
    polinom temp = *this;
    Iter i = temp.poly.begin();
    constIter j = P.poly.begin();
    int resultSize = 0;

    if (temp.poly.size() < 2) {
        if (i->pow >= j->pow) {
            Result.insert(i->coef / j->coef, i->pow - j->pow);
            temp = temp - Result * P;
        }
        else {
            Result.insert(0, 0);
        }

    }   

    else {
        while (true) {
            if (i->pow >= j->pow) {    
                Result.insert(i->coef / j->coef, i->pow - j->pow);
                if (Result.poly.size() < 2)
                    temp2 = Result;
                else {
                    temp2 = Result;
                    resultSize = Result.poly.size();
                    for (int k = 1 ; k != resultSize; k++) 
                         temp2.poly.pop_front();
                }
                temp = temp - temp2 * P;             
            }
            else
                break;
        }
    }

    return Result;
}

};

前三个程序工作正常,但除法并不像看起来那样,程序处于无限循环中。

最终更新在听了戴夫之后,我终于重载了/和&返回商数和剩余部分,所以非常感谢大家的帮助,尤其是戴夫对你的好主意!

如果有人想让我发布这2位超载的接线员,请在我的帖子上发表评论(也许可以投票给每一个相关的人)。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-03-12 15:40:07

如果赋值可以改变poly,那么在第一次分配给*this之后,i是无效的;可以说,您幸运地获得了无限循环,而不是数据损坏。

我不知道你的算法是怎么工作的。

此外,operator / ()并不期望修改*this。它应该返回一个答案,而不是修改其中的任何一个论点。

票数 2
EN

Stack Overflow用户

发布于 2010-03-12 15:13:30

在除法过程中你永远不会改变I或J。while循环永远不会停止。

票数 4
EN

Stack Overflow用户

发布于 2010-03-12 15:13:38

在哪里递增迭代器?如果i和j没有改变,"while (i->pow >= j->pow)“每次都会返回相同的值,从而导致无限循环。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2433529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档