I have everything almost done, the only problem I have is that I can't seem to get the linked list to advance when the terms start out with different exponents and instead of just printing the results I will tie them into a third list. Can you see what I'm doing wrong with advancing through the lists when the terms start out different? This would apply only to the overloaded += and -= operators.

#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;
}
Polynomial::~Polynomial()
{
	TermType* newTerm=head;
	TermType* current=head;
	while(current != NULL)
  {
	  current = current->next;
      delete newTerm;
      newTerm=current;
  }



}
void Polynomial::operator-=(const Polynomial& p1)
{
	TermType* poly1=head;
	TermType* poly2=p1.head;
	int counter=0;
	while ((poly1!=NULL)||(poly2!=NULL))
	{
		if (poly2==NULL&&poly1!=NULL)
		{
			if (poly1->coefficient>0)
				cout<<"+";
			cout<<poly1->coefficient<<"x^"<<poly1->exponent;
			poly1=poly1->next;
		}
		if (poly1==NULL&&poly2!=NULL)
		{
			if (poly2->coefficient>0)
				cout<<"+";
			cout<<poly2->coefficient<<"x^"<<poly2->exponent;
			poly2=poly2->next;
		}
		if (poly1!=NULL&&poly2!=NULL)
		{
			if (poly1->exponent==poly2->exponent)
				{
					if(poly1->coefficient+poly2->coefficient>0)
						cout<<"+";
					cout<<poly1->coefficient-poly2->coefficient<<"x^"<<poly1->exponent;
							if (poly1!=NULL)
								poly1=poly1->next;
							if (poly2!=NULL)
								poly2=poly2->next;
							continue; 
				}
			if (poly1->exponent>poly2->exponent)
				{
					if (poly1->coefficient>0)
						cout<<"+";
					cout<<poly1->coefficient<<"x^"<<poly1->exponent;
						poly2=poly2->next;
					continue;
				}
			if (poly2->exponent>poly1->exponent)
				{
					if (poly2->coefficient>0)
						cout<<"+";
					cout<<poly2->coefficient<<"x^"<<poly2->exponent;
						poly1=poly1->next;				
					continue;
				}			
		}
	}
}
void Polynomial::operator+=(const Polynomial& p1)
{
	TermType* poly1=head;
	TermType* poly2=p1.head;
	int counter=0;
	while ((poly1!=NULL)||(poly2!=NULL))
	{
		if (poly2==NULL&&poly1!=NULL)
		{
			if (poly1->coefficient>0)
				cout<<"+";
	//		if (poly1->coefficient<0)
	//			cout<<"-";
			cout<<poly1->coefficient<<"x^"<<poly1->exponent;
			poly1=poly1->next;
		}
		if (poly1==NULL&&poly2!=NULL)
		{
			if (poly2->coefficient>0)
				cout<<"+";
	//		if (poly2->coefficient<0)
	//			cout<<"-";
			cout<<poly2->coefficient<<"x^"<<poly2->exponent;
			poly2=poly2->next;
		}
		if (poly1!=NULL&&poly2!=NULL)
		{
			if (poly1->exponent==poly2->exponent)
				{
					if(poly1->coefficient+poly2->coefficient>0)
						cout<<"+";
				//	if(poly1->coefficient+poly2->coefficient<0)
				//		cout<<"-";
					cout<<poly1->coefficient+poly2->coefficient<<"x^"<<poly1->exponent;
							if (poly1!=NULL)
								poly1=poly1->next;
							if (poly2!=NULL)
								poly2=poly2->next;
							continue; 
				}
			if (poly1->exponent>poly2->exponent)
				{
					if (poly1->coefficient>0)
						cout<<"+";
				//	if (poly1->coefficient<0)
				//		cout<<"-";
					cout<<poly1->coefficient<<"x^"<<poly1->exponent;
						poly2=poly2->next;
					continue;
				}
			if (poly2->exponent>poly1->exponent)
				{
					if (poly2->coefficient>0)
						cout<<"+";
				//	if (poly2->coefficient<0)
				//		cout<<"-";
					cout<<poly2->coefficient<<"x^"<<poly2->exponent;
						poly1=poly1->next;				
					continue;
				}			
		}
	}
}
void Polynomial::insertTerm(bool 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)
	{
		if (newTerm->coefficient>0)
			cout<<"+";
		cout<<newTerm->coefficient<<"x^"<<newTerm->exponent;
		newTerm=newTerm->next;
	}
	cout << endl;
}
int main()
{
	Polynomial p, p1;	
	
		char option;
do //setting up a menu
	{
		
		cout << endl << endl << "Choose from:" << endl << endl;
		cout << "1. Enter first polynomial:" << endl;
		cout << "2. Enter second polynomial:" << endl;
		cout << "3. Print first polynomial:" << endl;
		cout << "4. Print second polynomial:"<<endl;
		cout << "5. Add polynomials:"<<endl;
		cout << "6. Subtract Polynomials:"<<endl;
		cout << "9. Exit:"<<endl;
		cin >> option;

		switch (option)
		{
		case '1':
			p.read();
			break;
		case '2':
			p1.read();
			break;
		case '3':
			p.print();
			break;
		case '4':
			p1.print();
			break;
		case '5':
			p+=(p1);
			break;
		case '6':
			p-=(p1);
			break;
		case '9':
			
		default:
			cout << "invalid input!!! Try it again." << endl;
		}
	} while (option != '9');
	return 0;
}
Member Avatar for jencas

Any reason why you don't use std::list or std::vector?

one thing i would suggest is that you should make a varible class that holds all the information, then you could store them in a dynamic array inside the polynomial class and sort them by order of the power of x.

class varible
{
public:
 varible(coeff, powerOfX);
~varible() {}
private:
int coeff;
int powerOfX;
};

hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.