Note that there are some explanatory texts on larger screens.

plurals
  1. POoverloading stream operator >> for polynomials
    primarykey
    data
    text
    <p>how could I overload input stream operator for this code:</p> <pre><code>struct Node { int degree; int coeff; Node* link; }; Node* cons (int c,int d,Node* p); Node* doCancelation(int d,Node* p); class polynomial { private: Node* poly; static const int CHAR='X'; char character; public: polynomial(); polynomial(int c,int d); //void printPoly()const; void insert (int c,int d); int degree() const; int coeff(int d) const; void setPrintVariable(char x); // changes the variable used when printing the polynomial char getPrintVariable() const; // returns the variable used when printing the polynomial friend std::ostream&amp; operator &lt;&lt; (std::ostream &amp;output, const polynomial &amp;a); friend std::istream&amp; operator &gt;&gt; (std::istream &amp;input, polynomial&amp;a); }; polynomial::polynomial() { poly= new Node; poly=NULL; } polynomial::polynomial(int c,int d) { character = CHAR; poly= new Node; poly-&gt;coeff=c; poly-&gt;degree=d; poly-&gt;link=NULL; } void polynomial::setPrintVariable(char x) { character=x; } char polynomial::getPrintVariable() const { return character; } void polynomial::insert (int c,int d) { if(poly==NULL) { poly=cons(c,d,poly); return; } if(d&lt;poly-&gt;degree) { poly=cons(c,d,poly); return; } if(d==poly-&gt;degree) { if(c==-(poly-&gt;coeff)) poly=doCancelation(d,poly); else poly-&gt;coeff += c; return; } Node* q=poly; Node* r=q-&gt;link; while(r!=NULL &amp;&amp; d&gt;=r-&gt;degree) { if(d==r-&gt;degree) { if(c==-(r-&gt;coeff)) poly=doCancelation(d,poly); else r-&gt;coeff += c; return; } q=r; r=r-&gt;link; } q-&gt;link=cons(c,d,r); } Node* doCancelation(int d,Node* p) { if(p==NULL)return p; if(p-&gt;degree==d) { Node* q=p-&gt;link; delete p; return q; } else { p-&gt;link = doCancelation (d,p-&gt;link); return p; } } std::ostream&amp; operator &lt;&lt; (std::ostream &amp;output, const polynomial &amp;a) { Node* q=a.poly; if(a.poly==NULL) output&lt;&lt;"( )"; else while(q != NULL) { output&lt;&lt;std::showpos&lt;&lt;q-&gt;coeff&lt;&lt;std::noshowpos&lt;&lt;'x'&lt;&lt;"^"&lt;&lt;q-&gt;degree&lt;&lt;" "; q=q-&gt;link; } return output; } const polynomial operator +(const polynomial &amp;a,const polynomial &amp;b ) { } int polynomial::degree() const { Node* q=poly; if(poly==NULL) return 0; while(q-&gt;link !=NULL) q=q-&gt;link; return q-&gt;degree; } int polynomial::coeff(int d) const { Node* q=poly; if(poly==NULL) return 0; while(q !=NULL &amp;&amp; d &lt;= q-&gt;degree) { if(d==q-&gt;degree) return q-&gt;coeff; q=q-&gt;link; } return 0; } Node* cons (int c,int d,Node* p) { Node* q= new Node; q-&gt;coeff=c; q-&gt;degree=d; q-&gt;link=p; return q; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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