版本比较
C++plus
正文
增加 799 行删除 492 行
123456578791091011121314111213141915161718192021222324252627282930313233343536373839403141424344454647483949505152535455564657585960616252636454656667575859606168697071727374757677787980818283848565668687888990916869929394959697727398991001011021031041051061077610810911011111280818211311411511611711811912012112212384858612412512612712812913013113213313413513613788899091138139140141142143144145146147148149150151152153154939415515615715815916016116297981631641651661671681691701711721731741001011021751761771781791801811821831841851861871881891041051061901911921931941951961971981992002012022031081091101112042052062072082092102112122132142152162172181131141152192202212222232242252262272282291181192302312322332342352362372382392402411231241252422432442452462472482492502512522532542552562572582592601271282612622632642652662672682692702712722731301311322742752762772782792802812822832842852862871352882892902912922932942951382962972982993003013023031411421431441451463043053063073083093103113123133143153163173183193203213223233243253263273283293301481491501511521531541551561571581591601611621631643313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994005 行未改动4064074084091784104114124134144151841854164171884184194204214224231944244251964264274284294307 行未改动4384394404412122134424434442164454464474484492214504514524532264544554562292304574584594604614622354634644652382394664674684694702432444714724734744754764772502514784794804814824834842594854864874884892654902674914924934944954962732744974984992782795005015022832845035045055062885075085092905105115125132935145155165175185195203005215225235245255263073085275285295305315323153163173183193205335345355365375385395405413245423265435445455465473295485495505515523325535545555563355575585595605615624 行未改动5675685695703485715725735745755765775785793573583593605805813635825835845853673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894905865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946954936966976986997007014997027035017047057067077087097107115097127135127147157167177187195185195205215227207217227237247257267277287297307317327337347357367375265277387397407417427435295307447457467477487495335347507517527537547557567577587595377607617627637645415425437657667677687697707717727737747755455465477767777787797807817827837847857867877887895495505515527907917927937947957967977987998008018028038048058065545558078088098108118128138145585598158168178188198208218228238248258265615625638278288298308318328338348358368378388398408415655665678428438448458468478488498508518528538548555695705715728568578588598608618628638648658668678688698705745755768718728738748758768778788798808815795808828838848858868878888898908918928935845855868948958968978988999009019029039049059069079089099109119125885899139149159169179189199209219229239249255915925939269279289299309319329339349359369379389395969409419429439449459469475999489499509519529539549556026036046056066079569579589599609619629639649659669679689699709719729739749759769779789799809819826096106116126136146156166176186196206216226236246259839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104962710501051630105210531054105510566351057105810591060640106110621063643644106410651066106710681069649107010711072652653107310741075107610776576581078107910801081108210831084664665108510861087108810891090109167310921093109410951096679109768110981099110011011102110368768811041105110669269311071108110969769811101111111211137021114111511167041117111811191120707112111221123112411251126112771411281129113011311132113372172211341135113611371138113972973073173273373411401141114211431144114511461147114873811497401150115111521153115474311551156115711581159746116011611162116374911641165116611677527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578581168860861116911701171117211738661174- [C++plus文档地址](https://www.luogu.com.cn/article/ykup0ycs)(洛谷)。
## 前言
起因:这些东西好麻烦。
中间忘了,然后就有了它——`C++plus`。中间忘了,然后就有了它。
~~请不要叫它CP。~~欢迎私信反馈qwq
功能无法做到完全优秀或正确,欢迎私信反馈。
## 广告
欢迎想加入的同学[私信作者](https://www.luogu.com.cn/chat?uid=1706930)。
## 正文
### 编译选项
若无特别说明,下文采用的编译选项均为若无特别说明,下文采用的**编译选项**均为
```bash-std=c++11 -O2```
:::warning[编译选项设置]{open}
以 `Dev-C++` 编译器为例,点击“工具-编译选项”打开窗口。

其他默认选项不要动,把编译器和编译器配置修改为如图所示。

不执行此操作可能会导致编译错误。
:::
### 开始
导入必须的库。
:::info[导入库]
```cpp```cpp line-numbers#include <iostream>#include <vector>#include <string>#include <map>#include <algorithm>#include <sstream>#include <fstream>#include <windows.h>// #include <windows.h> // 作为使用者,您不需要该库#include <cmath>#include <stdexcept>// #include <chrono> // 作为使用者,您不需要该库```
:::
### 高精度实现### 新的数据类型
通过C++的高精模板与重载运算符实现。
你可能会问 `frint` 是什么意思,因为FR是我的用户名缩写(
:::info[高精度]:::info[frint]
```cpp```cpp line-numbersclass frint{private: str v;bool ng; struct C{float x,y;C(float _x=0.0,float _y=0.0):x(_x),y(_y){} C operator+(const C&b)const{return C(x+b.x,y+b.y);} C operator-(const C&b)const{return C(x-b.x,y-b.y);} C operator*(const C&b)const{return C(x*b.x-y*b.y,x*b.y+y*b.x);}}; str v; bool ng; struct C{ float x,y; C(float _x=0.0,float _y=0.0):x(_x),y(_y){} C operator+(const C&b)const{ return C(x+b.x,y+b.y); } C operator-(const C&b)const{ return C(x-b.x,y-b.y); } C operator*(const C&b)const{ return C(x*b.x-y*b.y,x*b.y+y*b.x); } }; static void FFT(vector<C>&a,int inv){ int n=a.size(); for(int i=1,j=0;i<n;i++){ int bit=n>>1;for(;j&bit;bit>>=1)j^=bit;j^=bit; if(i<j)swap(a[i],a[j]);} int bit=n>>1; for(;j&bit;bit>>=1)j^=bit; j^=bit; if(i<j)swap(a[i],a[j]); } for(int len=2;len<=n;len<<=1){ float ang=2*acos(-1.0)/len*inv;C wlen(cos(ang),sin(ang)); for(int i=0;i<n;i+=len){C w(1,0); float ang=2*acos(-1.0)/len*inv; C wlen(cos(ang),sin(ang)); for(int i=0;i<n;i+=len){ C w(1,0); for(int j=0;j<len/2;j++){ C u=a[i+j],v=a[i+j+len/2]*w; a[i+j]=u+v;a[i+j+len/2]=u-v;w=w*wlen;}}} if(inv==-1)for(int i=0;i<n;i++)a[i].x/=n;} a[i+j]=u+v; a[i+j+len/2]=u-v; w=w*wlen; } } } if(inv==-1)for(int i=0;i<n;i++)a[i].x/=n; } static str mul_fft(const str&a,const str&b){ if(a=="0"||b=="0")return "0"; int n=1,tot=a.size()+b.size();while(n<tot)n<<=1; int n=1,tot=a.size()+b.size(); while(n<tot)n<<=1; vector<C>c1(n),c2(n); for(int i=0;i<a.size();i++)c1[i]=C(a[a.size()-1-i]-'0',0); for(int i=0;i<b.size();i++)c2[i]=C(b[b.size()-1-i]-'0',0); FFT(c1,1);FFT(c2,1);for(int i=0;i<n;i++)c1[i]=c1[i]*c2[i];FFT(c1,-1); vector<int>res(n);for(int i=0;i<n;i++)res[i]=int(c1[i].x+0.5); for(int i=0;i<n-1;i++){res[i+1]+=res[i]/10;res[i]%=10;} FFT(c1,1); FFT(c2,1); for(int i=0;i<n;i++)c1[i]=c1[i]*c2[i]; FFT(c1,-1); vector<int>res(n); for(int i=0;i<n;i++)res[i]=int(c1[i].x+0.5); for(int i=0;i<n-1;i++){ res[i+1]+=res[i]/10; res[i]%=10; } while(res.size()>1&&res.back()==0)res.pop_back(); str ans;for(int i=res.size()-1;i>=0;i--)ans+=char(res[i]+'0');return ans;} static str rlz(const str&s){int i=0;while(i<s.size()-1&&s[i]=='0')i++;return s.substr(i);} static bool ca(const str&a,const str&b){if(a.size()!=b.size())return a.size()<b.size();return a<b;} str ans; for(int i=res.size()-1;i>=0;i--)ans+=char(res[i]+'0'); return ans; } static str rlz(const str&s){ int i=0; while(i<s.size()-1&&s[i]=='0')i++; return s.substr(i); } static bool ca(const str&a,const str&b){ if(a.size()!=b.size())return a.size()<b.size(); return a<b; } static str aa(const str&a,const str&b){ str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end()); int n=max(s1.size(),s2.size());while(s1.size()<n)s1+='0';while(s2.size()<n)s2+='0'; int c=0;str r;for(int i=0;i<n;i++){int s=(s1[i]-'0')+(s2[i]-'0')+c;r+=char(s%10+'0');c=s/10;} if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);} str s1=a,s2=b; reverse(s1.begin(),s1.end()); reverse(s2.begin(),s2.end()); int n=max(s1.size(),s2.size()); while(s1.size()<n)s1+='0'; while(s2.size()<n)s2+='0'; int c=0; str r; for(int i=0;i<n;i++){ int s=(s1[i]-'0')+(s2[i]-'0')+c; r+=char(s%10+'0'); c=s/10; } if(c)r+=char(c+'0'); reverse(r.begin(),r.end());return rlz(r); } static str sa(const str&a,const str&b){ str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end()); while(s2.size()<s1.size())s2+='0';int b0=0;str r; str s1=a,s2=b; reverse(s1.begin(),s1.end()); reverse(s2.begin(),s2.end()); while(s2.size()<s1.size())s2+='0'; int b0=0; str r; for(int i=0;i<s1.size();i++){ int t=(s1[i]-'0')-(s2[i]-'0')-b0; if(t<0){t+=10;b0=1;}else b0=0;r+=char(t+'0');} while(r.size()>1&&r.back()=='0')r.pop_back();reverse(r.begin(),r.end());return rlz(r);} if(t<0){ t+=10; b0=1; } else b0=0; r+=char(t+'0'); } while(r.size()>1&&r.back()=='0')r.pop_back(); reverse(r.begin(),r.end()); return rlz(r); } static str mul_small(const str&a,int b){ if(b==0)return "0";str s1=a;reverse(s1.begin(),s1.end());int c=0;str r; for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')*b+c;r+=char(cur%10+'0');c=cur/10;} if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);} if(b==0)return "0"; str s1=a; reverse(s1.begin(),s1.end()); int c=0; str r; for(int i=0;i<s1.size();i++){ int cur=(s1[i]-'0')*b+c; r+=char(cur%10+'0'); c=cur/10; } if(c)r+=char(c+'0'); reverse(r.begin(),r.end()); return rlz(r); } static str add_one(const str&a){ str s1=a;reverse(s1.begin(),s1.end());int c=1;str r; for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')+c;r+=char(cur%10+'0');c=cur/10;} if(c)r+='1';reverse(r.begin(),r.end());return rlz(r);} str s1=a; reverse(s1.begin(),s1.end()); int c=1; str r; for(int i=0;i<s1.size();i++){ int cur=(s1[i]-'0')+c; r+=char(cur%10+'0'); c=cur/10; } if(c)r+='1'; reverse(r.begin(),r.end()); return rlz(r); } static str div2(const str&a){ str r;int cur=0;for(int i=0;i<a.size();i++){ cur=cur*10+(a[i]-'0');if(cur>=2||!r.empty())r+=char(cur/2+'0');cur%=2;} if(r.empty())r="0";return rlz(r);} static str mid_str(const str&a,const str&b){str sum=aa(a,b);return div2(sum);} str r; int cur=0; for(int i=0;i<a.size();i++){ cur=cur*10+(a[i]-'0'); if(cur>=2||!r.empty())r+=char(cur/2+'0'); cur%=2; } if(r.empty())r="0"; return rlz(r); } static str mid_str(const str&a,const str&b){ str sum=aa(a,b); return div2(sum); } static pair<str,str>div_opt(const str&a,const str&b){ if(b=="0")throw runtime_error("Division by zero");if(ca(a,b))return{"0",a}; str low="0",high=a,pow2="1";while(!ca(a,mul_fft(pow2,b))){low=pow2;pow2=aa(pow2,pow2);} if(high=="0"||ca(high,low))high=add_one(mul_small(low,2));str best="0"; if(b=="0")throw runtime_error("Division by zero"); if(ca(a,b))return{"0",a}; str low="0",high=a,pow2="1"; while(!ca(a,mul_fft(pow2,b))){ low=pow2; pow2=aa(pow2,pow2); } if(high=="0"||ca(high,low))high=add_one(mul_small(low,2)); str best="0"; while(!ca(high,low)){ str mid=mid_str(low,high),pro=mul_fft(mid,b); if(!ca(a,pro)){best=mid;low=add_one(mid);}else high=sa(mid,"1");} str rem=sa(a,mul_fft(best,b));return{best,rem};} if(!ca(a,pro)){ best=mid; low=add_one(mid); } else high=sa(mid,"1"); } str rem=sa(a,mul_fft(best,b)); return{best,rem}; }public: frint():v("0"),ng(0){} frint(const str&s){ str num=s;ng=0;if(!s.empty()&&s[0]=='-'){ng=1;num=s.substr(1);}else if(!s.empty()&&s[0]=='+')num=s.substr(1); v=rlz(num);if(v=="0")ng=0;} frint(long long n){if(n<0){ng=1;n=-n;}else ng=0;v=to_string(n);} str num=s; ng=0; if(!s.empty()&&s[0]=='-'){ ng=1; num=s.substr(1); } else if(!s.empty()&&s[0]=='+')num=s.substr(1); v=rlz(num); if(v=="0")ng=0; } frint(long long n){ if(n<0){ ng=1; n=-n; } else ng=0; v=to_string(n); } frint(const frint&o):v(o.v),ng(o.ng){} frint&operator=(const frint&o){if(this!=&o){v=o.v;ng=o.ng;}return*this;} frint operator-()const{frint r=*this;if(v!="0")r.ng=!ng;return r;} frint&operator=(const frint&o){ if(this!=&o){ v=o.v; ng=o.ng; } return*this; } frint operator-()const{ frint r=*this; if(v!="0")r.ng=!ng; return r; } frint operator+(const frint&o)const{ if(ng==o.ng){frint r;r.v=aa(v,o.v);r.ng=ng;return r;} if(ng)return o-(-*this);else return*this-(-o);} frint operator-(const frint&o)const{return*this+(-o);} if(ng==o.ng){ frint r; r.v=aa(v,o.v); r.ng=ng; return r; } if(ng)return o-(-*this); else return*this-(-o); } frint operator-(const frint&o)const{ return*this+(-o); } frint operator*(const frint&o)const{ if(v=="0"||o.v=="0")return frint("0"); frint r;r.v=mul_fft(v,o.v);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;} frint r; r.v=mul_fft(v,o.v); r.ng=(ng!=o.ng); if(r.v=="0")r.ng=0; return r; } frint operator/(const frint&o)const{ if(o.v=="0")throw runtime_error("Division by zero"); auto p=div_opt(v,o.v);frint r(p.first);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;} auto p=div_opt(v,o.v); frint r(p.first); r.ng=(ng!=o.ng); if(r.v=="0")r.ng=0; return r; } frint operator%(const frint&o)const{ if(o.v=="0")throw runtime_error("Modulo by zero"); auto p=div_opt(v,o.v);frint r(p.second);r.ng=ng;if(r.v=="0")r.ng=0;return r;} frint&operator+=(const frint&o){*this=*this+o;return*this;} frint&operator-=(const frint&o){*this=*this-o;return*this;} frint&operator*=(const frint&o){*this=*this*o;return*this;} frint&operator/=(const frint&o){*this=*this/o;return*this;} frint&operator%=(const frint&o){*this=*this%o;return*this;} auto p=div_opt(v,o.v); frint r(p.second); r.ng=ng; if(r.v=="0")r.ng=0; return r; } frint&operator+=(const frint&o){ *this=*this+o; return*this; } frint&operator-=(const frint&o){ *this=*this-o; return*this; } frint&operator*=(const frint&o){ *this=*this*o; return*this; } frint&operator/=(const frint&o){ *this=*this/o; return*this; } frint&operator%=(const frint&o){ *this=*this%o; return*this; } bool operator<(const frint&o)const{ if(ng!=o.ng)return ng;if(ng)return ca(o.v,v);else return ca(v,o.v);} bool operator>(const frint&o)const{return o<*this;} bool operator<=(const frint&o)const{return!(*this>o);} bool operator>=(const frint&o)const{return!(*this<o);} bool operator==(const frint&o)const{return v==o.v&&ng==o.ng;} bool operator!=(const frint&o)const{return!(*this==o);} frint&operator++(){*this=*this+frint(1);return*this;} frint operator++(int){frint t=*this;++(*this);return t;} frint&operator--(){*this=*this-frint(1);return*this;} frint operator--(int){frint t=*this;--(*this);return t;} friend ostream&operator<<(ostream&os,const frint&f){if(f.ng&&f.v!="0")os<<'-';os<<f.v;return os;} friend istream&operator>>(istream&is,frint&f){str s;is>>s;f=frint(s);return is;} str ts()const{if(ng&&v!="0")return"-"+v;return v;} frint ab()const{frint r=*this;r.ng=0;return r;} bool iz()const{return v=="0";} bool ip()const{return!ng&&v!="0";} bool in()const{return ng;} if(ng!=o.ng)return ng; if(ng)return ca(o.v,v); else return ca(v,o.v); } bool operator>(const frint&o)const{ return o<*this; } bool operator<=(const frint&o)const{ return!(*this>o); } bool operator>=(const frint&o)const{ return!(*this<o); } bool operator==(const frint&o)const{ return v==o.v&&ng==o.ng; } bool operator!=(const frint&o)const{ return!(*this==o); } frint&operator++(){ *this=*this+frint(1); return*this; } frint operator++(int){ frint t=*this; ++(*this); return t; } frint&operator--(){ *this=*this-frint(1); return*this; } frint operator--(int){ frint t=*this; --(*this); return t; } friend ostream&operator<<(ostream&os,const frint&f){ if(f.ng&&f.v!="0")os<<'-'; os<<f.v; return os; } friend istream&operator>>(istream&is,frint&f){ str s; is>>s; f=frint(s); return is; } str ts()const{ if(ng&&v!="0")return"-"+v; return v; } frint ab()const{ frint r=*this; r.ng=0; return r; } bool iz()const{ return v=="0"; } bool ip()const{ return!ng&&v!="0"; } bool in()const{ return ng; }};```
:::5 行未改动如果你不喜欢打 `frint`,请使用宏定义。
例如,你想要用 `fr` 代替这个类型:
```cpp```cpp line-numbers#define fr frint```
:::warning{open}
此行需要写在主函数上方、所有实现代码的下方,其他基于本代码实现的函数等也是同理,下文不再提醒。
如果你写了
```cpp```cpp line-numbers#define int frint```
请把主函数改为
```cpp```cpp line-numberssigned main(){
return 0;}```
7 行未改动因为乘法使用 `FFT`,干脆就把复数实现了。
:::info[复数实现]
```cpp// 复数类```cpp line-numbersclass Complex{private: frint a,b; // a+bi frint a,b;public: Complex():a(0),b(0){} Complex(frint _a,frint _b):a(_a),b(_b){} Complex(str s){ // 解析 a+bi 或 a-bi 格式 size_t pos=s.find('+'); if(pos!=str::npos){ str sa=s.substr(0,pos); str sb=s.substr(pos+1); sb.pop_back(); // 去掉最后的'i' sb.pop_back(); a=frint(sa); b=frint(sb); }else{ // 可能是 a-bi 或只有实数或虚数 } else{ pos=s.find('-'); if(pos!=0&&pos!=str::npos){ str sa=s.substr(0,pos); str sb=s.substr(pos); sb.pop_back(); // 去掉最后的'i' sb.pop_back(); a=frint(sa); b=frint(sb); }else if(s.back()=='i'){ // 纯虚数 } else if(s.back()=='i'){ a=0; str sb=s;sb.pop_back(); b=frint(sb); }else{ // 纯实数 } else{ a=frint(s); b=0; } } } // 四则运算 Complex operator+(const Complex& o)const{ return Complex(a+o.a,b+o.b); } Complex operator-(const Complex& o)const{ return Complex(a-o.a,b-o.b); } Complex operator*(const Complex& o)const{ // (a+bi)(c+di)=ac-bd+(ad+bc)i frint ra=a*o.a-b*o.b; frint rb=a*o.b+b*o.a; return Complex(ra,rb); } Complex operator/(const Complex& o)const{ // (a+bi)/(c+di)=[(ac+bd)/(c^2+d^2)]+[(bc-ad)/(c^2+d^2)]i frint c=o.a,d=o.b; frint den=c*c+d*d; // 分母 frint den=c*c+d*d; if(den.iz())throw runtime_error("Complex division by zero"); frint ra=(a*c+b*d)/den; frint rb=(b*c-a*d)/den; return Complex(ra,rb); } // 共轭复数 Complex conj()const{ return Complex(a,-b); } // 模的平方(整数) frint mod2()const{ return a*a+b*b; } // 输入输出 friend ostream& operator<<(ostream& os,const Complex& c){ if(c.a.iz()&&c.b.iz()){ os<<"0"; } }else if(c.b.iz()){ else if(c.b.iz()){ os<<c.a; } }else if(c.a.iz()){ else if(c.a.iz()){ if(c.b.ip())os<<c.b<<"i"; else os<<c.b<<"i"; } }else{ else{ os<<c.a; if(c.b.ip())os<<"+"<<c.b<<"i"; else os<<c.b<<"i"; } return os; } friend istream& operator>>(istream& is,Complex& c){ str s; is>>s; c=Complex(s); return is; } // 比较相等 bool operator==(const Complex& o)const{ return a==o.a&&b==o.b; } bool operator!=(const Complex& o)const{ return!(*this==o); } // 获取实部虚部 frint real()const{return a;} frint imag()const{return b;} // 其他运算 frint real()const{ return a; } frint imag()const{ return b; } Complex operator-()const{ return Complex(-a,-b); } Complex& operator+=(const Complex& o){ a+=o.a;b+=o.b;return*this; a+=o.a; b+=o.b; return*this; } Complex& operator-=(const Complex& o){ a-=o.a;b-=o.b;return*this; a-=o.a; b-=o.b; return*this; } Complex& operator*=(const Complex& o){ *this=*this*o;return*this; *this=*this*o; return*this; } Complex& operator/=(const Complex& o){ *this=*this/o;return*this; *this=*this/o; return*this; }};```
4 行未改动很简单的宏定义就可以做到。
:::info[数据类型名称替换]{open}
```cpp```cpp line-numbers#define str string#define float double```
请不要在程序中删除它。
:::
### To-do list
- [ ] 私有成员函数在类外部的使用。
### 测试用
此部分与正文无关,仅用于作者测试相关功能。此部分与正文无关,仅用于笔者测试相关功能。
:::info[测试]
```cppvoid test() { cout << "=== frint 高精度整数类测试 ===" << endl; // 基本测试 cout << "\n1. 基本构造函数和输出测试:" << endl; frint a("12345678901234567890"); frint b("98765432109876543210"); frint c(-123456); frint d = 999999999; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; cout << "d = " << d << endl; // 算术运算测试 cout << "\n2. 算术运算测试:" << endl; cout << "a + b = " << a + b << endl; cout << "b - a = " << b - a << endl; cout << "a * b = " << a * b << endl; cout << "b / a = " << b / a << endl; cout << "b % a = " << b % a << endl; // 负数运算测试 cout << "\n3. 负数运算测试:" << endl; frint e("-123456789"); frint f("987654321"); cout << "e = " << e << endl; cout << "f = " << f << endl; cout << "e + f = " << e + f << endl; cout << "e - f = " << e - f << endl; cout << "e * f = " << e * f << endl; cout << "f / e = " << f / e << endl; // 复合赋值测试 cout << "\n4. 复合赋值运算测试:" << endl; frint g("1000000000000000000"); frint h("3"); g += h; cout << "g += 3: " << g << endl; g *= frint("2"); cout << "g *= 2: " << g << endl; g /= frint("7"); cout << "g /= 7: " << g << endl; // 比较运算测试 cout << "\n5. 比较运算测试:" << endl; cout << "a < b: " << (a < b) << endl; cout << "a > b: " << (a > b) << endl; cout << "a == b: " << (a == b) << endl; cout << "a != b: " << (a != b) << endl; cout << "e < f: " << (e < f) << endl; // 递增递减测试 cout << "\n6. 递增递减测试:" << endl; frint i("999999999999999999"); cout << "i = " << i << endl; cout << "++i = " << ++i << endl; cout << "i++ = " << i++ << endl; cout << "i after i++ = " << i << endl; cout << "--i = " << --i << endl; cout << "i-- = " << i-- << endl; cout << "i after i-- = " << i << endl; // 大数性能测试 cout << "\n7. 大数运算性能测试:" << endl; frint j("1" + string(100, '9')); // 101位数字 frint k("1234567890"); auto start = chrono::high_resolution_clock::now(); frint l = j * k; auto end = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<chrono::milliseconds>(end - start); cout << "100位数字 * 10位数字: " << duration.count() << " ms" << endl; // 异常处理测试 cout << "\n8. 异常处理测试:" << endl; try { frint zero("0"); frint result = a / zero; cout << "Should not reach here!" << endl; } catch (const runtime_error& err) { cout << "Caught exception: " << err.what() << endl; } // Complex类测试 cout << "\n=== Complex 复数类测试 ===" << endl; Complex c1("3+4i"); Complex c2("1+2i"); Complex c3("-5-6i"); cout << "\n1. 复数基本运算:" << endl; cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; cout << "c3 = " << c3 << endl; cout << "c1 + c2 = " << c1 + c2 << endl; cout << "c1 - c2 = " << c1 - c2 << endl; cout << "c1 * c2 = " << c1 * c2 << endl; cout << "c1 / c2 = " << c1 / c2 << endl; cout << "\n2. 复数共轭和模平方:" << endl; cout << "conj(c1) = " << c1.conj() << endl; cout << "|c1|^2 = " << c1.mod2() << endl; cout << "\n3. 复数输入测试(请输入复数,如 5+3i 或 -2-4i): "; Complex c4; cin >> c4; cout << "你输入的复数是: " << c4 << endl; // 混合运算测试 cout << "\n4. 混合运算测试:" << endl; Complex c5 = c1 * c2 + c3; cout << "c1 * c2 + c3 = " << c5 << endl; cout << "\n5. 大数复数测试:" << endl; Complex big1("12345678901234567890+98765432109876543210i"); Complex big2("11111111111111111111+22222222222222222222i"); cout << "big1 = " << big1 << endl; cout << "big2 = " << big2 << endl; cout << "big1 + big2 = " << big1 + big2 << endl; cout << "big1 * big2 = " << big1 * big2 << endl; cout << "\n=== 所有测试完成 ===" << endl;}```cpp line-numbers//void test() { // cout << "=== frint 高精度整数类测试 ===" << endl; // // 基本测试 // cout << "\n1. 基本构造函数和输出测试:" << endl; // frint a("12345678901234567890"); // frint b("98765432109876543210"); // frint c(-123456); // frint d = 999999999; // cout << "a = " << a << endl; // cout << "b = " << b << endl; // cout << "c = " << c << endl; // cout << "d = " << d << endl; // // 算术运算测试 // cout << "\n2. 算术运算测试:" << endl; // cout << "a + b = " << a + b << endl; // cout << "b - a = " << b - a << endl; // cout << "a * b = " << a * b << endl; // cout << "b / a = " << b / a << endl; // cout << "b % a = " << b % a << endl; // // 负数运算测试 // cout << "\n3. 负数运算测试:" << endl; // frint e("-123456789"); // frint f("987654321"); // cout << "e = " << e << endl; // cout << "f = " << f << endl; // cout << "e + f = " << e + f << endl; // cout << "e - f = " << e - f << endl; // cout << "e * f = " << e * f << endl; // cout << "f / e = " << f / e << endl; // // 复合赋值测试 // cout << "\n4. 复合赋值运算测试:" << endl; // frint g("1000000000000000000"); // frint h("3"); // g += h; // cout << "g += 3: " << g << endl; // g *= frint("2"); // cout << "g *= 2: " << g << endl; // g /= frint("7"); // cout << "g /= 7: " << g << endl; // // 比较运算测试 // cout << "\n5. 比较运算测试:" << endl; // cout << "a < b: " << (a < b) << endl; // cout<< "a> b: " << (a > b) << endl; // cout << "a == b: " << (a == b) << endl; // cout << "a != b: " << (a != b) << endl; // cout << "e < f: " << (e < f) << endl; // // 递增递减测试 // cout << "\n6. 递增递减测试:" << endl; // frint i("999999999999999999"); // cout << "i = " << i << endl; // cout << "++i = " << ++i << endl; // cout << "i++ = " << i++ << endl; // cout << "i after i++ = " << i << endl; // cout << "--i = " << --i << endl; // cout << "i-- = " << i-- << endl; // cout << "i after i-- = " << i << endl; // // 大数性能测试 // cout << "\n7. 大数运算性能测试:" << endl; // frint j("1" + string(100, '9')); // 101位数字 // frint k("1234567890"); // auto start = chrono::high_resolution_clock::now(); // frint l = j * k; // auto end = chrono::high_resolution_clock::now(); // auto duration = chrono::duration_cast<chrono::milliseconds>(end - start); // cout << "100位数字 * 10位数字: " << duration.count() << " ms" << endl; // // 异常处理测试 // cout << "\n8. 异常处理测试:" << endl; // try { // frint zero("0"); // frint result = a / zero; // cout << "Should not reach here!" << endl; // } catch (const runtime_error& err) { // cout << "Caught exception: " << err.what() << endl; // } // // Complex类测试 // cout << "\n=== Complex 复数类测试 ===" << endl; // Complex c1("3+4i"); // Complex c2("1+2i"); // Complex c3("-5-6i"); // cout << "\n1. 复数基本运算:" << endl; // cout << "c1 = " << c1 << endl; // cout << "c2 = " << c2 << endl; // cout << "c3 = " << c3 << endl; // cout << "c1 + c2 = " << c1 + c2 << endl; // cout << "c1 - c2 = " << c1 - c2 << endl; // cout << "c1 * c2 = " << c1 * c2 << endl; // cout << "c1 / c2 = " << c1 / c2 << endl; // cout << "\n2. 复数共轭和模平方:" << endl; // cout << "conj(c1) = " << c1.conj() << endl; // cout << "|c1|^2 = " << c1.mod2() << endl; // cout << "\n3. 复数输入测试(请输入复数,如 5+3i 或 -2-4i): "; // Complex c4; // cin >> c4; // cout << "你输入的复数是: " << c4 << endl; // // 混合运算测试 // cout << "\n4. 混合运算测试:" << endl; // Complex c5 = c1 * c2 + c3; // cout << "c1 * c2 + c3 = " << c5 << endl; // cout << "\n5. 大数复数测试:" << endl; // Complex big1("12345678901234567890+98765432109876543210i"); // Complex big2("11111111111111111111+22222222222222222222i"); // cout << "big1 = " << big1 << endl; // cout << "big2 = " << big2 << endl; // cout << "big1 + big2 = " << big1 + big2 << endl; // cout << "big1 * big2 = " << big1 * big2 << endl; // cout << "\n=== 所有测试完成 ===" << endl; //}```
该测试函数使用deepseekR1生成。该测试函数使用deepseek-R1生成。
:::
## 集成
:::info[集成]:::info[用户集成]{open}
```cpp```cpp line-numbers#include <iostream>#include <vector>#include <string>#include <map>#include <algorithm>#include <sstream>#include <fstream>#include <windows.h>#include <cmath>#include <stdexcept>#include <chrono>#define str string#define float double#define endl '\n'using namespace std;class frint{private: str v;bool ng; struct C{float x,y;C(float _x=0.0,float _y=0.0):x(_x),y(_y){} C operator+(const C&b)const{return C(x+b.x,y+b.y);} C operator-(const C&b)const{return C(x-b.x,y-b.y);} C operator*(const C&b)const{return C(x*b.x-y*b.y,x*b.y+y*b.x);}}; str v; bool ng; struct C{ float x,y; C(float _x=0.0,float _y=0.0):x(_x),y(_y){} C operator+(const C&b)const{ return C(x+b.x,y+b.y); } C operator-(const C&b)const{ return C(x-b.x,y-b.y); } C operator*(const C&b)const{ return C(x*b.x-y*b.y,x*b.y+y*b.x); } }; static void FFT(vector<C>&a,int inv){ int n=a.size(); for(int i=1,j=0;i<n;i++){ int bit=n>>1;for(;j&bit;bit>>=1)j^=bit;j^=bit; if(i<j)swap(a[i],a[j]);} int bit=n>>1; for(;j&bit;bit>>=1)j^=bit; j^=bit; if(i<j)swap(a[i],a[j]); } for(int len=2;len<=n;len<<=1){ float ang=2*acos(-1.0)/len*inv;C wlen(cos(ang),sin(ang)); for(int i=0;i<n;i+=len){C w(1,0); float ang=2*acos(-1.0)/len*inv; C wlen(cos(ang),sin(ang)); for(int i=0;i<n;i+=len){ C w(1,0); for(int j=0;j<len/2;j++){ C u=a[i+j],v=a[i+j+len/2]*w; a[i+j]=u+v;a[i+j+len/2]=u-v;w=w*wlen;}}} if(inv==-1)for(int i=0;i<n;i++)a[i].x/=n;} a[i+j]=u+v; a[i+j+len/2]=u-v; w=w*wlen; } } } if(inv==-1)for(int i=0;i<n;i++)a[i].x/=n; } static str mul_fft(const str&a,const str&b){ if(a=="0"||b=="0")return "0"; int n=1,tot=a.size()+b.size();while(n<tot)n<<=1; int n=1,tot=a.size()+b.size(); while(n<tot)n<<=1; vector<C>c1(n),c2(n); for(int i=0;i<a.size();i++)c1[i]=C(a[a.size()-1-i]-'0',0); for(int i=0;i<b.size();i++)c2[i]=C(b[b.size()-1-i]-'0',0); FFT(c1,1);FFT(c2,1);for(int i=0;i<n;i++)c1[i]=c1[i]*c2[i];FFT(c1,-1); vector<int>res(n);for(int i=0;i<n;i++)res[i]=int(c1[i].x+0.5); for(int i=0;i<n-1;i++){res[i+1]+=res[i]/10;res[i]%=10;} FFT(c1,1); FFT(c2,1); for(int i=0;i<n;i++)c1[i]=c1[i]*c2[i]; FFT(c1,-1); vector<int>res(n); for(int i=0;i<n;i++)res[i]=int(c1[i].x+0.5); for(int i=0;i<n-1;i++){ res[i+1]+=res[i]/10; res[i]%=10; } while(res.size()>1&&res.back()==0)res.pop_back(); str ans;for(int i=res.size()-1;i>=0;i--)ans+=char(res[i]+'0');return ans;} static str rlz(const str&s){int i=0;while(i<s.size()-1&&s[i]=='0')i++;return s.substr(i);} static bool ca(const str&a,const str&b){if(a.size()!=b.size())return a.size()<b.size();return a<b;} str ans; for(int i=res.size()-1;i>=0;i--)ans+=char(res[i]+'0'); return ans; } static str rlz(const str&s){ int i=0; while(i<s.size()-1&&s[i]=='0')i++; return s.substr(i); } static bool ca(const str&a,const str&b){ if(a.size()!=b.size())return a.size()<b.size(); return a<b; } static str aa(const str&a,const str&b){ str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end()); int n=max(s1.size(),s2.size());while(s1.size()<n)s1+='0';while(s2.size()<n)s2+='0'; int c=0;str r;for(int i=0;i<n;i++){int s=(s1[i]-'0')+(s2[i]-'0')+c;r+=char(s%10+'0');c=s/10;} if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);} str s1=a,s2=b; reverse(s1.begin(),s1.end()); reverse(s2.begin(),s2.end()); int n=max(s1.size(),s2.size()); while(s1.size()<n)s1+='0'; while(s2.size()<n)s2+='0'; int c=0; str r; for(int i=0;i<n;i++){ int s=(s1[i]-'0')+(s2[i]-'0')+c; r+=char(s%10+'0'); c=s/10; } if(c)r+=char(c+'0'); reverse(r.begin(),r.end());return rlz(r); } static str sa(const str&a,const str&b){ str s1=a,s2=b;reverse(s1.begin(),s1.end());reverse(s2.begin(),s2.end()); while(s2.size()<s1.size())s2+='0';int b0=0;str r; str s1=a,s2=b; reverse(s1.begin(),s1.end()); reverse(s2.begin(),s2.end()); while(s2.size()<s1.size())s2+='0'; int b0=0; str r; for(int i=0;i<s1.size();i++){ int t=(s1[i]-'0')-(s2[i]-'0')-b0; if(t<0){t+=10;b0=1;}else b0=0;r+=char(t+'0');} while(r.size()>1&&r.back()=='0')r.pop_back();reverse(r.begin(),r.end());return rlz(r);} if(t<0){ t+=10; b0=1; } else b0=0; r+=char(t+'0'); } while(r.size()>1&&r.back()=='0')r.pop_back(); reverse(r.begin(),r.end()); return rlz(r); } static str mul_small(const str&a,int b){ if(b==0)return "0";str s1=a;reverse(s1.begin(),s1.end());int c=0;str r; for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')*b+c;r+=char(cur%10+'0');c=cur/10;} if(c)r+=char(c+'0');reverse(r.begin(),r.end());return rlz(r);} if(b==0)return "0"; str s1=a; reverse(s1.begin(),s1.end()); int c=0; str r; for(int i=0;i<s1.size();i++){ int cur=(s1[i]-'0')*b+c; r+=char(cur%10+'0'); c=cur/10; } if(c)r+=char(c+'0'); reverse(r.begin(),r.end()); return rlz(r); } static str add_one(const str&a){ str s1=a;reverse(s1.begin(),s1.end());int c=1;str r; for(int i=0;i<s1.size();i++){int cur=(s1[i]-'0')+c;r+=char(cur%10+'0');c=cur/10;} if(c)r+='1';reverse(r.begin(),r.end());return rlz(r);} str s1=a; reverse(s1.begin(),s1.end()); int c=1; str r; for(int i=0;i<s1.size();i++){ int cur=(s1[i]-'0')+c; r+=char(cur%10+'0'); c=cur/10; } if(c)r+='1'; reverse(r.begin(),r.end()); return rlz(r); } static str div2(const str&a){ str r;int cur=0;for(int i=0;i<a.size();i++){ cur=cur*10+(a[i]-'0');if(cur>=2||!r.empty())r+=char(cur/2+'0');cur%=2;} if(r.empty())r="0";return rlz(r);} static str mid_str(const str&a,const str&b){str sum=aa(a,b);return div2(sum);} str r; int cur=0; for(int i=0;i<a.size();i++){ cur=cur*10+(a[i]-'0'); if(cur>=2||!r.empty())r+=char(cur/2+'0'); cur%=2; } if(r.empty())r="0"; return rlz(r); } static str mid_str(const str&a,const str&b){ str sum=aa(a,b); return div2(sum); } static pair<str,str>div_opt(const str&a,const str&b){ if(b=="0")throw runtime_error("Division by zero");if(ca(a,b))return{"0",a}; str low="0",high=a,pow2="1";while(!ca(a,mul_fft(pow2,b))){low=pow2;pow2=aa(pow2,pow2);} if(high=="0"||ca(high,low))high=add_one(mul_small(low,2));str best="0"; if(b=="0")throw runtime_error("Division by zero"); if(ca(a,b))return{"0",a}; str low="0",high=a,pow2="1"; while(!ca(a,mul_fft(pow2,b))){ low=pow2; pow2=aa(pow2,pow2); } if(high=="0"||ca(high,low))high=add_one(mul_small(low,2)); str best="0"; while(!ca(high,low)){ str mid=mid_str(low,high),pro=mul_fft(mid,b); if(!ca(a,pro)){best=mid;low=add_one(mid);}else high=sa(mid,"1");} str rem=sa(a,mul_fft(best,b));return{best,rem};} if(!ca(a,pro)){ best=mid; low=add_one(mid); } else high=sa(mid,"1"); } str rem=sa(a,mul_fft(best,b)); return{best,rem}; }public: frint():v("0"),ng(0){} frint(const str&s){ str num=s;ng=0;if(!s.empty()&&s[0]=='-'){ng=1;num=s.substr(1);}else if(!s.empty()&&s[0]=='+')num=s.substr(1); v=rlz(num);if(v=="0")ng=0;} frint(long long n){if(n<0){ng=1;n=-n;}else ng=0;v=to_string(n);} str num=s; ng=0; if(!s.empty()&&s[0]=='-'){ ng=1; num=s.substr(1); } else if(!s.empty()&&s[0]=='+')num=s.substr(1); v=rlz(num); if(v=="0")ng=0; } frint(long long n){ if(n<0){ ng=1; n=-n; } else ng=0; v=to_string(n); } frint(const frint&o):v(o.v),ng(o.ng){} frint&operator=(const frint&o){if(this!=&o){v=o.v;ng=o.ng;}return*this;} frint operator-()const{frint r=*this;if(v!="0")r.ng=!ng;return r;} frint&operator=(const frint&o){ if(this!=&o){ v=o.v; ng=o.ng; } return*this; } frint operator-()const{ frint r=*this; if(v!="0")r.ng=!ng; return r; } frint operator+(const frint&o)const{ if(ng==o.ng){frint r;r.v=aa(v,o.v);r.ng=ng;return r;} if(ng)return o-(-*this);else return*this-(-o);} frint operator-(const frint&o)const{return*this+(-o);} if(ng==o.ng){ frint r; r.v=aa(v,o.v); r.ng=ng; return r; } if(ng)return o-(-*this); else return*this-(-o); } frint operator-(const frint&o)const{ return*this+(-o); } frint operator*(const frint&o)const{ if(v=="0"||o.v=="0")return frint("0"); frint r;r.v=mul_fft(v,o.v);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;} frint r; r.v=mul_fft(v,o.v); r.ng=(ng!=o.ng); if(r.v=="0")r.ng=0; return r; } frint operator/(const frint&o)const{ if(o.v=="0")throw runtime_error("Division by zero"); auto p=div_opt(v,o.v);frint r(p.first);r.ng=(ng!=o.ng);if(r.v=="0")r.ng=0;return r;} auto p=div_opt(v,o.v); frint r(p.first); r.ng=(ng!=o.ng); if(r.v=="0")r.ng=0; return r; } frint operator%(const frint&o)const{ if(o.v=="0")throw runtime_error("Modulo by zero"); auto p=div_opt(v,o.v);frint r(p.second);r.ng=ng;if(r.v=="0")r.ng=0;return r;} frint&operator+=(const frint&o){*this=*this+o;return*this;} frint&operator-=(const frint&o){*this=*this-o;return*this;} frint&operator*=(const frint&o){*this=*this*o;return*this;} frint&operator/=(const frint&o){*this=*this/o;return*this;} frint&operator%=(const frint&o){*this=*this%o;return*this;} auto p=div_opt(v,o.v); frint r(p.second); r.ng=ng; if(r.v=="0")r.ng=0; return r; } frint&operator+=(const frint&o){ *this=*this+o; return*this; } frint&operator-=(const frint&o){ *this=*this-o; return*this; } frint&operator*=(const frint&o){ *this=*this*o; return*this; } frint&operator/=(const frint&o){ *this=*this/o; return*this; } frint&operator%=(const frint&o){ *this=*this%o; return*this; } bool operator<(const frint&o)const{ if(ng!=o.ng)return ng;if(ng)return ca(o.v,v);else return ca(v,o.v);} bool operator>(const frint&o)const{return o<*this;} bool operator<=(const frint&o)const{return!(*this>o);} bool operator>=(const frint&o)const{return!(*this<o);} bool operator==(const frint&o)const{return v==o.v&&ng==o.ng;} bool operator!=(const frint&o)const{return!(*this==o);} frint&operator++(){*this=*this+frint(1);return*this;} frint operator++(int){frint t=*this;++(*this);return t;} frint&operator--(){*this=*this-frint(1);return*this;} frint operator--(int){frint t=*this;--(*this);return t;} friend ostream&operator<<(ostream&os,const frint&f){if(f.ng&&f.v!="0")os<<'-';os<<f.v;return os;} friend istream&operator>>(istream&is,frint&f){str s;is>>s;f=frint(s);return is;} str ts()const{if(ng&&v!="0")return"-"+v;return v;} frint ab()const{frint r=*this;r.ng=0;return r;} bool iz()const{return v=="0";} bool ip()const{return!ng&&v!="0";} bool in()const{return ng;} if(ng!=o.ng)return ng; if(ng)return ca(o.v,v); else return ca(v,o.v); } bool operator>(const frint&o)const{ return o<*this; } bool operator<=(const frint&o)const{ return!(*this>o); } bool operator>=(const frint&o)const{ return!(*this<o); } bool operator==(const frint&o)const{ return v==o.v&&ng==o.ng; } bool operator!=(const frint&o)const{ return!(*this==o); } frint&operator++(){ *this=*this+frint(1); return*this; } frint operator++(int){ frint t=*this; ++(*this); return t; } frint&operator--(){ *this=*this-frint(1); return*this; } frint operator--(int){ frint t=*this; --(*this); return t; } friend ostream&operator<<(ostream&os,const frint&f){ if(f.ng&&f.v!="0")os<<'-'; os<<f.v; return os; } friend istream&operator>>(istream&is,frint&f){ str s; is>>s; f=frint(s); return is; } str ts()const{ if(ng&&v!="0")return"-"+v; return v; } frint ab()const{ frint r=*this; r.ng=0; return r; } bool iz()const{ return v=="0"; } bool ip()const{ return!ng&&v!="0"; } bool in()const{ return ng; }};// 复数类class Complex{private: frint a,b; // a+bi frint a,b;public: Complex():a(0),b(0){} Complex(frint _a,frint _b):a(_a),b(_b){} Complex(str s){ // 解析 a+bi 或 a-bi 格式 size_t pos=s.find('+'); if(pos!=str::npos){ str sa=s.substr(0,pos); str sb=s.substr(pos+1); sb.pop_back(); // 去掉最后的'i' sb.pop_back(); a=frint(sa); b=frint(sb); }else{ // 可能是 a-bi 或只有实数或虚数 } else{ pos=s.find('-'); if(pos!=0&&pos!=str::npos){ str sa=s.substr(0,pos); str sb=s.substr(pos); sb.pop_back(); // 去掉最后的'i' sb.pop_back(); a=frint(sa); b=frint(sb); }else if(s.back()=='i'){ // 纯虚数 } else if(s.back()=='i'){ a=0; str sb=s;sb.pop_back(); b=frint(sb); }else{ // 纯实数 } else{ a=frint(s); b=0; } } } // 四则运算 Complex operator+(const Complex& o)const{ return Complex(a+o.a,b+o.b); } Complex operator-(const Complex& o)const{ return Complex(a-o.a,b-o.b); } Complex operator*(const Complex& o)const{ // (a+bi)(c+di)=ac-bd+(ad+bc)i frint ra=a*o.a-b*o.b; frint rb=a*o.b+b*o.a; return Complex(ra,rb); } Complex operator/(const Complex& o)const{ // (a+bi)/(c+di)=[(ac+bd)/(c^2+d^2)]+[(bc-ad)/(c^2+d^2)]i frint c=o.a,d=o.b; frint den=c*c+d*d; // 分母 frint den=c*c+d*d; if(den.iz())throw runtime_error("Complex division by zero"); frint ra=(a*c+b*d)/den; frint rb=(b*c-a*d)/den; return Complex(ra,rb); } // 共轭复数 Complex conj()const{ return Complex(a,-b); } // 模的平方(整数) frint mod2()const{ return a*a+b*b; } // 输入输出 friend ostream& operator<<(ostream& os,const Complex& c){ if(c.a.iz()&&c.b.iz()){ os<<"0"; } }else if(c.b.iz()){ else if(c.b.iz()){ os<<c.a; } }else if(c.a.iz()){ else if(c.a.iz()){ if(c.b.ip())os<<c.b<<"i"; else os<<c.b<<"i"; } }else{ else{ os<<c.a; if(c.b.ip())os<<"+"<<c.b<<"i"; else os<<c.b<<"i"; } return os; } friend istream& operator>>(istream& is,Complex& c){ str s; is>>s; c=Complex(s); return is; } // 比较相等 bool operator==(const Complex& o)const{ return a==o.a&&b==o.b; } bool operator!=(const Complex& o)const{ return!(*this==o); } // 获取实部虚部 frint real()const{return a;} frint imag()const{return b;} // 其他运算 frint real()const{ return a; } frint imag()const{ return b; } Complex operator-()const{ return Complex(-a,-b); } Complex& operator+=(const Complex& o){ a+=o.a;b+=o.b;return*this; a+=o.a; b+=o.b; return*this; } Complex& operator-=(const Complex& o){ a-=o.a;b-=o.b;return*this; a-=o.a; b-=o.b; return*this; } Complex& operator*=(const Complex& o){ *this=*this*o;return*this; *this=*this*o; return*this; } Complex& operator/=(const Complex& o){ *this=*this/o;return*this; *this=*this/o; return*this; }};//void test() {// cout << "=== frint 高精度整数类测试 ===" << endl;// // 基本测试// cout << "\n1. 基本构造函数和输出测试:" << endl;// frint a("12345678901234567890");// frint b("98765432109876543210");// frint c(-123456);// frint d = 999999999;// cout << "a = " << a << endl;// cout << "b = " << b << endl;// cout << "c = " << c << endl;// cout << "d = " << d << endl;// // 算术运算测试// cout << "\n2. 算术运算测试:" << endl;// cout << "a + b = " << a + b << endl;// cout << "b - a = " << b - a << endl;// cout << "a * b = " << a * b << endl;// cout << "b / a = " << b / a << endl;// cout << "b % a = " << b % a << endl;// // 负数运算测试// cout << "\n3. 负数运算测试:" << endl;// frint e("-123456789");// frint f("987654321");// cout << "e = " << e << endl;// cout << "f = " << f << endl;// cout << "e + f = " << e + f << endl;// cout << "e - f = " << e - f << endl;// cout << "e * f = " << e * f << endl;// cout << "f / e = " << f / e << endl;// // 复合赋值测试// cout << "\n4. 复合赋值运算测试:" << endl;// frint g("1000000000000000000");// frint h("3");// g += h;// cout << "g += 3: " << g << endl;// g *= frint("2");// cout << "g *= 2: " << g << endl;// g /= frint("7");// cout << "g /= 7: " << g << endl;// // 比较运算测试// cout << "\n5. 比较运算测试:" << endl;// cout << "a < b: " << (a < b) << endl;// cout << "a > b: " << (a > b) << endl;// cout << "a == b: " << (a == b) << endl;// cout << "a != b: " << (a != b) << endl;// cout << "e < f: " << (e < f) << endl;// // 递增递减测试// cout << "\n6. 递增递减测试:" << endl;// frint i("999999999999999999");// cout << "i = " << i << endl;// cout << "++i = " << ++i << endl;// cout << "i++ = " << i++ << endl;// cout << "i after i++ = " << i << endl;// cout << "--i = " << --i << endl;// cout << "i-- = " << i-- << endl;// cout << "i after i-- = " << i << endl;// // 大数性能测试// cout << "\n7. 大数运算性能测试:" << endl;// frint j("1" + string(100, '9')); // 101位数字// frint k("1234567890");// auto start = chrono::high_resolution_clock::now();// frint l = j * k;// auto end = chrono::high_resolution_clock::now();// auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);// cout << "100位数字 * 10位数字: " << duration.count() << " ms" << endl;// // 异常处理测试// cout << "\n8. 异常处理测试:" << endl;// try {// frint zero("0");// frint result = a / zero;// cout << "Should not reach here!" << endl;// } catch (const runtime_error& err) {// cout << "Caught exception: " << err.what() << endl;// }// // Complex类测试// cout << "\n=== Complex 复数类测试 ===" << endl;// Complex c1("3+4i");// Complex c2("1+2i");// Complex c3("-5-6i");// cout << "\n1. 复数基本运算:" << endl;// cout << "c1 = " << c1 << endl;// cout << "c2 = " << c2 << endl;// cout << "c3 = " << c3 << endl;// cout << "c1 + c2 = " << c1 + c2 << endl;// cout << "c1 - c2 = " << c1 - c2 << endl;// cout << "c1 * c2 = " << c1 * c2 << endl;// cout << "c1 / c2 = " << c1 / c2 << endl;// cout << "\n2. 复数共轭和模平方:" << endl;// cout << "conj(c1) = " << c1.conj() << endl;// cout << "|c1|^2 = " << c1.mod2() << endl;// cout << "\n3. 复数输入测试(请输入复数,如 5+3i 或 -2-4i): ";// Complex c4;// cin >> c4;// cout << "你输入的复数是: " << c4 << endl;// // 混合运算测试// cout << "\n4. 混合运算测试:" << endl;// Complex c5 = c1 * c2 + c3;// cout << "c1 * c2 + c3 = " << c5 << endl;// cout << "\n5. 大数复数测试:" << endl;// Complex big1("12345678901234567890+98765432109876543210i");// Complex big2("11111111111111111111+22222222222222222222i");// cout << "big1 = " << big1 << endl;// cout << "big2 = " << big2 << endl;// cout << "big1 + big2 = " << big1 + big2 << endl;// cout << "big1 * big2 = " << big1 * big2 << endl;// cout << "\n=== 所有测试完成 ===" << endl;//}signed main(){ // test();
return 0;}```
:::