高精度
来源:互联网 发布:编程自学网站 编辑:程序博客网 时间:2024/04/29 22:31
高精度乘法
/*支持高精度与高精度的加减乘除和取余。支持高精度乘单精度 不过要单精度在右边。支持所有比较。支持cin和cout*/#include<iostream>#include<iomanip>#include<cstring>#include<cstdio>#include<cmath>#include<memory>#include<algorithm>#include<string>#include<climits>using namespace std;const int THP_MAXLEN=20050;const int THP_MAXSTRLEN=20050; //这两个常量都是必须要的,表示高精度长度.const int L0=10000; //每一位存4位数char s[THP_MAXLEN*4]; //int给高精度赋值用class thp //高精度类的名称{ friend istream& operator>>(istream&,thp&); /*输入运算符友元*/ friend ostream& operator<<(ostream&,const thp&); /*输出运算符友元*/ friend const thp operator-(const thp&,const thp&); friend const thp operator+(const thp&,const thp&); friend const thp operator*(const thp&,int); /*高精度乘单精度*/ friend const thp operator*(const thp&,const thp&); friend const thp operator/(const thp&,const thp&); friend void thpdiv(const thp&,const thp&,thp&,thp&); friend const thp operator%(const thp&,const thp&); public: const thp& operator=(const thp&); const thp& operator=(const char*); const thp& operator=(int num); bool operator==(const thp&)const; bool operator>=(const thp&)const; bool operator<=(const thp&)const; inline bool operator!=(const thp&)const; bool operator>(const thp&)const; bool operator<(const thp&)const; inline void shl(const int); const thp& operator ++ (); inline void makeempty(); const char *tostr()const; inline thp& operator-=(const thp&); thp(); thp(const char*); const int thpstd(); protected: //几个数据项,_high表示位数。 int _data[THP_MAXLEN],_high,_sign; //_data 是每位的存储。};const int thp::thpstd(){ int i; for(i=0;i<=_high;i++) { while(_data[i]<0) { _data[i]+=L0; _data[i+1]--; } } return(0);}const thp operator % (const thp &a,const thp &b){ thp re1,re2; thpdiv(a,b,re1,re2); return(re2);}const thp& thp::operator ++ (){ _data[0]++; if(_data[0]>=L0)this->thpstd(); return(*this); }void thp::shl(const int n){ int i; for(i=_high;i>=0;i--) { _data[i+n]=_data[i]; } _high+=n; if(_data[_high]==0)_high=0;}thp& thp::operator -= (const thp &o){ int tint,i,len; for(i=0;i<=_high;i++) { tint=_data[i]-o._data[i]; if(tint<0) { tint+=L0; _data[i+1]--; } _data[i]=tint; } for(len=this->_high;len>=0;len--) { if(_data[len]!=0)break; if(len==0)break; } _high=len; return(*this);}void thpdiv(const thp &a,const thp &b,thp &c,thp &d){ int i; d.makeempty(); c.makeempty(); for(i=a._high;i>=0;i--) { d.shl(1); d._data[0]=a._data[i]; while(b<=d) { d-=b; c._data[i]++; } if(i==0)break; } c._high=0; for(i=a._high;i>0;i--) { if(c._data[i]!=0) { c._high=i;break; } }}inline bool thp::operator != (const thp &o)const{ return(!(0==memcmp(this,&o,sizeof(thp))));}bool thp::operator == (const thp &o)const{ return(0==memcmp(this,&o,sizeof(thp)));}inline bool thp::operator < (const thp &o)const{ int i; if(*this==o)return(false); { if(this->_high!=o._high)return(this->_high<o._high); for(i=this->_high;i>=0;i--) { if(this->_data[i]!=o._data[i])return(this->_data[i]<o._data[i]); if(i==0)break; } } return(false);}bool thp::operator > (const thp &o)const{ if(*this==o)return(false); return(!(*this<o));}bool thp::operator <= (const thp &o)const{ return(*this<o||*this==o);}bool thp::operator >= (const thp &o)const{ return(!(*this<o));}inline int max(int a,int b){ return(a>b?a:b);}thp::thp(){ makeempty();}thp::thp(const char *s){ *this=s;}const thp operator-(const thp &a,const thp &b){ int i,len,tint; thp re; len=max(a._high,b._high); for(i=0;i<=len;i++) { tint=a._data[i]-b._data[i]+re._data[i]; if(tint<0) { tint+=L0; re._data[i+1]--; } re._data[i]=tint; } while(len>0&&re._data[len]<1)len--; re._high=len; return(re);}const thp operator/(const thp &a,const thp &b){ thp re1,re2; thpdiv(a,b,re1,re2); return(re1);}const thp operator*(const thp &a,const thp &b){ int i,j,tint,len; thp re; for(i=0;i<=a._high;i++) for(j=0;j<=b._high;j++) { tint=a._data[i]*b._data[j]+re._data[i+j]; re._data[i+j]=tint%L0; re._data[i+j+1]+=tint/L0; } len=i+j; if(re._data[len+1]!=0)len++; re._high=len; return(re);} /*高精度乘以低精度(注意:必须是bign*int顺序不能颠倒,要么会与高精度乘高精度发生冲突*/ const thp operator*(const thp& a,int b) { int i,t,c=0; thp res; for(i=0;i<=a._high;i++) { t=a._data[i]*b+c; res._data[i]=t%L0; c=t/L0; } res._high=a._high; while(c!=0) { res._data[i++]=c%L0; c/=L0; res._high++; } return res; } /*(可选)加法运算符重载*/const thp operator+(const thp &a,const thp &b){ int i,len,tint; thp re; len=max(a._high,b._high); for(i=0;i<=len;i++) { tint=a._data[i]+b._data[i]+re._data[i]; re._data[i]=tint%L0; re._data[i+1]=tint/L0; } if(re._data[len+1]!=0)len++; re._high=len; return(re);}const char *thp::tostr()const{ char *re; char ts[5]; int i,j,tint; re=(char *)malloc(THP_MAXSTRLEN); memset(ts,0,sizeof(ts)); memset(re,0,THP_MAXSTRLEN); for(i=_high;i>=0;i--) { tint=_data[i]; for(j=0;j<4;j++) { ts[3-j]=tint%10+'0'; tint/=10; } strcat(re,ts); } while(strlen(re)>1&&*re=='0')re++; return(re);}inline void thp::makeempty(){ memset(this,0,sizeof(thp));}const thp& thp::operator = (const char *s){ char ts[5]; memset(ts,0,sizeof(ts)); int len,i; makeempty(); len=strlen(s); _high=(len-1)/4; i=(len-1)%4+1; strncpy(ts,s,i); s+=i; _data[_high]=atoi(ts); i=_high; while(i>0) { i--; memset(ts,0,sizeof(ts)); strncpy(ts,s,4); s+=4; _data[i]=atoi(ts); } return(*this);}const thp& thp::operator = (const thp &hp){ memcpy(this,&hp,sizeof(thp)); return(*this);}const thp& thp::operator=(const int num) { memset(s,0,sizeof(s)); sprintf(s,"%d",num); *this=s; return *this; } istream& operator>>(istream &in,thp& x) { string s; in>>s; x=s.c_str(); return in; } ostream& operator<<(ostream &out,const thp& x) { out<<x.tostr(); return out; }int main(){ //freopen("P1109.in","r",stdin); thp a,b,c; cin>>a>>b; c=a/b; cout<<c<<endl; return 0;}
0 0
- 高精度
- 高精度
- 高精度
- 高精度。。
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 【最好用的】Java APNS开源库apns4j-1.1.1发布
- 『python工作』文件搜索器
- 学生成绩管理详细设计
- android:windowSoftInputMode属性
- HBase数据的导入和导出
- 高精度
- Mysql优化
- gdb使用指南
- Javascript--$.post()执行无效的问题解决
- 文件下载,IE下文件名乱码【解决方案】
- GD Feed项目流程问题
- “鲇鱼”致浙江78.01万人受灾 直接经济损失逾20亿
- lazyload.js实现图片异步载入
- 视图控制器背景图片设置