多项式除法运算符

时间:2010-03-12 14:56:17

标签: c++ operators operator-overloading division polynomial-math

确定。这是我成功编码的操作到目前为止,谢谢你的帮助:

Adittion:

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

减法:

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

乘:

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

分部(已编辑):

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

};

前三个正常工作,但除非看起来程序处于无限循环中。

最终更新   听了戴夫之后,我终于通过重载/和&amp;返回商和余数,所以非常感谢大家的帮助,特别是你戴上了你的好主意!

P.S。如果有人想让我发布这两个超载的运营商,请通过评论我的帖子来询问它(也许可以为所有参与者投票)。

3 个答案:

答案 0 :(得分:4)

你在分裂期间永远不会改变i或j。 while循环永远不会停止。

答案 1 :(得分:3)

你在哪里增加你的迭代器?如果i和j没有改变,“while(i-&gt; pow&gt; = j-&gt; pow)”每次都会返回相同的值,导致你的无限循环。

答案 2 :(得分:2)

如果分配可以更改poly,则i在第一次分配到*this后无效;可以说,你很幸运能够摆脱无限循环,而不是数据损坏。

我不遵循你的算法应该如何工作。

此外,operator / ()修改*this不是预期的行为。它应该返回一个答案而不是修改它的任何一个参数。