Polynomial operations using operator overloading
I'm trying to use operator overloading to define the basic operations (+,-,*,/) for my polynomial class but when i run the program it crashes and my computer frozes.
Update4
Ok. i successfully made three of the operations, the only one left is division.
Here's what I got:
polinom operator*(const polinom& P) const
{
polinom Result;
constIter i, j, lastItem = Result.poly.end();
Iter it1, it开发者_如何学JAVA2, 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;
}
while (i != poly.end() || P.i != P.End())
I think you'll need && there, otherwise the loop terminates only if i and p.i reach their respective end at the same time.
Logic with negations is tricky. Probably simpler to think of this as:
while (!(i == poly.end() || j == P.End())) //while neither iterator has reached end
which according to boolean arithmetic is the same as:
while (!(i == poly.end()) && !(j == P.End()))
while (i != poly.end() && j != P.End())
You also don't seem to be incrementing the iterators if both are equal (infinite loop leading to infinitely many memory allocations?).
Style issues: you are better off using iterators as local variables. Don't make variables class members if they are supposed to be "initialized" before you start using them in a method, and they become useless after the method completes.
Also prefer passing arguments by const reference, and mark member functions const if they don't modify the current object (operator+
shouldn't):
polinom operator+(const polinom& P) const;
(which would reveal the problem making locally used iterators members - you would be modifying the instances!)
There are other design and correctness issues with your code, but I think the crash occurs in this line
if (i->pow > P.i->pow)
when i == poly.end() && P.i != P.End() or i != poly.end() && P.i == P.End(). Dereferencing i when it points after the last element will crash.
As to getting the function correctly adding up polynomials, I'd recommend this simple logic:
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;
}
The last part can probably also be left to standard library functions
#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));
... but would probably be simpler using list.insert:
Result.poly.insert(Result.poly.end(), i, poly.end());
Result.poly.insert(Result.poly.end(), j, P.poly.end());
精彩评论