Note that there are some explanatory texts on larger screens.

plurals
  1. POPolynomial division overloading operator
    text
    copied!<p>Ok. here's the operations i successfully code so far thank's to your help:</p> <p>Adittion:</p> <pre><code>polinom operator+(const polinom&amp; P) const { polinom Result; constIter i = poly.begin(), j = P.poly.begin(); while (i != poly.end() &amp;&amp; j != P.poly.end()) { //logic while both iterators are valid if (i-&gt;pow &gt; j-&gt;pow) { //if the current term's degree of the first polynomial is bigger Result.insert(i-&gt;coef, i-&gt;pow); i++; } else if (j-&gt;pow &gt; i-&gt;pow) { // if the other polynomial's term degree is bigger Result.insert(j-&gt;coef, j-&gt;pow); j++; } else { // if both are equal Result.insert(i-&gt;coef + j-&gt;coef, i-&gt;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-&gt;coef, i-&gt;pow); ++i; } while (j != P.poly.end()) { Result.insert(j-&gt;coef, j-&gt;pow); ++j; } return Result; } </code></pre> <p>Subtraction:</p> <pre><code>polinom operator-(const polinom&amp; P) const //fixed prototype re. const-correctness { polinom Result; constIter i = poly.begin(), j = P.poly.begin(); while (i != poly.end() &amp;&amp; j != P.poly.end()) { //logic while both iterators are valid if (i-&gt;pow &gt; j-&gt;pow) { //if the current term's degree of the first polynomial is bigger Result.insert(-(i-&gt;coef), i-&gt;pow); i++; } else if (j-&gt;pow &gt; i-&gt;pow) { // if the other polynomial's term degree is bigger Result.insert(-(j-&gt;coef), j-&gt;pow); j++; } else { // if both are equal Result.insert(i-&gt;coef - j-&gt;coef, i-&gt;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-&gt;coef, i-&gt;pow); ++i; } while (j != P.poly.end()) { Result.insert(j-&gt;coef, j-&gt;pow); ++j; } return Result; } </code></pre> <p>Multiplication:</p> <pre><code>polinom operator*(const polinom&amp; 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-&gt;coef * j-&gt;coef, i-&gt;pow + j-&gt;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-&gt;pow == it1-&gt;pow) { it1-&gt;coef += it2-&gt;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; } </code></pre> <p>Division(<strong>Edited</strong>): </p> <pre><code>polinom operator/(const polinom&amp; 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() &lt; 2) { if (i-&gt;pow &gt;= j-&gt;pow) { Result.insert(i-&gt;coef / j-&gt;coef, i-&gt;pow - j-&gt;pow); temp = temp - Result * P; } else { Result.insert(0, 0); } } else { while (true) { if (i-&gt;pow &gt;= j-&gt;pow) { Result.insert(i-&gt;coef / j-&gt;coef, i-&gt;pow - j-&gt;pow); if (Result.poly.size() &lt; 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; } </code></pre> <p>};</p> <p>The first three are working correctly but division doesn't as it seems the program is in a infinite loop. </p> <p><strong>Final Update</strong> After listening to Dave, I finally made it by overloading both / and &amp; to return the quotient and the remainder so thanks a lot everyone for your help and especially you Dave for your great idea!</p> <p>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).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload