Note that there are some explanatory texts on larger screens.

plurals
  1. POPolynomial Calculator
    text
    copied!<p>I'm doing a polynomial calculator and i'll need some help as i'll progress with the code.</p> <p>For now I made only the polinom class which i represented it as a linked list with terms and some functions(only read and print the polynomial functions for now).</p> <p>Here's the main program which for now only read a polynomial and prints it:</p> <pre><code>#include "polinom.h" int main() { polinom P1; bool varStatus = false; char var = '\0', readStatus = '\0'; cout &lt;&lt; "P1 = "; P1.read(readStatus, var, varStatus); // i don't need readStatus yet as i haven't implemented the reset and quit functions cout &lt;&lt; "\n\nP = "; P1.print(var); getch(); return 0; } </code></pre> <p>And the header file polinom.h:</p> <pre><code>#ifndef _polinom_h #define _polinom_h #include &lt;iostream&gt; #include &lt;list&gt; #include &lt;cstdlib&gt; #include &lt;cctype&gt; #include &lt;cstdio&gt; #include &lt;conio.h&gt; using namespace std; class polinom { class term { public: int coef; int pow; term() { coef = 1; pow = 0; } }; list&lt;term&gt; poly; list&lt;term&gt;::iterator i; public: bool printable(char c) { return ( ((int(c) &gt; 42 &amp;&amp; int(c) &lt; 123) || isspace(c)) &amp;&amp; int(c) != 44 &amp;&amp; int(c) != 46 &amp;&amp; int(c) != 47 &amp;&amp; int(c) != 58 &amp;&amp; int(c) != 59 &amp;&amp; int(c) != 60 &amp;&amp; int(c) != 61 &amp;&amp; int(c) != 62 &amp;&amp; int(c) != 63 &amp;&amp; int(c) != 64 &amp;&amp; int(c) != 65 &amp;&amp; int(c) != 91 &amp;&amp; int(c) != 92 &amp;&amp; int(c) != 93 &amp;&amp; int(c) != 95 &amp;&amp; int(c) != 96 ); } void read(char &amp;readStatus, char &amp;var, bool &amp;varStatus) { term t; // term variable to push it into the list of terms char c, lc, sign; // c = current char, lc = lastchar and sign the '+' or '-' sign before a coefficient int coef, pow; //variables to pass the coef and power to term t bool coefRead = false, powRead = false; //reading status of coef and power while (c != '\r') { //we read characters until carriage return c = getch(); // get the new imputed char if (tolower(c) == 'r' || tolower(c) == 'q') { //if the user inputed r or q we reset the input or quit the program readStatus = c; //pass current char value to readStatus so the program will know what to do next return; //aborting the reading process } else { if (printable(c)) cout &lt;&lt; c; //print on screen only the correct characters if (!coefRead &amp;&amp; !powRead) //we set term coef to the inputed value { if (isdigit(c)) { if (isdigit(lc)) coef = coef * 10 + int(c); //if the last char was also a digit we multiply the last value of coef by 10 and add current char else { if (sign == '-') coef = -(int(c));//if the current coef has '-' before we set coef to it's negative value else coef = int(c); //this means a new term's coef is read } if (!isdigit(c) &amp;&amp; isdigit(lc)) coefRead = true; //if the last char was a digit and we reached the var name we stop reading the coefficient } else if (coefRead &amp;&amp; !powRead) //after coefficient is read we get the term's varname and power { if (isdigit(c)) { // just like in the case with coefficient we read the power until the current char is not a digit if (isdigit(lc)) pow = pow * 10 + int(c); else pow = int(c); } else if (isalpha(c) &amp;&amp; isdigit(lc) &amp;&amp; !varStatus) { //if the last char was a digit and the current not we reached the var name var = c; //also even though the variable is inputed more than once we save it only once varStatus = true; //we mark the var name as read } else { if (isdigit(lc)) powRead = true; } } else { if (c == '+' || c == '-') { // if a sign was inputed it means a new term is coming and we push the current term to the list and reset t.coef = coef; // coefRead and powRead so we can read another term t.pow = pow; poly.push_back(t); sign = c; coefRead = false; powRead = false; } } lc = c; // we save the last character } } } 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 (i == poly.end() - 1) { // 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 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 the coef value is positive cout &lt;&lt; *(i-&gt;coef) &lt;&lt; var &lt;&lt; "^" &lt;&lt; *(i-&gt;pow) &lt;&lt; " + "; //we also add the '+' sign else cout &lt;&lt; *(i-&gt;coef) &lt;&lt; var &lt;&lt; "^" &lt;&lt; *(i-&gt;pow) &lt;&lt; " - "; // otherwise we add '-' sign } } } }; #endif </code></pre> <p><strong>EDIT</strong></p> <p>All compile errors fixed now thanks to JonH, but the read function is not working as the input characters aren't correctly inserted into the list. I know it may be trivial for you guys, but it would be great if you help me out.</p> <p>Thanks!</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