// Name: Fidelia Omondi // Class: CS 3305/W01 // Term: Fall 2021 // Instructor: Prof Perry // Assignment: 2 #include // Provides toupper #include // Provides EXIT_SUCCESS #include // Provides cout and...

need code in c++ in codelite. Output should be polynomials


// Name: Fidelia Omondi // Class: CS 3305/W01 // Term: Fall 2021 // Instructor: Prof Perry // Assignment: 2 #include // Provides toupper #include // Provides EXIT_SUCCESS #include // Provides cout and cin #include #include #include #include //using namespace std; #include "Polynomial.hpp" // Provides the polynomial class const unsigned int MANY = 3; // Number of polynomials allowed in this test program._ //polynomial :: MANY; //Constructors polynomial :: polynomial (double a, unsigned int exponent){ size = MANY; coef = new double[size]; current_degree = exponent; coef [current_degree] = a if(a == 0.0){ current_degree = 0; } //Copy Constructor polynomial :: polynomial(const polynomial& source){ coef = new double[source.size]; size = source.size; current_degree = source.current_degree; for(int i = 0; i= degree()) current_degree = exponent; int i = current_degree; while(i >= 0 && coef[i] == 0.0) { current_degree--; i--; } } void polynomial::clear() { current_degree = 0; for (int i=size; i>=0; i--) coef[i] = 0.0; } void polynomial::reserve(unsigned int number) { double *larger_array; if (number == size) return; if (number < degree())="" number="current_degree;" larger_array="new" double[number];="" current_degree="degree();" for="" (int="" i="0;" i=""><= number;="" i++)="" {="" larger_array[i]="coef[i];" if="" (i=""> degree()) larger_array[i] = 0.0; } delete [] coef; coef = larger_array; size = number; } // CONSTANT MEMBER FUNCTIONS double polynomial::coefficient(unsigned int exponent) const { if (exponent > size) return 0.0; else return coef[exponent]; } double polynomial::eval(double x) const { double answer = 0.0; for (unsigned int i=0; i<=size; i++)="" {="" double="" term="0.0;" double="" multiple="pow(x,i);" term="multiple" *="" coef[i];="" answer="" +="term;" }="" return="" answer;="" }="" unsigned="" int="" polynomial::next_term(unsigned="" int="" e)="" const="" {="" unsigned="" int="" next="0;" for="" (int="" i="e;"><=size; i++)="" if="" (coefficient(i+1)="" !="0)" {="" next="i+1;" break;="" }="" if="" (next="=" 0)="" return="" 0;="" else="" return="" next;="" }="" non-member="" binary="" operators="" polynomial="" operator+(const="" polynomial&="" p1,="" const="" polynomial&="" p2)="" {="" polynomial="" answer;="" if="" (p1.degree()=""> p2.degree()) { for (int i=0; i<=p1.degree(); i++)="" answer.assign_coef(p1.coefficient(i),="" i);="" for="" (int="" i="0;"><=p2.degree(); i++)="" answer.add_to_coef(p2.coefficient(i),="" i);="" for="" (int="" i="0;"><=p1.degree(); i++)="" answer.add_to_coef(0.0,="" i);="" }="" else="" if="" (p1.degree()="=" p2.degree())="" {="" for="" (int="" i="0;"><=p1.degree(); i++)="" {="" answer.assign_coef(p1.coefficient(i),="" i);="" answer.add_to_coef(p2.coefficient(i),="" i);="" }="" }="" else="" {="" for="" (int="" i="0;"><=p2.degree(); i++)="" answer.assign_coef(p2.coefficient(i),="" i);="" for="" (int="" i="0;"><=p1.degree(); i++)="" answer.add_to_coef(p1.coefficient(i),="" i);="" for="" (int="" i="0;"><=p2.degree(); i++)="" answer.add_to_coef(0.0,="" i);="" }="" return="" answer;="" }="" polynomial="" operator-(const="" polynomial&="" p1,="" const="" polynomial&="" p2)="" {="" polynomial="" answer;="" if="" (p1.degree()=""> p2.degree()) { for (int i=0; i<=p1.degree(); i++)="" answer.assign_coef(p1.coefficient(i),="" i);="" for="" (int="" i="0;"><=p2.degree(); i++)="" answer.add_to_coef((p2.coefficient(i)*-1),="" i);="" for="" (int="" i="0;"><=p1.degree(); i++)="" answer.add_to_coef(0.0,="" i);="" }="" else="" if="" (p1.degree()="=" p2.degree())="" {="" for="" (int="" i="0;"><=p1.degree(); i++)="" {="" answer.assign_coef(p1.coefficient(i),="" i);="" answer.add_to_coef((p2.coefficient(i)*-1),="" i);="" }="" }="" else="" {="" for="" (int="" i="0;"><=p2.degree(); i++)="" answer.assign_coef(p2.coefficient(i),="" i);="" for="" (int="" i="0;"><=p1.degree(); i++)="" answer.add_to_coef((p1.coefficient(i)*-1),="" i);="" for="" (int="" i="0;"><=p2.degree(); i++)="" answer.add_to_coef(0.0,="" i);="" }="" return="" answer;="" }="" char="" get_command()="" {="" char="" command;="" cout="">< "="">"; cin >> command; return (toupper(command)); } void view_poly(const polynomial& test) { // modify this function to view the polynomial in a meaningful way cout < "="" (your="" polynomial="" is="" :=""><>< ")"="">< endl;="" }="" void="" view_degree(const="" polynomial&="" test)="" {="" modify="" this="" function="" to="" view="" the="" polynomial="" degree="" cout="">< "="" (degree="" is="" "="">< test.degree()="">< ")"="">< endl;="" }="" size_t="" set_current()="" {="" size_t="" i;="" char="" command;="" do="" {="" cout="">< "polynomials="" ";="" for(i="0;" i="">< many;="" ++i)="" cout="">< char('a'="" +="" i)="">< '="" ';="" cout="">< "."="">< endl;="" cout="">< "enter="" the="" polynomial="" you="" want="" to="" work="" on:="" ";="" command="toupper(get_command());" }="" while((command="">< 'a')="" ||="" (command="">= char('A' + MANY))); return command - 'A'; } void test_add(polynomial& test) { double coefficient; unsigned int exponent; cout < "enter="" exponent:="" ";="" cin="">> exponent; cout < "enter="" coefficient:="" ";="" cin="">> coefficient; test.add_to_coef(coefficient, exponent); cout < "after="" adding:="" ";="" view_poly(test);="" }="" void="" test_assign(polynomial&="" test)="" {="" double="" coefficient;="" unsigned="" int="" exponent;="" cout="">< "enter="" exponent:="" ";="" cin="">> exponent; cout < "enter="" coefficient:="" ";="" cin="">> coefficient; test.assign_coef(coefficient, exponent); cout < "after="" assigning:="" ";="" view_poly(test);="" }="" void="" test_eval(const="" polynomial&="" test)="" {="" double="" x_value;="" cout="">< "enter="" the="" x="" value:="" ";="" cin="">> x_value; cout < "for="" the="" poly:="" ";="" view_poly(test);="" cout="">< "the="" evaluation="" returned="" is="" "="">< test(x_value)="">< endl;="" }="" void="" view_all(const="" polynomial="" p[])="" {="" size_t="" i;="" cout="">< endl;="" for(i="0;" i="">< many;="" ++i)="" {="" cout="">< char(i="" +="" 'a')="">< ":="" ";="" view_poly(p[i]);="" }="" }="" void="" test_clear(polynomial&="" test)="" {="" test.clear();="" cout="">< "after="" clearing:="" ";="" view_poly(test);="" }="" void="" test_next(const="" polynomial&="" test)="" {="" unsigned="" int="" exponent;="" cout="">< "enter="" exponent:="" ";="" cin="">> exponent; cout < "for="" polynomial:="" ";="" view_poly(test);="" cout="">< "next_term("="">< exponent="">< ")=" << test.next_term(exponent) << endl; } // Name: Fidelia Omondi // Class: CS 3305/W01 // Term: Fall 2021 // Instructor: Prof Perry // Assignment: 2 #ifndef POLYNOMIAL_HPP #define POLYNOMIAL_HPP #include // Provides size_t type #include // Provides istream and ostream #include // Provides size_t type #include // Provides istream and ostream namespace main_savitch_3 { class polynomial { public: // CONSTRUCTORS and DESTRUCTOR polynomial(); polynomial(double a0); polynomial(const polynomial& source); ~polynomial(); // MODIFICATION MEMBER FUNCTIONS void add_to_coef(double amount, unsigned int k); void assign_coef(double coefficient, unsigned int k); void clear(); void reserve(size_t number); // MODIFICATION OPERATORS polynomial& operator=(const polynomial& source); polynomial& operator=(double a0) // in line function { clear(); assign_coef(a0, 0); return *this; } // CONSTANT MEMBER FUNCTIONS double coefficient(unsigned int k) const; unsigned int degree() const; unsigned int next_term(unsigned int k) const; // EVALUATION MEMBER FUNCTIONS double eval(double x) const; double operator()(double x) const // in line function { return eval(x); } private: double* coef; size_t current_array_size; }; // NON-MEMBER BINARY OPERATORS polynomial operator+(const polynomial& p1, const polynomial& p2); polynomial operator-(const polynomial& p1, const polynomial& p2); } #endif // Name: Fidelia Omondi // Class: CS 3305/W01 // Term: Fall 2021 // Instructor: Prof Perry // Assignment: 2 #include #include #include " polynomial.hpp"="" using="" namespace="" std;="" using="" namespace="" main_savitch_3;="" const="" unsigned="" int="" many="3;" int="" main(int="" argc,="" char**="" argv)="" {="" polynomial="" p[many];="" size_t="" current_index="0;" char="" command;="" size_t="" i;="" cout="">< "polynomials="" ";="" for(i="0;" i="">< many;="" ++i)="" cout="">< char('a'="" +="" i)="">< '="" ';="" cout="">< "have="" all="" been="" initialized."="">< endl;="" do="" {="" print_menu();="" command="toupper(get_command());" switch(command)="" {="" case="" 's':="" current_index="set_current();" break;="" case="" '1':="" test_assign(p[current_index]);="" break;="" case="" '2':="" test_add(p[current_index]);="" break;="" case="" 'c':="" test_clear(p[current_index]);="" break;="" case="" 'd':="" view_degree(p[current_index]);="" break;="" case="" 'v':="" view_poly(p[current_index]);="" break;="" case="" 'a':="" view_all(p);="" break;="" case="" 'e':="" test_eval(p[current_index]);="" break;="" case="" 'n':="" test_next(p[current_index]);="" break;="" case="" '+':="" cout="">< "a="" +="" b:="" ";="" view_poly(p[0]="" +="" p[1]);="" break;="" case="" '-':="" cout="">< "a - b: "; view_poly(p[0] - p[1]); break; case 'q': // do nothing.. break; "a="" -="" b:="" ";="" view_poly(p[0]="" -="" p[1]);="" break;="" case="" 'q':="" do="" nothing..="">
Sep 11, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here