How to write the backbone of this program project

I have the following homework assignment. And I'm having trouble getting started. I'm familiar with overloading operators and such. I just don't know where to begin. Do I create a class with derived classes? How would I go about setting up the backbone of this program? How many functions would I need? What would those functions do? Looking for some guidance on this Data Structures homework. Thanks!

Develop a Polynomial class that stores and performs calculations on polynomials. It should store
coefficients as dynamic arrays of integers. It should provide default and copy constructors, and
overloaded operators for addition, multiplication, and stream input/output.

Analyze the worst-case running time of the addition and multiplication functions, in terms of all
executable statements. Give your results in O-notation, where n is the larger of the two
polynomial degrees. For the purpose of analysis, assume that both polynomials are the same size.
For example, if one polynomial is degree 4 and the other is degree 10, your analysis should
assume that both are degree 10.

Your program should allow testing of these features. I recommend reading tests from a file and
writing results to another file, but you may do it with command-line tests if you wish (if
anything, a command-line menu is harder and you have to keep re-typing your test data).
Last edited on
Do I create a class with derived classes?


Just one class. Called Polynomial. Start by doing that. Just create that. Then show us it.
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
class Polynomial
{
private:
	double *coefficients; //this will be the array where we store the coefficients
	int degree; //this is the degree of the polynomial (i.e. one less then the length of the array of coefficients)

public:
	Polynomial(); //the default constructor to initialize a polynomial equal to 0
	Polynomial(double[], int); //the constructor to initialize a polynomial with the given coefficient array and degree
	Polynomial(Polynomial&); //the copy constructor
	Polynomial(double); //the constructor to initialize a polynomial equal to the given constant
	~Polynomial() { delete coefficients; } //the deconstructor to clear up the allocated memory

	//the operations to define for the Polynomial class
	Polynomial operator+(Polynomial p) const;
	Polynomial operator-(Polynomial p) const;
	Polynomial operator*(Polynomial p) const;
};

//This is what the default constructor should look like
Polynomial::Polynomial() {
	degree = 0;
	coefficients = new double[degree + 1];
	coefficients[0] = 0;
}
Last edited on
Here's what I have so far. Is it right? Can anyone help me write the functions? Thanks again.
Strongly consider using std::vector to hold the coefficients. https://cal-linux.com/tutorials/vectors.html

Here's a skeleton to get you started:

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' ;
}

http://coliru.stacked-crooked.com/a/12e1bc4c093288d1
Last edited on
Topic archived. No new replies allowed.