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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

高精度模板 c++/类封装

發布時間:2025/6/15 c/c++ 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 高精度模板 c++/类封装 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實還在POJ上面寫過一個浮點數的乘法和加法運算,但個別題目會用到,所以不加進去了.

[cpp]?view plaincopy
  • #include<iostream>??
  • #include<cstring>??
  • #include<string>??
  • #include<cstdio>??
  • ??
  • using?namespace?std;??
  • ??
  • #define?MAXN?1000??
  • ??
  • struct?HP??
  • {??
  • ????int?len,s[MAXN];??
  • ????HP()??
  • ????{??
  • ????????memset(s,0,sizeof(s));??
  • ????????len=1;??
  • ????}??
  • ??
  • ????HP?operator?=(const?char?*num)?//字符串賦值??
  • ????{??
  • ????????len=strlen(num);??
  • ????????for(int?i=0;i<len;i++)?s[i]=num[len-i-1]-'0';??
  • ????}??
  • ??
  • ????HP?operator?=(int?num)?//int?賦值??
  • ????{??
  • ????????char?s[MAXN];??
  • ????????sprintf(s,"%d",num);??
  • ????????*this=s;??
  • ????????return?*this;??
  • ????}??
  • ??
  • ????HP(int?num)?{?*this=num;}??
  • ??
  • ????HP(const?char*num)?{*this=num;}??
  • ??
  • ????string?str()const?//轉化成string??
  • ????{??
  • ????????string?res="";??
  • ????????for(int?i=0;i<len;i++)?res=(char)(s[i]+'0')+res;??
  • ????????if(res=="")?res="0";??
  • ????????return?res;??
  • ????}??
  • ??
  • ????HP?operator?+(const?HP&?b)?const??
  • ????{??
  • ????????HP?c;??
  • ????????c.len=0;??
  • ????????for(int?i=0,g=0;g||i<max(len,b.len);i++)??
  • ????????{??
  • ????????int?x=g;??
  • ????????if(i<len)?x+=s[i];??
  • ????????if(i<b.len)?x+=b.s[i];??
  • ????????c.s[c.len++]=x%10;??
  • ????????g=x/10;??
  • ????????}??
  • ????????return?c;??
  • ????}??
  • ????void?clean()??
  • ????{??
  • ????????while(len?>?1?&&?!s[len-1])?len--;??
  • ????}??
  • ??
  • ????HP?operator?*(const?HP&?b)??
  • ????{??
  • ????????HP?c;??
  • ????????c.len=len+b.len;??
  • ????????for(int?i=0;i<len;i++)??
  • ????????for(int?j=0;j<b.len;j++)??
  • ????????????c.s[i+j]+=s[i]*b.s[j];??
  • ????????for(int?i=0;i<c.len-1;i++)??
  • ????????{??
  • ????????c.s[i+1]+=c.s[i]/10;??
  • ????????c.s[i]%=10;??
  • ????????}??
  • ????????c.clean();??
  • ????????return?c;??
  • ????}??
  • ??
  • ????HP?operator?-?(const?HP&?b)??
  • ????{??
  • ????????HP?c;??
  • ????????c.len?=?0;??
  • ????????for(int?i=0,g=0;i<len;i++)??
  • ????????{??
  • ????????int?x=s[i]-g;??
  • ????????if(i<b.len)?x-=b.s[i];??
  • ????????if(x>=0)??
  • ????????????g=0;??
  • ????????else??
  • ????????{??
  • ????????????g=1;??
  • ????????????x+=10;??
  • ????????}??
  • ????????c.s[c.len++]=x;??
  • ????????}??
  • ????????c.clean();??
  • ????????return?c;??
  • ????}??
  • ????HP?operator?/?(const?HP?&b)??
  • ????{??
  • ????????HP?c,?f?=?0;??
  • ????????for(int?i?=?len-1;?i?>=?0;?i--)??
  • ????????{??
  • ????????????f?=?f*10;??
  • ????????????f.s[0]?=?s[i];??
  • ????????????while(f>=b)??
  • ????????????{??
  • ????????????????f?=f-b;??
  • ????????????????c.s[i]++;??
  • ????????????}??
  • ????????}??
  • ????????c.len?=?len;??
  • ????????c.clean();??
  • ????????return?c;??
  • ????}??
  • ?????HP?operator?%?(const?HP?&b)??
  • ????{??
  • ????????HP?r?=?*this?/?b;??
  • ????????r?=?*this?-?r*b;??
  • ????????return?r;??
  • ????}??
  • ??????
  • ?????HP?operator?/=?(const?HP?&b)??
  • ????{??
  • ????????*this??=?*this?/?b;??
  • ????????return?*this;??
  • ????}??
  • ??
  • ??
  • ????HP?operator?%=?(const?HP?&b)??
  • ????{??
  • ????????*this?=?*this?%?b;??
  • ????????return?*this;??
  • ????}??
  • ??????
  • ????bool?operator?<?(const?HP&?b)?const??
  • ????{??
  • ????????if(len?!=?b.len)?return?len?<?b.len;??
  • ????????for(int?i?=?len-1;?i?>=?0;?i--)??
  • ????????if(s[i]?!=?b.s[i])?return?s[i]?<?b.s[i];??
  • ????????????return?false;??
  • ????}??
  • ??
  • ????bool?operator?>?(const?HP&?b)?const??
  • ????{??
  • ????????return?b?<?*this;??
  • ????}??
  • ??
  • ????bool?operator?<=?(const?HP&?b)??
  • ????{??
  • ????????return?!(b?<?*this);??
  • ????}??
  • ??
  • ????bool?operator?==?(const?HP&?b)??
  • ????{??
  • ????????return?!(b?<?*this)?&&?!(*this?<?b);??
  • ????}??
  • ????bool?operator?!=?(const?HP?&b)??
  • ????{??
  • ????????return?!(*this?==?b);??
  • ????}??
  • ????HP?operator?+=?(const?HP&?b)??
  • ????{??
  • ????????*this?=?*this?+?b;??
  • ????????return?*this;??
  • ????}??
  • ????bool?operator?>=?(const?HP?&b)??
  • ????{??
  • ????????return?*this?>?b?||?*this?==?b;??
  • ????}??
  • ??
  • ?????
  • };??
  • ??
  • istream&?operator?>>(istream?&in,?HP&?x)??
  • {??
  • ??string?s;??
  • ??in?>>?s;??
  • ??x?=?s.c_str();??
  • ??return?in;??
  • }??
  • ??
  • ostream&?operator?<<(ostream?&out,?const?HP&?x)??
  • {??
  • ??out?<<?x.str();??
  • ??return?out;??
  • }??
  • ??
  • ??
  • int?main()??
  • {??
  • ??HP?a;??
  • ??cin?>>?a;??
  • ??a?+=?"123456789123456789000000000";??
  • ??cout?<<?a*2?<<?endl;??
  • ??system("pause");??
  • ??return?0;??
  • }??

  • ?當需要用long long 壓縮多位的時候

    ?POJ Heritage?? (一個long long 壓縮8位)

    [cpp]?view plaincopy
  • #include<iostream>??
  • #include<cstring>??
  • #include<string>??
  • #include<cstdio>??
  • ??
  • using?namespace?std;??
  • ??
  • #define?MAXN?4000??
  • ??
  • struct?HP??
  • {??
  • ????int?len;??
  • ????long?long?s[MAXN];??
  • ????HP()??
  • ????{??
  • ????????memset(s,0,sizeof(s));??
  • ????????len=1;??
  • ????}??
  • ??
  • ????HP?operator?=(const?char?*num)??
  • ????{??
  • ????????int?k=0,j=0;??
  • ????????long?long?mul=1,tmp=0;??
  • ????????for(int?i=strlen(num)-1;i>=0;i--)??
  • ????????{??
  • ????????????tmp=tmp*mul+(long?long)(num[i]-'0');??
  • ????????????j++,mul*=10;??
  • ????????????if(j==8)??
  • ????????????{??
  • ????????????????s[k++]=tmp;??
  • ????????????????j=0;??
  • ????????????????mul=1;??
  • ????????????????tmp=0;??
  • ????????????}??
  • ????????}??
  • ????????if(tmp!=0)??
  • ????????????s[k++]=tmp;??
  • ????????len=k;??
  • ????????return?*this;??
  • ????}??
  • ??
  • ????HP?operator?=(int?num)??
  • ????{??
  • ????????char?s[MAXN];??
  • ????????sprintf(s,"%d",num);??
  • ????????*this=s;??
  • ????????return?*this;??
  • ????}??
  • ??
  • ????HP(int?num)?{?*this=num;}??
  • ??
  • ????HP(const?char*num)?{*this=num;}??
  • ??
  • ????string?str()const??
  • ????{??
  • ????????string?res="";??
  • ????????for(int?i=0;i<len-1;i++)??
  • ????????{??
  • ????????????long?long?tmp=s[i];??
  • ????????????for(int?j=0;j<8;j++)??
  • ????????????{??
  • ????????????????res=(char)(tmp%10+'0')+res;??
  • ????????????????tmp/=10;??
  • ????????????}??
  • ????????}??
  • ????????long?long?tmp=s[len-1];??
  • ????????while(tmp!=0)??
  • ????????{??
  • ????????????res=(char)(tmp%10+'0')+res;??
  • ????????????tmp/=10;??
  • ????????}??
  • ????????if(res=="")?res="0";??
  • ????????return?res;??
  • ????}??
  • ??
  • ????HP?operator?+(const?HP&?b)?const??
  • ????{??
  • ????????HP?c;??
  • ????????c.len=0;??
  • ????????long?long?g=0;??
  • ????????for(int?i=0;g!=0||i<max(len,b.len);i++)??
  • ????????{??
  • ????????????long?long?x=g;??
  • ????????????if(i<len)?x+=s[i];??
  • ????????????if(i<b.len)?x+=b.s[i];??
  • ????????????c.s[c.len++]=x%100000000;??
  • ????????????g=x/100000000;??
  • ????????}??
  • ????????return?c;??
  • ????}??
  • ????void?clean()??
  • ????{??
  • ????????while(len?>?1?&&?!s[len-1])?len--;??
  • ????}??
  • ??
  • ????HP?operator?*(const?HP&?b)??
  • ????{??
  • ????????HP?c;??
  • ????????c.len=len+b.len;??
  • ????????for(int?i=0;i<len;i++)??
  • ????????????for(int?j=0;j<b.len;j++)??
  • ????????????????c.s[i+j]+=s[i]*b.s[j];??
  • ????????for(int?i=0;i<c.len-1;i++)??
  • ????????{??
  • ????????????c.s[i+1]+=c.s[i]/100000000;??
  • ????????????c.s[i]%=100000000;??
  • ????????}??
  • ????????c.clean();??
  • ????????return?c;??
  • ????}??
  • ??
  • ????HP?operator?-?(const?HP&?b)??
  • ????{??
  • ????????HP?c;??
  • ????????c.len?=?0;??
  • ????????long?long?g=0;??
  • ????????for(int?i=0;i<len;i++)??
  • ????????{??
  • ????????????long?long?x=s[i]-g;??
  • ????????????if(i<b.len)?x-=b.s[i];??
  • ????????????if(x>=0)??
  • ????????????????g=0;??
  • ????????????else??
  • ????????????{??
  • ????????????????g=1;??
  • ????????????????x+=100000000;??
  • ????????????}??
  • ????????????c.s[c.len++]=x;??
  • ????????}??
  • ????????c.clean();??
  • ????????return?c;??
  • ????}??
  • ????void?output()??
  • ????{??
  • ????????printf("%lld",s[len-1]);??
  • ????????for(int?i=len-2;i>=0;i--)??
  • ????????????printf("%.8lld",s[i]);??
  • ????????printf("\n");??
  • ????}??
  • };??
  • ??
  • ??
  • HP?a[20];??
  • int?n;??
  • ??
  • int?main()??
  • {??
  • ????a[0]=2;??
  • ????for(int?i=1;i<18;i++)??
  • ????????a[i]=a[i-1]*a[i-1]-a[i-1]+1;??
  • ????while(~scanf("%d",&n))??
  • ????{??
  • ????????for(int?i=0;i<n;i++)??
  • ????????????a[i].output();??
  • ????}??
  • ????return?0;??
  • ??
  • }??
  • ?


    浮點乘法?? 前導零只用clean清除,但是末尾需要轉化成string之后自行處理.del代表小數點第幾位之后

    [cpp]?view plaincopy
  • HP?operator?*(const?HP&?b)??
  • {??
  • ????HP?c;??
  • ????c.len=len+b.len;??
  • ????c.del=del+b.del;??
  • ????for(int?i=0;i<len;i++)??
  • ????????for(int?j=0;j<b.len;j++)??
  • ????????????c.s[i+j]+=s[i]*b.s[j];??
  • ????for(int?i=0;i<c.len;i++)??
  • ????{??
  • ????????c.s[i+1]+=c.s[i]/10;??
  • ????????c.s[i]%=10;??
  • ????}??
  • ????c.clean();??
  • ????return?c;??
  • }??
  • 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的高精度模板 c++/类封装的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。