Question
Assignment Write a program that will prompt the user for two polynomials and then display information about each one. A valid polynomial will have 1)
Assignment Write a program that will prompt the user for two polynomials and then display information about each one. A valid polynomial will have 1) At least one term, 2) Terms entered in descending order by exponent value, and 3) No terms with zero coefficient (except for the polynomial with value zero). Next, the program will list the terms needed to multiply the two polynomials. The user is then allowed to evaluate the first polynomial entered with any value for variable x. Finally, the user is able to scale the first polynomial with a scale factor. Write your solution in the provided template file. This template provides definitions for two Classes (Term and Poly) and an implementation for the main function. You will *NOT* receive credit for this assignment if you: 1) Change (i.e. add, delete, or 2 modify) anything in the definition of these two classes, 2) Change any code in the main function, and 3) Change the definition of a non-member function. All function headers have been provided for you. You will replace the comment // Replace with your code in each function with your solution. Study the data members that are defined in the classes Term and Poly in the provided template file. Each object of the Term class holds a value pair (coefficient, exponent) and provides operations on the pair. Each object of the Poly class holds a single data member, terms, which is a list of objects of the Term class to represent the polynomial. Run the provided solution, poly solution.exe, to understand the main function. It is highly recommended that you write your code incrementally, meaning function by function. You should compile your code often, e.g. after you have implemented a function, test it as soon as possible before writing the next function.
1. Write a comment for eah function explaining what it does.
2. Write a comment for each function parameter explaining the value it represents. Write the member functions for the Term and Poly classes using the desciptions below. Pay attention to the input parameters (the use of pass-by-value, pass-by-reference, and const) and the return type for each function. The class Term has two instance variables, coeff and exp. Write the member functions for the class Term:
3. The function getCoeff returns the coefficient of a term (an object of the Term class).
4. The function getExp returns the exponent of a term.
5. The function setCoeff updates the coefficient of a term. Assume that this function will be called with an input parameter value of zero only if the exponent for the term has been set to zero. Note, this function will be called in the functions multiply and derivative in the Term class and the functions addTerm and scale in the Poly class.
6. The function setExp updates the exponent of a term. Assume that this function will only be called with a positive or zero input parameter. Note, this function will be called in the functions multiply and derivative in the Term class and the functions addTerm and scale in the Poly class.
7. The function eval returns the evaluation of the term given an input value x passed into the function. Note, this function will be called in the function eval in the Poly class. 3
8. The function derivative returns a new term that is the derivative of the term the function is called on. Note, this function will be called in the function derivative in the Poly class. Also note, this function must call the functions setCoeff and setExp.
9. The function multiply returns a new term that is the multiplication of the term that the function is called on and the term passed in as an input parameter. Assume that the input parameter will be a valid term, i.e. the value zero or a term with a non-zero coefficient. Note, this function will be called in the function displayMultiply in the Poly class. Also note, this function must call the functions setCoeff and setExp.
10. The member function displayFirst displays a term to the screen formatted as the first term in a polynomial. If the coefficient is a negative value then it is displayed with a negation sign. If the exponent is one then the one is not displayed. If the exponent is zero then only the coefficient is displayed. Note, this function will be called in the functions display and displayMultiply in the Poly class.
11. The member function displayNext displays a term to the screen formatted as a term after the first term in a polynomial. If the coefficient is a negative value then a minus sign surrounded by spaces is displayed before the term. If the coefficient is a positive value then a plus sign surrounded by spaces is displayed before the term. If the exponent is one then the exponent is not displayed. If the exponent is zero then only the coefficient is displayed. Note, this function will be called in the functions display and displayMultiply in the Poly class. The class Poly has one instance variable, terms, that contains a list of terms in decreasing order by exponent values to represent a polynomial. Write the member functions for the class Poly:
12. The function getTerm returns the term in the instance variable terms located at the index specified in the input parameter. E.g., if the list terms has the following terms {(3, 8), (2, 3), (7, 2), (10, 1), (1, 0)}, then passing in zero as the index returns the term (3, 8), passing in one as the index will return the term (2, 3), etc. Assume that this function will only be called with an input index between 0 and the size of list terms - 1. Note, this function will be called in the Class Poly member function displayMultiply.
13. The function degree returns the largest exponent in the list, i.e. the exponent of the first term in the list terms. Note, this function will be called in the function display stats. Also note, this function calls the function getExp.
14. The function termCount returns the number of terms in the polynomial. Note, this function will be called in the functions main, display stats, and displayMultiply. 4
15. The function addTerm adds a term to the list terms given the terms coefficient and exponent. Assume that the function will not be called with a coefficient of zero except for the value pair (0, 0). Also assume that the function will not be passed a negative exponent value. This function will be called by the functions read poly and derivative.
16. The function scale changes the polynomial by multiplying each term with a scale factor that is passed into the function. If the scale factor is zero then the resulting polynomial will be the value zero, i.e. a single term (0, 0). If the scale factor is one then the polynomial will not change. Otherwise, the polynomial will change but will have the same number of terms. Note, this function will be called in the main function. Also note, this function must call the functions setCoeff and setExp.
17. The function eval evaluates the polynomial given an input value for variable x. Note, this function will be called in the function evaluate poly. Also note, you must call the eval function in the Term class.
18. The function derivative returns a polynomial that is the derivative of the polyonmial it is called on. Note, this function will be called in the function display stats. Also note, you must call the functions derivative, setCoeff, and setExp in the Term class and function addTerm in the Poly class.
19. The function display prints a label (passed to the function) and then prints the polynomial to the screen in readable format. Note, this function will be called in the function display stats. Also note, you must call the functions displayFirst and displayNext.
20. The function displayMultiply prints a label (passed to the function) and then prints the terms needed to calculate a multiplication of the polynomial the function is called on, call this polynomial A, and the polynomial passed in as an input parameter, call this polynomial B. The terms are computed by multiplying the first term in polynomial A with each of the terms in polynomial B, then multiplying the second term in polynomial A with each of the terms in polynomial B, etc. Note, this function will be called in the main function. Also note, you must call the functions termCount, getTerm, displayFirst, and displayNext. Write the following functions that are not member functions of the Term and Poly classes :
21. The function display banner that prints the welcome banner.
22. The function read poly prompts and then reads a polynomial from the user one term per line. The function has one integer input parameter to indicate which polynomial (1 or 2) the user will enter. The user will enter each term by typing the coefficient and 5 then the exponent. Terms must be entered in descending order by exponent value. For example, the user will enter polynomial 3x 8 2x 3 + 7x 2 + 10x 1 1x 0 (whose list of terms is {(3, 8), (-2, 3), (7, 2), (10, 1), (-1, 0)}) as the first polynomial in the following way: Enter poly #1: 3 8 -2 3 7 2 10 1 -1 0 To validate that the user has entered a valid polynomial, your function must implement the following rules. The exponent of the last term entered must be zero, which is a sentinal value that indicates the end of user input. To enter a polynomial that has at least one term with a positive exponent and the last term does not have a zero exponent, the user will enter (0, 0) as the last term. Though, the term (0, 0) will not be stored in the polynomial. If the user enters the term (0, 0) as the first term, then the polynomial is the value zero and only a single term (0, 0) is stored in the polynomial. A term entered with a zero coefficient should be ignored by your program unless it is the term (0, 0). A term entered with a negative exponent results in message Invalid Term. Bye! printed to the screen and your program exiting with the command exit(EXIT FAILURE), which is defined in the cstdlib library and already included for you. In addition, your program should exit the same way if a term is entered with an exponent that is greater than or equal to the last term entered. Here are examples of your functions behavior on different user input: (a) Input: {(0, 0)} results in a VALID polynomial where the list terms is {(0, 0)}, i.e. 0. (b) Input: {(0, 4), (0, 0)} results in a VALID polynomial where the list terms is {(0, 0)}, i.e. 0. Term (0, 4) is ignored. (c) Input: {(0, 4), (0, 3), (0, 0)} results in a VALID polynomial where the list terms is {(0, 0)}, i.e. 0. Both terms (0, 4) and (0, 3) are ignored. (d) Input: {(5, 0)} results in a VALID polynomial where the list terms is {(5, 0)}, i.e. 5. (e) Input: {(-1, 3)} results in a VALID polynomial where the list terms is {(-1, 3)}, i.e. 1x 3 . (f) Input: {(0, 4), (0, 3), (5, 0)} results in a VALID polynomial where the list terms is {(5, 0)}, i.e. 5. Both terms (0, 4) and (0, 3) are ignored. 6 (g) Input: {(1, 4), (0, 0)} results in a VALID polynomial where the list terms is {(1, 4)}, i.e. x 4 . (h) Input: {(1, 4), (-2, 2), (0, 0)} results in a VALID polynomial where the list terms is {(1, 4), (-2, 2)}, i.e. x 4 2x 2 . (i) Input: {(1, 4), (-2, 2), (5, 0)} results in a VALID polynomial where the list terms is {(1, 4), (-2, 2), (5, 0)}, i.e. x 4 2x 2 + 5. (j) Input: {(1, 4), (-2, 2), (0, 5), (5, 0)} results in a VALID polynomial where the list terms is {(1, 4), (-2, 2), (5, 0)}, i.e. x 4 2x 2 + 5. Term (0, 5) is ignored. (k) Input: {(1, 4), (2, -2)} results in an INVALID polynomial. You program will print Invalid Term. Bye! and exit. The term (2, -2) is invalid. (l) Input: {(1, 4), (2, 6)} results in an INVALID polynomial. Your program will print Invalid Term. Bye! and exit. The term (2, 6) is invalid.
23. The function display stats displays information for a polynomial including the polynomial in readable format, its degree, number of terms, and derivative. Note, the function will call the functions display, degree, termCount and derivative in the Poly class.Also note, the function is called from the main function.
24. The function evaluate poly prompts the user to enter how many values to evaluate with the first polynomial. The function will prompt the user again if a negative value is entered. When a valid value is entered, the user is prompted for values of the variable x and displays the evaluation of the polynomial for each value. Note, this function will call the eval function in the Poly class.
25. The function goodbye banner prints a goodbye message.
26. Test your code thoroughly. You must test your code with different polynomials as input. Test your code with a polynomial with value zero, polynomials with a single constant term, and higher degree polynomials.
27. Be sure to modify the header comments File, Created by, Creation Date, and Synopsis at the top of the file. Each synopsis should contain a brief description of what the program does.
28. Be sure that there is a comment documenting each variable.
29. Be sure that your if statements, for and while loops and blocks are properly indented.
30. Check your output against the output from the solution executable provided.
Template:
#include
#include
#include
#include
#include
using namespace std;
class Term
{
private:
int coeff;
int exp;
public:
int getCoeff() const;
int getExp() const;
void setCoeff(const int coefficient);
void setExp(const int exponent);
double eval(const double x) const;
Term derivative() const;
Term multiply(const Term & term) const;
void displayFirst() const;
void displayNext() const;
};
class Poly
{
private:
vector
public:
// member functions
Term getTerm(const int index) const;
int degree() const; // highest degree
int termCount() const; // number of terms
void addTerm(const int coeff, const int exp);
void scale(const int fact);
double eval(const double x) const; // evaluate polynomial with value x
Poly derivative() const; // derivative
void display(const string & label) const;
void displayMultiply(const Poly & poly, const string & label) const;
};
void display_banner();
Poly read_poly(const int label);
void display_stats(const Poly & poly, const string & label);
void evaluate_poly(const Poly & poly);
void goodbye_banner();
int main()
{
Poly poly1, poly2;
int scale;
display_banner();
poly1 = read_poly(1);
cout << endl;
display_stats(poly1, "You entered polynomial #1");
poly2 = read_poly(2);
cout << endl;
display_stats(poly2, "You entered polynomial #2");
cout << "Multiplication terms - " << endl << endl;;
poly1.displayMultiply(poly2, "Poly #1 * Poly #2");
poly2.displayMultiply(poly1, "Poly #2 * Poly #1");
cout << endl;
evaluate_poly(poly1);
cout << endl;
if (poly1.termCount() > 0) {
cout << "Enter a scale factor to apply to polynomial #1: ";
cin >> scale;
poly1.scale(scale);
display_stats(poly1, "The polynomial after scaling");
}
goodbye_banner();
return 0;
}
// CLASS TERM MEMBER FUNCTIONS
int Term::getCoeff() const
{
// Replace with your code
}
int Term::getExp() const
{
// Replace with your code
}
void Term::setCoeff(const int coefficient)
{
// Replace with your code
}
void Term::setExp(const int exponent)
{
// Replace with your code
}
double Term::eval(const double x) const
{
// Replace with your code
}
Term Term::derivative() const
{
// Replace with your code
}
Term Term::multiply(const Term & term) const
{
// Replace with your code
}
void Term::displayFirst() const
{
// Replace with your code
}
void Term::displayNext() const
{
// Replace with your code
}
// CLASS POLY MEMBER FUNCTIONS
Term Poly::getTerm(const int index) const
{
// Replace with your code
}
int Poly::degree() const
{
// Replace with your code
}
int Poly::termCount() const
{
// Replace with your code
}
void Poly::addTerm(const int coeff, const int exp)
{
// Replace with your code
}
void Poly::scale(const int fact)
{
// Replace with your code
}
double Poly::eval(const double x) const
{
// Replace with your code
}
Poly Poly::derivative() const
{
// Replace with your code
}
void Poly::display(const string & label) const
{
// Replace with your code
}
void Poly::displayMultiply(const Poly & poly, const string & label) const
{
// Replace with your code
}
// NON-MEMBER FUNCTIONS
void display_banner() {
// Replace with your code
}
Poly read_poly(const int label)
{
// Replace with your code
}
void display_stats(const Poly & poly, const string & label)
{
// Replace with your code
}
void evaluate_poly(const Poly & poly)
{
// Replace with your code
}
void goodbye_banner()
{
// Replace with your code
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started