C++ 使用运算符重载的多项式运算
我试图使用运算符重载来定义多项式类的基本运算(+、-、*、/),但是当我运行程序时,它崩溃了,我的计算机冻结了 更新4 嗯。我成功地进行了三次行动,只剩下一次行动了 以下是我得到的:C++ 使用运算符重载的多项式运算,c++,operations,polynomial-math,C++,Operations,Polynomial Math,我试图使用运算符重载来定义多项式类的基本运算(+、-、*、/),但是当我运行程序时,它崩溃了,我的计算机冻结了 更新4 嗯。我成功地进行了三次行动,只剩下一次行动了 以下是我得到的: polinom operator*(const polinom& P) const { polinom Result; constIter i, j, lastItem = Result.poly.end(); Iter it1, it2, first, last; int
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;
}
您的代码还有其他设计和正确性问题,但我认为崩溃发生在这一行
if (i->pow > P.i->pow)
当i==poly.end()&&p.i!=P.End()或i!=poly.end()和&P.i==P.end()。当i指向最后一个元素之后时取消引用i将崩溃
while (i != poly.end() || P.i != P.End())
我认为您需要&&there,否则循环只有在I和p.I同时到达各自的端点时才会终止
带有否定的逻辑是很棘手的。也许更简单的说法是:
while (!(i == poly.end() || j == P.End())) //while neither iterator has reached end
根据布尔运算,其与:
while (!(i == poly.end()) && !(j == P.End()))
while (i != poly.end() && j != P.End())
如果两个迭代器相等(无限循环导致无限多个内存分配?),您似乎也不会增加迭代器
样式问题:最好使用迭代器作为局部变量。如果变量在方法中开始使用之前就应该被“初始化”,那么不要让它们成为类成员,并且在方法完成后它们就变得无用了 另外,如果成员函数不修改当前对象(
运算符+
不应修改),则更喜欢通过常量引用传递参数,并标记成员函数const:
(这将揭示生成本地使用的迭代器成员的问题-您将修改实例!)至于如何正确地将多项式相加,我建议使用以下简单逻辑:
polinom operator+(const polinom& P) const //fixed prototype re. const-correctness
{
polinom Result;
std::list<term>::const_iterator //fixed iterator type
i = poly.begin(), j = P.poly.begin();
while (i != poly.end() && j != P.poly.end()) {
//logic while both iterators are valid
}
//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;
}
+1.风格问题。更好的风格会带来更好的编程:)所以我应该去掉SetIterBegin()?是的,这也是不必要的。另外,如果将条件更改为&&则崩溃可能会消失,但具有更多项的多项式不会在结果中包含所有项。在主循环之后,您需要检查两个迭代器,并查看是否还有剩余的项可以放入结果中。@马克:崩溃是因为在两个迭代器相等时没有放入i++而发生的,但您是对的,我认为在这种情况下| |更好@UncleBens:但是如果我通过const Polinom,我就不能增加P迭代器,为什么我要通过&Polinom?编辑之后,这就变成了一个完全不同的问题。我不太明白,既然你从不让迭代器等于结束符,循环怎么会终止。我明白它为什么会暂停,因为它会插入3x^2,直到它最终耗尽内存并使我的计算机崩溃。现在看看如何修复它。你的“last”函数不会返回任何东西。那可能会引起问题。是的,你说得对,马克,谢谢!使用您刚刚发布的内容,任何输入都会暂停,而且如果我这样插入它们,它不会从最高级别到最低级别排序。这个空循环是由您填写的:)(基本上是您已经拥有的代码,没有试图使其完成全部工作的混淆)。但据我所知,您的代码不会添加相同幂的项并将它们按顺序排列—它只是插入所有项,对吗?不,第一个循环同时处理两个范围,以确保保留顺序。更简单的是,它可以安全地假设两个迭代器都是有效的。处理完该部分后,列表(或无)的最后一部分将未处理。然后,您可以将该部分附加到结果中。它将被订购,因为剩下的部分应该在公共部分之后。这是一个基本的合并算法。我想我现在明白了,我会测试它。顺便说一句,再次感谢!
polinom operator+(const polinom& P) const //fixed prototype re. const-correctness
{
polinom Result;
std::list<term>::const_iterator //fixed iterator type
i = poly.begin(), j = P.poly.begin();
while (i != poly.end() && j != P.poly.end()) {
//logic while both iterators are valid
}
//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;
}
#include <iterator>
#include <algorithm>
//...
//handle remaining items in either list (if any)
std::copy(i, poly.end(), std::back_inserter(Result.poly));
std::copy(j, P.poly.end(), std::back_inserter(Result.poly));
Result.poly.insert(Result.poly.end(), i, poly.end());
Result.poly.insert(Result.poly.end(), j, P.poly.end());