-
March 3rd, 2013, 08:22 PM
#1
HELP Linked List and operation overloading
Hello. I am having a bit of difficulty with implementing an object oriented program that uses both linked lists and operator overloading and would like some help. The program calls for adding and multiplying polynomials together, with each single polynomial being represented as a node of a linked list (which is further a data member of an object of a class I have defined to implement this program). For example:
polynomial A will be: 3x^4 // 1 node of a linked list
polynomial B will be: 5x^2 // 1 node of a linked list
polynomial C will be blank for the time being. // empty list
Now, I need to use operator overloading so that this following line of code can be implemented:
C = A + B;
C should now be: 3x^4 + 5x^2.
The checklist of which parts of my code that work:
constructor works;
copy constructor works;
destructor works;
operator= works;
print function needs work but i can worry about that later;
operator+ HELP: HIGHLIGHTED IN RED
operator* work on later
Here is my code:
Code:
#include <iostream>
using namespace std;
struct termNode
{
int exp; // exponent
int coef; // coefficient
termNode * next;
termNode(int e = 0, int c = 0, termNode * link = NULL)
{
coef = c;
exp = e;
next = link;
}
};
class polyType
{
public:
polyType(); // default constructor
polyType(int, int); // constructor makes a 1 node polynomial
polyType(const polyType &); // copy constructor
~polyType(); // destructor
void print(); // prints out the polynomial in descending order
polyType operator=(const polyType &); // equals
polyType operator+(const polyType &) const; // returns sum of the parameter + self
polyType operator*(const polyType &) const; // return the product of the parameter + self
private:
termNode *polyPtr;
};
polyType::polyType ()
{
polyPtr = new termNode();
}
polyType::polyType (int e, int c)
{
polyPtr = new termNode(e, c);
}
polyType::polyType(const polyType &rhs)
{
termNode * orig = rhs.polyPtr;
termNode * newP = NULL;
polyPtr = NULL;
while(orig != NULL)
{
if(newP == NULL)
{
polyPtr = new termNode;
polyPtr->exp = orig->exp;
polyPtr->coef = orig->coef;
polyPtr->next = NULL;
newP = polyPtr;
}
else
{
newP->next = new termNode;
newP = newP->next;
newP->exp = orig->exp;
newP->coef = orig->coef;
newP->next = NULL;
}
orig = orig->next;
}
}
polyType::~polyType()
{
delete polyPtr;
polyPtr = NULL;
}
void polyType::print()
{
termNode * header = new termNode;
header = polyPtr;
while(header != NULL)
{
cout << header->coef << "x^" << header->exp << " + ";
header = header->next;
}
}
polyType polyType::operator=(const polyType &rhs)
{
delete polyPtr;
termNode * orig = rhs.polyPtr;
termNode * newP = NULL;
polyPtr = NULL;
while(orig != NULL)
{
if(newP == NULL)
{
polyPtr = new termNode;
polyPtr->exp = orig->exp;
polyPtr->coef = orig->coef;
polyPtr->next = NULL;
newP = polyPtr;
}
else
{
newP->next = new termNode;
newP = newP->next;
newP->exp = orig->exp;
newP->coef = orig->coef;
newP->next = NULL;
}
orig = orig->next;
}
return *this;
}
polyType polyType::operator+(const polyType &rhs) const
{
polyType re = *this;
re.polyPtr->next = new termNode(rhs.polyPtr->exp, rhs.polyPtr->coef);
return re;
}
polyType polyType::operator*(const polyType &rhs) const
{
polyType re;
termNode * ptr = polyPtr;
while(ptr != NULL)
{
re.polyPtr->exp = ptr->exp * rhs.polyPtr->exp;
re.polyPtr->coef = ptr->coef * rhs.polyPtr->coef;
ptr = ptr->next;
}
return re;
}
int main() // DRIVER TESTING
{
polyType a(2,2);
polyType b(7,1);
polyType d(5,3);
polyType c;
polyType e;
cout << "A: ";
a.print();
cout << endl << "C: ";
c = a + b;
c.print();
cout << endl << "E: ";
e.print();
cout << endl << "E: ";
e = a * c;
e.print();
cout << endl << "A: ";
a.print();
cout << endl << "D: ";
d.print();
return 0;
}
For the time being I need help with adding multiple nodes together (with the result being in descending order). So for example:
polyType a(2,3), b(4,5), c(6,7), d;
d = a + b + c;
d.print(); // should print out 7x^6 + 5x^4 + 3x^2, but it will only print out: 3x^2 + 7x^6
Thanks for the help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|