#include #include #include #include "Polynomial.h" std::string Polynomial::toString() const { std::string res = ""; for (int i = degree(); i >= 0; --i) { double c = (*this)[i]; if (fabs(c) <= POLYNOMIAL_EPS) continue; if (res.size() > 0) { // Sign of operation: either + or - if (c >= 0.) { res += " + "; } else { c = (-c); res += " - "; } } std::stringstream buffer; // buffer << std::fixed << std::setprecision(3); bool coeffPrinted = false; if (i == 0 || fabs(c - 1.) > POLYNOMIAL_EPS) { buffer << c; res += buffer.str(); coeffPrinted = true; } if (i > 0) { if (coeffPrinted) res += "*"; res += "x"; if (i > 1) { res += "^"; res += std::to_string(i); } } } return res; } std::istream& operator>>(std::istream& s, Polynomial& f) { int d = f.degree(); double c; for (int i = d; i >= 0; --i) { if (!(s >> c)) return s; f[i] = c; } f.trim(); return s; }