// O(n^2) factoring void Polynomial::factor() { /* PRE-CONDITIONS: The polynomial is of the correct format. POST-CONDITIONS: The polynomial will combine all like terms in preparation for either output or addition. */ Polynomial newPoly; Term currentTerm; int newCoefficient; // Loop while there is a term left to check while (termList.size() > 0) { bool foundLikeTerm = false; // If there are no terms in the new polynomial add the first term if (newPoly.getNumTerms() == 0) { // If the exponent is anything but 0, add the term as is. if (termList.front().getExponent() != 0) { newPoly.addTermToList(termList.front()); termList.pop_front(); } else{ // If the exponent is zero, drop the variable and add the coefficient newPoly.addTermToList(Term(termList.front().getCoefficient())); termList.pop_front(); } } else { // Set new current term to be checked against the new polynomial currentTerm = termList.front(); // If the exponent of the term is zero, drop the variable if (currentTerm.getExponent() == 0) currentTerm = Term(termList.front().getCoefficient()); // Iterate through the new polynomial to check for matches of exponents // If the exponents match add the terms together, else add to the end of the new poly for (list<Term>::iterator iter = newPoly.termList.begin(); iter != newPoly.termList.end();) { // If the exponents match or they are both numbers with no variables, add them together if ((iter->getExponent() == currentTerm.getExponent()) || (iter->getHasVariable() == false && currentTerm.getHasVariable() == false)) { newCoefficient = iter->getCoefficient() + currentTerm.getCoefficient(); iter->setCoefficient(newCoefficient); foundLikeTerm = true; break; } else{ // Advance iterator if no matches are found on the current term iter++; } } // If it gets through the new poly without a match, add it to the new poly if(foundLikeTerm == false) newPoly.addTermToList(currentTerm); termList.pop_front(); } } // Clear everything from old polynomial, update with the new polynomial termList = newPoly.termList; }
// O(n^2 + m^2) from factor // O(n log n) sort // O(n * m) adding Polynomial Polynomial::operator +(Polynomial& rhs) { /* PRE-CONDITIONS: The two polynomials have been entered in the correct format. Factoring called in-function. POST-CONDITIONS: The polynomial will combine all like terms from both of the polynomials and return the result as a new polynomial. */ Polynomial newPoly; bool foundLikeTerm; int newCoefficient; // factor polynomials before adding them together factor(); rhs.factor(); // begin comparing terms from each Polynomial and combining them for (list<Term>::iterator iter = termList.begin(); iter != termList.end(); iter++) { foundLikeTerm = false; // compare each term from poly1 with every term from poly2 for (list<Term>::iterator iter2 = rhs.termList.begin(); iter2 != rhs.termList.end();) { if (iter->getHasVariable() == true && iter->getExponent() == iter2->getExponent()) { // If there is a like term, add the coefficients together and add to new poly list foundLikeTerm = true; newCoefficient = iter->getCoefficient() + iter2->getCoefficient(); // Advance iterator and delete current term iter2 = rhs.termList.erase(iter2); if (newCoefficient != 0) // Add combined term to new polynomial newPoly.addTermToList(Term(newCoefficient, iter->getExponent())); } else if (iter->getHasVariable() == false && iter2->getHasVariable() == false) { foundLikeTerm = true; newCoefficient = iter->getCoefficient() + iter2->getCoefficient(); // Advance iterator and delete current term iter2 = rhs.termList.erase(iter2); // Add combined term to new polynomial if it is anything but zero. if (newCoefficient != 0) newPoly.addTermToList(Term(newCoefficient)); } else{ iter2++; } } // If there are no terms to be combined with, add term as it is. // drop stand alone zeros from the polynomial if (foundLikeTerm == false && iter->getCoefficient() != 0) newPoly.addTermToList(*iter); } // add remaining terms from poly2 that had no matching terms in poly1 for (list<Term>::iterator iter2 = rhs.termList.begin(); iter2 != rhs.termList.end(); iter2++) newPoly.addTermToList(*iter2); // Sort (ascending order) and reverse to get it descending newPoly.termList.sort(greater<Term>()); return newPoly; }