Ok. here's the operations i successfully code so far thank's to your help:
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;
}
Subtraction:
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;
}
Multiplication:
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):
polinom operator/(const polinom& P)
{
polinom Result, temp;
Iter i = poly.begin();
constIter j = P.poly.begin();
if (poly.size() < 2) {
if (i->pow >= j->pow) {
Result.insert(i->coef, i->pow - j->pow);
*this = *this - Result;
}
}
else {
while (true) {
if (i->pow >= j->pow) {
Result.insert(i->coef, i->pow - j->pow);
temp = Result * P;
*this = *this - temp;
}
else
break;
}
}
return Result;
}
The first three are working correctly but division doesn't as it seems the program is in a infinite loop.
Update
Because no one seems to understand how i thought the algorithm, i'll explain:
If the dividend contains only one term, we simply insert the quotient in Result, then we multiply it with the divisor ans subtract it from the first polynomial which stores the remainder. If the polynomial we do this until the second polynomial( P in this case) becomes bigger. I think this algorithm is called long division, isn't it?
So based on these, can anyone help me with overloading the / operator correctly for my class?
Thanks!