Note that there are some explanatory texts on larger screens.

plurals
  1. POBasic polynomial reading using linked lists
    text
    copied!<p>Ok, after failing to read a polynomial, I'm trying first a basic approach to this.</p> <p>So i have class polinom with function read and print:</p> <pre><code>#ifndef _polinom_h #define _polinom_h #include &lt;iostream&gt; #include &lt;list&gt; #include &lt;cstdlib&gt; #include &lt;conio.h&gt; using namespace std; class polinom { class term { public: double coef; int pow; term(){ coef = 0; pow = 0; } }; list&lt;term&gt; poly; list&lt;term&gt;::iterator i; public: void read(int id) { term t; double coef = 1; int pow = 0; int nr_term = 1; cout &lt;&lt; "P" &lt;&lt; id &lt;&lt; ":\n"; while (coef != 0) { cout &lt;&lt; "Term" &lt;&lt; nr_term &lt;&lt; ": "; cout &lt;&lt; "coef = "; cin &gt;&gt; coef; if (coef == 0) break; cout &lt;&lt; " grade = "; cin &gt;&gt; pow; t.coef = coef; t.pow = pow; if (t.coef != 0) poly.push_back(t); nr_term++; } } void print(char var) { for (i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them if (poly.size() &lt; 2) { if (i-&gt;pow == 0) //if the last term's power is 0 we print only it's coefficient cout &lt;&lt; i-&gt;coef; else if (i-&gt;pow == 1) { if (i-&gt;coef == 1) cout &lt;&lt; var; else if (i-&gt;coef == -1) cout &lt;&lt; "-" &lt;&lt; var; else cout &lt;&lt; i-&gt;coef &lt;&lt; var; } else cout &lt;&lt; i-&gt;coef &lt;&lt; var &lt;&lt; "^" &lt;&lt; i-&gt;pow; //otherwise we print both } else { if (i == poly.end()) { // if we reached the last term if (i-&gt;pow == 0) //if the last term's power is 0 we print only it's coefficient cout &lt;&lt; i-&gt;coef; else if (i-&gt;pow == 1) cout &lt;&lt; i-&gt;coef &lt;&lt; var; else cout &lt;&lt; i-&gt;coef &lt;&lt; var &lt;&lt; "^" &lt;&lt; i-&gt;pow; //otherwise we print both } else { if (i-&gt;coef &gt; 0) { if (i-&gt;pow == 1)//if the coef value is positive cout &lt;&lt; i-&gt;coef &lt;&lt; var &lt;&lt; " + "; //we also add the '+' sign else cout &lt;&lt; cout &lt;&lt; i-&gt;coef &lt;&lt; var &lt;&lt; "^" &lt;&lt; i-&gt;pow &lt;&lt; " + "; } else { if (i-&gt;pow == 1)//if the coef value is positive cout &lt;&lt; i-&gt;coef &lt;&lt; var &lt;&lt; " + "; //we also add the '+' sign else cout &lt;&lt; cout &lt;&lt; i-&gt;coef &lt;&lt; var &lt;&lt; "^" &lt;&lt; i-&gt;pow &lt;&lt; " + "; } } } } } }; #endif </code></pre> <p>Well, it works when reading only one term but when reading more the printed coefficients are some random values and also after the last term it print '+' or '-' when it shouldn't.</p> <p>So any idea what's wrong?</p> <p>Thanks!</p> <p><strong>FINAL UPDATE</strong></p> <p>Ok, i made it work perfectly by modifying Bill's code so thanks a lot Bill and everyone else who commented or answered!</p> <p>Here's the final print function:</p> <pre><code> void print(char var) { list&lt;term&gt;::iterator endCheckIter; for (i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them endCheckIter = i; ++endCheckIter; if (i-&gt;pow == 0) cout &lt;&lt; i-&gt;coef; else if (i-&gt;pow == 1) cout &lt;&lt; i-&gt;coef &lt;&lt; var; else cout &lt;&lt; i-&gt;coef &lt;&lt; var &lt;&lt; "^" &lt;&lt; i-&gt;pow; if (endCheckIter != poly.end()) { if (endCheckIter-&gt;coef &gt; 0) cout &lt;&lt; " + "; else { cout &lt;&lt; " - "; endCheckIter-&gt;coef *= -1; } } } </code></pre> <p>} </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