I am working on a project to solve a class in polynomial. I have successfully managed to do the header file and 75% of the code..but two functions to write and add the polynomials don't seem to work. Can someone help? The last part of the code!

#include <cassert>
#include <iostream>
#include "poly.h"

using namespace std;

#define NULL 0

const unsigned int poly::SIZEINCR = 5;

poly::poly()
{

	variable = 'x';
	nterms = 0;
	terms = new term[MAXSIZE = SIZEINCR];
};

void poly::copy (const poly & p)
{
	
	
	variable = p.variable;
	nterms = p.nterms;
	terms = new term[MAXSIZE = p.MAXSIZE];
	for (unsigned int i=0; i<nterms; i++)
		terms[i] = p.terms[i];
};

poly::poly (const poly & p) 
{
	
	copy (p);
};

void poly::free (void)
{
	
	nterms = 0;
	delete [] terms;
	terms = NULL;
};

poly::~poly (void)
{
	
	free ();
};

poly & poly::operator= (const poly & p) 
{
	
	if (this != &p)
	{
		free ();
		copy (p);
	};
	return (*this);
};

void poly::expand ()
{
	
	termptr p;

	p = new term[MAXSIZE += SIZEINCR];
	for (unsigned int j=0; j<nterms; j++) p[j] = terms[j];
	delete [] terms;
	terms = p;
};

void poly::InsertTerm (term t)
{
	

	int i = nterms-1;
	unsigned int e = t.exp;

	if (nterms == MAXSIZE) expand ();

	nterms++;
	while ((i>=0) && (e > terms[i].exp))
	{
		terms[i+1] = terms[i];
		i--;
	};
  
	assert ((i<0) || (e != terms[i].exp));
	terms[i+1] = t;
};

void poly::read ()
{
	
	
	poly temp;
	int coeff;
	int exp;
	cout << "Input a polynomial by specifying the variable and all terms in any order." << endl
		<< "Each term is specified by an integer coefficient and" << endl
		<< "a non-negative integer exponent." << endl
		<< "Indicate END by specifying a dummy term with" << endl
		<< "a zero coefficient and/or a negative exponent." << endl;
	cin >> temp.variable;
	do
	{
		cin >> coeff;
		if (coeff)
		{
			cin >> exp;
			if (exp >= 0)
				temp.InsertTerm (term(coeff, exp));
		}
		else
			while (cin && (cin.peek() != '\n')) cin. ignore();
	} while (coeff && (exp >= 0));
	*this = temp; 
};

void poly::write() const
{
	// I need help!!!
};

poly poly::plus (const poly & right) const
{
	
};

poly poly::minus (const poly & right) const
{
	
};

float poly::evaluate (float value) const
{
	
};

I would insert each term in terms in ascending order based on the exponent so that the exp term with exponent 0 has index 0 in terms, etc. I would expand terms if the exponent of a term to be added to terms is above value of nterms. The default coefficient of each term in terms would be zero. Each term inserted into terms would appropriately adjust the value of the coeffient of the term with the same exponent.

To write in standard polynomial form with largest exponent first I would then start at the end of terms and loop backward. Each term could be output only in the value of the coefficient wasn't zero. The display format would be coefficient followed by variable followed by ^ sign followed by exponent without spaces between each of the values in the display.

To add two terms I would determine the polynomial with the smallest value of nterms and expand it to the same number of nterms as the larger one. Looping through each term in the polynomials and adding ( or subtracting as the case may be) the coefficients of terms with the same exponent will result in the coefficient of the term a third polynomial representing the sum.

Comments
Good post.

Thank you!
But the write function doesn't seem to work or i am going in the wrong direction. can u help me

Thank you!
But the write function doesn't seem to work or i am going in the wrong direction. can u help me

Post poly.h and poly::write (). We have no idea what you might be doing wrong unless you post the code.

this is the poly.h file

class poly
{
public:
	poly();
	poly (const poly &);
	~poly ();
	poly & operator= (const poly &);
	void read ();
	void write () const;
	poly plus (const poly &) const;
	poly minus (const poly &) const;
	float evaluate (float) const;

private:
	struct term
	{
		int coeff;
		unsigned int exp;
		term () {}; declaration.
		term (int c, unsigned int e) : coeff(c), exp(e) {};
		char sign () const {return ((coeff<0)?'-':((coeff==0)?'0':'+'));};
	};
	typedef term * termptr;
	static const unsigned int SIZEINCR;
	unsigned int MAXSIZE;
	unsigned int nterms;
	termptr terms;
	char variable;
	
	void copy (const poly &);
	void free ();
	void InsertTerm (term);
	void expand ();
};

Thank you!
But the write function doesn't seem to work or i am going in the wrong direction. can u help me

The poly::write () function you posted in post 1 is empty. You're saying "it doesn't seem to work", so presumably it's no longer empty. Please post the updated poly::write () function.

That is where i am working on! am not able to go further

That is where i am working on! am not able to go further

Read Lerner's earlier post again. You want to loop through your terms[] array. For each element, print the sign, then print the coefficient, then print the variable symbol ('x'), then the '^' symbol, then the exponent.

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