I am supposed to implement an ADT polynomial, using an array for polynomial coefficients- using-
degree() - gives the highest degree in the polynomial.
coefficient(power)throw(InvalidPowerException)
changeCoefficient(newCoefficient, power)throw(InvalidPowerException)
overloaded + operator to add 2 polynomials
overloaded << operator

I cannot use a list class for this project.
special cout cases:
+1, -1 coefficients: don't display the 1 (unless power is 0)
power 0: don't display x^0
negative coefficients: don't display +
dirst term: don't display a preceding +

I am going to paste everything I did- the problem is when I am trying to slowly create the private data member (string) finalStr, it will not do the proper +, and I don't have a string to show at the end. it has got to be in the makeFinalString function.

#include <iostream>
#include <string>
using namespace std;

class Polynomial
{
		
	private:
		
		int thing[10];
		
		string finalStr;

		
	
	public:
		
		void makeFinalString(Polynomial& poly);
		
		Polynomial(const Polynomial& poly); // copy constructor
		
		Polynomial();	//default constructor

		~Polynomial(); // Destructor

		
		
		void setPolynomial();

		void showPolynomial();

		friend Polynomial operator+(const Polynomial& poly_a, const Polynomial& poly_b);

		friend ostream& operator<<(ostream& out, const Polynomial& poly);

		

};
//#endif

Polynomial::Polynomial()
{
	for(int i=0; i<10; i++)//initialize to 0's
		thing[i]=0;
	
	for(int i =0;i<10;i++)//display the polynomial
		cout << thing[i] << " ";
	
	cout << endl << "default constructor called\n";
}
Polynomial::~Polynomial()
{
	cout << "Destructor called\n";
}

void Polynomial::setPolynomial()
{
	int coefficient = 0;
	int power = 0;
	cout << "Please input values for the polynomial:\n";
	
	
	
    while(cin >> coefficient >> power)
    {
		if(power<0)
			break; // throw
		if(coefficient<0 && power<0)
			break; //end of polynomial
		if(power>9)
			break; //throw
        if(thing[power]==0)
			thing[power] = coefficient;
		else
			thing[power] +=coefficient;
    }
	for(int i =0;i<10;i++)//display the polynomial
		cout << thing[i] << " ";
}

void Polynomial::showPolynomial()
{
	bool success = 0;
	
	for(int i=9;i>=0;i--)
	{
		
		if(i == 0 && thing[0]!=0) // if power is 0
		{
			if(thing[0]<0)
			{
				cout << thing[0];
			}
			else 
			{
				if(success==1)
					cout << "+" << thing[0];
				else 
					cout << thing[0];
			}
		}
		if(i == 1 && thing[1]!=0) //if power is 1
		{
			if(thing[1]<0)
			{
				cout << thing[1] << "x";
				success = 1;
			}
			else //if(thing[1]>0)
			{
				if(success ==1)
				{
					cout << "+" << thing[1] << "x";
				}
				else
				{
					cout << thing[1] << "x";
					success = 1;
				}
			}
		}
		if(i>1 && thing[i]!=0) //if power is greater than 1
		{
			if(thing[i]<0)
			{
				cout << thing[i] << "x^" << i;
				success = 1;
			}
			else //if(thing[i]>0)
			{
				if(success==1)
				{
					cout << "+" << thing[i] <<"x^" << i;
				}
				else
				{
					cout << thing[i] << "x^" << i;
					success = 1;
				}
			}
		}
	}
}

Polynomial::Polynomial(const Polynomial& poly)
{
	for(int i=0; i<10; i++)
		thing[i] = poly.thing[i];
	finalStr = poly.finalStr;//??? or do I just want to re-calculate it?
	cout << "Copy constructor called\n";
}

Polynomial operator+(const Polynomial& polyA, const Polynomial& polyB)
{
	Polynomial tempPoly;

	for(int i=0; i<10; i++)
	{
		tempPoly.thing[i] = polyA.thing[i]+polyB.thing[i];
	}
	
	return tempPoly;//or do I want to recalculate the finalStr???
}

ostream& operator<<(ostream& out, const Polynomial& poly)
{
	out << poly.finalStr;

	return out;

}












void Polynomial::makeFinalString(Polynomial& poly)
{
	bool success = 0;
	
	for(int i=9;i>=0;i--)
	{
		
		if(i == 0 && poly.thing[0]!=0) // if power is 0
		{
			if(poly.thing[0]<0)
			{
				poly.finalStr += poly.thing[0];
				cout << endl << poly.finalStr;
			}
			else 
			{
				if(success==1)
				{
					poly.finalStr += '+';
					poly.finalStr += poly.thing[0];
					cout << endl << poly.finalStr;
				}
				else 
					poly.finalStr += poly.thing[0];
					cout << endl << poly.finalStr;
			}
		}
		if(i == 1 && poly.thing[1]!=0) //if power is 1
		{
			if(poly.thing[1]<0)
			{
				poly.finalStr += poly.thing[1]; + 'x';
				success = 1;
				cout << endl << poly.finalStr;
			}
			else //if(thing[1]>0)
			{
				if(success ==1)
				{
					poly.finalStr += '+' + poly.thing[1] + 'x';
					cout << endl << poly.finalStr;
				}
				else
				{
					poly.finalStr += poly.thing[1] + 'x';
					success = 1;
					cout << endl << poly.finalStr;
				}
			}
		}
		if(i>1 && poly.thing[i]!=0) //if power is greater than 1
		{
			if(poly.thing[i]<0)//if coefficient is smaller than zero 
			{
				if(poly.thing[i]!=1 && poly.thing[i]!=-1)//and it is not -1 or 1
				{
					poly.finalStr += poly.thing[i] + 'x^' + i;
					success = 1;
					cout << endl << poly.finalStr;
				}
				else//it is smaller than zero and it is -1 or 1
				{
					if(poly.thing[i]==-1)//if it is -1
					{
						poly.finalStr += '-x^' + i;
						success = 1;
						cout << endl << poly.finalStr;
					}
					else//it must be 1
					{
						poly.finalStr += 'x^' + i;
						success = 1;
						cout << endl << poly.finalStr;
					}
				}
			}
			else //if(thing[i]>0)
			{
				if(success==1)//it is not the first number
				{
					poly.finalStr += '+' + poly.thing[i] + 'x^' + i;
					cout << endl << poly.finalStr;
				}
				else//it is the first number
				{
					poly.finalStr += poly.thing[i] + 'x^' + i;
					success = 1;
					cout << endl << poly.finalStr;
				}
			}
		}
	}
	cout << endl << poly.finalStr << endl;
}





int main()
{
	Polynomial x;
	x.setPolynomial();
	cout << endl;
	x.makeFinalString(x);
	
	cout << x;
	cout << endl;

}

PLEASE HELP!!!!

I need to create a polynomial adder function at the end.

I need to display the polynomial with the cout << operator that I am overloading. I need to do something like cout << MyPolynomial and that it will display the polynomial the way it should be:
4x^5+x^6-5x^7+9 is an example.

YES!!! I figured it out, but I still have an issue there: I got it to work, but where do I put the try and catch?

This article has been dead for over six months. Start a new discussion instead.