Design and implement a class that is a class for polynomials. The polynomial
anxn + an-1xn-1 +...+ a0
will be implemented as a linked list. Each node will contain an int value for the power of x
and an int value for the corresponding coefficient. The class operations should include
addition, subtraction, multiplication, and evaluation of a polynomial. Overload the operators +, −, and * for addition, subtraction, and multiplication. Evaluation of a polyno-
mial is implemented as a member function with one argument of type int. The evaluation
member function returns the value obtained by plugging in its argument for x and per-
forming the indicated operations.
That's a nice project,I take it for my revision...
should the signature be an object or pointer to an object in order to perform addition ?
Up to you. I would hazard that if I had two polynomial objects and I wanted to add them, I would expect to be able to add the objects directly and not have to pass pointers to objects to add the actual objects together. I would expect addition involving pointers of objects to be pointer arithmetic.
i.e. your operator+ should behave like all other operator+s
If you do need to use pointers to add, implement a method of some kind.
Reading the problem description, it sounds to me like the Polynomial class should contain a linked list of nodes (or terms). Not that it should be a node in the linked list itself.
Reading the problem description, it sounds to me like the Polynomial class should contain a linked list of nodes (or terms). Not that it should be a node in the linked list node itself.
Ah right,you mean I should make a list class that has private member of class type Polynomial* ?
If I do so , I have to delete the *link in Polynomial class and put it in new class I think
Your Term looks ok. Now you just need operator+, operator-, operator* for the Polynomial and your class declarations are pretty much complete. Apart from the "eval" method(s).
Have you done templates yet? If so, consider implementing a generic list and then using that to implement your polynomial (you may well have already written a list?)
Andy
PS You will need extra interal methods. I can think of a couple of possibilities, but it does depend on how you go about your implement. As does your final choice of Term versus const Term& for the i/p params of you operators.
I didn't learn english algebra,could you shortly explain "evaluation of a polynomial" I don't understand so I havn't created it yet.
And also all of the operations I think they need the word virtual,as we can add 2 terms of the same coefficient and add 2 polynomial too...
can an operator be virtual in this case?
Have you done templates yet? If so, consider implementing a generic list and then using that to implement your polynomial (you may well have already written a list?)
I learnt template,but this exercice seems to require int for A and N,what do you mean by generic list ? A list with double coefficient or exponent or sth ?
I now have difficult on the output and input of the polynomial...
My addnew :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void Polynomial::addNew(int theA,int theN)
{
if(isEmpty())
{
top = new Term(theA,theN,NULL);
}
else
{
Term *temp = top;
while(top->getLink() != NULL)
top = top->getLink();
//When reach end !
top->setLink(new Term(theA,theN,NULL));
top = temp;
}
}
My output :
1 2 3 4 5 6 7 8 9 10 11
ostream& operator << (ostream& out,const Polynomial& object)
{
Term *temp = object.top;
while(object.top->getLink() != NULL)
{
out << *temp;
temp = temp->getLink();
}
out << temp->getA();
return out;
}
The code output what I wanted but the compiler shout an error (has stopped working error)
I think it encounters an infinite loop but can't figure out where...
EDIT : Now the output is fine It forgot while(object.top->getLink() != NULL) must be while(temp->getLink() != NULL) ! LOL! fine now...I'm working on the input
void Polynomial::addNew(int theA,int theN)
{
if(isEmpty())
{
top = new Term(theA,theN,NULL);
}
else
{
Term *temp = top;
while(temp->getLink() != NULL) // use temp in loop
temp = temp->getLink();
//When reach end !
temp->setLink(new Term(theA,theN,NULL));
// this way you don't have to remember to reset top
}
}
It might be better to keep your terms in order. So you need to test to see if the new link is a bigger term that the current link. If it is smaller, advance one link and try again. Until you get to the end.
This does raise the question of how you should handled equal terms with equal powers? Do you keep all of them? Or merge them? If the former, maybe you need a "simplify" method?
Having the terms in order will help with the add and subtract.
I think you add is a good start, but it's incomplete
If your terms are unordered, you will have to reset the inner loop to find all matches. And remember the terms which have not been added and deal with them after the loops end.
It might be better to modify the addNew to the terms are ordered, then it should be easier to implement add. And subtract, etc.
Also,
1 2 3 4 5 6 7 8 9 10 11
ostream& operator << (ostream& out,const Polynomial& object)
{
Term *temp = object.top;
while(object.top->getLink() != NULL) // <= this line does not look right?
{
out << *temp;
temp = temp->getLink();
}
out << temp->getA(); // <= nor does this
return out;
}
Oh, and I'd leave the operator>> aside to start with. It could be tricky till you're sorted out the other parts.
What you need is a set of test cases. To start with, you can just code a set of functions, which report/return pass or fail. Later on you could load your test cases from a file if you can come up with a reasonably straightforwards way to store polymonials in a file.
Thank you very much,I'll work at things you mentioned
Include four constructors: a default constructor, a copy constructor, a constructor with a
single argument of type int that produces the polynomial that has only one constant term
that is equal to the constructor argument, and a constructor with two arguments of type
int that produces the one-term polynomial whose coefficient and exponent are given by the
two arguments. (In the previous notation the polynomial produced by the one-argument
constructor is of the simple form consisting of only a0. The polynomial produced by the
two-argument constructor is of the slightly more complicated form anxn.) Include a suit-
able destructor. Include member functions to input and output polynomials.
Some more of the subject.
I don't understand this one.
a constructor with a
single argument of type int that produces the polynomial that has only one constant term
that is equal to the constructor argument
EDIT 1 : I'm working on this again.THis is my << after handling an empty Polynomial...Plz check :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
ostream& operator << (ostream& out,const Polynomial& object)
{
Term *temp = object.top;
if(temp == NULL)
out << "0";
else
{
while(temp->getLink() != NULL)
{
out << *temp;
temp = temp->getLink();
}
out << *temp;
}
return out;
}
EDIT 2 : I have to use while 2 times for BEFORE NULL & IN-NULL CASE...If I use Iterator q with q.begin() and q.end() I only have 1 case to solve...It automically loop throughout
a constructor with a single argument of type int that produces the polynomial that has only one constant term that is equal to the constructor argument