In: Computer Science
CPP Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that will allow creation and handling of univariate monomials, as described below.
Monomial Description
Each monomial object has following properties:
Class Details
The single constructor for the Monomial class should have 2 parameters: an floating point coefficient value (optional, with a default value of 1.0); and an integer power value (optional, with a default value of 1). If the power value is less then 1, set the power to 1. The class will need to provide internal storage for any member data that must be kept track of.
There should be member functions GetCoefficient, and GetPower, which will return the monomial’s coefficient value, and monomial’s power value, respectively. The GetPower method should return integer results. The GetCoefficient function should return its result as a float.
There should be member functions SetPower, which will set the monomial’s power to the arbitrary positive value. If the parameter is not positive then set the power to the default value 1.
There should be member function Add, which adds the one monomial to the other. The parameter of this member function need to be a constant reference to the other monomial object. The monomials can only be added if they have the same power, otherwise error message must be printed. The result of the addition is saved in the caller object.
There should be two member functions Multiply (overrides), which each allow to multiply a monomial to a number and to another monomial. The first member function need to accept floating point parameter that is used to perform multiplication of the real number on the the monomial object. The second member function need to accept a constant reference to the other monomial object. Use monomials to coefficients and powers to perform their multiplication. The result of the multiplication is saved in the caller object.
There should be a member function called Exponent that perform exponentiation (raise to power) of the monomial to the specified power value. You only can raise monomial to the positive power, if the power parameter is not positive print error message. The result of the exponentiation is saved in the caller object.
A sample driver program (called monomial-driver.cpp) is provided. It uses objects of type Monomial and illustrates sample usage of the member functions.
Your class declaration and definition files must work with my main program from the test driver, as-is (do not change my program to make your code work!). You are encouraged to write your own driver routines to further test the functionality of your class, as well. Most questions about the required behavior of the class can be determined by carefully examining my driver program and the sample execution. Keep in mind, this is just a sample. Your class must meet the requirements listed above in the specification - not just satisfy this driver program. (For instance, I haven’t tested every illegal fill character in this driver program - I’ve just shown a sample). Your class will be tested with a larger set of calls than this driver program represents.
General Requirements
Test Driver:
// // driver.cpp -- driver program to demonstrate the behavior of // the Monomial class #include <iostream> #include "monomial.h" using namespace std; int main() { // create some monomial: x, 2x, 12.5x³, -3xâ¶ Monomial m1, m2( 2.0 ), m3( 12.5, -1 ), m4( -3.0 , 6); // display monomial cout << "Monomial `m1` has the coefficient " << m1.GetCoefficient() << " and power " << m1.GetPower() << ": "; m1.Print(); cout << "Monomial `m2` has the coefficient " << m2.GetCoefficient() << " and power " << m2.GetPower() << ": "; m2.Print(); cout << "Monomial `m3` has the coefficient " << m3.GetCoefficient() << " and power " << m3.GetPower() << ": "; m3.Print(); cout << "Monomial `m4` has the coefficient " << m4.GetCoefficient() << " and power " << m4.GetPower() << ": "; m4.Print(); // Change monomial power cout << "Monomial `m3` power wasn't changed: "; m3.SetPower(-1); m3.Print(); cout << "Monomial `m3` power was changed: "; m3.SetPower(3); m3.Print(); // can add monomials with the same powers cout << "Monomial addition" << endl; m1.Add(m2); cout << "x + 2x = "; m1.Print(); // cannot add monomials with different powers cout << "x³ + 12.5x³ = "; m2.Add(m3); // can multiply monomial to a number cout << "Monomial multiplication by a number" << endl; m2.Multiply(2.5); cout << "2x * 2.5 = "; m2.Print(); // can multiply monomials cout << "Monomial multiplication by a monomial" << endl; m3.Multiply(m4); cout << "12.5x³ * -3xâ¶ = "; m3.Print(); // can raise monomial to the power cout << "Monomial exponentiation" << endl; m4.Exponent(3); cout << "(-3xâ¶)³ = "; // -27x^18 m4.Print(); cout << "(3x)â° = "; // -27x^18 m1.Exponent(-10); return 0; }
Test Driver Output
Monomial `m1` has the coefficient 1 and power 1: 1x^1 Monomial `m2` has the coefficient 2 and power 1: 2x^1 Monomial `m3` has the coefficient 12.5 and power 1: 12.5x^1 Monomial `m4` has the coefficient -3 and power 6: -3x^6 Monomial `m3` power wasn't changed: 12.5x^1 Monomial `m3` power was changed: 12.5x^3 Monomial addition x + 2x = 3x^1 x³ + 12.5x³ = Cannot add monomials with different powers Monomial multiplication by a number 2x * 2.5 = 5x^1 Monomial multiplication by a monomial 12.5x³ * -3x⁶ = -37.5x^9 Monomial exponentiation (-3x⁶)³ = -27x^18 (3x)⁰ = Can raise only to a positive power
monomial.h - Declaration file
monomial.cpp - Definition file
1.
2.
3.
The monomial.h and monomial.cpp was run via monomial-driver.cpp and passed all tests successfully.
Here are the results (given code for monomial-driver.cpp above was copied as is) :
P.S - The file monomial.cpp is broken into three parts just for the sake of uploading clear images, but they represent one single file.