CODE
#include "Poly.h"
#include <cmath>
Poly::Poly(int order) // Default constructor
{
int i;
order = order + 1;
a = new double [order+1];
for (i=0;i<order+1;i++) a[i] = 0.0;
}
Poly::Poly(const Poly &p) // Copy constructor
{
int i;
order = p.order+1;
a = new double [order+1];
for (i=0;i<order+1;i++) a[i] = p.a[i];
}
Poly::~Poly()
{
delete [] a;
}
//Functions
void Poly::Resize(int newlength)
{
if (order+1 != newlength) {
if (order+1 > 0) delete [] a;
a = new double [newlength];
order = newlength;
}
}
Poly Poly:: operator=(const Poly &p)
{
int i;
Resize(p.order+1);
for (i=0; i< order+1; i++) a[i] = p.a[i];
return(*this);
}
Poly Poly::operator+(const Poly &p)
{
int i;
Poly temp(*this);
for (i=0; i< order+1; i++) temp.a[i] += p.a[i];
return temp;
}
Poly Poly::operator-(const Poly &p)
{
int i;
Poly temp(*this);
for (i=0; i< order+1; i--) temp.a[i] -= p.a[i];
return temp;
}
Poly Poly::operator*(const Poly &p){
int i, j;
Poly temp;
double b;
for (i=0;i<order+1; i++) {
b = a[i];
for (j=0; j <p.order+1;j++) {
temp = temp+ b*p.a[j];
}
}
return temp;
}
double Poly::Evaluate(double a){ // Compute and return the polynomial value at x
/*double temp, b;
int i;
for (i=0; i<order+1; i++) {
b = a[i];
temp += b * pow(a, i); // something wrong here!!!
}
return temp;*/
return 0;
}
void Poly::SetCoeff(int o, double a){
return;
}
istream &operator>>(istream &is, Poly &p)
{
cout<<"Enter Polynomial order: \n";
int order;
is>>order;
p.Resize(order+1);
cout<<"Enter coefficients: \n";
int i;
for (i=0; i<p.order+1; i++) {
double b;
is>>b;
p.a[i]=b;
}
return is;
}
ostream &operator<<(ostream &os, Poly &p)
{
int i;
for (i=p.order; i>1; i--) {
os<<p.a[i]<<"*x^"<<i-1<<"+";
}
os<<p.a[1];
return os;
}
*Mod Edit: added code tags: