scuzzo
asked on
Can anyone tell me why the overloaded += operator is not advancing through the linked lists if the lists have a different number of nodes?
I am trying to figure this out. I need to get this done pronto so any help is appreciated.
Here is the outline of my assignment:
Define and implement a class named Polynomial that has operators for creating a polynomial, reading a polynomial, and adding and subtracting polynomials. Its partial declaration is given below (you can add some other auxiliary member functions to the class, if necessary).
struct TermType
{
int coefficient;
int exponent;
TermType* next;
};
class Polynomial
{
private:
TermType* head;
public:
// default constructor: create an empty polynomial
Polynomial();
// copy constructor: use the content of p1 to create a polynomial
Polynomial(const Polynomial& p1);
// destructor
// release dynamic memory related to the polynomial
~Polynomial();
// overloaded assignment operator
// assign the content of p1 to a polynomial
void operator=(const Polynomial& p1);
// overloaded += operator
// add a polynomial
void operator+=(const Polynomial& p1);
// overloaded -= operator
// subtract a polynomial
void operator-=(const Polynomial& p1);
// read a polynomial
bool read();
// output a polynomial
void print();
};
Here is the outline of my assignment:
Define and implement a class named Polynomial that has operators for creating a polynomial, reading a polynomial, and adding and subtracting polynomials. Its partial declaration is given below (you can add some other auxiliary member functions to the class, if necessary).
struct TermType
{
int coefficient;
int exponent;
TermType* next;
};
class Polynomial
{
private:
TermType* head;
public:
// default constructor: create an empty polynomial
Polynomial();
// copy constructor: use the content of p1 to create a polynomial
Polynomial(const Polynomial& p1);
// destructor
// release dynamic memory related to the polynomial
~Polynomial();
// overloaded assignment operator
// assign the content of p1 to a polynomial
void operator=(const Polynomial& p1);
// overloaded += operator
// add a polynomial
void operator+=(const Polynomial& p1);
// overloaded -= operator
// subtract a polynomial
void operator-=(const Polynomial& p1);
// read a polynomial
bool read();
// output a polynomial
void print();
};
ASKER
I can't see that my source code got posted. Here it is.
#include <string>
#include <iostream>
using namespace std;
struct TermType
{
int coefficient;
int exponent;
bool NegCoef;
TermType* next;
};
class Polynomial
{
private:
TermType* head;
public:
// default constructor: create an empty polynomial
Polynomial();
// copy constructor: use the content of p1 to create a polynomial
Polynomial(const Polynomial& p1);
// destructor
// release dynamic memory related to the polynomial
//~Polynomial();
// overloaded assignment operator
// assign the content of p1 to a polynomial
void operator=(const Polynomial& p1);
// overloaded += operator
// add a polynomial
void operator+=(const Polynomial& p1);
// overloaded -= operator
// subtract a polynomial
void operator-=(const Polynomial& p1);
// read a polynomial
void read();
// output a polynomial
void print();
//new input function
void insertTerm(bool,int,int);
};
Polynomial::Polynomial()
{
head=NULL;
}
void Polynomial::operator+=(con st Polynomial& p1)
{
TermType* poly1=head;
TermType* poly2=p1.head;
while (poly1!=NULL&&poly2!=NULL)
{
if (poly1->exponent==poly2->e xponent)
{
cout<<poly1->coefficient+p oly2->coef ficient<<" x^"<<poly1 ->exponent ;
}
if (poly1->exponent>poly2->ex ponent)
{
cout<<poly1->coefficient<< "x^"<<poly 1->exponen t;
}
if (poly2->exponent>poly1->ex ponent)
{
cout<<poly2->coefficient<< "x^"<<poly 2->exponen t;
}
if (poly2==NULL)
cout<<poly1->coefficient<< "x^"<<poly 1->exponen t;
if (poly1==NULL)
cout<<poly2->coefficient<< "x^"<<poly 2->exponen t;
if (poly1!=NULL)
poly1=poly1->next;
if (poly2!=NULL)
poly2=poly2->next;
}
}
void Polynomial::insertTerm(boo l negCoef, int coef, int exp)
{
cout << "begin insert!" << endl;
cout << negCoef << " " << coef << " " << exp << endl;
TermType* newTerm = new TermType;
if (negCoef == 1)
newTerm->coefficient = 0 - coef;
else
newTerm->coefficient = coef;
newTerm->exponent = exp;
newTerm->next = NULL;
TermType* current = head;
if (head == NULL)
head = newTerm;
else
{
while (current->next != NULL)
current=current->next;
current->next = newTerm;
}
cout << "end insert!" << endl;
}
//read a polynomial
void Polynomial::read()
{
string s;
cout << "Enter a term (ex: +4x^3): ";
cin >> s;
int index = 0;
bool firstTerm = 1;
bool nowExp = 0;
bool negativeCoef = 0;
int coef = 0;
int exp = 0;
while (s[index] != '#')
{
cout << "char : " << s[index] << endl;
if (s[index] == '+' || s[index] == '-')
{
cout << "+-" << endl;
if (firstTerm == 0)
{
if (coef != 0)
insertTerm(negativeCoef, coef, exp);
}
else
{
firstTerm = 0;
}
nowExp = 0;
if (s[index] == '-')
negativeCoef = 1;
else
negativeCoef = 0;
coef = 0;
exp = 0;
}
else if (s[index] == 'x')
{
cout << "x" << endl;
if (s[index+1] != '^')
{
cout << "Incorrect formula!" << endl;
head = NULL;
break;
}
}
else if (s[index] == '^')
{
cout << "^" << endl;
nowExp = 1;
}
else if (s[index]>='0' && s[index]<='9')
{
cout << "number" << endl;
if (nowExp == 0)
coef = coef*10 + (s[index]-48);
else
exp = exp*10 + (s[index]-48);
}
else
{
cout << "The formula contains invalid characters: " << s[index] << endl;
head = NULL;
break;
}
index++;
}
if (coef != 0)
insertTerm(negativeCoef, coef, exp);
cout << "done!"<< endl;
}
void Polynomial::print()
{
TermType* newTerm=head;
while (newTerm != NULL)
{
cout<<newTerm->coefficient <<"x^"<<ne wTerm->exp onent;
newTerm=newTerm->next;
}
cout << endl;
}
int main()
{
Polynomial p, p1;
p.read();
cout<<"Second Term:"<<endl;
p1.read();
//p.print();
//p1.print();
p+=(p1);
system ("pause");
return 0;
}
#include <string>
#include <iostream>
using namespace std;
struct TermType
{
int coefficient;
int exponent;
bool NegCoef;
TermType* next;
};
class Polynomial
{
private:
TermType* head;
public:
// default constructor: create an empty polynomial
Polynomial();
// copy constructor: use the content of p1 to create a polynomial
Polynomial(const Polynomial& p1);
// destructor
// release dynamic memory related to the polynomial
//~Polynomial();
// overloaded assignment operator
// assign the content of p1 to a polynomial
void operator=(const Polynomial& p1);
// overloaded += operator
// add a polynomial
void operator+=(const Polynomial& p1);
// overloaded -= operator
// subtract a polynomial
void operator-=(const Polynomial& p1);
// read a polynomial
void read();
// output a polynomial
void print();
//new input function
void insertTerm(bool,int,int);
};
Polynomial::Polynomial()
{
head=NULL;
}
void Polynomial::operator+=(con
{
TermType* poly1=head;
TermType* poly2=p1.head;
while (poly1!=NULL&&poly2!=NULL)
{
if (poly1->exponent==poly2->e
{
cout<<poly1->coefficient+p
}
if (poly1->exponent>poly2->ex
{
cout<<poly1->coefficient<<
}
if (poly2->exponent>poly1->ex
{
cout<<poly2->coefficient<<
}
if (poly2==NULL)
cout<<poly1->coefficient<<
if (poly1==NULL)
cout<<poly2->coefficient<<
if (poly1!=NULL)
poly1=poly1->next;
if (poly2!=NULL)
poly2=poly2->next;
}
}
void Polynomial::insertTerm(boo
{
cout << "begin insert!" << endl;
cout << negCoef << " " << coef << " " << exp << endl;
TermType* newTerm = new TermType;
if (negCoef == 1)
newTerm->coefficient = 0 - coef;
else
newTerm->coefficient = coef;
newTerm->exponent = exp;
newTerm->next = NULL;
TermType* current = head;
if (head == NULL)
head = newTerm;
else
{
while (current->next != NULL)
current=current->next;
current->next = newTerm;
}
cout << "end insert!" << endl;
}
//read a polynomial
void Polynomial::read()
{
string s;
cout << "Enter a term (ex: +4x^3): ";
cin >> s;
int index = 0;
bool firstTerm = 1;
bool nowExp = 0;
bool negativeCoef = 0;
int coef = 0;
int exp = 0;
while (s[index] != '#')
{
cout << "char : " << s[index] << endl;
if (s[index] == '+' || s[index] == '-')
{
cout << "+-" << endl;
if (firstTerm == 0)
{
if (coef != 0)
insertTerm(negativeCoef, coef, exp);
}
else
{
firstTerm = 0;
}
nowExp = 0;
if (s[index] == '-')
negativeCoef = 1;
else
negativeCoef = 0;
coef = 0;
exp = 0;
}
else if (s[index] == 'x')
{
cout << "x" << endl;
if (s[index+1] != '^')
{
cout << "Incorrect formula!" << endl;
head = NULL;
break;
}
}
else if (s[index] == '^')
{
cout << "^" << endl;
nowExp = 1;
}
else if (s[index]>='0' && s[index]<='9')
{
cout << "number" << endl;
if (nowExp == 0)
coef = coef*10 + (s[index]-48);
else
exp = exp*10 + (s[index]-48);
}
else
{
cout << "The formula contains invalid characters: " << s[index] << endl;
head = NULL;
break;
}
index++;
}
if (coef != 0)
insertTerm(negativeCoef, coef, exp);
cout << "done!"<< endl;
}
void Polynomial::print()
{
TermType* newTerm=head;
while (newTerm != NULL)
{
cout<<newTerm->coefficient
newTerm=newTerm->next;
}
cout << endl;
}
int main()
{
Polynomial p, p1;
p.read();
cout<<"Second Term:"<<endl;
p1.read();
//p.print();
//p1.print();
p+=(p1);
system ("pause");
return 0;
}
while (poly1!=NULL&&poly2!=NULL)
just for safety
while ((poly1!=NULL) && (poly2!=NULL))
otherwise it should loop through both lists. (I don't understand what it should be doing though)
just for safety
while ((poly1!=NULL) && (poly2!=NULL))
otherwise it should loop through both lists. (I don't understand what it should be doing though)
ASKER
It is to bring in two linked lists and then add them together. The linked lists are to represent polynomials. So what I have are two linked lists that have values for coefficient and exponents. Then I need to be able to add the polynomials together and output the result.
aha, so that is what it does.
As I said - it looks like it should process both lists. Have you tried single stepping through the code. (Maybe the lists aren't what they should be)
As I said - it looks like it should process both lists. Have you tried single stepping through the code. (Maybe the lists aren't what they should be)
ASKER
I've tried everything I know but I've been working for about 48 hrs trying to get this up and running and I'm running out of steam.
This is all I can think of - use this instead of the one you have (without brackets)
while ((poly1!=NULL) && (poly2!=NULL))
while ((poly1!=NULL) && (poly2!=NULL))
(duh - I was going to say that first but then thought for some reason that it was what you wanted to do).
You want OR not AND in the check
while ((poly1!=NULL) || (poly2!=NULL))
You want OR not AND in the check
while ((poly1!=NULL) || (poly2!=NULL))
ASKER
I tried using || but the problem is that I throw an Unhandled exception at this line:
while ((poly1!=NULL)||(poly2!=NU LL))
{
if (poly1->exponent==poly2->e xponent)
{
->->->-> cout<<poly1->coefficient+p oly2->coef ficient<<" x^"<<poly1 ->exponent ;
}
if (poly1->exponent>poly2->ex ponent)
{
cout<<poly1->coefficient<< "x^"<<poly 1->exponen t;
}
if (poly2->exponent>poly1->ex ponent)
{
cout<<poly2->coefficient<< "x^"<<poly 2->exponen t;
}
if (poly2==NULL)
cout<<poly1->coefficient<< "x^"<<poly 1->exponen t;
if (poly1==NULL)
cout<<poly2->coefficient<< "x^"<<poly 2->exponen t;
if (poly1!=NULL)
poly1=poly1->next;
if (poly2!=NULL)
poly2=poly2->next;
}
}
while ((poly1!=NULL)||(poly2!=NU
{
if (poly1->exponent==poly2->e
{
->->->-> cout<<poly1->coefficient+p
}
if (poly1->exponent>poly2->ex
{
cout<<poly1->coefficient<<
}
if (poly2->exponent>poly1->ex
{
cout<<poly2->coefficient<<
}
if (poly2==NULL)
cout<<poly1->coefficient<<
if (poly1==NULL)
cout<<poly2->coefficient<<
if (poly1!=NULL)
poly1=poly1->next;
if (poly2!=NULL)
poly2=poly2->next;
}
}
only compare poly1 and poly2 components if both are NOT null
ASKER
I'm not following you, I thought that while ((poly1!=NULL)||poly2!=NUL L)) was comparing if both were not NULL
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
First of all you are getting big points for helping me this far and I want you to know I appreciate it. Secondly I've managed to get the code refined a little and now for some reason I'm getting stuck in an infinite loop using the operloaded += operator. Any ideas?
#include <string>
#include <iostream>
using namespace std;
struct TermType
{
int coefficient;
int exponent;
bool NegCoef;
TermType* next;
};
class Polynomial
{
private:
TermType* head;
public:
// default constructor: create an empty polynomial
Polynomial();
// copy constructor: use the content of p1 to create a polynomial
Polynomial(const Polynomial& p1);
// destructor
// release dynamic memory related to the polynomial
//~Polynomial();
// overloaded assignment operator
// assign the content of p1 to a polynomial
void operator=(const Polynomial& p1);
// overloaded += operator
// add a polynomial
void operator+=(const Polynomial& p1);
// overloaded -= operator
// subtract a polynomial
void operator-=(const Polynomial& p1);
// read a polynomial
void read();
// output a polynomial
void print();
//new input function
void insertTerm(bool,int,int);
};
Polynomial::Polynomial()
{
head=NULL;
}
void Polynomial::operator+=(con st Polynomial& p1)
{
cout<<"Start print"<<endl;
TermType* poly1=head;
TermType* poly2=p1.head;
int counter=0;
while ((poly1!=NULL)||(poly2!=NU LL))
{
counter++;
if (poly2==NULL)
cout<<poly1->coefficient<< "x^"<<poly 1->exponen t;
if (poly1==NULL)
cout<<poly2->coefficient<< "x^"<<poly 2->exponen t;
if (poly1!=NULL&&poly2!=NULL)
{
if (poly1->exponent==poly2->e xponent)
{
cout<<poly1->coefficient+p oly2->coef ficient<<" x^"<<poly1 ->exponent ;
if (poly1!=NULL)
poly1=poly1->next;
if (poly2!=NULL)
poly2=poly2->next;
continue;
}
if (poly1->exponent>poly2->ex ponent)
{
cout<<poly1->coefficient<< "x^"<<poly 1->exponen t;
if (poly2!=NULL)
poly2=poly2->next;
continue;
}
if (poly2->exponent>poly1->ex ponent)
{
cout<<poly2->coefficient<< "x^"<<poly 2->exponen t;
if (poly1!=NULL)
poly1=poly1->next;
continue;
}
}
}
}
void Polynomial::insertTerm(boo l negCoef, int coef, int exp)
{
cout << "begin insert!" << endl;
cout << negCoef << " " << coef << " " << exp << endl;
TermType* newTerm = new TermType;
if (negCoef == 1)
newTerm->coefficient = 0 - coef;
else
newTerm->coefficient = coef;
newTerm->exponent = exp;
newTerm->next = NULL;
TermType* current = head;
if (head == NULL)
head = newTerm;
else
{
while (current->next != NULL)
current=current->next;
current->next = newTerm;
}
cout << "end insert!" << endl;
}
//read a polynomial
void Polynomial::read()
{
string s;
cout << "Enter a term (ex: +4x^3): ";
cin >> s;
int index = 0;
bool firstTerm = 1;
bool nowExp = 0;
bool negativeCoef = 0;
int coef = 0;
int exp = 0;
while (s[index] != '#')
{
cout << "char : " << s[index] << endl;
if (s[index] == '+' || s[index] == '-')
{
cout << "+-" << endl;
if (firstTerm == 0)
{
if (coef != 0)
insertTerm(negativeCoef, coef, exp);
}
else
{
firstTerm = 0;
}
nowExp = 0;
if (s[index] == '-')
negativeCoef = 1;
else
negativeCoef = 0;
coef = 0;
exp = 0;
}
else if (s[index] == 'x')
{
cout << "x" << endl;
if (s[index+1] != '^')
{
cout << "Incorrect formula!" << endl;
head = NULL;
break;
}
}
else if (s[index] == '^')
{
cout << "^" << endl;
nowExp = 1;
}
else if (s[index]>='0' && s[index]<='9')
{
cout << "number" << endl;
if (nowExp == 0)
coef = coef*10 + (s[index]-48);
else
exp = exp*10 + (s[index]-48);
}
else
{
cout << "The formula contains invalid characters: " << s[index] << endl;
head = NULL;
break;
}
index++;
}
if (coef != 0)
insertTerm(negativeCoef, coef, exp);
cout << "done!"<< endl;
}
void Polynomial::print()
{
TermType* newTerm=head;
while (newTerm != NULL)
{
cout<<newTerm->coefficient <<"x^"<<ne wTerm->exp onent;
newTerm=newTerm->next;
}
cout << endl;
}
int main()
{
Polynomial p, p1;
p.read();
cout<<"Second Term:"<<endl;
p1.read();
//p.print();
//p1.print();
p+=(p1);
system ("pause");
return 0;
}
#include <string>
#include <iostream>
using namespace std;
struct TermType
{
int coefficient;
int exponent;
bool NegCoef;
TermType* next;
};
class Polynomial
{
private:
TermType* head;
public:
// default constructor: create an empty polynomial
Polynomial();
// copy constructor: use the content of p1 to create a polynomial
Polynomial(const Polynomial& p1);
// destructor
// release dynamic memory related to the polynomial
//~Polynomial();
// overloaded assignment operator
// assign the content of p1 to a polynomial
void operator=(const Polynomial& p1);
// overloaded += operator
// add a polynomial
void operator+=(const Polynomial& p1);
// overloaded -= operator
// subtract a polynomial
void operator-=(const Polynomial& p1);
// read a polynomial
void read();
// output a polynomial
void print();
//new input function
void insertTerm(bool,int,int);
};
Polynomial::Polynomial()
{
head=NULL;
}
void Polynomial::operator+=(con
{
cout<<"Start print"<<endl;
TermType* poly1=head;
TermType* poly2=p1.head;
int counter=0;
while ((poly1!=NULL)||(poly2!=NU
{
counter++;
if (poly2==NULL)
cout<<poly1->coefficient<<
if (poly1==NULL)
cout<<poly2->coefficient<<
if (poly1!=NULL&&poly2!=NULL)
{
if (poly1->exponent==poly2->e
{
cout<<poly1->coefficient+p
if (poly1!=NULL)
poly1=poly1->next;
if (poly2!=NULL)
poly2=poly2->next;
continue;
}
if (poly1->exponent>poly2->ex
{
cout<<poly1->coefficient<<
if (poly2!=NULL)
poly2=poly2->next;
continue;
}
if (poly2->exponent>poly1->ex
{
cout<<poly2->coefficient<<
if (poly1!=NULL)
poly1=poly1->next;
continue;
}
}
}
}
void Polynomial::insertTerm(boo
{
cout << "begin insert!" << endl;
cout << negCoef << " " << coef << " " << exp << endl;
TermType* newTerm = new TermType;
if (negCoef == 1)
newTerm->coefficient = 0 - coef;
else
newTerm->coefficient = coef;
newTerm->exponent = exp;
newTerm->next = NULL;
TermType* current = head;
if (head == NULL)
head = newTerm;
else
{
while (current->next != NULL)
current=current->next;
current->next = newTerm;
}
cout << "end insert!" << endl;
}
//read a polynomial
void Polynomial::read()
{
string s;
cout << "Enter a term (ex: +4x^3): ";
cin >> s;
int index = 0;
bool firstTerm = 1;
bool nowExp = 0;
bool negativeCoef = 0;
int coef = 0;
int exp = 0;
while (s[index] != '#')
{
cout << "char : " << s[index] << endl;
if (s[index] == '+' || s[index] == '-')
{
cout << "+-" << endl;
if (firstTerm == 0)
{
if (coef != 0)
insertTerm(negativeCoef, coef, exp);
}
else
{
firstTerm = 0;
}
nowExp = 0;
if (s[index] == '-')
negativeCoef = 1;
else
negativeCoef = 0;
coef = 0;
exp = 0;
}
else if (s[index] == 'x')
{
cout << "x" << endl;
if (s[index+1] != '^')
{
cout << "Incorrect formula!" << endl;
head = NULL;
break;
}
}
else if (s[index] == '^')
{
cout << "^" << endl;
nowExp = 1;
}
else if (s[index]>='0' && s[index]<='9')
{
cout << "number" << endl;
if (nowExp == 0)
coef = coef*10 + (s[index]-48);
else
exp = exp*10 + (s[index]-48);
}
else
{
cout << "The formula contains invalid characters: " << s[index] << endl;
head = NULL;
break;
}
index++;
}
if (coef != 0)
insertTerm(negativeCoef, coef, exp);
cout << "done!"<< endl;
}
void Polynomial::print()
{
TermType* newTerm=head;
while (newTerm != NULL)
{
cout<<newTerm->coefficient
newTerm=newTerm->next;
}
cout << endl;
}
int main()
{
Polynomial p, p1;
p.read();
cout<<"Second Term:"<<endl;
p1.read();
//p.print();
//p1.print();
p+=(p1);
system ("pause");
return 0;
}
ASKER
I've posted in another topic, if you have time I can really use some more help. Either way, thanks for everything so far.
ASKER
Lots of work here, every bit was a help.
Glad to be of help - pity I didn't post that comment about the OR originally.
(ps. It sounds wierd what you say you are doing - are you adding two lists of polynomials to result in ONE final polynomial ?)