Expand|Select|Wrap|Line Numbers
- /********************************HEADER FILE***********************************/
- #ifndef POLYNOMIAL_H // conditional compilation
- #define POLYNOMIAL_H
- # include <iostream>
- using namespace std;
- class polynomial // rational class
- {
- public :
- // Constructors
- polynomial(); // default constructor
- polynomial( int sizeValue, double* coeffArrayValues );/* this should take the size of the polynomial
- and should prompt the user to enter the value
- ( depending on the size ) and also store all
- the values in the coeff array*/
- polynomial( polynomial& copy );/* here u r passing the address of the polynomial so
- that u can save different polynomials : copy constructor */
- void display( ) ;
- const polynomial add(const polynomial& otherPoly ) const;
- const polynomial difference(const polynomial& otherPoly ) const;
- const polynomial multiply(const polynomial& otherPoly ) const;
- polynomial& operator =( const polynomial& expression );
- ~polynomial( );
- public:
- int size;
- double *coeffArray;
- };
- polynomial n_add( polynomial poly1, polynomial poly2 );
- polynomial n_difference( polynomial poly1, polynomial poly2 );
- polynomial n_multiply( polynomial poly1, polynomial poly2 );
- polynomial operator +( polynomial ploy1, polynomial poly2 );
- polynomial operator -( polynomial ploy1, polynomial poly2 );
- polynomial operator *( polynomial ploy1, polynomial poly2 );
- void n_display( polynomial result );
- // End of class interface.
- #endif
- /*********************IMPLEMENTATION****************************/
- #include <iostream>
- #include <cmath>
- #include "polynomial.h"
- using namespace std;
- polynomial::polynomial()
- {
- //initializing the size of the array to 10.
- size = 0;
- //creating the dynamic array.
- coeffArray = NULL;
- }
- polynomial::polynomial( int sizeValue, double* coeffArrayValues )// here u can also give double coeffArrayValues[]
- {
- sizeValue = size;
- coeffArray = new double[ sizeValue ];
- for( int initial = sizeValue; sizeValue > 0; sizeValue-- )
- {
- cout<<"Please enter x^";
- cout<<sizeValue;
- cout<<" coefficient : ";
- cin>>coeffArray[ sizeValue ];
- cout<<endl;
- }
- }
- polynomial::polynomial( polynomial& copy )
- {
- copy.size = size;
- copy.coeffArray = new double[ copy.size ];
- }
- const polynomial polynomial::add(const polynomial& otherPoly ) const
- {
- int localSize;
- if( otherPoly.size > this->size )// also you can use *this in place of 'this->'
- localSize = otherPoly.size ;
- else
- localSize = this->size ;
- double *resultCoeff;
- resultCoeff = new double[ localSize ];
- for( int count1 = 0; count1 < localSize; count1++ )
- resultCoeff[ count1 ] = 0;
- polynomial poly( localSize, resultCoeff );//to store the result sum
- for( int count2 = 0; count2 < localSize; count2++ )
- resultCoeff[ count2 ] = resultCoeff[ count2 ] + this->coeffArray[ count2 ];
- for( int count3 = 0; count3 < localSize; count3++ )
- resultCoeff[ count3 ] = resultCoeff[ count3 ] + otherPoly.coeffArray[ count3 ];
- return polynomial( size, resultCoeff );
- }
- const polynomial polynomial::difference(const polynomial& otherPoly ) const
- {
- int localSize;
- if( otherPoly.size > this->size )// also you can use *this in place of 'this->'
- localSize = otherPoly.size ;
- else
- localSize = this->size ;
- double *resultCoeff;
- resultCoeff = new double[ localSize ];
- for( int count1 = 0; count1 < localSize; count1++ )
- resultCoeff[ count1 ] = 0;
- polynomial poly( localSize, resultCoeff );//to store the result sum
- for( int count2 = 0; count2 < this->size; count2++ )
- resultCoeff[ count2 ] = resultCoeff[ count2 ] + this->coeffArray[ count2 ];
- for( int count3 = 0; count3 < localSize; count3++ )
- resultCoeff[ count3 ] = resultCoeff[ count3 ] - otherPoly.coeffArray[ count3 ];
- return polynomial( size, resultCoeff );
- }
- const polynomial polynomial::multiply(const polynomial& otherPoly ) const
- {
- int localSize;
- if( otherPoly.size > this->size )// also you can use *this in place of 'this->'
- localSize = otherPoly.size ;
- else
- localSize = this->size ;
- double *resultCoeff;
- resultCoeff = new double[ localSize ];
- for( int count1 = 0; count1 < localSize; count1++ )
- resultCoeff[ count1 ] = 0;
- polynomial poly( localSize, resultCoeff );//to store the result
- for( int count2 = 0; count2 < localSize; count2++ )
- resultCoeff[ count2 ] = resultCoeff[ count2 ] + this->coeffArray[ count2 ];
- for( int count3 = 0; count3 < localSize; count3++ )
- resultCoeff[ count3 ] = resultCoeff[ count3 ] * otherPoly.coeffArray[ count3 ];
- return polynomial( size, resultCoeff );
- }
- void polynomial::display()
- {
- if( this->size > 0 )
- {
- for( int i = ( this->size - 1 ); i >=0 ; i-- )
- {
- if( this->coeffArray[ i ] != 0 )
- {
- cout<<"resultant polygon = ";
- cout<<this->coeffArray[ i ];
- cout<<"x^"<<i;
- if( i != 0 )
- cout<<"+";
- }
- }
- }
- else
- cout<<"resultant polygon = 0";
- }
- polynomial& polynomial::operator =( const polynomial& otherPoly )
- {
- if( this->size != otherPoly.size ) //(u can also give only size )
- {
- delete [] coeffArray;
- coeffArray = new double[ otherPoly.size ];
- }
- this->size = otherPoly.size;
- for( int count = 0; count < this->size; count++ )
- coeffArray[ count ] = otherPoly.coeffArray[ count ];
- return *this;
- }
- polynomial::~polynomial( )
- {
- delete [] coeffArray;
- }
- /*****************NON-MEMBER FUNCTIONS******************/
- polynomial n_add( polynomial poly1, polynomial poly2 )
- {
- int localSize;
- if( poly1.size > poly2.size )
- localSize = poly1.size;
- else
- localSize = poly2.size;
- double *resultCoeff;
- resultCoeff = new double[ localSize ];
- for( int count = 0; count < localSize; count++ )
- resultCoeff[ count ] = 0;
- for( int count1 = 0; count1 < localSize; count1++ )
- resultCoeff[ count1 ] = resultCoeff[ count1 ] + poly1.coeffArray[ count1 ];
- for( int count2 = 0; count2 < localSize; count2++ )
- resultCoeff[ count2 ] = resultCoeff[ count2 ] + poly2.coeffArray[ count2 ];
- return polynomial( localSize, resultCoeff );
- }
- polynomial n_multiply( polynomial poly1, polynomial poly2 )
- {
- int localSize;
- if( poly1.size > poly2.size )
- localSize = poly1.size;
- else
- localSize = poly2.size;
- double *resultCoeff;
- resultCoeff = new double[ localSize ];
- for( int count = 0; count < localSize; count++ )
- resultCoeff[ count ] = 0;
- for( int count1 = 0; count1 < localSize; count1++ )
- resultCoeff[ count1 ] = resultCoeff[ count1 ] + poly1.coeffArray[ count1 ];
- for( int count2 = 0; count2 < localSize; count2++ )
- resultCoeff[ count2 ] = resultCoeff[ count2 ] * poly2.coeffArray[ count2 ];
- return polynomial( localSize, resultCoeff );
- }
- polynomial n_difference( polynomial poly1, polynomial poly2 )
- {
- int localSize;
- if( poly1.size > poly2.size )
- localSize = poly1.size;
- else
- localSize = poly2.size;
- double *resultCoeff;
- resultCoeff = new double[ localSize ];
- for( int count = 0; count < localSize; count++ )
- resultCoeff[ count ] = 0;
- for( int count1 = 0; count1 < localSize; count1++ )
- resultCoeff[ count1 ] = resultCoeff[ count1 ] + poly1.coeffArray[ count1 ];
- for( int count2 = 0; count2 < localSize; count2++ )
- resultCoeff[ count2 ] = resultCoeff[ count2 ] - poly2.coeffArray[ count2 ];
- return polynomial( localSize, resultCoeff );
- }
- polynomial operator +( polynomial ploy1, polynomial poly2 )
- {
- return n_add( poly1, poly2 );
- }
- polynomial operator -( polynomial ploy1, polynomial poly2 )
- {
- return n_difference( poly1, poly2 );
- }
- polynomial operator *( polynomial ploy1, polynomial poly2 )
- {
- return n_multiply( poly1, poly2 );
- }
- void n_display( polynomial result )
- {
- if( result.size > 0 )
- {
- for( int i = ( result.size - 1 ); i >=0 ; i-- )
- {
- if( result.coeffArray[ i ] != 0 )
- {
- cout<<"resultant polygon = ";
- cout<<result.coeffArray[ i ];
- cout<<"x^"<<i;
- if( i != 0 )
- cout<<"+";
- }
- }
- }
- else
- cout<<"resultant polygon = 0";
- }
How to solve it?
Thanks in advance.