Question

please provide full answer with comments this is just begining course of c++ so don't use advanced tequenicks I'll put main.cpp, polynomial.h, polynomial.cpp and Cimg.h at the bottom of pictures. If you help me with this will be greatly thankful thank youDownload the starter code main. cpp, polynomial.h, polynomial.cpp, and CImg h. You will have to modify the header file but you may not change the signatures of the provided public methods of class polynomial. You are free to add any public and/or private members.) We have provided main.cpp to give you an idea of how we intend to use the functions. Put the implementations into the files polynomial. h and/or polynomial.cpp. All files you submit must not contain a main function You may not use global variables. You may not use the using-directive, using namespace std; You may not use #pragma once. You may not use any libraries aside from cassert, crnath, iostream, algorithm, vector, string, and CImg.h. We may take off up to 20 of the total marks for poor style; make sure to name your variables reasonably, indent properly, and comment sufficiently. Submit polynomial h and polynomial.cpp. In hw7 and hw8, weve forced you to put all function definitions into cpp files. However, theres nothing wrong with putting very short definitions into header files and putting only the long definitions into cpp files Problem 1: (Polynomial) Write a class that represents a polynomial The constructor pol y nom ial (double c 0.0); creates a polynomial that corresponds to p(a) The method int degree returns the degree of the polynomial. (For the purpose of this assignment, lets say the zero polynomial p(ar) 0 is degree 0. The method nonzero Terms int returns the number of nonzero terms of the polynomial. For example, 1 has 2 nonzero terms. The procedure void set Coeff int deg double c) sets the coefficient of the term deg to c. Calling this function can increase or decrease the degree of the polynomial. assert that deg is nonnegative The method

main.cpp

#include "polynomial.h"
#include "polynomial.h"
#include "polynomial.h"
#include "polynomial.h"
#include <iostream>
using std::cout;
using std::endl;

int main() {
  pic10a::polynomial p1;
  p1.setCoeff(0, 1.2);
  p1.setCoeff(3, 2.2);
  p1.setCoeff(7, -9.0);
  p1.setCoeff(7, 0.0);
  //degree of polynomial is now 3
  cout << p1 << endl;
  cout << -p1 << endl;
  cout << p1 * 2 << endl; //why does this work?
  cout << 2 * p1 << endl;
  p1 *= 4;
  cout << p1 << endl;
  cout << p1.degree() << endl; //should be 3
  cout << p1.nonzeroTerms() << endl; //should be 2


 
  pic10a::polynomial p2;
  p2.setCoeff(0, 1);
  p2.setCoeff(1, 1);
  pic10a::polynomial p3;
  p3.setCoeff(0, -1);
  p3.setCoeff(1, 1);
  cout << p2 << endl;
  cout << p3 << endl;
  
  cout << p2.getCoeff(0) << endl;
  cout << p2.getCoeff(1) << endl;
  cout << p2.getCoeff(2) << endl; //should be zero
  cout << p2.getCoeff(3) << endl; //should be zero
  cout << p2.getCoeff(4) << endl; //should be zero
  cout << p2.getCoeff(5) << endl; //should be zero
  
  cout << p2 + p3 << endl;
  cout << p2 - p3 << endl;
  cout << p2 - p2 << endl;
  cout << p2 * p3 << endl;

  cout << (p2 + p3).degree() << endl; //should be 1
  cout << (p2 - p3).degree() << endl; //should be 0
  cout << (p2 - p2).degree() << endl; //should be 0
  

  pic10a::polynomial x;
  x.setCoeff(1, 1);
  pic10a::polynomial p4 = -1 + 2 * x + x*x*x;
  cout << p4 << endl;
  cout << p4(0.0) << endl;
  pic10a::plot(p4*p4);

}

polynomial.cpp

#include "polynomial.h"
#include "CImg.h"
#include <iostream>



std::ostream& operator<<(std::ostream& s, polynomial p) {
  if (p.degree() == 0 && p.getCoeff(0) == 0.0)
    return (s << double(0));
  for (int i = p.degree(); i >= 0; i--) {
    if (p.getCoeff(i) == 0)
      continue;
    if (i < p.degree() && p.getCoeff(i)>0)
      s << "+";
    if (p.getCoeff(i) != 1.0 || i==0)
      s << p.getCoeff(i);
    if (i == 1) {
      s << "x";
    } else if (i > 1){
      s << "x^" << i;
    }
  }
  return s;
}


