自定义C++一元多项式类

 自己定义了一个一元多项式的简单类,主要是用来实现一个简单的框架,另外就是验证编译器默认情况下会为我们的类添加那些构造函数。

 

  
  
  1. /* 
  2. * 该类定义一元多项式 
  3. * MAXDEGREE :定义该类最大可表示的次数 
  4. * MINCOEFF:定义绝对值最小的系数 
  5. * coefficient[  ] :存放一元多项式的系数 
  6. * currentDegree:存放当前对象的最高次数 
  7. * 构造函数:默认构造函数设置coefficient[]和currentDegree全为0 
  8. *                可以使用一个float数组和数组长度来构造一个一元多项式 
  9. * display( ) :用于显示对象的信息 
  10. * value(  ):计算给定x时的一元多项式的值 
  11. * operator +/-/*():计算多项式之间的加减乘 
  12. * power(  ) :用于计算给定x值的x的degree次幂的值 
  13. */ 
  14. #ifndef __POLINOMIAL_H 
  15. #define __POLINOMIAL_H 
  16.  
  17. class polynomial 
  18. public
  19.     polynomial(  ); 
  20.     polynomial( float coeff [ ],int size ); 
  21.     ~polynomial(  ){} 
  22.  
  23.     void  display(  ); 
  24.     double value(float xx); 
  25.  
  26.     friend polynomial operator +( const polynomial& one,const polynomial& two ); 
  27.     friend polynomial operator -( const polynomial& one,const polynomial& two ); 
  28.     friend polynomial operator *( const polynomial& one,const polynomial& two ); 
  29.      
  30.      
  31. private
  32.     double power( float xx,int degree ); 
  33.      
  34.          
  35. private
  36.     static const int MAXDEGREE=20; 
  37.     static const float MINCOEFF=0.000001; 
  38.     float coefficient[ MAXDEGREE+1 ]; 
  39.     int currentDegree; 
  40.  
  41. }; 
  42.  
  43. #endif 

 

  
  
  1. #include "polynomial.h" 
  2. #include <iostream> 
  3. #include <cstdlib> 
  4.  
  5. polynomial::polynomial(  ) 
  6.     forint i=0;i<MAXDEGREE+1;i++ ) 
  7.         coefficient[ i ]=0.0; 
  8.     currentDegree=0; 
  9.      
  10.  
  11. polynomial::polynomial( float coeff [  ],int size ) 
  12.    if( size>MAXDEGREE ) 
  13.        { 
  14.            std::cout<<"the degree is bigger than the MAXDEGREE/n"
  15.            exit( -1 ); 
  16.             
  17.        } 
  18.    if( coeff==NULL ) 
  19.        { 
  20.            std::cout<<"you have input nothing/n"
  21.            exit( -1 ); 
  22.             
  23.        } 
  24.  
  25.    forint i=0;i<size;i++ ) 
  26.        coefficient[ i ]=coeff[ i ]; 
  27.  
  28.    currentDegree=size-1; 
  29.  
  30.  
  31. void polynomial::display(  ) 
  32.     { 
  33.         std::cout<<"The MAXDEGREE = "<<MAXDEGREE<<"/n"
  34.         std::cout<<"这个一员多项式为:"
  35.  
  36.         std::cout<<coefficient[ 0 ]; 
  37.         forint i=1;i<currentDegree+1;++i ) 
  38.             { 
  39.                 if( (coefficient[ i ] > (0.0-MINCOEFF)) && (coefficient[ i ] <MINCOEFF )) ; 
  40.                 else 
  41.                 { 
  42.                     if( coefficient[ i ]==1 ) 
  43.                         std::cout<<"+"<<"x^"<<i; 
  44.                     else if( coefficient[ i ]==-1 ) 
  45.                         std::cout<<"-"<<"x^"<<i; 
  46.                     else if( coefficient[ i ]>0.0 ) 
  47.                         std::cout<<"+"<<coefficient[ i ]<<"x^"<<i; 
  48.                     else 
  49.                         std::cout<<coefficient[ i ]<<"x^"<<i; 
  50.                 } 
  51.             } 
  52.         std::cout<<std::endl<<std::endl; 
  53.     } 
  54.  
  55. double polynomial::power( float xx,int degree ) 
  56.     { 
  57.         double temp=1.0; 
  58.         while( degree-- ) 
  59.             temp*=xx; 
  60.         return temp; 
  61.          
  62.     } 
  63.  
  64. double polynomial::value( float xx ) 
  65.     { 
  66.         double sum=0.0; 
  67.         forint i=0;i<currentDegree+1;++i ) 
  68.             { 
  69.                 sum = sum+coefficient[ i ]*power( xx,i ); 
  70.                  
  71.             } 
  72.         return sum; 
  73.          
  74.     } 
  75.  
  76. polynomial operator +( const polynomial& one,const polynomial& two ) 
  77.     { 
  78.         polynomial temp; 
  79.          
  80.         int biggerDegree=( one.currentDegree > two.currentDegree ?one.currentDegree:two.currentDegree ); 
  81.         forint i=0;i<biggerDegree+1;++i ) 
  82.             { 
  83.                 temp.coefficient[ i ] = one.coefficient[ i ]+two.coefficient[ i ]; 
  84.                  
  85.             } 
  86.         temp.currentDegree=biggerDegree; 
  87.          
  88.         return temp; 
  89.          
  90.     } 
  91.  
  92. polynomial operator -( const polynomial& one,const polynomial& two ) 
  93.     { 
  94.         polynomial temp; 
  95.          
  96.         int biggerDegree=( one.currentDegree > two.currentDegree ?one.currentDegree:two.currentDegree ); 
  97.         forint i=0;i<biggerDegree+1;++i ) 
  98.             { 
  99.                 temp.coefficient[ i ] = one.coefficient[ i ]-two.coefficient[ i ]; 
  100.                  
  101.             } 
  102.         temp.currentDegree=biggerDegree; 
  103.          
  104.         return temp; 
  105.          
  106.     } 
  107.  
  108. polynomial operator *( const polynomial& one,const polynomial& two ) 
  109.     { 
  110.         int sum_degree=one.currentDegree+two.currentDegree; 
  111.         polynomial temp; 
  112.         polynomial result; 
  113.         temp.currentDegree=result.currentDegree=sum_degree; 
  114.         std::cout<<"sum_degree= "<<sum_degree<<"/n"
  115.          
  116.         int i,j,mm; 
  117.         /*用one的每一个系数和two相乘,然后累加每次的结果*/ 
  118.         for( i=0;i<one.currentDegree+1;++i )       //这两个for循环模拟我们计算乘法的过程 
  119.             { 
  120.                 for( j=0;j<two.currentDegree+1;++j ) 
  121.                 { 
  122.                     temp.coefficient[ i+j ]=one.coefficient[ i ]*two.coefficient[ j ]; 
  123.                 } 
  124.                 result=result+temp;                   //每一项乘完以后累加和 
  125.                  
  126.                 for( mm=0;mm<sum_degree+1;++mm )     //主要是清空temp的各个系数,因为temp只是一个中间值 
  127.                     temp.coefficient[ mm ]=0.0; 
  128.                  
  129.             } 
  130.         return result; 
  131.              
  132.     } 

 

  
  
  1. #include <iostream> 
  2. #include "polynomial.h" 
  3.  
  4. int main(  ) 
  5.     { 
  6.         float one[  ]={1,2,3,4}; 
  7.         polynomial poly_one( one,4 ); 
  8.         poly_one.display(  ); 
  9.         std::cout<<"当x=1.0时,多项式的值为 "<< poly_one.value( 1.0 )<<"/n"
  10.  
  11.         float two[  ]={2,1,1}; 
  12.         polynomial poly_two( two,3 ); 
  13.         poly_two.display(  ); 
  14.         std::cout<<"当x=1.0时,多项式的值为 "<< poly_two.value( 1.0 )<<"/n"
  15.  
  16.        std::cout<<"下面将演示多项式的乘法功能/n"
  17.        polynomial poly_three; 
  18.        poly_three=poly_one*poly_two; 
  19.        poly_three.display(  ); 
  20.         
  21.         return 0; 
  22.          
  23.     } 

从main中,poly_three和poly_four可知,如果我们没有声明复制构造函数和赋值构造函数的化,编译器会为我们自动添加。

另外,如果我们没有定义类的默认构造函数的话,编译器会为我们添加一个函数体为空的默认构造函数。

还有就是operator =()函数也会被自动添加,但是只满足同类对象之间相互赋值。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值