3. Polynomial

3. Polynomial#

Implement a class called Polynomial, which holds a polynomial (eg. \(ax^2 + bx^1\)), and satisfies the following invariants,

  • No similar term.

    • Users shouldn't observe similar terms in the polynomial. That is, \(3 + 3x^2 + 5 + 2x^2\) should be observed as \(8 + 5x^2\).

  • Sorted by ascending power order.

    • Users should observe an ascendingly sorted polynomial. That is, \(2x^2 + 3 + x\) should be observed as \(3 + x + 2x^2\).

  • For terms with 0 as coefficient, the observable state shows as if the term dosen't exsit. That is, \(0x^2 + x^3\) should be observed as \(x^3\).

You should implement the following functions for it,

  • auto operator+=(Polynomial const& other) -> Polynomial&

    • Add *this and other, store the result in *this.

  • friend auto operator+(Polynomial const& lhs, Polynomial const& rhs) -> Polynomial

    • Add lhs and rhs, store the result in a new Polynomial.

  • friend auto operator<<(std::ostream& ostream, Polynomial const& polynomial) -> std::ostream&

    • Output the polynomial with the format 3 + 5x^2 + 1x^3 + 1x^5.

After that, write a main function to test all your public functions.