Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>#ifndef POLYNOMIAL_H #define POLYNOMIAL_H #include &lt;iostream&gt; using namespace std; #define MAX 10 class Polynomial { friend ostream &amp;operator&lt;&lt; (ostream &amp;, const Polynomial &amp;); public : Polynomial (); void enterTerms(); Polynomial operator +(const Polynomial &amp; ); private : int terms[MAX][2]; //either static size(MAX rows) or use "new" for dynamic allocation int n; //number of terms }; #endif #include "polynomial.h" ostream &amp;operator&lt;&lt; (ostream &amp; out, const Polynomial &amp; p){ for ( int i = 0 ; i &lt; p.n ; i++ ){ if ( i == p.n - 1 )//last term does not have + appended out &lt;&lt; p.terms[i][0] &lt;&lt;"x^"&lt;&lt;p.terms[i][1]&lt;&lt;endl; else out &lt;&lt; p.terms[i][0]&lt;&lt;"x^"&lt;&lt;p.terms[i][1]&lt;&lt;" + "; } return out; } Polynomial :: Polynomial(){ for ( int i = 0; i &lt; MAX; i++ ){ terms[i][0] = 0; terms[i][1] = 0; } } void Polynomial :: enterTerms(){//enterTerms() not in constructor so that no prompt for entering //terms while doing + - etc., they also produce Poylnomial instance (i.e. invoke constructor) int num; cout&lt;&lt;"enter number of terms in polynomial\n"; cin &gt;&gt; num; n = num &gt;= 0 ? num : 1; cout &lt;&lt; "enter coefficient followed by exponent for each term in polynomial\n"; for ( int i = 0; i &lt; n ; i++) cin &gt;&gt; terms[i][0] &gt;&gt; terms[i][1] ; } Polynomial Polynomial :: operator + ( const Polynomial &amp; p ){ Polynomial temp, sum; temp.n = n + p.n; int common = 0; // first write sum as concatenation of p1 and p2 for ( int i = 0 ; i &lt; n ; i++ ){ temp.terms[i][0] = terms[i][0]; temp.terms[i][1] = terms[i][1]; } //notice j and k for traversing second half of sum, and whole p2 resp for ( int j = n, k = 0; j &lt; n + p.n, k &lt; p.n ; j++, k++ ){ temp.terms[j][0] = p.terms[k][0]; temp.terms[j][1] = p.terms[k][1]; } for ( int l = 0; l &lt; temp.n - 1 ; l++ ){ // 0 to 1 less than length for ( int m = l + 1 ; m &lt; temp.n ; m++ ){ // 1 more than l to length,so that compared pairs are non redundant if( temp.terms[l][1] == temp.terms[m][1] ){ common++; //common terms reduce no. of terms in sum (see sum.n decl) temp.terms[l][0] += temp.terms[m][0]; //coefficients added if exponents same temp.terms[m][0] = 0; } } } sum.n = temp.n - common; //if you place it above, common taken as 0 and sum.n is same as temp.n (logical error) //just to debug, print temporary array cout &lt;&lt; endl &lt;&lt; temp; for ( int q = 0, r = 0; q &lt; temp.n; q++ ){ if ( temp.terms[q][0] == 0 ) continue; else{ sum.terms[r][0] = temp.terms[q][0]; sum.terms[r][1] = temp.terms[q][1]; r++; } } cout &lt;&lt; endl &lt;&lt; sum; return sum; } int main(){ Polynomial p1 , p2; p1.enterTerms(); p2.enterTerms(); p1 + p2; system("PAUSE"); return EXIT_SUCCESS; } </code></pre> <p><img src="https://i.stack.imgur.com/J50SD.jpg" alt="op"></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