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 you
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
Note:
Code Image:
Sample Output:
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;
}
please provide full answer with comments this is just begining course of c++ so don't use...
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 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 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 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 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) 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 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 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( ) { 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);...