Hey guys, I'm mostly done with this but need a little help with multiplication and the output. (Just outputting +'s)

#include<iostream>


using namespace std;

class Polynomial
{
friend ostream &operator<<(ostream&, Polynomial&);

private:
	int degree;
	int coefficient[10];

public:
	Polynomial(int, int[]);
	Polynomial operator+(Polynomial&);
	Polynomial operator-(Polynomial&);
	Polynomial operator*(Polynomial);
	
};

Polynomial::Polynomial(int d, int c[])
{
	for(int x=0;x<10;x++)
	{
		coefficient[x]=0;
	}

	degree = d;
	
	for(int x=d;x>=0;x--)
	{
		coefficient[x]=c[d-x];
	}
}

Polynomial Polynomial::operator+(Polynomial &right)
{
	int newdegree = right.degree;
	int sum[10]={0}; 
	
	for(int x= 0;x<=newdegree;x++)
	{
		sum[newdegree-x] = coefficient[x] + right.coefficient[x];
	}

	return Polynomial(newdegree, sum);
}

Polynomial Polynomial::operator-(Polynomial &right)
{
	int newdegree = right.degree;
	int sum[10]={0}; 
	
	for(int x=0; x<=newdegree;x++)
	{
		sum[newdegree-x] = right.coefficient[x] - coefficient[x];
	}

	return Polynomial(newdegree, sum);
}

Polynomial Polynomial::operator*(Polynomial right)
{
	int newdegree = right.degree;
	int sum[10]={0}; 
	
	for(int x= 0;x<=newdegree;x++)
	{
		sum[newdegree-x] = coefficient[x] * right.coefficient[x];
	}

	return Polynomial(newdegree,sum);
}

ostream &operator<<(ostream &output, Polynomial &p)
{
	for(int n=p.degree;n>=0;n--)
	{
		if(p.coefficient[n]>0)
		{
			output << p.coefficient[n];
			if(n > 0 && n != 1)
			{
				output << "x^" << n;
			}
			else if(n==1)
			{
				output << "x";
			}
			if(p.coefficient[n-1] > 0 && n != 0)
			{
				if(n > 0)
				{
					output << "+";
				}
			}
		
		

		}
		if(p.coefficient[n]<0)
		{
			output << p.coefficient[n];
			if(n > 0 && n != 1)
			{
				output << "x^" << n;
			}
			else if(n==1)
			{
				output << "x";
			}
		}
		
	}
	return output;
}


int main()
{
	int test1[] = {1, 2, 3, 4};
	Polynomial f(3, test1);

	cout << f << endl;			// OUTPUT:  1x^3 + 2x^2 + 3x + 4

	int test2[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 4};
	Polynomial g(9, test2);

	cout << g << endl;			// OUTPUT:  1x^9 + 4

	int empty[] = {0};
	Polynomial h(0, empty);
	h = f + g;					// do NOT just do cout << f + g << endl;
	cout << h << endl;

	
	Polynomial j(0, empty);
	j = f - g;
	cout << j << endl;

	// (FOR * symbol, make sure to NOT use any &s.  Otherwise, it thinks the *
	//   refers to POINTER *, not MULTIPICATION *)

	Polynomial k(0, empty);
        k = g*3;
	cout << k << endl;

	return 0;
}

this is my current error
poly.cpp(146): error C2679: binary '*' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
1> poly.cpp(18): could be 'Polynomial Polynomial::operator *(Polynomial)'
1> while trying to match the argument list '(Polynomial, int)'

Thanks!

Try this:

//if any of the remaining coefficients is more than zero, then print out "+"
for(int j = n-1; j > 0; --j)      
   if(p.coefficient[j] > 0)
        cout << "+";

Assuming degree is the largest exponent in the polynomial associated with a nonzero coeffieient then the degree of the product is the sum of the degrees of the two polynomials being multiplied together. Multiplication involes multiply each term of one polynomial by each term of the other. Each individual product term thus produced has the coefficient of p1.coeff * p2.coeff and the exponent is the sum of p1.exp + p2.exp. Once all of the individual product terms have been generated you can simplify the polynomial by adding the coefficients of each term with the same exponent, or you can simplify as you go by setting the coefficients of each term in the product to zero before you start multiplying terms and add each exponent to the current value of the coefficient of the appropriate term as it is generated. Using a nested loop is probably the most efficient way I know of to generate each of the product terms.

>>poly.cpp(146): error C2679: binary '*' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

You could create a constructor that takes an int and converts it into a polynomial or you could overload the * operator to take a polynomial and an int. But that wouldn't cover you in this case:

polyB = 3 * polyA;

Try this:

//if any of the remaining coefficients is more than zero, then print out "+"
for(int j = n-1; j > 0; --j)      
   if(p.coefficient[j] > 0)
        cout << "+";

i tried this and it printed multiple +'s and some times didn't print when necessary.

Tweak it a bit. Try putting a break; after finding the first remaining nonzero coefficient to prevent the multiple + signs and changing the starting point for j.

for(int j = degree - 1; j > 0; --j) 
{     
   if(p.coefficient[j] > 0)
   {
        cout << "+";
        break;
   }
}

Nah, you don't want to start j at degree either. Must be getting tired, sorry. All right, so try something this.

//assuming p.coefficient contains all coeffients, even if the coefficient is zero. 
//for each term in p
for(int n = p.degree; n >= 0; n--)	
{		
    if(p.coefficient[n] != 0)//print only terms with non zero coefficient	
   {			
       output << p.coefficient[n];			
       if(n > 0)			
      {
          if(n > 1)				
             output << "x^" << n;					
          else //if n == 1				
            output << "x";			
      			
          if(p.coefficient[n - 1] > 0)			
              output << " + ";				
      }  
  } 		
}

Skip the empty line between the if and the else, must be a tab or something I didn't erase.

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.