Trouble making a polynomial class

Hello i'm making a linked polynomial program that requires two classes. I'll put the description, and what I have below, I think I made the Term class correct? But i'm completely lost on how to make the polynomial class. Help is MUCH appreciated!

What I'm thinking, is it basically telling me to add setTerm and getTerm in the polynomial class? Or I think it's actually something needed in my public to manipulate the private "list <Term> poly;"

Prompt: ---Implement a Term Class

-The Term class should store an integer degree and a double coefficient.
-You are free to add any private/public methods to support functional operations.


---Implement a Polynomial Class
The Polynomial class should store a linked list of terms and must implement the following functionality:

-Create a new polynomial

-Your class should have methods to add and remove terms to and from a polynomial.

-You should store the polynomial in descending order of degrees.
-Display a polynomial

-This method displays a polynomial in descending order of degrees.

-Multiply a polynomial a number


Code I have now: - h file for term class
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
 #include <string>
#include <iostream>
using namespace std;

#ifndef TERM_H
#define TERM_H


class Term // stores integer and double coefficient
{
    public:
        Term();
        Term(double coefficient, int power); // whats being stored

         ~Term();

        double Getcoefficient() { return m_coefficient; } // gets coefficient
        void Setcoefficient(double val) { m_coefficient = val; } // sets coefficient
        int Getpower() { return m_power; } // gets the power
        void Setpower(int val) { m_power = val; }  // sets the power
        void display(); // displays the getters. ex) whatever.display()

    protected:

    private:
        double m_coefficient;
        int m_power;

};

#endif // TERM_H
 


.CPP file for term
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
#include "Term.h"
#include <iostream>
#include <string>
using namespace std;

Term::Term()
{
    m_coefficient = 0;
    m_power = 0;
}

Term::Term(double coefficient, int power)
{
    m_coefficient = coefficient;
    m_power = power;
}

Term::~Term()
{
    //dtor
}

void Term::display()
{
    cout << m_coefficient << " " << m_power << endl;
}


Polynomial Class that I don't know how to make.
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
#include <string>
#include <iostream>
using namespace std;


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial
{
    public:
        Polynomial();


         ~Polynomial();




    private:
        list <Term> poly;

};

#endif // POLYNOMIAL_H 


.cpp for polyclass
1
2
3
4
5
6
7
8
9
10
11
#include "Polynomial.h"

Polynomial::Polynomial()
{
    //ctor
}

Polynomial::~Polynomial()
{
    //dtor
}
Last edited on
I've gotten this far, but now i'm confused on what to do next. Do I write more in the Get and Set functions? and then for the multiplying do poly3 = poly1 * poly2 ?

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
 #include <string>
#include <iostream>
#include <list>
#include "Term.h"

using namespace std;


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial
{
    public:
        Polynomial();
        Polynomial(int poly); // creates the polynomial.

         ~Polynomial();

         int Getpoly();   // gets the poly
         void Setpoly();  // sets the poly
         void display();  // displays the poly




    private:
        list <Term> poly;

};

#endif // POLYNOMIAL_H
  
Last edited on
Your class should have methods to add and remove terms to and from a polynomial.
I would do this next
After you have done this do Display a polynomial so you can do some tests
Getpoly() and Setpoly() don't make much sense. What do they get/set?

Do what thmm says. Then write a small test program to add a few terms to a polynomial and display the result.

