views:

285

answers:

3

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

};

The first three are working correctly but division doesn't as it seems the program is in a infinite loop.

Final Update After listening to Dave, I finally made it by overloading both / and & to return the quotient and the remainder so thanks a lot everyone for your help and especially you Dave for your great idea!

P.S. If anyone wants for me to post these 2 overloaded operator please ask it by commenting on my post (and maybe give a vote up for everyone involved).

+4  A: 

You never change i or j during division. The while loop will never halt.

Turtle
+3  A: 

Where are you incrementing your iterators? If i and j are not changing, "while (i->pow >= j->pow)" will return the same value every time, causing your infinite loop.

RickNotFred
@RickNotFredi thougt it wouldn't because *this is getting smaller every time until it becomes smaller than the divisor has a bigger degree than the dividend. What should I do
Vlad
Why would changing this change i and j?
Turtle
+1  A: 

If assignment can change poly, then i is not valid after the first assignment to *this; arguably you're lucky to get away with an infinite loop, instead of data corruption.

I don't follow how your algorithm is supposed to work.

Further, it is not expected behaviour for operator / () to modify *this. It should be returning an answer and not modifying either of its arguments.

Dave Hinton
well i wanted to store the remainder in *this (the initial dividend).What's wrong with that?
Vlad
Do you know a better way? If you do please show us. ;)
Vlad
Consider `a = b / c;` If these variables are built-in types, then `b` and `c` will be unaltered, but if they are of your `polynom` type, `b` will be changed, and this will violate the expectations of every programmer that uses it. If you want to return quotient and remainder from a function, it's probably best to use output reference parameters. Keep `operator / ()` for returning only the quotient and `operator % ()` for returning only the remainder.
Dave Hinton