Pregunta

Ok. Aquí está el código de operaciones i con éxito hasta el momento gracias a su ayuda de:

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

La resta:

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

Multiplicación:

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

División ( 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;
}

};

Los tres primeros están trabajando correctamente, pero la división no lo hace, ya que parece que el programa se encuentra en un bucle infinito.

Actualización Final   Después de escuchar a Dave, finalmente lo hizo por la sobrecarga de ambos / y y para devolver el cociente y el resto por lo que muchas gracias a todos por su ayuda y sobre todo que Dave por su gran idea!

P.S. Si alguien quiere que me cree estos 2 operador sobrecargado por favor pregunta comentando en mi post (y tal vez dar un voto para todos los involucrados).

¿Fue útil?

Solución

Si la asignación puede cambiar poly, entonces i no es válido después de la primera asignación a *this; podría decirse que tiene suerte de salir con un bucle infinito, en lugar de la corrupción de datos.

No sigo cómo se supone que el algoritmo para trabajar.

comportamiento Además, no se espera para operator / () a modificar *this. Se debe devolver una respuesta y no modificar ninguno de sus argumentos.

Otros consejos

Nunca cambia ioj durante la división. El bucle mientras que nunca se detendrá.

¿Dónde está incrementando sus iteradores? Si i y j no cambian "mientras que (i-> pow> = j-> pow)" devolverá el mismo valor cada vez, la causa de su bucle infinito.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top