Hey there, I'm having trouble creating a multiplication function for the program as I have two classes to keep in mind (Monomial and Polynomial). And if possible, a little advice on how to make my for statements in the arithmetic operators more flexible to user input such as i<PolynomialSize, which I just can't seem to find a way around it. So any help or hints would be helpful. My functions work partially, by the way. Here's what I got so far:

/*-- Polynomial.h ---------------------------------------------------------
 Defines the monomial and polynomial classes. Polynomial is implemented
 basically as a list of monomials.
---------------------------------------------------------------------------*/

#include <iostream>

#ifndef PNOM
#define PNOM

const int CAPACITY = 1024;
//typedef int ElementType;

class Monomial {
private:
    float coef;
    int        exp;
public:
    Monomial(){};
    /*----------------------------------------------------------------------
     Construct a Monomial object.
     
     Precondition:  None
     Postcondition: An empty Monomial object has been constructed..
   -----------------------------------------------------------------------*/
    Monomial(float c,int p) { coef = c; exp = p;};
    /*----------------------------------------------------------------------
     Construct a Monomial object with specified coeffient and exponent.
     
     Precondition:  None
     Postcondition: A Monomial object has been constructed with the 
                  specified coeffient and exponent.
   -----------------------------------------------------------------------*/
    friend Monomial operator+ (Monomial&, Monomial&);
    /*----------------------------------------------------------------------
     Overloading the operator+ so we can sum two monomials.
     
     Precondition:  The monomials have the same exponent.
     Postcondition: A Monomial object has been created and returned.
   -----------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------------------------*/
//Prototypes for the functions of the exercise #5 of the homework.

    friend Monomial operator- (Monomial&, Monomial&);
    /*----------------------------------------------------------------------
     Overloading the operator- so we can substract two monomials.
     
     Precondition:  The monomials have the same exponent.
     Postcondition: A Monomial object has been created and returned.
   -----------------------------------------------------------------------*/

    friend Monomial operator* (Monomial&, Monomial&);
    /*----------------------------------------------------------------------
     Overloading the operator* so we can multiply two monomials.
     
     Precondition:  The monomials do not have to have the same exponent.
     Postcondition: A Monomial object has been created and returned.
   -----------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------------------------*/
    
    
    friend ostream & operator<< (ostream & out, const Monomial & mono);    
    /*----------------------------------------------------------------------
     Overloading the OUTPUT operator for Monomials. 
     
     Precondition:  None.
     Postcondition: The coefficient and exponent (if != 0) of the monomial are 
                    displayed in the default output device.
   -----------------------------------------------------------------------*/
};

typedef Monomial ElementType;

class Polynomial
{
 public:

   Polynomial();
   /*----------------------------------------------------------------------
     Construct a List object.

     Precondition:  None
     Postcondition: An empty List object has been constructed; mySize is 0.
   -----------------------------------------------------------------------*/

   /***** empty operation *****/
   bool empty() const;
   /*----------------------------------------------------------------------
     Check if a list is empty.

     Precondition:  None
     Postcondition: true is returned if the list is empty, false if not.
   -----------------------------------------------------------------------*/

   /***** insert and erase *****/
   void insert(ElementType item, int pos);
   /*----------------------------------------------------------------------
     Insert a value into the list at a given position.

     Precondition:  item is the value to be inserted; there is room in
         the array (mySize < CAPACITY); and the position satisfies
         0 <= pos <= mySize. 
     Postcondition: item has been inserted into the list at the position
         determined by pos (provided there is room and pos is a legal
         position).
   -----------------------------------------------------------------------*/

   void erase(int pos);
   /*----------------------------------------------------------------------
     Remove a value from the list at a given position.

     Precondition:  The list is not empty and the position satisfies
         0 <= pos < mySize.
     Postcondition: element at the position determined by pos has been
         removed (provided pos is a legal position).
   ----------------------------------------------------------------------*/

