Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Class Polynomial

Status
Not open for further replies.

mspeppa19

IS-IT--Management
Sep 26, 2002
92
US
Hi, I have a class Polynomial defined as the following...

Class polynomial{
public:
Polynomial(int, int, int);
}

I'm not sure where i'm trying to go with this because I need to add two polynomials together for example

Poly1: 3x^3 + 2x^2 + x
Poly2: 4x^3 + x^2 + 2x


I am wondering how do I go about just reading the values in? That is where I am stuck...Will that polynomial constructor with three arguments work? or do I need to have 6 arguments??? If anyone can help me just understand how this process should work that would be perfect. Thanks
 
How can you represent a Polynomial ( 1 … n) with a limited number of input parameters? That makes no sense. Also where are the “operators”? How do you know if you should multiply/divide/add or subtract. Then where is the precedence?

I just don’t get it.
 
I dont knoooow :( That's the problem. I need a class polynomial, that will take two polynomials as input and add them together! I just dont understand how to set-up the initial class. How do I go about having a member function, let's say GetPoly(); that will allow the user to input two polynomials, and then add the two togethor, and output the result...
 
So... you say you already have the class. Do you mean you've been given this class to work with, or are you currently trying to create it and have room to work with it?

If you're using a pre-existing class, there should be some documentation for it somewhere. If not, you may have to wade through the code youself to figure out what it's doing. Not fun for normal classes, less fun for mathematical ones.

If I were making a polynomial class, I might make a class Term that contains an exponent and a coefficient. A Polynomial would be a collection of Terms. Two Terms in the same Polynomial with the same exponent have their coefficients added together. That would make it easy to add two Polynomials together: just make a Polynomial that has all the terms in both of its arguments. Subtraction and other arithmetic should be relatively easy, too.

Keep in mind I've never had cause to use a Polynomial class, and I've never used them for anything in real life (except passing math classes), so I don't know what types of things you'd expect to use it for. There may be some design that's more appropriate.
 
No, I dont have the class. I have no idea where to start blah. Pretty much the specifics are that the program should be able to cin two polynomials, and then the program will then add the two polynomials together...My problem is I'm not sure how to store polynomials in a class. So let's say I do have a class term{ would I make int exponent1, exponent2....int coefficient1,coefficient2 etc... Meaning, one variable for each coefficient that may exist (i.e. three degree polynomial 3x^3 + 2x^2 + x)
 
Can you please provide a little additional detail relating to the format of the input. For example woukld the following be acceptable input..

poly1: 3x^3 + 2x^2 + x
poly2: 2x^4 + 3x^2

Or would the required input be..

poly1: 0x^4 + 3x^3 + 2x^2 + x
poly2: 2x^4 + 0x^3 + 3x^2 + 0x

I suppose what I am asking is, can we guarantee that each input will have the same number of terms?
 
What I had suggested was to use a class Term that looks like this:

class Term
{
public:
Term( int coefficient, int exponent );
double getCoefficient() const;
double getExponent() const;
void setCoefficient( int c );
void setExponent( int e );
};

which would represent something like "3x^4" or "5x^2."

A Polynomial would be a collection of Terms. So something like:

class Polynomial
{
public:
void addTerm( Term t );
void removeTerm( int i ); // remove term with exp i
Term getTermAt( int i ) const; // get term with exp i
};


Adding a Term to a Polynomail would involve combining Terms with like exponents.
 
The proper input would be:

poly1: 3x^3 + 2x^2 + x
poly2: 2x^3 + 3x^2 + 5x

Being that there the exponent is no higher than 3, and there are always 3 terms....So ChipperMDW, I understand the definition of the class term and polynomial, but how would it be implemented/initialized???
 
Well, Term should be easy to implement. It'd make your life easier if you told it you can only add two terms with the same exponent.

Adding terms to a Polynomial would involve adding the Terms contained within the Polynomial.

An important programming skill is being able to figure out how to implement a class given a declaration and a description.


However, this sounds like a class assignment, and since they give you a limit on the highest exponent, they probably want something simpler. So, given that information, your original declaration of class Polynomial makes more sense. The three parameters are probably the three coefficients, and they probably go in the order of ^1, ^2, ^3 (or ^3, ^2, ^1).
 
So, with all this how do I input and then store the polynomial??!? I am trying to figure it out but I just dont understand how to store the terms and polynomials, and how to go about declaring my function so it can input the terms for both polynomials...ooh yeah btw, the polynomial when inputted won't be inputted with the operators (+, - etc...) just the coefficient and exponents
 
As I said before, an important part of programming is being able to look at a problem and reason out how to solve it. You should be able to look at a class declaration and think of how to implement it. This is a pretty simple class you're describing. It's not complex. You may be making it too complicated.


So it's just the coefficient and the exponents on input? Like this?

2 3 3 2 5 1

I'm not sure what could be problematic about input there. Just read in the integers.


If it's more like this:

2x^3 3x^2 5x^1

Then that's still pretty easy. Read in the integer, ignore until 'x' (assuming that's always the variable; read in a char and ignore it if that's not the case), ignore until '^', then read in the next integer; repeat twice.


As for storing it, then assuming your exponents are always 1, 2, and 3, then all you have to do is store the coefficients and keep track of which one is for which exponent.


About declaring you input function... what's the problem? What do you have so far? Or what do you have in mind?
 
Ok, that's great. Another question, how do I go about overloading the addition operator??? For example I have


Polynomial operator+(......)

how do I complete this function so I will be able to add the two polynomials together?
 
Code:
// declaration
Polynomial operator +(const Polynomial & rhs);

// implementation
Polynomial Polynomial::operator +(const Polynomial & rhs){

	Polynomial poly;
	// implement addition and set values of "poly"
	return poly;
}
You might need to do some research on operator overloading in C++. It is not always as simple as one might think. The implications can be numerous.

-pete
-pete
 
In the function what does "rhs" stand for???
 
>> In the function what does "rhs" stand for???

Right Hand Side

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top