Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, now that Vlad has decided how he's <em>going</em> to do it, here's how I'd do it:</p> <pre><code>#ifndef _polinom_h #define _polinom_h #include &lt;iostream&gt; #include &lt;list&gt; #include &lt;cstdlib&gt; #include &lt;cmath&gt; #include "infix_iterator.h" using namespace std; char var; class polinom { class term { double coef; int power; ostream &amp;write(ostream &amp;os) const { // At least to me, the logic is easier to follow if we // handle one piece at a time. // It may be longer, but I think it's easier to understand. // First, if the coefficient is negative, subtract the term instead of adding it. if (coef &lt; 0) // backspace over the "+ " and print '- ' in its place. os &lt;&lt; "\b\b- "; // Then print the absolute value of the coefficient (if needed). if (fabs(coef) != 1) os &lt;&lt; fabs(coef); // Then print the var (if needed) if (power != 0) os &lt;&lt; var; // then print the power (if needed) if (abs(power) &gt; 1) os &lt;&lt; "^" &lt;&lt; power; // And we're done. return os; } // support inserting a term into a stream. friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, term const &amp;t) { return t.write(os); } public: term(double c=0.0, int p=0) : coef(c), power(p) {} bool read(std::ostream &amp;os, std::istream &amp;is, int num) { // This is only slightly modified from the originally posted question os &lt;&lt; "\nTerm " &lt;&lt; num &lt;&lt; ": coef = "; is &gt;&gt; coef; if (coef == 0.0) return false; if (coef != 0.0) { os &lt;&lt; " grade = "; is &gt;&gt; power; } return true; } bool operator&lt;(term const &amp;other) const { // order by descending powers. return other.power &lt; power; } }; list&lt;term&gt; poly; public: void read(int id) { term t; int nr_term = 1; std::cout &lt;&lt; "P: " &lt;&lt; id; // Read and save individual terms: while (t.read(std::cout, std::cin, nr_term++)) poly.push_back(t); } void write(char var) { // sort the polynomial so the highest powers come first. poly.sort(); // save the variable name for later use. ::var = var; // Print out all the terms: std::copy(poly.begin(), poly.end(), infix_ostream_iterator&lt;term&gt;(std::cout, " + ")); } }; #endif </code></pre> <p>Using this is pretty trivial:</p> <pre><code>#include "polynom.h" int main() { polinom p; p.read(1); p.write('x'); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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