Since you have to store terms in order, you may want to add a less-than operator to compare them by their power:
1
2
3
class Term {
...
    bool operator<(const Term &rhs) const {return m_power < rhs.m_power; }


Now you can compare Terms directly:
1
2
3
Term t1, t2;
// set t1 and t2
if (t1 < t2) doSomething();


Finally, I have to say that in the real world, you wouldn't store the terms in a list, you'd probably use vector<double> terms; where term[n] is the coefficient of xn
Finally, I have to say that in the real world, you wouldn't store the terms in a list, you'd probably use vector<double> terms; where term[n] is the coefficient of xn


I'd actually go with map<int, double> so that you don't need to create a bunch of empty elements for 0'd element. x100 - x + 10 should not need 101 entries.
Your class should have methods to add and remove terms to and from a polynomial.

I'm having trouble understanding what this means? Is my polynomial class supposed to be a child class of the Terms class? But then I just include "add, remove, multiply etc.. so that it can add terms to the Term class?
Last edited on
or do u mean something like this, (using pointers)? "It says poly does not name a type", so i'm not sure what i'm doing wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <string>
#include <iostream>
#include <list>
#include "Term.h"

using namespace std;


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial
{
    public:
        Polynomial();


        Term *poly = new Term();
                    poly->Setcoefficient(coefficient);
                    poly->Setpower(power);
                    myList.push_front(*poly);  
Last edited on
Below: where i put the // add methods comments , i was thinking of stuff, would something like the insert() or push_front() functions be what i'm looking for?

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
#include <string>
#include <iostream>
#include <list>
#include "Term.h"

using namespace std;


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial
{
    public:
        Polynomial();
        // add methods to add and remove terms to and from a polynomial
        



         ~Polynomial();


    private:
        list <Term> poly;

};
I watched a couple videos and i feel like i'm doing this completely wrong lol, help please

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
#include "Term.h"

using namespace std;


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial
{
    public:
        Polynomial();
        // add methods to add and remove terms to and from a polynomial

        // insert node at the beginning of the list
        Term *ptr = new Term(); // creates a term/node

        // add a polynomial into the term now
        void Setcoefficient(float val) {new Term;}
        void Setpower(int val) {new Term;}
        push_front(float val);
        push_front(int val);
        









         ~Polynomial();


    private:
        list <Term> poly;

};

#endif // POLYNOMIAL_H
Here is some starter code:

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
#include <iostream>
#include <list>

struct term
{
    // The Term class should store an integer degree and a double coefficient.
    int degree ;
    double coefficient ;

    friend std::ostream& operator<< ( std::ostream& stm, const term& t ) // display a term
    { return stm << t.coefficient << " x^" << t.degree  ; }
};

struct polynomial
{
    polynomial() = default ; // Create a new empty polynomial

    // Your class should have methods to add and remove terms to and from a polynomial.
    // You should store the polynomial in descending order of degrees.
    void add_term( const term& a_term )
    {
        // check if there already is a term with this degree
        for( term& t : terms ) if( t.degree == a_term.degree )
        {
             t.coefficient += a_term.coefficient ; // if there is one, add the coefficient to it
             return ; // and we are done
        }

        // we did not find a term with this degree, add this term to he back of the list
        terms.push_back(a_term) ;

        // and rearrange the list to store the polynomial in descending order of degrees.
        // (note: this is certainly not the most efficient way, but it is adequate for our purposes
        //  we do not expect this polynomial to hold scores of terms).
        terms.sort( []( const term& a, const term& b ) { return a.degree > b.degree ; } ) ;
    }

    friend std::ostream& operator<< ( std::ostream& stm, const polynomial& poly ) // display a polynomial
    {
        // std::showpos: place a + or - sign before each number
       stm << std::showpos << "[ " ;
       for( const term& t : poly.terms ) stm << t << "  " ;
       return stm << ']' ;
    }

    // The Polynomial class should store a linked list of terms
    std::list<term> terms ;
};

int main()
{
    polynomial poly ; // create an empty polynomial

    // add some terms to it and print out the result
    for( term t : { term{-1,2.3}, term{2,5.6}, term{3,6.7}, term{-1,-4.2}, term{2,-8.2} } )
    {
        std::cout << poly << " plus " << t << "  ==  " ;
        poly.add_term(t) ;
        std::cout << poly << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/faeda2ec5830c392

TO DO: add the missing functionality - subtract a term, multiply polynomials etc.
I went through that whole code and made comments piece by piece and i still don't understand how to do it in terms of classes. I feel like I shelled out what to do correctly now, i think? Can someone explain another way that uses my terms from the object class "coefficient & power" and lets me store it in my polyclass. Every video and example i see shows me structs which is not what i'm doing.

Does what I put by the private make sense or am i still understanding this wrong.

TO DO: add the missing functionality - subtract a term, multiply polynomials etc


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
#include <string>
#include <iostream>
#include <list>
#include "Term.h"

using namespace std;


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial
{
    public:
        Polynomial();
        // add methods to add and remove terms to and from a polynomial

        // create empty polynomial shell that stores terms
        


        // store the polynomial in descending order of degrees

        // display a polynomial

        // multiply polynomials




         ~Polynomial();


    private:  
        list <Term> poly; // the list stores the attributes from Term class
                          // and poly represents the copy n paste shell for
                          // ex poly1, poly2, etc.. and in "poly" it asks for
                          // the coefficient and power.


};

#endif // POLYNOMIAL_H
Last edited on
How do I store what the user is inputting into poly1? Lines 28-41, I tried entering the classname.the variable I want to access but it's not working.

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
#include <string>
#include <iostream>
#include <list>
#include "Term.h"

using namespace std;


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial : public Term
{
    public:
        Polynomial();

         ~Polynomial();

          // function to build a polynomial

        list <Term> myList;
        list <Term>::iterator(it);


       // do {
                // create a term object
               Term poly1;

                // prompt the user for coefficient and power values and set them into the term object
                void setter(float)
                {
                std::cout << "Enter the coefficient: ";
                cin >> poly1.Setcoefficient;
                }

                void setter(int)
                {
                    std:: cout << "Enter the power: ";
                    cin >> poly1.Setpower;
                }

                // use an iterator to add the term object to the list...push_front

                // insert the term object into the list




      //  } while (()



    private:
        list <Term> poly; // the list stores the attributes from Term class
                          // and poly represents the copy n paste shell for
                          // ex poly1, poly2, etc.. and in "poly" it asks for
                          // the coefficient and power.


};

#endif // POLYNOMIAL_H 
Last edited on
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
#include <iostream>
#include <list>
#include <initializer_list>
using namespace std;

struct Term
{
   double coefficient;
   int exponent;
   Term &operator *= ( Term b ) { coefficient *= b.coefficient; exponent += b.exponent; return *this; }
};


class Polynomial
{
   list<Term> terms;

public:
   Polynomial(){};
   Polynomial( initializer_list<Term> L ){ for ( Term t : L ) addTerm( t ); }

   void add( const Polynomial &P ) { for ( Term t : P.terms ) addTerm( t ); }

   void multiply( const Polynomial &P )
   {
      Polynomial result;
      for ( Term t : P.terms )
      {
         Polynomial temp = *this;
         for ( Term &e : temp.terms ) e *= t;;
         result.add( temp );
      }
      terms = result.terms;
   }

   void addTerm( Term t )
   {
      if ( terms.size() == 0 || t.exponent < terms.back().exponent ) { terms.push_back( t ); return; } 
      for ( auto it = terms.begin(); it != terms.end(); it++ )
      {
         if ( t.exponent == it->exponent ) { it->coefficient += t.coefficient; return; }
         if ( t.exponent >  it->exponent ) { terms.insert( it, t )           ; return; }
      }
   }

   friend ostream &operator << ( ostream &out, const Polynomial &P )
   {
      if ( P.terms.size() == 0 ) out << 0;
      bool first = true;
      for ( Term t : P.terms )
      {
         if ( !first && t.coefficient >= 0.0 ) out << "+";
         first = false;
         out << t.coefficient;
         if      ( t.exponent >  1 ) out << "x^" << t.exponent;
         else if ( t.exponent == 1 ) out << "x";
      }
      return out;
   }
};


int main()
{
   Polynomial P( { { 1.0, 0 }, { 2.0, 1 } } );      // 1 + 2x reordered as 2x + 1
   Polynomial Q( { { 5.0, 2 }, { 3.0, 0 } } );      // 5x^2 + 3
   cout << "P = " << P << '\n';
   cout << "Q = " << Q << '\n';
   Polynomial R = P;   R.add( Q )     ;    cout << "P + Q = " << R << '\n';
   Polynomial S = P;   S.multiply( Q );    cout << "P * Q = " << S << '\n';
}


P = 2x+1
Q = 5x^2+3
P + Q = 5x^2+2x+4
P * Q = 10x^3+5x^2+6x+3

Last edited on
Topic archived. No new replies allowed.