1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <iomanip>
#include <vector>
// Develop a Polynomial class that stores and performs calculations on polynomials.
struct polynomial
{
// the default constructor to initialize a polynomial equal to 0
polynomial() : coefficients( 1, 0 ) {} // vector of one element zero
// constructor to initialize a polynomial with the given coefficients
explicit polynomial( const std::vector<int>& coeffs )
: coefficients( coeffs.rbegin(), coeffs.rend() ) {} // store coefficients in reverse
// constructor to initialize a polynomial equal to the given constant
explicit polynomial( int v ) : coefficients( 1, v ) {} // vector of one element v
// implicitly declared copy constructor, destructor, assignment, move etc.
// overloaded operators for addition, multiplication
// see: Canonical implementations: Binary arithmetic operators
// https://en.cppreference.com/w/cpp/language/operators
polynomial& operator += ( const polynomial& that ) ;
polynomial& operator *= ( int v ) { for( int& coeff : coefficients ) coeff *= v ; return *this ; }
private:
// It should store coefficients as dynamic arrays of integers.
// it is convenient to store the coefficients in reverse order
// ie. polynomial == coefficients[0] + coefficients[1] *x + coefficients[2] * x^2 etc.
std::vector<int> coefficients ;
// overloaded operators for addition, multiplication, stream input/output.
// see: Canonical implementations: Binary arithmetic operators
// https://en.cppreference.com/w/cpp/language/operators
friend polynomial operator+ ( polynomial a, const polynomial& b ) { return a+= b ; }
friend polynomial operator* ( polynomial a, int b ) { return a*= b ; }
friend polynomial operator* ( int a, polynomial b ) { return b*= a ; }
friend std::ostream& operator<< ( std::ostream& stm, const polynomial& poly ) ;
friend std::istream& operator>> ( std::istream& stm, polynomial& poly ) ;
};
polynomial& polynomial::operator += ( const polynomial& that )
{
// make the coefficients vector large enough
if( coefficients.size() < that.coefficients.size() ) coefficients.resize( that.coefficients.size() ) ;
for( std::size_t i = 0 ; i < that.coefficients.size() ; ++i )
coefficients[i] += that.coefficients[i] ; // add the coefficients
return *this ;
}
std::ostream& operator<< ( std::ostream& stm, const polynomial& poly )
{
// TO DO: make the output prettier
int p = 0 ;
for( auto coeff : poly.coefficients )
stm << std::showpos << coeff << " * x^" << std::noshowpos << p++ << " " ;
return stm ;
}
int main()
{
const polynomial a( { 1, 2, 3, 4, 5 } ) ; // 1 + 2.x + 3.x^2 + 4.x^3 + 5.x^4
// stored internally as [ 5, 4, 3, 2, 1 ]
const polynomial b( { 6, 0, 0, 7, 0, 3, 1 } ) ;
std::cout << " a == " << a << '\n'
<< " b == " << b << '\n'
<< " a+b == " << a+b << '\n'
<< "3*(b+a) == " << 3*(b+a) << '\n' ;
}
|