   /***** output *****/
   void display(ostream & out) const;
   /*----------------------------------------------------------------------
     Display a list.

     Precondition:  The ostream out is open. 
     Postcondition: The list represented by this List object has been
         inserted into out. 
   -----------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------------------------*/
//Prototypes for the functions of the exercise #5 of the homework.

friend Polynomial operator+(Polynomial &, Polynomial &);
friend Polynomial operator-(Polynomial &, Polynomial &);
friend Polynomial operator*(Polynomial &, Polynomial &);


 private:
   int mySize;                     // current size of list stored in myArray
   ElementType myArray[CAPACITY];  // array to store the Monomials

}; //--- end of List class

//------ Prototype of output operator
ostream & operator<< (ostream & out, const Polynomial & p);

#endif
/*-- Polynomial.cpp------------------------------------------------------------
 
   This file implements List member functions.

-------------------------------------------------------------------------*/

#include <cassert>
using namespace std;

#include "Polynomial.h"


/*----------------------------------------------------------------------
 Member and friend functions for Monomial.
 -----------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------------*/
//Functions for the exercise #5 of the homework.

Monomial operator+(Monomial& a, Monomial& b) {
    Monomial res;

    for(int i = 0; i < 4; i++){
    if(a.exp == b.exp){
        res.coef = a.coef + b.coef;
        res.exp = a.exp;
    }
    else if(a.exp != b.exp){
        res.coef = a.coef;
        res.exp = a.exp;
    }
    else if(b.exp != a.exp){
        res.coef = b.coef;
        res.exp = b.exp;
    }
    }
    return res;
}
Monomial operator-(Monomial& a, Monomial& b) {
    Monomial res;

    for(int i = 0; i < res.mySize; i++){
    if(a.exp == b.exp){
        res.coef = a.coef - b.coef;
        res.exp = a.exp;
    }
    else if(a.exp != b.exp){
        res.coef = a.coef;
        res.exp = a.exp;
    }
    else if(b.exp != a.exp){
        res.coef = b.coef;
        res.exp = b.exp;
    }
    }
    return res;
}

Monomial operator*(Monomial& a, Monomial& b) {
    Monomial res;

    res.coef = a.coef * b.coef;
    res.exp    = a.exp + b.exp;

    return res;
}
/*-------------------------------------------------------------------------------------------*/



ostream & operator<< (ostream & out, const Monomial & mono)
{
    out << mono.coef ;
    if (mono.exp > 0) out << "x^" << mono.exp; 
    return out;
}


/*----------------------------------------------------------------------
 Member and friend functions for Polinomial.
 -----------------------------------------------------------------------*/

Polynomial::Polynomial()                 
: mySize(0)
{}


bool Polynomial::empty() const
{
   return mySize == 0;
}


void Polynomial::display(ostream & out) const
{
    for (int i = mySize-1; i >= 0 ; i--) {
        if (i != mySize-1) out << " + ";
        out << myArray[i];
    }

}


ostream & operator<< (ostream & out, const Polynomial & aList)
{
   aList.display(out);
   return out;
}


void Polynomial::insert(ElementType item, int pos)
{
   if (mySize == CAPACITY)
   {
      cerr << "*** No space for list element -- terminating "
              "execution ***\n";
      exit(1);
   }
   if (pos < 0 || pos > mySize)
   {
      cerr << "*** Illegal location to insert -- " << pos 
           << ".  List unchanged. ***\n";
      return;
   }

   // First shift array elements right to make room for item

   for(int i = mySize; i > pos; i--)
      myArray[i] = myArray[i - 1];

   // Now insert item at position pos and increase list size  
   myArray[pos] = item;
   mySize++;
}


void Polynomial::erase(int pos)
{
   if (mySize == 0)
   {
      cerr << "*** List is empty ***\n";
      return;
   }
   if (pos < 0 || pos >= mySize)
   {
      cerr << "Illegal location to delete -- " << pos
           << ".  List unchanged. ***\n";
      return;
   }

   // Shift array elements left to close the gap
   for(int i = pos; i < mySize; i++)
       myArray[i] = myArray[i + 1];

   // Decrease list size
    mySize--;
}

