Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

jo p please follow this code #include #include #include using namespace std; class Term { private: int coef; int exp; public: Term(int c = 0,

joimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedp

please follow this code

#include

#include

#include

using namespace std;

class Term

{

private:

int coef;

int exp;

public:

Term(int c = 0, int e = 0); // given

string toString() const; // given

};

class Polynomial

{

private:

public:

};

//--------------------------------------------------------------------------------------------

// Class Term Definition

//--------------------------------------------------------------------------------------------

Term::Term(int c, int e) : coef(c), exp(e) {}

// The code for the method toString() is given

string Term::toString() const

{

if (coef == 0)

return "";

if (exp == 0)

return to_string(coef);

string strTerm;

switch (coef)

{

case -1:

strTerm = "-x";

break;

case 1:

strTerm = "x";

break;

default:

strTerm = to_string(coef) + "x"; //the rest, is of form, eg. 3x^2

}

if (exp != 1)

strTerm += "^" + to_string(exp);

return strTerm;

}

//--------------------------------------------------------------------------------------------

// Class Polynomial Definition

//--------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------

// Main Function

//--------------------------------------------------------------------------------------------

int main()

{

cout

cout

cout

cout

cout

cout

cout

cout

// The code for getting the x values from user input is given

int n;

int x[10];

cout

cout

cout ";

cin >> n;

cout ";

for (int i=0; i

cin >> x[i];

cout

cout

cout

cout

system("pause");

return 0;

}

please use OOP C++

Question In this exercise you will be implementing the concept of composition to model polynomials. In mathematics, a polynomial is a function composed of unit expressions called terms. A polynomial can have one or more variables. However, the scope for this exercise is limited only to one or single variable polynomials. The following is an example of a single-variable polynomial. 512 - 2x + 7 Each term of a polynomial contains a coefficient and an exponent. The polynomial from the above example has three terms and their coeffiecient and exponent are shown in Table 1. Term Table 1 Coeffiecient 5 Exponent 2 5x2 -2x -2 1 7 7 0 Then, a single-variable polynomial can be modeled with two classes as shown in Figure 1 and their descriptions are given in Table 2. Polynomial Polynomial (...) read() evaluate (x) term(e) largestTerm (0) constant() degree() toString() Term coef exp Term (...) set (...) coefficient () exponent() evaluate (x) toString() Figure 1 Table 2 Description Class Members (attributes / methods) class Term coef and exp The attributes for the terms's coefficient and exponent, respectively. Tips:The declaration for these attributes have been provided in the template program Term() The constructor (s) such as overloaded, default constructor, etc. Tips:The code for the constructor has been provided in the template program sets the term's attributes, coef and exp respectively. set (c,e) coefficient() and exponent() return the term's attributes, coef and exp respectively. evaluate (x) evaluates the term with the value of x. For example, if x= 2, then the term 5r will evaluate to 5(22) = 20, and the term -2x will evaluate to -2(2)=-4. Tips: Use the math function, pow() to implement this method. Returns a string representing the term. For example, if the term is 31+ (i.e. coef=3 and exp=4), this method will retum a string of "3x^4". Notes: the character - represents to the power of". Tips:The code for this method has been provided in the template program. toString() class Polynomial Attributes Polynomial() read() evaluate (x) Determine the attributes for this class on your own. The constructor (s) such as overloaded, default constructor, etc. sets the terms of a polynomial from user input. The user needs to enter the coefficient and exponent for each term evaluates the polynomial by summing up all the terms based on the value ofx. For example, if x=2, then the polynomial 5x2 - 2x + 7 will evaluate to 20-4+ 7 = 23. Tips: this method should make use of the Term's evaluate () method. returns the term whose exponent e. If the polynomial does not have a term with the exponent e. then this method returns a zero term. i.e., a tem with the coefficient and exponent set to 0. Notes: A zero term can be created by the default constructor of the class Term. term(e) largestTerm () returns the term whose the largest exponent. For example, the largest term in the polynomial 5x2 - 2x + 7 is 5r? 5 + x isx", x2 - 4x3 + x is - 4x and so on. returns the constant value of a polynomial. For example, the constant of the constant (0) 2030, matomus Exercise-SECS1023 5 degree () polynomial 5x-2x+7 is 7. N-5 is -5, 9x is 0 and so on. Tips: this method should make use of the method term(). returns the degree of a polynomial by taking the largest exponent. For example, the degree of the polynomial 522 - 2x + 7 is 2, 3 - 5 is 3. 9x is 1 and so on. Tips: this method should make use of the method largestTerm(). Retums a string representing the polynomial. For example, if the polynomial is 5x2 - 2x + 7 then this method will return a string of "5x^2-2x+7". Tips: this method should make use of the Term's toString() method. toString() Based on the classes above, complete the program exercise2.cpp: Notes: Use only a single source code file for this exercise. However, the class declaration and definition must be separated. 1. Implement the class Term. Do not add any additional members for this class. 2. Implement the class Polynomial. Add all required attributes. Do not add additional members. 3. In the main function write the code to a. create a Polynomial object and add terms to the polynomial using user inputs. b. print the information about the polynomial including: o the equation o the degree o constant term o the largest term c. evaluate the polynomial with several values of x entered by the user and print the results onto the screen. See Figure 2 for example runs of the program Output: Expected result from the program is as shown in Figure 2. Bold text indicates keyboard inputs. Run 1 - user enters the polynomial 512 - 2x + 7 Enter a polynomial: How many terms ? => 3 Enter term #1 (coef and exp) => 5 Enter term #2 (coet and exp) = -2 Enter term #3 (coef and exp] => 7 2 1 0 Polynomial Information Equation : f(x) = 5x2-2x+7 Degree Constant . 7 Largest Term : 5x2 Polynomial Evaluation Enter how many x values => 3 Enter the x values => 5 0 1 x f(x) --- 5 0 1 122 7 10 Run 2 - user enters the polynomial -51% +2 +1-7x Enter a polynomial: How many terms ? => 4 Enter term #1 (coef and exp) => -5 2 Enter term #2 (coef and exp) => 1 3 Enter term #3 (coef and exp) => 1 0 Enter term #4 (coef and exp) => -7 1 2 Polynomial Information Equation : F(x) = -5x^2+x^3+1-7X Degree 3 Constant 1 Largest Term : X3 Polynomial Evaluation . . Enter how many x values => 4 Enter the x values => -1 0 1 2 -1 2 1 -10 -25 Run 3 - user enters the polynomial 3r? Enter a polynomial: How many terms? => 1 Enter term #1 (coef and exp) => 3 2 Polynomial Information f(x) = 3x^2 Equation : Degree Constant : Largest Term: 0 3x^2 Polynomial Evaluation Enter how many x values => 5 Enter the x values => 5 4 3 2 1 75 5 4 3 CO 27 12 3 1 Run 4 - user enters the polynomial - 10 Enter a polynomial: How many terms? => 2 Enter term #1 (coef and exp) => 1 1 Enter term #2 (coef and exp) => -10 0 Polynomial Information Equation f(x) = x-10 Degree 1 Constant -10 Largest Term : : : : x Polynomial Evaluation Enter how many x values => 3 Enter the x values => -10 0 X 10 -10 0 10 -20 -10 0 Figure 2<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions