1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
#ifndef POLY_H
#define POLY_H
#include <iostream>
#include <math.h>
#include <iostream>
#include <fstream>
using namespace std;
#define max(_a,_b) ((_a)>(_b) ? (_a) : (_b))
class Poly
{
private:
int degree;
double *coefficient;
public:
Poly(int deg, const double *coeff);
Poly(const Poly & p); // copy constructor
static Poly FromStream(istream & is);
~Poly();
double &operator[] (int index);
int getDegree() const;
double getCoefficient(int index) const;
void setCoefficient(int index, double value);
double eval(double x) const;
void print(ostream & os = cout) const;
void printToFile(ofstream & ofs) const;
Poly operator+(const Poly & right) const;
Poly operator-(const Poly & right) const;
void operator=(const Poly & right); // assignment operator.
};
#endif
Poly::Poly(int deg, const double *coeff) :
degree(deg)
{
coefficient = NULL;
if (coeff) {
coefficient = new double[deg + 1];
for (int i = 0; i < deg + 1; i++)
coefficient[i] = coeff[i];
}
}
Poly::Poly(const Poly & p)
{
degree = p.getDegree();
coefficient = new double[degree + 1];
for (int i = 0; i < degree + 1; i++)
coefficient[i] = p.getCoefficient(i);
}
Poly
Poly::FromStream(istream & is)
{
int deg;
is >> deg;
double *tmpCoeff = new double[deg + 1];
for (int i = 0; i < deg + 1; i++) {
is >> tmpCoeff[i];
}
Poly tmpPoly(deg, tmpCoeff);
delete[]tmpCoeff;
return tmpPoly;
}
Poly::~Poly()
{
if (coefficient)
delete[]coefficient;
}
double &
Poly::operator[] (int index)
{
return coefficient[index];
}
int
Poly::getDegree() const
{
return degree;
}
double
Poly::getCoefficient(int index) const
{
return coefficient[index];
}
void
Poly::setCoefficient(int index, double value)
{
coefficient[index] = value;
}
double
Poly::eval(double x) const
{
double result = 0.0;
for (int i = 0; i < degree + 1; i++) {
result += coefficient[i] * pow(x, i);
}
return result;
}
void
Poly::print(ostream & os) const
{
double curCoeff;
for (int i = 0; i < degree + 1; i++) {
curCoeff = coefficient[i];
if (curCoeff != 0.0) {
if (i > 0 && curCoeff > 0.0)
os << "+";
if (i == 0 || curCoeff != 1.0) {
os << curCoeff;
}
if (i > 0)
os << "X^" << i;
}
}
os << endl;
}
void
Poly::printToFile(ofstream & ofs) const
{
ofs << degree << " ";
for (int i = 0; i < degree + 1; i++) {
ofs << coefficient[i] << " ";
}
ofs << endl;
}
Poly
Poly::operator+(const Poly & right) const
{
int maxDegree = max(degree, right.getDegree());
double *mergedValues = new double[maxDegree + 1];
for (int i = 0; i < maxDegree + 1; i++) {
mergedValues[i] = getCoefficient(i) + right.getCoefficient(i);
}
Poly tmp(maxDegree, mergedValues);
delete[]mergedValues;
return tmp;
}
Poly
Poly::operator-(const Poly & right) const
{
int maxDegree = max(degree, right.getDegree());
double *mergedValues = new double[maxDegree + 1];
for (int i = 0; i < maxDegree + 1; i++) {
mergedValues[i] = getCoefficient(i) - right.getCoefficient(i);
}
Poly tmp(maxDegree, mergedValues);
delete[]mergedValues;
return tmp;
}
void
Poly::operator=(const Poly & right)
{
if (coefficient)
delete[]coefficient;
degree = right.getDegree();
coefficient = new double[degree + 1];
for (int i = 0; i < degree + 1; i++)
coefficient[i] = right.getCoefficient(i);
}
ostream & operator <<(ostream & os, const Poly & right)
{
right.print(os);
return os;
}
istream & operator >>(istream & is, Poly & right)
{
Poly cur = Poly::FromStream(is);
right = cur;
return is;
}
int
main()
{
double p1Coeff[] = { -1, 0, 2, -5 };
Poly p1(3, p1Coeff);
double p2Coeff[] = { 2, 4, 3, 2, 1 };
Poly p2(4, p2Coeff);
cout << "Result p1(x=2): " << p1.eval(2) << endl;
p1.print();
cout << "Result p2(x=1): " << p2.eval(1) << endl;
p2.print();
Poly p3 = p1 + p2;
cout << "Result p3(x=1): " << p3.eval(1) << endl;
p3.print();
cout << p3 << endl;
cout << "----------" << endl;
Poly p4(0, 0);
cin >> p4;
cout << p4 << endl;
/*
* // File Testing. ofstream outFile; outFile.open("mydata.txt");
* p4.printToFile(outFile); outFile.close();
*
* ifstream inFile; inFile.open("mydata.txt"); Poly p5(0, 0); p5 =
* Poly::FromStream(inFile); inFile.close(); p5.print(); */
return 0;
}
|