| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 10573 | Polynomial |
|
Description
Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent, e.g., the term 2x4 has the coefficient 2 and the exponent 4.
Develop a complete class containing proper constructor functions. The class should also provide the following overloaded operator capabilities:
- Overload the addition operator (+) to add two polynomials.
- Overload the subtraction operator (-) to subtract two polynomials.
- Overload the multiplication operator (*) to multiply two polynomials.
- Overload the stream insertion operator (<<).
Note:
1. This problem involves three files.
- function.h: Class definition of Polynomial.
- function.cpp: Member-function definitions of Polynomial.
- main.cpp: A driver program to test your class implementation.
You will be provided with main.cpp, and asked to implement function.h and function.cpp.
function.h
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
class Polynomial
{
/*Overloading the stream insertion operator (<<)*/
public:
Polynomial();
Polynomial(const int &, const int [51]);
/*Overloading the addition operator (+):
Polynomial yourFunctionName (const Polynomial &) const;*/
/*Overloading the subtraction operator (-):
Polynomial yourFunctionName (const Polynomial &) const;*/
/*Overloading the multiplication operator (*):
Polynomial yourFunctionName (const Polynomial &) const;*/
private:
int coefficients[101];
int greatestPower;
}; // end class Polynomial
#endif
main.cpp
#include <iostream>
#include <string>
#include "function.h"
using namespace std;
int main()
{
int greatestPower1, greatestPower2;
int coefficient1[51], coefficient2[51];
Polynomial ans;
cin>>greatestPower1;
for(int i = greatestPower1 ; i >= 0; i--)
{
cin>>coefficient1[i];
}
cin>>greatestPower2;
for(int i = greatestPower2 ; i >= 0 ; i--)
{
cin>>coefficient2[i];
}
Polynomial a(greatestPower1, coefficient1), b(greatestPower2, coefficient2);
ans = a + b;
cout<<ans<<endl;
ans = a - b;
cout<<ans<<endl;
ans = a * b;
cout<<ans<<endl;
} // end main
2. For OJ submission:
Step 1. Submit only your function.cpp into the submission block. (***Note that you don’t need to submit your function.h.)
Step 2. Check the results and debug your program if necessary.
Input
There are four lines.
The first two lines represent the greatest power and the corresponding coefficients of the first polynomial.
The last two lines represent the greatest power and the corresponding coefficients of the second polynomial.
Note that the coefficients are in descending order and each element is separated by a blank.
Output
Your program should print the coefficients of the sum, difference and product of these two polynomials in descending order, respectively.
Note that every answer should be followed by a new line character.