/*-------------------------------------------------------------------------------------------*/
//Functions for the exercise #5 of the homework.

Polynomial operator+(Polynomial &p1, Polynomial &p2){
    Polynomial res;
    int i = 0;

    if (p1.mySize > p2.mySize){
        res.mySize = p1.mySize;
    }else{
        res.mySize = p2.mySize;
    }

    for(i = 0; i < res.mySize; i++){
        res.myArray[i] = p1.myArray[i] + p2.myArray[i];
    }

    return res;

}

Polynomial operator-(Polynomial &p1, Polynomial &p2){
    Polynomial res;
    int i = 0;

    if (p1.mySize > p2.mySize){
        res.mySize = p1.mySize;
    }else{
        res.mySize = p2.mySize;
    }

    for(i = 0; i < res.mySize; i++){
        res.myArray[i] = p1.myArray[i] - p2.myArray[i];
    }

    return res;

}

Polynomial operator*(Polynomial &p1, Polynomial &p2){
    Polynomial res;
    int i = 0;

    if (p1.mySize > p2.mySize){
        res.mySize = p1.mySize;
    }else{
        res.mySize = p2.mySize;
    }

    for(i = 0; i < res.mySize; i++){
        res.myArray[i] = p1.myArray[i] * p2.myArray[i];
    }

    return res;

}
//--- Program to test Polynomial class.

#include <iostream>
using namespace std;

#include "Polynomial.h"

int main()
{
    float coefficient;
    int exp;

    Polynomial myPoly01;
    Polynomial myPoly02;
    Polynomial poly03;
    
    //cout << "Please enter coefficient <ENTER> power <ENTER>" << endl;
    do  {
        cout << "Enter coefficient of monomial (or 0 to end):";
        cin >> coefficient;
        if (coefficient !=0) {
            cout << "Enter exponent of monomial:";
            cin >> exp;
            myPoly01.insert(Monomial(coefficient,exp),0); }
    } while(coefficient != 0);
    cout << myPoly01 << endl;

/*---------------------------------------------------------------------------------------------*/
// Edited function main() for the functions of the exercise #5 of the homework.

    cout << "\nNow create another polynomial: " << endl;
    do  {
        cout << "Enter coefficient of monomial (or 0 to end):";
        cin >> coefficient;
        if (coefficient !=0) {
            cout << "Enter exponent of monomial:";
            cin >> exp;
            myPoly02.insert(Monomial(coefficient,exp),0); }
    } while(coefficient != 0);
    cout << myPoly02 << endl;

    cout << "\nAdding the two polynomials: " << endl;
    poly03 = myPoly01 + myPoly02;
    cout << poly03 << endl;

    cout << "\nSubtracting the two polynomials: " << endl;
    poly03 = myPoly01 - myPoly02;
    cout << poly03 << endl;

    cout << "\nMultiplying the two polynomials together: " << endl;
    poly03 = myPoly01 * myPoly02;
    cout << poly03 << endl;
    
    return 1;
    
}

Recommended Answers

All 2 Replies

Fundarmentaly you have two problems , (a) that you are using the wrong algorithm, (b) that you have put the multiply operator as a friend method, and not a standard method of the class.

class Polynominal
{
   // stuff
   Polynominal operator*(const Polynomial&,const Polynominal&) const;
};

is a better way forward.

Then you want to consider some simple polynomials and do the multiplication e.g (x^2+3x+2)*(x^3+3x)=x^5+3x^4+5x^3+9x^2+6x You get up to degree of first * degree of second polynomial terms. Therefore you are going to need a double loop e.g. one over the first array, and one over the second array of Monomials, and then you are going to have to add up individual terms [Note: If the sum of the term goes to zero it needs to be removed!!-- e.g. (x-1)*(x+1) is x^2-1.

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.