Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a c++ project. PROJECT INSTRUCTIONS You are to implement a polynomial cl

ID: 3810113 • Letter: T

Question

This is a c++ project.

PROJECT INSTRUCTIONS
You are to implement a polynomial class and driver program for testing it


You shall write your class as two files, a header file, Polynomial.h, that will contain the class definition.
And an implementation file, Polynomial.cpp, that will contain constructor, destructor, and member functions.
Unless a member function is declared "inline", its body shall reside in the implementation file.
The driver (containing main()) shall reside in a third source code file, named poly_class.cpp.
Functionally, your polynomial class shall implement:
1) object construction, using the default constructor
2) object construction, using an overloaded constructor
3) object construction, using a copy constructor
4) object destruction
5) object assignment, overloading the = operator
6) object output, overloading the << operator
7) object input, overloading the >> operator
8) object addition, overloading the + operator
9) object subtraction, overloading the - operator
10) object multiplication, overloading the * operator
11) object equality testing, overloading the == operator
12) object evaluation, overloading the () operator, (value of polynomial for a given x, e.g. f (x)).
To make this project doable, our polynomials shall have only integer coefficients and shall be of degree 5 or less.

Explanation / Answer

main.cpp

#include <iostream>
#include "poly.h"

using namespace std;

void cctest(polyno);

int main()
{
   polyno S1, S2, S3;
   int howmany;
   double c, x, result;
   unsigned int e;
  
   cout << "Enter how many coeffiecients the polyno : ";
   cin >> howmany;
   while (howmany > 0)
   {
       cout << "Enter coefficient : ";
       cin >> c;
       cout << "Enter exponent : ";
       cin >> e;
       S1.assign_coef(c,e);
       howmany--;
   }
  
   cout << "Enter how many coeffiecients the polyno : ";
   cin >> howmany;
   while (howmany > 0)
   {
       cout << "Enter coefficient : ";
       cin >> c;
       cout << "Enter exponent : ";
       cin >> e;
       S2.assign_coef(c,e);
       howmany--;
   }
  
   cout << "The first polyno is " << S1 << endl
       << "The second polyno is " << S2 << endl << endl;
      
   S3 = S2;
   cout << "Testing = operator ... " << endl
       << "The second polyno is " << S3 << endl << endl;

   cout << "Results of Addition : " << S1 + S2 << endl;
   // cout << "Results of Subtraction : " << S1 - S2 << endl << endl;
   // cout << "Results of Multiplication : " << S1 * S2 << endl << endl;
  
  
  
   cout << "Testing Evaluate ..." << endl;
   cout << " Enter value of x : ";
   cin >> x;
   result = S1.eval(x);
   cout << " The result of S1 when x is " << x << " is " << result << endl << endl;
  
   cout << "Testing copy constructor..." << endl;
   cctest(S1);
      
   return 0;
}

void cctest(polyno P)
{
   cout << " The first polyno is " << P << endl << endl;
}

poly.h


#ifndef POLY_H
#define POLY_H
#include <iostream>
#include <cstdlib>

using namespace std;


const unsigned int DEFAULT_CAPACITY = 10;
typedef size_t size_type;
typedef double value_type;

class polyno
{
    public:
       // CONSTRUCTORS and DESTRUCTOR
       polyno();
       polyno(const polyno& source);
       ~polyno( );

       // MODIFICATION MEMBER FUNCTIONS
       void operator =(const polyno& source);
       void assign_coef(double coefficient, unsigned int exponent);
        void reserve(unsigned int number);
       void setcurrent_degree(int init_degree);
       void setsize(int init_size);

       // CONSTANT MEMBER FUNCTIONS
       double coefficient(unsigned int exponent) const;
       unsigned int degree( ) const;
       double eval(double x) const;
       unsigned int getsize ( ) const;
   private:
       double *coef;                 // Pointer to the dynamic array
       unsigned int size;            // Current size of the dynamic array
       unsigned int current_degree; // Current degree of the polyno
};
  
        // NON-MEMBER FUNCTIONS
       ostream& operator << (ostream& cout, const polyno& r);
       polyno operator +(const polyno& l, const polyno& r);
       polyno operator -(const polyno& l, const polyno& r);


