1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Polynomial help

Discussion in 'C++' started by risha_messi, Dec 27, 2009.

  1. risha_messi

    risha_messi New Member

    Joined:
    Dec 27, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    # include <iostream>
    using namespace std ;
    
    class Polynomial
    {
          private :
                    int *array ;
                    int x ;
          public  :
                    Polynomial(int y=0 , int arr[]=0)
                    {
                      x = y ;
                      array = new int [x+1] ; 
                    }
                    void  setPolynomial() ;
                    void  setPolynomial(int*) ;
                    int getPolynomial() ;
                     void operator =  (const Polynomial& ) ;
                     Polynomial operator *  (const Polynomial& ) ;
    };
    Polynomial Polynomial::operator *  (const Polynomial& p) 
    {
       x = p.x ;
       int* arr = new int [x+1];
       for ( int i = 0 ; i < x+1 ; i++ )
       {
          arr[i] = array[i] * p.array[i] ;
       }
       Polynomial result(x);
       result.setPolynomial(arr);
       return result;
    }
    
    int main ()
    {
        int  power ;
        char choice ;
    
        cout << "The Polynomial Function to the Power : " ;
        cin >> power ;
    
        int* poly1 ;
        Polynomial p1(power ,poly1);
        p1.setPolynomial() ;
        cout << "Enter Operator ( + , - , * , = , +=(1) , -=(2) , *=(3) ) : " ;
        cin  >> choice ;
        if ( choice == '*')
        {
             int* poly2 ;
             Polynomial p2(power ,poly2);
             p2.setPolynomial();
    
             Polynomial p3 ;
             p3 = p1 * p2 ;
             cout << endl ;
             p3.getPolynomial() ;
        }
    
        system("pause");
        return 0 ;
    }
    excuse me i want help
    in the operator * it only multiply the cofficients but i want it to multiply in this form

    (4x2 – 4x – 7)(x + 3)
    = (4x2 – 4x – 7)(x) + (4x2 – 4x – 7)(3)
    = 4x2(x) – 4x(x) – 7(x) + 4x2(3) – 4x(3) – 7(3)
    = 4x3 – 4x2 – 7x + 12x2 – 12x – 21
    = 4x3 – 4x2 + 12x2 – 7x – 12x – 21
    = 4x3 + 8x2 – 19x – 21

    can any one help me in this code plz
     
    Last edited by a moderator: Dec 28, 2009

Share This Page