In: Computer Science
C++
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
#ifndef MONOMIAL_H
#define MONOMIAL_H
class Monomial
{
private:
float coefficient;
int power;
public:
Monomial(float coefficient=1.0, int power=1);
float GetCoefficient() const;
int GetPower() const;
void SetPower(int power);
void Add(const Monomial& obj);
void Multiply(float coeff);
void Multiply(const Monomial& obj);
void Exponent(int power);
void Print() const;
};
#endif
// end of Monomial.h
// Monomial.cpp
#include <iostream>
#include "Monomial.h"
using namespace std;
// constructor to initialize coefficient and power
Monomial::Monomial(float coefficient, int power) :
coefficient(coefficient), power(power)
{
if(this->power < 1) // power is not greater than 0, update
power to 1
this->power = 1;
}
// function to return coefficient
float Monomial:: GetCoefficient() const
{
return coefficient;
}
// function to return the power
int Monomial:: GetPower() const
{
return power;
}
// function to update power
void Monomial:: SetPower(int power)
{
if(power >= 1) // input power >= 1, update power
this->power = power;
else // else set power to 1
this->power = 1;
}
// function to add monomials
void Monomial:: Add(const Monomial& obj)
{
if(power == obj.power) // if powers are equal, add coefficient of
obj to this coefficient
coefficient += obj.coefficient;
else // display error message
cout<<"Cannot add monomials with different
powers"<<endl;
}
// function to multiply Monomial with given coeff
void Monomial:: Multiply(float coeff)
{
coefficient *= coeff; // multiply coefficient with coeff
}
// function to multiply Monomial with given obj
void Monomial:: Multiply(const Monomial& obj)
{
coefficient *= obj.coefficient; // multiply coefficients
power += obj.power; // add the powers
}
// function to raise Monomial to power
void Monomial:: Exponent(int power)
{
if(power >= 1) // input power is positive
{
float coeff = 1;
// update the coefficient
for(int i=0;i<power;i++)
coeff *= coefficient;
coefficient = coeff;
// multiply powers
this->power *= power;
}else
cout<<"Can raise only to a positive power"<<endl;
}
// function to display Monomial
void Monomial:: Print() const
{
cout<<coefficient<<"x^"<<power<<endl;
}
//end of Monomial.cpp
//
// 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^3 = ";
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^3 * -3x^6 = ";
m3.Print();
// can raise monomial to the power
cout << "Monomial exponentiation" << endl;
m4.Exponent(3);
cout << "(-3x^6)^3 = "; // -27x^18
m4.Print();
cout << "(3x)^0 = "; // -27x^18
m1.Exponent(-10);
return 0;
}
// end of driver.cpp
Output: