Polynomial

This is a discussion on Polynomial within the C++ Programming forums, part of the General Programming Boards category; Hi there, Could someone help with multiplying two polynomials. I have implemented a Polynomial class to overload the +, -, ...

  1. #1
    Unregistered
    Guest

    Polynomial

    Hi there,

    Could someone help with multiplying two polynomials. I have implemented a Polynomial class to overload the +, -, *operators...

    Polynomial class{

    public:
    Polynomial operator+(const Polynomial &)
    Polynomial operator*(const Polynomial &)
    // overload more operators ........

    private:
    int* ptr; // pointer of the first array element
    int size; // size of array
    }

    Description: a polynomial is an array of terms (each term will contain a coefficent & an exponent). In the array, the subscript is the exponent, whereas the contents is the coefficient.

    This is how the addition works. All it does is to add like terms.
    // ================== addition ====================
    // pass in a 2nd polynomial
    // Polynomial Polynomial:: operator+(const Polynomial &rt) {
    Polynomial tempPly(*this)
    for(int i=0; i<rt.size; i++)
    tempPly.ptr[i] = ptr[i] + rt.ptr[i];
    return tempPly;
    }

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Code:
    Polynomial &Polynomial:: operator+(const Polynomial &rt) {
       Polynomial *tempPly;
       tempPly = new Polynomial(rt.size);
       for(int i=0; i<rt.size; i++)
          tempPly->ptr[i] = ptr[i] + rt.ptr[i]; 
       return *tempPly; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Entering in an nth degree polynomial
    By Noah in forum C Programming
    Replies: 1
    Last Post: 03-02-2006, 08:02 PM
  2. Polynomial
    By CompiConQuiso in forum C++ Programming
    Replies: 4
    Last Post: 05-16-2005, 12:13 AM
  3. Polynomial Problem
    By softcoder76 in forum C++ Programming
    Replies: 5
    Last Post: 03-01-2002, 01:07 PM
  4. linked lists of a polynomial
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2002, 06:51 PM
  5. linked lists of a polynomial
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2002, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21