Question: I couldn't print out the entire code below. I think there are some error in the MAIN function or maybe in Class Polynomial. Please double
I couldn't print out the entire code below. I think there are some error in the MAIN function or maybe in Class Polynomial. Please double check and fix all errors from the entire code below. Make sure we can run the code to have the output as the pictures below.
The output should be like this please:

#include
#include
using namespace std;
class Polynomial {
private:
int degree;
double* coefficients;
public:
Polynomial(int n) {
degree = n;
coefficients = new double[degree + 1];
for (int i = 0; i
coefficients[i] = 0;
}
}
Polynomial(double* coeffs, int n) {
degree = n;
coefficients = new double[degree + 1];
for (int i = 0; i
coefficients[i] = coeffs[i];
}
}
Polynomial(const Polynomial& p) {
degree = p.degree;
coefficients = new double[degree + 1];
for (int i = 0; i
coefficients[i] = p.coefficients[i];
}
}
~Polynomial() {
delete[] coefficients;
}
int getDegree() const {
return degree;
}
double getCoefficient(int i) const {
if (i degree) {
cerr
return 0;
}
return coefficients[i];
}
void setCoefficient(int i, double value) {
if (i degree) {
cerr
return;
}
coefficients[i] = value;
}
double evaluate(double x) const {
double result = 0;
for (int i = 0; i
result += coefficients[i] * pow(x, i);
}
return result;
}
friend Polynomial operator+(const Polynomial& p1, const Polynomial& p2) {
int maxDegree = max(p1.degree, p2.degree);
double* resultCoeffs = new double[maxDegree + 1];
for (int i = 0; i
double p1Coeff = (i
double p2Coeff = (i
resultCoeffs[i] = p1Coeff + p2Coeff;
}
Polynomial result(resultCoeffs, maxDegree);
delete[] resultCoeffs;
return result;
}
friend Polynomial operator*(const Polynomial& p1, const Polynomial& p2) {
int maxDegree = p1.degree + p2.degree;
double* resultCoeffs = new double[maxDegree + 1];
for (int i = 0; i
resultCoeffs[i] = 0;
for (int j = 0; j
if (j
resultCoeffs[i] += p1.coefficients[j] * p2.coefficients[i - j];
}
}
}
Polynomial result(resultCoeffs, maxDegree);
delete[] resultCoeffs;
return result;
}
friend ostream& operator
for (int i = p.degree; i >= 0; i--) {
if (i == p.degree) {
os
int main()
{
ifstream infile;
infile.open("A.txt");
if (infile.fail()) {
cout
exit(1);
}
int degree_A;
infile >> degree_A;
int* coef_A = new int[degree_A+1];
for (int i = 0; i
coef_A[i] = 0;
int coef, exp;
while (!infile.eof())
{
infile >> coef >> exp;
coef_A[exp] = coef;
}
infile.close();
// read data from B.txt
infile.open("B.txt");
if (infile.fail()) {
cout
exit(1);
}
int degree_B;
infile >> degree_B;
int* coef_B = new int[degree_B + 1];
for (int i = 0; i
coef_B[i] = 0;
while (!infile.eof())
{
infile >> coef >> exp;
coef_B[exp] = coef;
}
infile.close();
Polynomial p1(coef_A, degree_A);
Polynomial p2(coef_B, degree_B);
Polynomial p3 = add(p1, p2);
Polynomial p4 = subtract(p1, p2);
cout
int *ans1=p3.getCoef();
int*ans2=p4.getCoef();
for(int i=0;i
cout
cout
for(int i=0;i
cout
cout
return 0;
}
input output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