//Credit for this function:
//http://cimg.eu/
//http://stackoverflow.com/questions/39414084/plotting-a-vector-in-c-with-cimg
using namespace cimg_library;
void plot(polynomial p) {
  int argc = 0; char** const argv = NULL;
  const char *const formula = cimg_option("-f", "p(x)", "Formula to    plot");
  const double x0 = cimg_option("-x0", -2.0, "Minimal X-value");
  const double x1 = cimg_option("-x1", 2.0, "Maximal X-value");
  const int resolution = cimg_option("-r", 5000, "Plot resolution");
  const unsigned int nresolution = resolution>1 ? resolution : 5000;
  const unsigned int plot_type = cimg_option("-p", 1, "Plot type");
  const unsigned int vertex_type = cimg_option("-v", 1, "Vertex type");

  //Create plot data
  CImg<double> values(1, nresolution, 1, 1, 0);

  const unsigned int r = nresolution - 1;

  for (int i1 = 0; i1 < resolution; ++i1) {
    double xtime = x0 + i1*(x1 - x0) / r;
    values(0, i1) = p(xtime);
  }

  CImg<double> values2;
  values2 = values.display_graph(formula, plot_type, vertex_type, "X Axis", x0, x1, "Y Axis");
  values.normalize(0, 255);
  values.save_bmp("plot.bmp");
}

polynomial.h

#include <iostream>

class polynomial {
 public:
  polynomial(double c = 0.0);
  int degree();
  int nonzeroTerms();
  void setCoeff(int deg, double c);
  double getCoeff(int deg);
  double operator()(double x);
  polynomial operator+(polynomial p);
  polynomial& operator+=(polynomial p);
  polynomial operator-();
  polynomial operator-(polynomial p);
  polynomial& operator-=(polynomial p);
  polynomial operator*(polynomial p);
  polynomial& operator*=(polynomial p);
};

polynomial operator+(double c, polynomial p);
polynomial operator*(double c, polynomial p);
std::ostream& operator<<(std::ostream& s, polynomial p);
void plot(polynomial p);
For Cimg.h this is the link it is too long i couldn't copy paste here

http://www.math.ucla.edu/~eryu/courses/pic10a/hw/hw9/CImg.h

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note:

  • As per the question I have implemented the code in used visual studio so for that reason CImg.h not included.
  • I have commented the line CImg.h in polynomial.cpp file.
  • I have removed the Implementation part of plot function from polynomial.cpp file. If possible remove or comment the plot function.
  • I have provided sample output of the code.

Code Image:

//polynomial.h #include <iostream> #include<vector> // Implementation of polynomial class class polynomial public: //Declarat// Declare operator+ as function with parameters polynomial operator+ (double c, polynomial p); J/Declare operator* as functi/polynomial.cpp Finclude polynomial.h /#include CImg.h #include <iostream> / Implementation of Parameterized constructor// Implementation of setCoeff function void polynomial::set Coeff(int deg, double c) // check deg is greater than return valu// Implementation of operator() function /which evaluates the polynomial double polynomial:: operator() (double x) //Declare// Implementation of operator++ function polynomial polynomial::operator+(polynomial p) //Declare tempResult as type of polyn// Implementation of operator- function polynomial polynomial:: operator- (polynomial p) //Declare result as type of polynomi// Implementaion of operator* function polynomial polynomial::operator* (polynomial p) //Declare the result as type of polyno// Implementation of operator+ function polynomial operator+ (double c, polynomial p) //Declare result as type of polynomialstd::ostream& operator<<(std::ostream& s, polynomial p) // check the return value of p.degree() and p.getCoeff (0) is //equal#include polynomial.h #include <iostream> using std::cout; using std::endl; // Implementation of main class lint main() //c//create p2object is an object for polynomial class polynomial p2object; //call set Coeff function with p2Object p2object.set

Sample Output:

1.5x^3+3.2 -1.5x3-3.2 6x^3+12.8 ONONNNN 4x 4-8x2+4 PEN

Code to Copy:

polynomial.h:

//polynomial.h

#include <iostream>

#include<vector>

//Implementation of polynomial class

class polynomial

{

public:

     //Declaration of parameterized constructor

     polynomial(double c = 0.0);

     //Declaration of degree function

     int degree();

     //Declaration of nonzeroTerms function

     int nonzeroTerms();

     //Declare setCoeff function

     void setCoeff(int deg, double c);

     //Declaration of getCoeff function

     double getCoeff(int deg);

     //Declaration of operator(double x) function

     double operator()(double x);

     //Declaration of operator+(polynomial p) function

     polynomial operator+(polynomial p);

     //Declaration of operator+=(polynomial p) function

