You've made good progress.
I've been proceeding on the implicit assumption that input to the constructors (file, array, vector) was in order by degree (either ascending or descending) and that you want to represent the polynomial in it's simplest form. Perhaps that is not a requirement.
It might be a reasonable assumption since one normally writes polynomials from left to right in decreasing order of degree. A std::map might have made more sense for this assignment, but we're stuck with std::list.
However, your sample input file belies this assumption. Looking at the sample input file, you can see three input terms of degree 2, two of degree 5 and in general, not in any particular order.
Representing CPolynomial in it's simplest form is going to make AddOneTerm() more complex. AddOneTerm() is going to have to navigate the list and determine if there is already a term of the specified degree, and if so, combine the term being added with the specified term. If there is not an existing term of the specified degree, the AddOneTerm() should insert the new term at the correct position in the list. See list::insert.
cpolynomial.h
----------------
line 4,14,15: deg is defined in Node as an
int
, which makes sense. However, in the array and vector constructors, deg is a double. Is this really how the supplied header file is? This is inconsistent. If this is how it is supplied in the header, we can deal with it. Just wanted to double check that this wasn't something you messed with.
line 22: CPolynomial capitalization is incorrect. I assume this is your typo and the provided header file is correct.
cpolynomial.cpp
-------------------
Lines 6-7:
using namespace std;
needs to be before
#include "cpolynomial.h"
since CPolymonial uses list<> without std::
Line 29: You have an extraneous CPolynomial::
Line 35-36: Every iteration of the array is adding the same first element of each array. You want to set term to the n'th degree and coefficient.
1 2 3 4 5 6 7 8 9
|
CPolynomial::CPolynomial (double *cof, double *deg, int n)
{ Node term;
for (int i = 0; i < n; i++)
{ term.cof = cof[i];
term.deg = (int)deg[i]; // cast double to int
AddOneTerm(term);
}
}
|
Line 52: This is essentially identical to the code above for arrays of doubles.
Line 63-77: Your operators need some work. From my previous post:
This is more complicated than what you have. You have two lists. this->m_polynominal and right.m_polynominal. You need to iterate through each list finding matching terms (degrees), then add the two terms (coefficients). Keep in mind that a term on the right may not exist on the left and visa-versa. |
Line 139-146: Some problems with ReadFromFile()
"file" is passed in as an argument, but is not used. The implication is that ReadFromFile() should open the file, rather than opening it in the constructor.
"num" is used, but not passed in. Again, the implication is that ReadFromFile should read the header line.
"myfile" is used, but not defined.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
void CPolynomial::ReadFromFile(string file)
{ fstream MyFile;
string p;
int num;
Node term;
MyFile.open(file);
if (! MyFile.is_open())
{ cout << "Unable to open input file" << endl;
exit (EXIT_FAILURE);
}
MyFile >> p >> num;
cout << "p=" << num;
for (int i = 0; i < num; i++)
{ MyFile >> term.deg >> term.cof;
AddOneTerm(term);
}
}
|
edit:
Lines 63,79,97,108: Your operators need to be qualified by CPolynomial::
Line 108: CPolynomial capitalization error on the return type.
Several problems with your print function:
Line 117: You need to walk the list using an iterator.
Line 117: num is not defined.
Line 119,121,123,125,127,129,131,133: deg and cof are simple members of node and are not arrays. You can't index them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
void CPolynomial::Print() const
{ list<Node>::const_iterator citer;
Node term;
// Need to iterate through the list
for (citer=m_Polynomial.begin(); citer!=m_Polynomial.end(); citer++)
{ term = *citer; // Making a copy of Node for clarity
if (term.cof == 0)
{ // Skip this term
}
else
{ if (term.cof > 0)
cout << "+";
cout << term.cof;
if (term.deg != 0)
cout << "x^" << term.deg;
}
}
cout << endl;
}
|