So this is my final project for my class. The idea is to use my RationalNum class (which is complete and works fine... I just didn't attach the .cpp since it was only the driver) and use it to add, sub, & multi polynomials (RationalNum being the coefficients i.e. 1/4x^3 + 1/2x^2 + 1/3x, etc.).

My over all goal is to input the amount of terms I want and then input the rational coefficients and then the exponets.... the rest will be automatically calculated from there.

I am in a sense just starting this out and my mind is completely foggy and some clarification to as what I should do next would help out a lot. I'm not looking for code, just an idea of which way to step next. Pretty much have an open discussion...

Anyway I am going to re-read vectors to see if that might give me some insight.


Here's what I have so far:

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <vector>
#include "RationalNum.h"

using namespace std;


class Term
{
public:

	RationalNum coeff;
	int exponet;
};

class Polynomial
{
public:

	static const int maxTerm = 10; //Temporary since I want to select the amount of terms.

	Polynomial();
	Polynomial(const Polynomial &p);
	~Polynomial();

	void enterTerm();
	int getNumTerm() const;
         int getTermExp(int) const;
         int getTermCoeff(int) const;
         void setCoeff(int, int);

	void printAll() const;

	Polynomial operator+(const Polynomial& );
	//Polynomial operator-(const Polynomial& ); 
	//Polynomial operator*(const Polynomial& );
	//Polynomial operator+=(const Polynomial& );
	//Polynomial operator-=(const Polynomial& );
	//Polynomial operator8=(const Polynomial& );

	Polynomial operator+(const Term& addend) const;//added just for refrence below

private:

	int numTerm;
	int exponet[maxTerm];
	int coeff[maxTerm];
};

Polynomial::Polynomial()
{
   for(int t = 0; t < maxTerm; t++)
   {
      coeff[t] = 0;
      exponet[t] = 0;
   }
   numTerm = 0;
}

Polynomial Polynomial::operator+(const Polynomial &rhs) //This is what is stated in the project instruction sheet
{
	Polynomial Polynomial_LHS;
	
	Polynomial result = *this;

	vector<Term>::const_iterator PolyIter;
	
		for(PolyIter = addend.Term.begin(); PolyIter != addend.Term.end(); PolyIter++)
		{
			//the loop for adding terms
                           //result +=*PolyIter;
		}
		return result;
}

Polynomial Polynomial::operator+(const Term& addend) const //This is what is stated in the project instruction sheet

{
	//empty for time being
}

#endif

Oddly enough the Rational Number's part of this project was rather easy, this Polynomial part is where I stumble...

[My rational numbers class is attached below]
Thanks for the help in advance!

The issue isn't really the coefficient value that you have with the polynomial, it is that you have to realize, that the size of your polynomial will change dramatically, on addition, subtraction etc. Once you have figured out how to handle that, [in particular to remove zeros e.g. (x^2+3x) + (3x^4-3x) should give (3x^4+x^2).]

Note that in doing this you are going to benefit by writing a tidy output method, that keeps the polynomial in the typical form that you expect to see. I would actually do that first, since it really will help debugging.

Next tackle addition, and add in the methods that are going to make life easier, e.g. a sort of the powers, a zero remover, and an evaluation tool (e.g what is the value of the polynomial if x=5. Again this is useful for rapid testing/checking.

Finally, you obviously know about std::vector, so either use a list / vector for the polynomial terms, and don't bother with the max coefficient part [your maxTerm]. The polynomial will change as required. Then it is easy.

Finally, you obviously know about std::vector, so either use a list / vector for the polynomial terms, and don't bother with the max coefficient part [your maxTerm]. The polynomial will change as required. Then it is easy.

So would it be wise to write this as an array for problem solving first... or just dig into the "nitty gritty" of writing the vector?

I've been hammering this out for the past couple of hours trying to make some progress... slowly but surely...

The array is the problem. The polynomials are going to change size on every operation.The effect of that is you have to have something fluid. Just use a vector to sort the terms. That way if you add another polynomial, then you see if you have the term in the first polynomial, if so add the coefficients, but if you don't just append that term to the end of your list/vector.

Basically, the pseudo code of addition is this:

Add poly A to B:

for each term T in B:
  
   if (A contains exponent of T):
      add coefficients
   else:
     append T to A

// After loop:
Sort A [helps in the first test in the loop/pretty write out]
Remove zeros.

After you have a pseudo code for the algorithm you intend to use, then you can figure out what sort of data/structures you need. If you use arrays, I think they will be a complete mess.

Okay that makes sense... because overall in the structure is goes:

--->Term (Term = n+1)
      |
       -->Poly A
      |     |
      |      -->Coeff A n1, n2, n3, etc.
      |
       -->Poly B
            |
             -->Coeff B n1, n2, n3, etc.

I'll see about fixing that up and posting what I have here... thanks

Okay maybe my head still foggy on this... tell me if I am wrong some where.

class Polynomial
{
    vector<int>Poly;
public:
    Polynomial();
    Polynomial operator+(const Polynomial &rhs) const;

private:
   int exponet;
   int coeff;
};

Polynomial() //constructor
  exponet = 0;
  coeff = 0;

Polynomial operator+(const Polynomial &rhs) const //start addition
  Polynomial PolyResult; //setting up a return

  vector<Term>::const_iterator polyIter; //creating the basis of a vector (am I right?)

  for(polyIter = Poly.begin(); polyIter != Poly.end(); polyIter++) //dynamically create the vector
     rhs.coeff = polyIter->coeff;
     rhs.exponet = polyIter->exponet;
     PolyResult += *polyIter; //go to next 

return PolyIter;

//end addition

Am I even close on this one? I totally feel lost in the sauce...

Ah I think my failure here lies in the fact that I don't have a strong grasp on Polynomials with Rational Coefficients... and some websites are horrible at explaining it.

Anyone have a "Polynomials with Rational Coefficients for Dummies" book handy?

Okay I realized I've been going about this all wrong... it's not the math... it's the vector/array. Once I get things together again I'll repost.

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.