     polynomial& operator+=(polynomial p);

     //Declaration of operator-() function

     polynomial operator-();

     //Declaration of operator-(polynomial p) function

     polynomial operator-(polynomial p);

     //Declaration of operator-=(polynomial p) function

     polynomial& operator-=(polynomial p);

     //Declaration of operator*(polynomial p) function

     polynomial operator*(polynomial p);

     //Declaration of operator*=(polynomial p) function

     polynomial& operator*=(polynomial p);

private:

     //Declare coeff as type of double

     std::vector<double> coeff;

};

//Declare operator+ as function with parameters

polynomial operator+(double c, polynomial p);

//Declare operator* as function with parameters

polynomial operator*(double c, polynomial p);

//Declare operator << as function with parameters

std::ostream& operator<<(std::ostream& s, polynomial p);

//Declaration of plot function

//void plot(polynomial p);

polynomial.cpp:

//polynomial.cpp

#include "polynomial.h"

//#include "CImg.h"

#include <iostream>

//Implementation of Parameterized constructor

polynomial::polynomial(double c )

{

     //call setCoeff function

     setCoeff(0, c);

}

//Implementation of degree function

int polynomial::degree()

{

     //return the coeff.size()-1 value

     return coeff.size() - 1;

}

//Implementationof nonzeroTerms function

int polynomial::nonzeroTerms()

{

     //Declare count as type of integer

     int polyCount = 0;

     //Iterate the loop

     for (int i = 0; i < coeff.size(); ++i)

     {

          //check coeff[i] is not equal to zero

          if (coeff[i] != 0)

          {

              //increment the polyCount

              polyCount = polyCount + 1;

          }

     }

     //return the polyCount

     return polyCount;

}

//Implementation of setCoeff function

void polynomial::setCoeff(int deg, double c)

{

     //check deg is greater than return value of the degree()

     if (deg > degree())

     {

          //Iterate the loop

          for (int k = degree() + 1; k <= deg; ++k)

          {

              //call push_back function with parameter 0

              coeff.push_back(0);

          }   

     }

     //assign c to coeff[deg]

     coeff[deg] = c;

     //checl c is 0 or not

     if (c == 0 && deg == degree())

     {

          //call pop_back function

          coeff.pop_back();

     }

}

//Implementation of getCoeff function

double polynomial::getCoeff(int deg)

{

     //check whether deg is less than zero

     //or deg is greater than return value of degree()

     if (deg<0 || deg > degree())

     {

          return 0;

     }

     //return coeff[deg]

     return coeff[deg];

}

//Implementation of operator() function

//which evaluates the polynomial

double polynomial::operator()(double x)

{

     //Declare tempValue and polytemp as type of double

     double tempValue , polytemp;

     //assign 0 to tempValue

     tempValue = 0;

     //Iterate the loop

     for (int k = 0;k < coeff.size();k++)

     {

          //check coeff[i] is equal to 0

          if (coeff[k] == 0)

          {

              continue;

          }

          //assign 1 to polytemp

          polytemp = 1;

          //Declare j as integer type and

          //assign 1 to j

          int j = 1;

          //Iterate the loop

          while (j <= k)

          {

              //multiply x with polytemp

              polytemp = polytemp * x;

              //increment j

              j++;

          }

         

          //calculate polytemp value

          polytemp = polytemp * coeff[k];

          //add polytemp to tempValue

          tempValue = tempValue + polytemp;

     }

     //return the tempValue

     return tempValue;

}

//Implementation of operator++ function

polynomial polynomial::operator+(polynomial p)

{

     //Declare tempResult as type of polynomial

     polynomial tempResult;

     //Iterate the loop

     for (int k = 0;k <= p.degree() || k <= p.degree(); ++k)

     {

          //call setCoeff function

          tempResult.setCoeff(k, p.getCoeff(k) + p.getCoeff(k));

     }

     //return tempResult

     return tempResult;

}

//Implementation of operator+= function

polynomial& polynomial::operator+=(polynomial p)

{

     *this = *this + p;

     //return *this value

     return *this;

}

//Implementation of operator-() function

polynomial polynomial::operator-()

{

     //Declare result as type of polynomial

     polynomial result;

    

     //Declare k as type of integer

     int k = 0;

     //Iterate the loop

     while (k <= degree())

     {

          //call setCoeff function

          result.setCoeff(k, -getCoeff(k));

          //increment the k

          k++;

     }

     return result;

}

//Implementation of operator- function

polynomial polynomial::operator-(polynomial p)

