自己定义了一个一元多项式的简单类,主要是用来实现一个简单的框架,另外就是验证编译器默认情况下会为我们的类添加那些构造函数。
- /*
- * 该类定义一元多项式
- * MAXDEGREE :定义该类最大可表示的次数
- * MINCOEFF:定义绝对值最小的系数
- * coefficient[ ] :存放一元多项式的系数
- * currentDegree:存放当前对象的最高次数
- * 构造函数:默认构造函数设置coefficient[]和currentDegree全为0
- * 可以使用一个float数组和数组长度来构造一个一元多项式
- * display( ) :用于显示对象的信息
- * value( ):计算给定x时的一元多项式的值
- * operator +/-/*():计算多项式之间的加减乘
- * power( ) :用于计算给定x值的x的degree次幂的值
- */
- #ifndef __POLINOMIAL_H
- #define __POLINOMIAL_H
- class polynomial
- {
- public:
- polynomial( );
- polynomial( float coeff [ ],int size );
- ~polynomial( ){}
- void display( );
- double value(float xx);
- friend polynomial operator +( const polynomial& one,const polynomial& two );
- friend polynomial operator -( const polynomial& one,const polynomial& two );
- friend polynomial operator *( const polynomial& one,const polynomial& two );
- private:
- double power( float xx,int degree );
- private:
- static const int MAXDEGREE=20;
- static const float MINCOEFF=0.000001;
- float coefficient[ MAXDEGREE+1 ];
- int currentDegree;
- };
- #endif
- #include "polynomial.h"
- #include <iostream>
- #include <cstdlib>
- polynomial::polynomial( )
- {
- for( int i=0;i<MAXDEGREE+1;i++ )
- coefficient[ i ]=0.0;
- currentDegree=0;
- }
- polynomial::polynomial( float coeff [ ],int size )
- {
- if( size>MAXDEGREE )
- {
- std::cout<<"the degree is bigger than the MAXDEGREE/n";
- exit( -1 );
- }
- if( coeff==NULL )
- {
- std::cout<<"you have input nothing/n";
- exit( -1 );
- }
- for( int i=0;i<size;i++ )
- coefficient[ i ]=coeff[ i ];
- currentDegree=size-1;
- }
- void polynomial::display( )
- {
- std::cout<<"The MAXDEGREE = "<<MAXDEGREE<<"/n";
- std::cout<<"这个一员多项式为:";
- std::cout<<coefficient[ 0 ];
- for( int i=1;i<currentDegree+1;++i )
- {
- if( (coefficient[ i ] > (0.0-MINCOEFF)) && (coefficient[ i ] <MINCOEFF )) ;
- else
- {
- if( coefficient[ i ]==1 )
- std::cout<<"+"<<"x^"<<i;
- else if( coefficient[ i ]==-1 )
- std::cout<<"-"<<"x^"<<i;
- else if( coefficient[ i ]>0.0 )
- std::cout<<"+"<<coefficient[ i ]<<"x^"<<i;
- else
- std::cout<<coefficient[ i ]<<"x^"<<i;
- }
- }
- std::cout<<std::endl<<std::endl;
- }
- double polynomial::power( float xx,int degree )
- {
- double temp=1.0;
- while( degree-- )
- temp*=xx;
- return temp;
- }
- double polynomial::value( float xx )
- {
- double sum=0.0;
- for( int i=0;i<currentDegree+1;++i )
- {
- sum = sum+coefficient[ i ]*power( xx,i );
- }
- return sum;
- }
- polynomial operator +( const polynomial& one,const polynomial& two )
- {
- polynomial temp;
- int biggerDegree=( one.currentDegree > two.currentDegree ?one.currentDegree:two.currentDegree );
- for( int i=0;i<biggerDegree+1;++i )
- {
- temp.coefficient[ i ] = one.coefficient[ i ]+two.coefficient[ i ];
- }
- temp.currentDegree=biggerDegree;
- return temp;
- }
- polynomial operator -( const polynomial& one,const polynomial& two )
- {
- polynomial temp;
- int biggerDegree=( one.currentDegree > two.currentDegree ?one.currentDegree:two.currentDegree );
- for( int i=0;i<biggerDegree+1;++i )
- {
- temp.coefficient[ i ] = one.coefficient[ i ]-two.coefficient[ i ];
- }
- temp.currentDegree=biggerDegree;
- return temp;
- }
- polynomial operator *( const polynomial& one,const polynomial& two )
- {
- int sum_degree=one.currentDegree+two.currentDegree;
- polynomial temp;
- polynomial result;
- temp.currentDegree=result.currentDegree=sum_degree;
- std::cout<<"sum_degree= "<<sum_degree<<"/n";
- int i,j,mm;
- /*用one的每一个系数和two相乘,然后累加每次的结果*/
- for( i=0;i<one.currentDegree+1;++i ) //这两个for循环模拟我们计算乘法的过程
- {
- for( j=0;j<two.currentDegree+1;++j )
- {
- temp.coefficient[ i+j ]=one.coefficient[ i ]*two.coefficient[ j ];
- }
- result=result+temp; //每一项乘完以后累加和
- for( mm=0;mm<sum_degree+1;++mm ) //主要是清空temp的各个系数,因为temp只是一个中间值
- temp.coefficient[ mm ]=0.0;
- }
- return result;
- }
- #include <iostream>
- #include "polynomial.h"
- int main( )
- {
- float one[ ]={1,2,3,4};
- polynomial poly_one( one,4 );
- poly_one.display( );
- std::cout<<"当x=1.0时,多项式的值为 "<< poly_one.value( 1.0 )<<"/n";
- float two[ ]={2,1,1};
- polynomial poly_two( two,3 );
- poly_two.display( );
- std::cout<<"当x=1.0时,多项式的值为 "<< poly_two.value( 1.0 )<<"/n";
- std::cout<<"下面将演示多项式的乘法功能/n";
- polynomial poly_three;
- poly_three=poly_one*poly_two;
- poly_three.display( );
- return 0;
- }
从main中,poly_three和poly_four可知,如果我们没有声明复制构造函数和赋值构造函数的化,编译器会为我们自动添加。
另外,如果我们没有定义类的默认构造函数的话,编译器会为我们添加一个函数体为空的默认构造函数。
还有就是operator =()函数也会被自动添加,但是只满足同类对象之间相互赋值。