日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發(fā)布時(shí)間:2024/1/17 c/c++ 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义C++一元多项式类 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?自己定義了一個(gè)一元多項(xiàng)式的簡單類,主要是用來實(shí)現(xiàn)一個(gè)簡單的框架,另外就是驗(yàn)證編譯器默認(rèn)情況下會(huì)為我們的類添加那些構(gòu)造函數(shù)。

?

  • /*?
  • *?polynomial.h?
  • *?該類定義一元多項(xiàng)式?
  • *?MAXDEGREE?:定義該類最大可表示的次數(shù)?
  • *?MINCOEFF:定義絕對值最小的系數(shù)?
  • *?coefficient[??]?:存放一元多項(xiàng)式的系數(shù)?
  • *?currentDegree:存放當(dāng)前對象的最高次數(shù)?
  • *?構(gòu)造函數(shù):默認(rèn)構(gòu)造函數(shù)設(shè)置coefficient[]和currentDegree全為0?
  • *????????????????可以使用一個(gè)float數(shù)組和數(shù)組長度來構(gòu)造一個(gè)一元多項(xiàng)式?
  • *?display(?)?:用于顯示對象的信息?
  • *?value(??):計(jì)算給定x時(shí)的一元多項(xiàng)式的值?
  • *?operator?+/-():計(jì)算多項(xiàng)式之間的加減?
  • *?power(??)?:用于計(jì)算給定x值的x的degree次冪的值?
  • */?
  • 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?);?
  • ?????
  • 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;?
  • ?
  • };?
  • ?

  • /*?
  • ?*polynomial.cpp?
  • ?*/?
  • #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<<coefficient[?0?];?
  • ????????for(?int?i=1;i<currentDegree+1;++i?)?
  • ????????????{?
  • ????????????????if(?(coefficient[?i?]?>?(0.0-MINCOEFF))?&&?(coefficient[?i?]?<MINCOEFF?))?;?
  • ????????????????else?
  • ????????????????{?
  • ????????????????????if(?coefficient[?i?]>0.0?)?
  • ????????????????????????std::cout<<"+"<<coefficient[?i?]<<"x^"<<i;?
  • ????????????????????else?
  • ????????????????????????std::cout<<coefficient[?i?]<<"x^"<<i;?
  • ????????????????}?
  • ????????????}?
  • ????????std::cout<<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;?
  • ?????????
  • ????}?
  • ?

  • /*?
  • *?test.cpp?
  • */?
  • #include?<iostream>?
  • #include?"polynomial.h"?
  • ?
  • int?main(??)?
  • ????{?
  • ????????float?one[??]={1,2,3,4,5};?
  • ????????polynomial?poly_one(?one,5?);?
  • ????????poly_one.display(??);?
  • ????????std::cout<<?poly_one.value(?1.0?)<<"\n";?
  • ?
  • ????????float?two[??]={1.2,2.5,-3.5,4.5,-5.5};?
  • ????????polynomial?poly_two(?two,5?);?
  • ????????poly_two.display(??);?
  • ????????std::cout<<?poly_two.value(?1.0?)<<"\n";?
  • ?
  • ????????polynomial?poly_three;?
  • ????????poly_three=poly_two;?
  • ????????poly_three.display(??);?
  • ?
  • ????????polynomial?poly_four(poly_two);?
  • ????????poly_four.display(??);?
  • ?????????
  • ?????????
  • ????????return?0;?
  • ?????????
  • ????}?
  • /*?
  • *?test.cpp?
  • */?
  • #include?<iostream>?
  • #include?"polynomial.h"?
  • ?
  • int?main(??)?
  • ????{?
  • ????????float?one[??]={1,2,3,4,5};?
  • ????????polynomial?poly_one(?one,5?);?
  • ????????poly_one.display(??);?
  • ????????std::cout<<?poly_one.value(?1.0?)<<"\n";?
  • ?
  • ????????float?two[??]={1.2,2.5,-3.5,4.5,-5.5};?
  • ????????polynomial?poly_two(?two,5?);?
  • ????????poly_two.display(??);?
  • ????????std::cout<<?poly_two.value(?1.0?)<<"\n";?
  • ?
  • ????????polynomial?poly_three;?
  • ????????poly_three=poly_two;?
  • ????????poly_three.display(??);?
  • ?
  • ????????polynomial?poly_four(poly_two);?
  • ????????poly_four.display(??);?
  • ?????????
  • ?????????
  • ????????return?0;?
  • ?????????
  • ????}?
  • 從main中,poly_three和poly_four可知,如果我們沒有聲明復(fù)制構(gòu)造函數(shù)和賦值構(gòu)造函數(shù)的化,編譯器會(huì)為我們自動(dòng)添加。

    另外,如果我們沒有定義類的默認(rèn)構(gòu)造函數(shù)的話,編譯器會(huì)為我們添加一個(gè)函數(shù)體為空的默認(rèn)構(gòu)造函數(shù)。

    還有就是operator =()函數(shù)也會(huì)被自動(dòng)添加,但是只滿足同類對象之間相互賦值。

    轉(zhuǎn)載于:https://blog.51cto.com/liuguangtao/537704

    總結(jié)

    以上是生活随笔為你收集整理的自定义C++一元多项式类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。