{

     //Declare result as type of polynomial

     polynomial result;

     //Iterate the loop

     for (int k = 0; k <= degree() || k <= p.degree(); ++k)

     {

          //call setCoeff function

          result.setCoeff(k, getCoeff(k) - p.getCoeff(k));

     }

     //return the result

     return result;

}

//Implementation of operator-= function with parameter type polynomial

polynomial& polynomial::operator-=(polynomial p)

{

     *this = *this - p;

     //return *this

     return *this;

}

//Implementaion of operator* function

polynomial polynomial::operator*(polynomial p)

{

     //Declare the result as type of polynomial

     polynomial result;

     int mValue = p.degree(), nValue = p.degree();

     //Declare sum as type of double

     double sumValue;

     //Iterate the loop

     for (int k = 0; k <= mValue + nValue; k++)

     {

          //assign 0 to sumValue

          sumValue = 0;

          //Iterat the loop

          for (int j = 0; j <= k;j++)

          {

              //calculate the sumValue

              sumValue = sumValue + p.getCoeff(j) * p.getCoeff(k - j);

              //increment j

              j++;

          }

          //call setCoeff function

          result.setCoeff(k, sumValue);

     }

     //return the result

     return result;

}

//Implementation of operator*= function with parameter p

polynomial& polynomial::operator*=(polynomial p)

{

     *this = *this * p;

     //return *this

     return *this;

}

//Implementation of operator+ function

polynomial operator+(double c, polynomial p)

{

     //Declare result as type of polynomial

     polynomial result;

     //Iterate the loop

     for (int k = 0; k <= p.degree(); ++k)

     {

          //call setCoeff function with parameters

          result.setCoeff(k, p.getCoeff(k));

     }

     //call setCoeff function with parameters

     result.setCoeff(0, p.getCoeff(0) + c);

     //return the result

     return result;

}

//Implementation of operator* function

polynomial operator*(double c, polynomial p)

{

     //Declare result as type of polynomial

     polynomial result;

     //Iterate the loop

     for (int i = 0; i <= p.degree(); ++i)

     {

          //call setCoeff function with parameter

          result.setCoeff(i, p.getCoeff(i) * c);

     }

     //return the result

     return result;

}

//Implementation of operator << function

std::ostream& operator<<(std::ostream& s, polynomial p)

{

     //check the return value of p.degree() and p.getCoeff(0) is

     //equal to zero

     if (p.degree() == 0 && p.getCoeff(0) == 0.0)

     {

          return (s << double(0));

     }

     //Iterate the loop

     for (int k = p.degree(); k >= 0; k--)

     {

          //check p.getCoeff(k) is equal to 0

          if (p.getCoeff(k) == 0)

          {

              continue;

          }

          //check i value is less than the return value of

          //p.degree() an return value of p.getCoeff(k) is greater than zero

          if (k < p.degree() && p.getCoeff(k)>0)

          {

              //Display statemet

              s << "+";

          }

          //check p.getCoeff(k) not equal to 1.0

          if (p.getCoeff(k) != 1.0 || k == 0)

          {

              //Display the return value of p.getCoeff(k)

              s << p.getCoeff(k);

          }

          if (k == 1)

          {

              //Display statemet

              s << "x";

          }

          else if (k > 1)

          {

              //Display statemet

              s << "x^" << k;

          }

     }

     return s;

}

main.cpp:

#include "polynomial.h"

#include <iostream>

using std::cout;

using std::endl;

//Implementation of main class

int main()

