#include <iostream>
using namespace std;
class Polynomial {
private:
int a, b, c;
public:
Polynomial(int a, int b, int c) : a(a), b(b), c(c) {}
int getA() const { return a; }
int getB() const { return b; }
int getC() const { return c; }
Polynomial operator+(const Polynomial& n) const {
return Polynomial(a + n.a, b + n.b, c + n.c);
}
Polynomial operator-(const Polynomial& n) const {
return Polynomial(a - n.a, b - n.b, c - n.c);
}
Polynomial operator*(int scalar) const {
return Polynomial(a * scalar, b * scalar, c * scalar);
}
};
ostream& operator<<(ostream& out, const Polynomial& n) {
if (n.getA() == 0 && n.getB() == 0 && n.getC() == 0) {
out << "0";
}
if (n.getA() != 0) {
out << n.getA() << "x^2";
}
if (n.getB() != 0) {
if (n.getB() > 0) {
out << "+" << n.getB() << "x";
} else {
out << n.getB() << "x";
}
}
if (n.getC() != 0) {
if (n.getC() > 0) {
out << "+" << n.getC();
} else {
out << n.getC();
}
}
return out;
}
int main() {
Polynomial n1(-2, 8, 3);
Polynomial n2(9, 0, 5);
cout << "First Polynomial: " << n1 << endl;
cout << "Second Polynomial: " << n2 << endl;
cout << "Addition: " << n1 + n2 << endl;
cout << "Difference: " << n1 - n2 << endl;
cout << "Multiplication: " << n1 * 8 << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgUG9seW5vbWlhbCB7CnByaXZhdGU6CiAgICBpbnQgYSwgYiwgYzsKcHVibGljOgogICAgUG9seW5vbWlhbChpbnQgYSwgaW50IGIsIGludCBjKSA6IGEoYSksIGIoYiksIGMoYykge30KCiAgICBpbnQgZ2V0QSgpIGNvbnN0IHsgcmV0dXJuIGE7IH0KICAgIGludCBnZXRCKCkgY29uc3QgeyByZXR1cm4gYjsgfQogICAgaW50IGdldEMoKSBjb25zdCB7IHJldHVybiBjOyB9CgogICAgUG9seW5vbWlhbCBvcGVyYXRvcisoY29uc3QgUG9seW5vbWlhbCYgbikgY29uc3QgewogICAgICAgIHJldHVybiBQb2x5b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