DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. thomas t Guest

Below is a program that creates, evaluates, adds, and subtracts polynomials.
I can input, output, and evaluate polynomials. I am having problems adding
them, haven't tried subtracting yet.
In main:

cout << "The sum of " << f1 << " and " << f2 << " is ";

The output is as follows:

The sum of 2X^3 + 4X^2 and 3X^2 + X is 4.9001e270

************I'm not getting the proper output.Is there something wrong with
the '+' function?? Do i need to add a copy constructor and overload the '='
operator before i can do the '+' function below??????

Any input would be appreciated.

//polynomialimp.cpp (implementation file)
//polynomialimp.cpp provides functions for the library
//#include "polynomial.h"

#include "polynomial.h"

polynomial::polynomial()
{

degree = 0;

coefficient = new double [10];

}

polynomial::polynomial(int degree1)
{

degree = degree1;

coefficient = new double [degree + 1];

}

polynomial::~polynomial()
{

delete [] coefficient;

}

double polynomial::evaluate(double& n_par)
{

double temp = 0;

int a;

if(n_par == 0)
temp = coefficient[0];

else
{

for( a = degree; a > 0; a--)
temp += pow(n_par,a)*(coefficient[a]);

}

return temp;

}

polynomial operator+(const polynomial& f1,const polynomial& f2)
{

polynomial t;

if (f1.degree >= f2.degree)
{

t.coefficient = f1.coefficient;

for(int a = f2.degree ; a < 0; a--)
t.coefficient[a] += f2.coefficient[a];

}

else
{

t.coefficient = f2.coefficient;

for(int a = f1.degree; a > 0 ; a++)
t.coefficient[a] += f1.coefficient[a];

}

return t;
}

ostream& operator <<(ostream& outs, const polynomial& f)
{

{

if(f.degree==1) outs << f.coefficient[0];

else
{

int n=f.degree;

for(int i=n;i>=0;i--)
{

if(f.coefficient[i]==0)continue;

if(i==n)
if(i==0)
outs << f.coefficient[i];

else if(i==1)
{

if(f.coefficient[i]==1 || f.coefficient[i]==-1)
f.coefficient[i]==1? outs << "x": outs << "-x";

else outs << f.coefficient[i] << "x";

}

else if(f.coefficient[i]==1 || f.coefficient[i]==-1)
f.coefficient[i]==1? outs << "x^" << i: outs << "-x^" << i;

else outs << f.coefficient[i] << "x^" << i;

if(i!=n && f.coefficient[i]>=0)
if(i==0)
outs << "+" << f.coefficient[i];

else if(i==1)
{

if(f.coefficient[i]==1) outs << "+" << "x";

else outs << "+" << f.coefficient[i] << "x";

}

else if(f.coefficient[i]==1)
outs << "+" << "x^" << i;

else outs << "+" << f.coefficient[i] << "x^" << i;

if(i!=n && f.coefficient[i]<0)
if(i==0)
outs << f.coefficient[i];

else if(i==1)
{

if(f.coefficient[i]==1) outs << "x";

else if(f.coefficient[i]==-1) outs << "-x";
else outs << f.coefficient[i] << "x";
}

else if(f.coefficient[i]==1 || f.coefficient[i]==-1)
f.coefficient[i]==1? outs << "x^" << i: outs << "-x^" << i;

else
outs << f.coefficient[i] << "x^" << i;
}
}

}

return outs;

}

istream& operator >>(istream& ins, polynomial& f)
{

cout << endl;
cout << "Enter the degree of your polynomial. ";

ins >> f.degree;

char ans;
int power, a;
float co;

do
{

cout << "Enter a coefficient and press enter. ";
ins >> co;
cout << "Enter the power and press enter. ";
ins >> power;

f.coefficient[power] = co;

cout << "Do you want to enter another term? ";
cout << "Press y for yes, n for no. ";
ins >> ans;
cout << endl;

} while (ans == 'y' || ans == 'Y');

return ins;
}

2. naomi Guest

"thomas t" <bulkster@aol.com> wrote:
>
>Below is a program that creates, evaluates, adds, and subtracts polynomials.
>I can input, output, and evaluate polynomials. I am having problems adding
>them, haven't tried subtracting yet.
>In main:
>
>
>
>cout << "The sum of " << f1 << " and " << f2 << " is ";
>
>The output is as follows:
>
>The sum of 2X^3 + 4X^2 and 3X^2 + X is 4.9001e270
>
>************I'm not getting the proper output.Is there something wrong with
>the '+' function?? Do i need to add a copy constructor and overload the

'='
>operator before i can do the '+' function below??????
>
>Any input would be appreciated.
>

>
>polynomial operator+(const polynomial& f1,const polynomial& f2)
>{
>
>
>
>polynomial t;
>
>
>if (f1.degree >= f2.degree)
>{
>
>
>t.coefficient = f1.coefficient;
>
>

Check this for statement..
Maybe try: for(int a = f2.degree ; a > 0; a--)
You will also need to fix the other one.
>for(int a = f2.degree ; a < 0; a--)
>t.coefficient[a] += f2.coefficient[a];
>
>}
>
>else
>{
>
>
>t.coefficient = f2.coefficient;
>
>
>
>for(int a = f1.degree; a > 0 ; a++) (it will keep going)
>t.coefficient[a] += f1.coefficient[a];
>
>}
>
>return t;
>}
>
>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise