Question
Please fix the code below because it is incomplete in the main function: My output should be print out like this: the assignments : #include
Please fix the code below because it is incomplete in the main function:
My output should be print out like this:
the assignments :
#include
#include
using namespace std ;
class Polynomial
{
private:
int degree;
int *coef;
public:
Polynomial()
{
degree=0;
}
Polynomial(int d)
{
degree=d;
coef=new int[d+1];
for(int i=0;i
coef[i]=0;
}
Polynomial(int*c ,int d )
{
degree=d;
coef=new int[d+1];
for(int i=0;i
coef[i]=c[i];
}
int getDegree()
{
return degree;
}
int* getCoef()
{
return coef;
}
friend Polynomial add(Polynomial a,Polynomial b)
{
int n,flag=0;
if(a.degree>b.degree)
{
n=a.degree;
flag=1;
}
else
n=b.degree;
int *ans=new int[n+1];
if(flag)
{
int i;
for(i=0;i
{
ans[i]=a.coef[i]+b.coef[i];
}
for(;i
{
ans[i]=a.coef[i];
}
}
else
{
int i;
for(i=0;i
{
ans[i]=a.coef[i]+b.coef[i];
}
for(;i
{
ans[i]=b.coef[i];
}
}
Polynomial p(ans,n);
return p;
}
friend Polynomial subtract(Polynomial a,Polynomial b)
{
int n,flag=0;
if(a.degree>b.degree)
{
n=a.degree;
flag=1;
}
else
n=b.degree;
int *ans=new int[n+1];
if(flag)
{
int i;
for(i=0;i
{
ans[i]=a.coef[i]-b.coef[i];
}
for(;i
{
ans[i]=a.coef[i];
}
}
else
{
int i;
for(i=0;i
{
ans[i]=a.coef[i]-b.coef[i];
}
for(;i
{
ans[i]=-b.coef[i];
}
}
Polynomial p(ans,n);
return p;
}
};
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 Implement a polynomial class with a member data of a dynamic array. A.txt A=4x7+5x6+3x3+2x1+8x0 egree (7) 45328876310 Dynamic array B.txt B=6x8+3x6+4x5+8x3+3x1+2x0 degree A=4x7+5x6+3x3+2x1+8x0ObjectAcoefdegree B=6x8+3x6+4x5+8x3+3x1+2x0 input output Implement a polynomial class with a member data of a dynamic array. A.txt A=4x7+5x6+3x3+2x1+8x0 egree (7) 45328876310 Dynamic array B.txt B=6x8+3x6+4x5+8x3+3x1+2x0 degree A=4x7+5x6+3x3+2x1+8x0ObjectAcoefdegree B=6x8+3x6+4x5+8x3+3x1+2x0Step 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