#endif

poly.cpp

#include <cassert>   // Provides assert
#include <cmath>     // Provides pow
#include <iostream>
#include <cstdlib>
#include "poly.h"

using namespace std;
//contructors n destructors
polyno::polyno()
{
   current_degree = 0;
   for ( int i = 0; i <= DEFAULT_CAPACITY; i++ )
      coef[i] = 0.0;

  
}


    polyno::polyno(const polyno& source)
   {
        coef = new double[source.size];
        size = source.size;
        current_degree = source.current_degree;
        for (int i = 0; i < size; i++)
           coef[i] = source.coef[i];
   }

  
       polyno::~polyno( )
       {
           delete [] coef;
       }

   //mod member fns
   void polyno::assign_coef(double coefficient, unsigned int exponent)
   {
       coef[exponent] = coefficient;


   if ( coefficient != 0.0 && exponent > current_degree )
      current_degree = coefficient;

   int i = current_degree;
   while ( i >= 0 && coef[i] == 0.0 )
   {
      current_degree--;
      i--;
   }
   }
  
     void polyno::operator =(const polyno& source)
    {
        double *new_coef;
  
        if (this == &source)
            return;
        if (size != source.size)
        {
           new_coef = new double[source.size];
           delete [ ] coef;
           coef = new_coef;
           size = source.size;
        }
        current_degree = source.current_degree;
        for(size_t i = 0; i < size; i++)
           coef[i] = source.coef[i];
          
    }

  
  
    void polyno::reserve(unsigned int number)
   {
       double *larger_array;
       if (number == size)
           return;
       if (number < current_degree)
           number = current_degree;

       larger_array = new double[number]();
  
       for (int i = 0; i < number; i++)
       {
           larger_array[i] = coef[i];
       }
       delete [ ] coef;
       coef = larger_array;
       size = number;
   }
  
void polyno::setcurrent_degree(int init_degree)
   {
       init_degree = current_degree;
   }

   void polyno::setsize(int init_size)
   {
       init_size = size;
   }
  
   // CONSTANT MEMBER FUNCTIONS

   double polyno::coefficient(unsigned int exponent) const
   {
       return coef[exponent];
   }
      
   unsigned int polyno::degree( ) const
   {
       {
      int d = 0;
      for ( int i = 0; i < 10; i++ )
         if ( coef[i] != 0 ) d = i;
      return d;
   }
   }
      
   double polyno::eval(double x) const
   {
       double power = 1,
          result = 0;

   for (unsigned int i = 0; i <= current_degree; i++)
   {
      result += coef[i] * power;
      power *= current_degree;
   }
   return result;

   }


   unsigned int polyno::getsize ( ) const
   {
   return size;
   }

   //nonmember fns

      
      
       ostream & operator<<(ostream & out, const polyno & r)
       {
           if ( r.degree( ) == -1 )
   {
      cout << 0;
      return out;
   }    
   out << r.coefficient( r.degree( ) ) << "x^" << r.degree( );
   for (int i = r.degree( )-1; i >= 0; i--)
   {
      out << " + ";
      if ( r.coefficient( i ) < 0.0 )
         out << '(' << r.coefficient( i ) << ')';
      else
         out << r.coefficient( i );
      out << "x^" << i;
   }

   return out;
       }
      
      
polyno operator+(const polyno & l, const polyno & r )
{
   int leftDegree = l.degree( ),
       rightDegree = r.degree( ),
       degree;
   double sum;
     
   if ( leftDegree > rightDegree )
      degree = leftDegree;
   else
      degree = rightDegree;
    
   polyno resPoly;

   for ( int i=degree; i >= 0; i-- )
   {
      sum = l.coefficient( i ) + r.coefficient( i );
     
   }

   return resPoly;
}
      

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote