학교 실습시간
#include <iostream> using namespace std; class Polynomial { private: int a, b, c; public: Polynomial(int a, int b, int c); Polynomial operator+( const Polynomial &T ); Polynomial operator-( const Polynomial &T ); friend std::ostream& operator <<( std::ostream& out, const Polynomial& T ); }; Polynomial::Polynomial(int a = 0, int b = 0, int c = 0) { this->a = a; this->b = b; this->c = c; } Polynomial Polynomial::operator +( const Polynomial &T ) { int a = this->a + T.a; int b = this->b + T.b; int c = this->c + T.c; Polynomial R(a, b,c); return R; } Polynomial Polynomial::operator -( const Polynomial &T ) { int a = this->a - T.a; int b = this->b - T.b; int c = this->c - T.c; Polynomial R(a, b, c); return R; } std::ostream& operator <<( std::ostream& out, const Polynomial& T ) { if( T.b < 0 && T.c < 0 ) out << T.a << "x - " << -1*T.b << "y - " << -1*T.c; else if ( T.b < 0 ) out << T.a << "x - " << -1*T.b << "y + " << T.c; else if ( T.c < 0 ) out << T.a << "x + " << T.b << "y - " << -1 *T.c; else out << T.a << "x + " << T.b << "y + " << T.c; return out; } int main(void) { int menu; int a1, b1, c1; int a2, b2, c2; while( cin >> menu ) { cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2; Polynomial p1(a1, b1, c1); Polynomial p2(a2, b2, c2); Polynomial p3; if( menu == 1 ) p3 = p1 + p2; else p3 = p1 - p2; cout << p3 << endl; } return 0; }
Posted by LaLuna