main.cpp polynomial.cpp polynomial.h http://www.math.ucla.edu/~eryu/courses/pic1
ID: 3846120 • 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
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 << 2 * p1 << endl;
cout << p1 * 2 << endl; //why does this work?
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 << "Begin arithmetic block" << endl;
cout << p2 + p3 << endl;
cout << p2 - p3 << endl;
cout << p2 - p2 << endl;
cout << p2 * p3 << endl;
cout << "End arithmetic block" << 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 = x + (-0.166666666666)*x*x*x + (0.008333333)*x*x*x*x*x + (-0.00019841269)*x*x*x*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>
#include <cassert>
using namespace pic10a;
polynomial::polynomial(double c)
{
polynomial::coeffs = std::vector<double>(1, 0);
polynomial::coeffs[0] = c;
}
int polynomial::degree()
{
return polynomial::mDegree;
}
int polynomial::nonzeroTerms()
{
int termCounter = 0;
for(int i = 0; i <= polynomial::mDegree; i++)
{
if(polynomial::coeffs[i] == 0.0D)
{
termCounter++;
}
}
return termCounter;
}
void polynomial::setCoeff(int deg, double c)
{
assert(deg >= 0);
if(deg > mDegree)
{
mDegree = deg;
}
coeffs.resize(mDegree + 1);
coeffs[deg] = c;
mDegree = 0;
for(int i = 0; i < coeffs.size(); i++)
{
if(coeffs[i] != 0.0D)
{
mDegree = i;
}
}
}
double polynomial::getCoeff(int deg)
{
assert(deg >= 0);
if(deg <= polynomial::mDegree)
{
return polynomial::coeffs[deg];
}
return 0.0;
}
double polynomial::operator()(double x)
{
double accumulator = 0;
for(int i = 0; i <= polynomial::mDegree; i++)
{
double powAccumulator = 1;
for(int j = 0; j < i; j++)
{
powAccumulator *= x;
}
accumulator += polynomial::coeffs[i] * powAccumulator;
}
return accumulator;
}
polynomial polynomial::operator+(polynomial p)
{
polynomial out;
for(int i = 0; i <= degree() || i <= p.degree(); i++)
{
out.setCoeff(i, getCoeff(i) + p.getCoeff(i));
}
return out;
}
polynomial polynomial::operator-()
{
polynomial out;
for(int i = 0; i <= degree(); i++)
{
out.setCoeff(i, -getCoeff(i));
}
return out;
}
polynomial polynomial::operator-(polynomial p)
{
return *this + (-p);
}
polynomial polynomial::operator*(polynomial p)
{
polynomial out;
for(int i = 0; i <= degree() + p.degree(); i++)
{
double c = 0;
for(int j = 0; j <= i; j++)
{
c += getCoeff(j) * p.getCoeff(i - j);
}
out.setCoeff(i, c);
}
return out;
}
polynomial& polynomial::operator+=(polynomial p)
{
polynomial out;
for(int i = 0; i <= degree() || i <= p.degree(); i++)
{
out.setCoeff(i, getCoeff(i) + p.getCoeff(i));
}
return *this;
}
polynomial& polynomial::operator-=(polynomial p)
{
return *this += -p;
}
polynomial& polynomial::operator*=(polynomial p)
{
for(int i = 0; i <= degree() + p.degree(); i++)
{
double c;
for(int j = 0; j <= i; i++)
{
c += getCoeff(j) * p.getCoeff(i - j);
}
setCoeff(i, c);
}
return *this;
}
polynomial& polynomial::operator*=(double c)
{
for(int i = 0; i <= degree(); i++)
{
setCoeff(i, getCoeff(i) * c);
}
return *this;
}
polynomial& polynomial::operator+=(double c)
{
setCoeff(0, getCoeff(0) + c);
return *this;
}
polynomial pic10a::operator+(double c, polynomial p)
{
polynomial temp(c);
return p + temp;
}
polynomial pic10a::operator*(double c, polynomial p)
{
polynomial out;
for(int i = 0; i <= p.degree(); i++)
{
out.setCoeff(i, c * p.getCoeff(i));
}
return out;
}
polynomial pic10a::operator*(polynomial p, double c)
{
return c * p;
}
//End my funcs
//
//
//
//
//
//End my funcs
std::ostream& pic10a::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 pic10a::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
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
#include <vector>
namespace pic10a
{
class polynomial {
private:
//coefficient of term a*x^n is represented by v[n]
unsigned int mDegree = 0;
std::vector<double> coeffs;
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& operator*=(double c);
};
polynomial operator+(double c, polynomial p);
polynomial operator*(double c, polynomial p);
polynomial operator*(polynomial p, double c);
std::ostream& operator<<(std::ostream& s, polynomial p);
void plot(polynomial p);
}
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.