main.cpp polynomial.cpp polynomial.h http://www.math.ucla.edu/~eryu/courses/pic1
ID: 3847245 • Letter: M
Question
main.cpp
polynomial.cpp
polynomial.h
http://www.math.ucla.edu/~eryu/courses/pic10a/hw/hw9/CImg.h
Download 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, we've forced you to put all function definitions into cpp files. However, there's 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, let's 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 methodExplanation / Answer
Here is completed code for the question. Please note that plotting polynomial is suggested as optional in the question and as I could not set it up on my system, I have commented out that code. But the rest of the functionality is implemented. Output also is attached.
Please don't forget to rate the answer if it helped. Thank you very much.
main.cpp
#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.h
#include <iostream>
#include <vector>
namespace pic10a
{
class polynomial {
private:
std::vector<double> coeff; //the coeffients of different terms
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);
}
polynomial.cpp
#include "polynomial.h"
//#include "CImg.h"
#include <iostream>
namespace pic10a
{
polynomial::polynomial(double c)
{
// c is the constant i.e its teh coeffient of x^0 , so set co-eff for 0 as c
setCoeff(0, c);
}
int polynomial::degree()
{
return coeff.size()-1;
}
//returns the number of non-zero terms
int polynomial::nonzeroTerms()
{
int count = 0;
for(int i = 0; i < coeff.size(); ++i)
{
//count only non-zero coeffients
if(coeff[i] != 0)
count++;
}
return count;
}
void polynomial::setCoeff(int deg, double c)
{
//if the deg is more than current degree, set all degrees between current and new degree to zero
if(deg > degree())
{
for(int i = degree() + 1 ; i <= deg; ++i)
coeff.push_back( 0);
}
coeff[deg] = c;
//if setting the coeff of term with highest power to 0, then the degree of the polynomial will reduce
//by 1, so pop out
if(c == 0 && deg == degree())
{
coeff.pop_back();
}
}
double polynomial::getCoeff(int deg)
{
if(deg<0 || deg > degree())
return 0;
return coeff[deg];
}
//evaluate the polynomial at x
double polynomial::operator()(double x)
{
double val = 0 , term;
for(int i = 0; i < coeff.size(); i++)
{
if(coeff[i] == 0)
continue;
term = 1;
for(int j = 1; j <= i; j++)
term *= x;
term *= coeff[i];
val += term;
}
return val;
}
polynomial polynomial::operator+(polynomial p)
{
polynomial res;
for( int i = 0; i <= degree() || i <= p.degree(); ++i)
{
res.setCoeff(i, getCoeff(i) + p.getCoeff(i)) ;
}
return res;
}
polynomial& polynomial::operator+=(polynomial p)
{
*this = *this + p;
return *this;
}
polynomial polynomial::operator-()
{
polynomial res;
for(int i = 0; i <= degree(); i++)
res.setCoeff(i , -getCoeff(i));
return res;
}
polynomial polynomial::operator-(polynomial p)
{
polynomial res;
for( int i = 0; i <= degree() || i <= p.degree(); ++i)
{
res.setCoeff(i, getCoeff(i) - p.getCoeff(i)) ;
}
return res;
}
polynomial& polynomial::operator-=(polynomial p)
{
*this = *this - p;
return *this;
}
polynomial polynomial::operator*(polynomial p)
{
polynomial res;
int m = degree(), n = p.degree();
double sum;
for(int i = 0; i <= m+n; i++)
{
sum = 0;
for(int j = 0; j <= i; j++)
{
sum+=getCoeff(j) * p.getCoeff(i-j);
}
res.setCoeff(i, sum);
}
return res;
}
polynomial& polynomial::operator*=(polynomial p)
{
*this = *this * p;
return *this;
}
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;
}
polynomial operator+(double c, polynomial p)
{
polynomial res;
for(int i = 0 ; i <= p.degree(); ++i)
res.setCoeff(i, p.getCoeff(i));
res.setCoeff(0, p.getCoeff(0)+c);
return res;
}
polynomial operator*(double c, polynomial p)
{
polynomial res;
for(int i = 0; i <= p.degree(); ++i)
{
res.setCoeff(i, p.getCoeff(i) * c);
}
return res;
}
/*
//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");
}
*/
}
output
+2.2x^3+1.2
-2.2x^3-1.2
+4.4x^3+2.4
+4.4x^3+2.4
+8.8x^3+4.8
5
2
x+1
x-1
1
1
0
0
0
0
2x
2
0
x^2-1
1
0
0
x^3+2x-1
-1
Program ended with exit code: 0
Note: If you would like to test the plotting as well... uncomment the following and check if it works on your system.
1. uncomment line 2 #include "cImg.h" in polynomial.cpp
2. uncomment the function plot() defined in the end of polynomial.cpp
3. uncomment the last line in main.cpp i.e pic10a::plot(p4*p4)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.