c++ - 多项式除法重载运算符

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

好的。这是我到目前为止成功编码的操作,感谢您的帮助:

添加:

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

};

前三个工作正常,但除法没有,因为程序似乎处于无限循环中。

最终更新 听了 Dave 的意见后,我最终通过重载/和 & 来返回商和余数,所以非常感谢大家的帮助,尤其是 Dave 的好主意!

附言如果有人想让我发布这 2 个重载的运算符,请通过对我的帖子发表评论来询问(并可能为所有相关人员投票)。

最佳答案

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

关于c++ - 多项式除法重载运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2433529/

相关文章:

c++ - 在 C++ 中使用反斜杠定义函数

c++ - 删除时没有 free() 的特殊模式? C++

python - 使用 ">>"和 "&"运算符的变量赋值

c++ - 在 C++ 中堆重载和读取文件的二维矩阵

c++ - Student&a, Student&a; 之间的区别

c++ - 需要有关 getline 函数错误 : no matching function for call to 'getline(std::istream&, char&)' | 的编译错误的帮助

operators - 我可以在 Tcl 中创建运算符吗?

python - ,= 运算符

c++ - std::enable_if_t 将字符串与非字符串函数参数分开

c++ - 你能把 operator+ 换边吗