{

     //create an object for polynomial class as p1Object

     polynomial p1Object;

     //call setCoeff function with p1Object

     p1Object.setCoeff(0, 3.2);

     //call setCoeff function with p1Object

     p1Object.setCoeff(3, 1.5);

     //call setCoeff function with p1Object

     p1Object.setCoeff(2, -5.0);

     //call setCoeff function with p1Object

     p1Object.setCoeff(2, 0.0);

     //Display statement

     cout << p1Object << endl;

     //Display statement for '-' operator with p1Object

     cout << -p1Object << endl;

     //Display statement for operator *

     cout << p1Object * 2 << endl;

     //Display statement

     cout << 4 * p1Object << endl;

     //perform an operation *= on p1Object

     p1Object *= 2;

     //Display statement

     cout << p1Object << endl;

     //Display statement

     cout << p1Object.degree() << endl;

     //Display statement

     cout << p1Object.nonzeroTerms() << endl;

     //create p2Object is an object for polynomial class

     polynomial p2Object;

     //call setCoeff function with p2Object

     p2Object.setCoeff(0, 2);

     //call setCoeff function with p2Object

     p2Object.setCoeff(2, 2);

     //create p3ObjectObject is an object for polynomial class

     polynomial p3Object;

     //call setCoeff function with p3Object

     p3Object.setCoeff(0, -2);

     //call setCoeff function with p3Object

     p3Object.setCoeff(2, 2);

     //Display statement

     cout << p2Object << endl;

     //Display statement

     cout << p3Object << endl;

     //Display statement

     cout << p2Object.getCoeff(0) << endl;

     //Display statement

     cout << p2Object.getCoeff(1) << endl;

     //Display statement

     cout << p2Object.getCoeff(2) << endl;

     //Display statement

     cout << p2Object.getCoeff(3) << endl;

     //Display statement for addition of two polynomials

     cout << p2Object + p3Object << endl;

     //Display statement for subtraction of two polynomials

     cout << p2Object - p3Object << endl;

     //Display statement for subtraction of two polynomials

     cout << p2Object - p2Object << endl;

     //Display statement for multiplication of two polynomials

     cout << p2Object * p3Object << endl;

     //Display statement for degree of two polynomials

     cout << (p2Object + p3Object).degree() << endl;

     //Display statement for degree of two polynomials

     cout << (p2Object - p3Object).degree() << endl;

     //Display statement for degree of two polynomials

     cout << (p2Object - p2Object).degree() << endl;

     return 0;

}

Add a comment
Know the answer?
Add Answer to:
please provide full answer with comments this is just begining course of c++ so don't use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi guys! I need help for the Data Structure class i need to provide implementation of...

    Hi guys! I need help for the Data Structure class i need to provide implementation of the following methods: Destructor Add Subtract Multiply Derive (extra credit ) Evaluate (extra credit ) ------------------------------------------------------- This is Main file cpp file #include "polynomial.h" #include <iostream> #include <sstream> using std::cout; using std::cin; using std::endl; using std::stringstream; int main(int argc, char* argv[]){    stringstream buffer1;    buffer1.str(        "3 -1 2 0 -2.5"    );    Polynomial p(3);    p.Read(buffer1);    cout << p.ToString()...

  • The goal of this problem set is to extend the solution oftutorial 3. In particular,...

     The extended specification of class Polynomial is shown below. You only need to implement the last four methods. The other features (i.e., constructor and operators) are given as part of the solution for tutorial 3. In the .cpp file for the new methods you need to include cmath that contains the definition of pow - raise to power.The goal of this problem set is to extend the solution of tutorial 3. In particular, we wish toadd methods to calculate a...

  • USING C++: Referring to the header file below named coord2d.h, Implement each of the 8 operator...

    USING C++: Referring to the header file below named coord2d.h, Implement each of the 8 operator overloads (operators: <<, [], >, <, two versions of +, two versions of *) #ifndef COORD2D_H #define COORD2D_H #include <iostream> using namespace std; //implement a class that keeps track of the coordinates of a point in the X-Y plane class coord2d { //overload the << operator so that if p is of type "coord2d" then "cout<<p"; //will print out (x_coord, y_coord) //e.g. if p.x_coord=3.4,...

  • Add and subtract polynomials using linked lists: The output should look like the following: The code...

    Add and subtract polynomials using linked lists: The output should look like the following: The code for the header file, Polynomial.h, is given as such: ***********HEADER************* #include <iostream> #include <cstdlib> using namespace std; class polyll { //this is class POLYLL, but all lower case private: struct polynode { float coeff; int exp; polynode* link; } * p; public: polyll(); void poly_append(float c, int e); void display_poly(); void poly_add(polyll& l1, polyll& l2); void poly_subtract(polyll& l1, polyll& l2); ~polyll(); }; polyll::polyll() {...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...

  • I need to implement a program that requests a x,y point and the radius of a...

    I need to implement a program that requests a x,y point and the radius of a circle. then it figures out the area and circumference using an overloaded operation. The full instructions, what I have and the errors I have are below. A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, pointType, that can store and process a point in the x-y plane. You should then perform operations on the point, such as...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) {...

    /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) { x=y=0;} shape(int xvalue, int yvalue); void setShape(int new_x, int new_y); void setX(int new_x); void setY(int new_y); int getX( ) const; int getY( ) const; virtual void move(int x, int y) = 0; virtual void shift(int dx, int dy) = 0; virtual void draw( ) = 0; virtual void rotate(double r) = 0; virtual void print(ostream&)const; friend ostream & operator<<(ostream & os, const shape& s);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT