PAT 1009. Product of Polynomials (25)
1009. Product of Polynomials (25)
This time, you are supposed to find A*B where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < ... < N2 < N1 <=1000.
Output Specification:
For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.
Sample Input2 1 2.4 0 3.2 2 2 1.5 1 0.5Sample Output
3 3 3.6 2 6.0 1 1.6
#include <iostream> #include <vector> #include <iomanip> class polynomial { private: std::vector<double> coefficient; std::vector<int> radix; public: polynomial(){} polynomial(const polynomial &poly){ *this = poly; } ~polynomial(){} friend polynomial operator+(const polynomial &poly1, const polynomial &poly2); friend polynomial operator-(const polynomial &poly1, const polynomial &poly2); polynomial& operator+=(const polynomial &poly){ *this = *this + poly; return *this; } polynomial& operator-=(const polynomial &poly){ *this = *this - poly; return *this; } friend polynomial operator*(const polynomial &poly1, const polynomial &poly2); friend std::istream& operator>>(std::istream &is, polynomial &poly); friend std::ostream& operator<<(std::ostream &os, const polynomial &poly); }; polynomial operator+(const polynomial &poly1, const polynomial &poly2) { polynomial polysum; int i = 0, j = 0; while (i<poly1.radix.size() || j<poly2.radix.size()) { //only poly1 if (i<poly1.radix.size() && j == poly2.radix.size()) { polysum.radix.push_back(poly1.radix[i]); polysum.coefficient.push_back(poly1.coefficient[i]); i++; } //only poly2 else if (j<poly2.radix.size() && i == poly1.radix.size()) { polysum.radix.push_back(poly2.radix[j]); polysum.coefficient.push_back(poly2.coefficient[j]); j++; } //the radix of poly1 greater than poly2 else if (poly1.radix[i] > poly2.radix[j]) { polysum.radix.push_back(poly1.radix[i]); polysum.coefficient.push_back(poly1.coefficient[i]); i++; } //the radix of poly1 smaller than poly2 else if (poly1.radix[i] < poly2.radix[j]) { polysum.radix.push_back(poly2.radix[j]); polysum.coefficient.push_back(poly2.coefficient[j]); j++; } //the radix of poly1 equal to poly2 else { if (poly1.coefficient[i] + poly2.coefficient[j] != 0) { polysum.radix.push_back(poly1.radix[i]); polysum.coefficient.push_back(poly1.coefficient[i] + poly2.coefficient[j]); } i++; j++; } } return polysum; } polynomial operator-(const polynomial &poly1, const polynomial &poly2) { polynomial negativePoly; negativePoly = poly2; for (int i = 0; i < negativePoly.coefficient.size(); i++) negativePoly.coefficient[i] = -negativePoly.coefficient[i]; return poly1 + negativePoly; } polynomial operator*(const polynomial &poly1, const polynomial &poly2) { polynomial mul; int i = 0; while (i < poly2.coefficient.size()) { polynomial part = poly1; double coefficient = poly2.coefficient[i]; int radix = poly2.radix[i]; for (int j = 0; j < part.coefficient.size(); j++) { part.radix[j] += radix; part.coefficient[j] *= coefficient; } mul += part; i++; } return mul; } std::istream& operator>>(std::istream &is, polynomial &poly) { int k; is >> k; for (int i = 0; i<k; i++) { double tmpcoe; int tmprad; is >> tmprad >> tmpcoe; poly.radix.push_back(tmprad); poly.coefficient.push_back(tmpcoe); } return is; } std::ostream& operator<<(std::ostream &os, const polynomial &poly) { os << poly.radix.size(); if (poly.radix.size() != 0) std::cout << " "; for (int i = 0; i<poly.radix.size(); i++) { os << poly.radix[i] << " " << std::fixed << std::setprecision(1) << poly.coefficient[i]; if (i != poly.radix.size() - 1) os << " "; } return os; } int main() { polynomial poly1, poly2; std::cin >> poly1 >> poly2; std::cout << poly1*poly2; }