高精度四则运算万进制C++_第1页
高精度四则运算万进制C++_第2页
高精度四则运算万进制C++_第3页
高精度四则运算万进制C++_第4页
高精度四则运算万进制C++_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、高进度四则运算 C+版(By小贝)压位高精(万进制)头文件:thp.h#ifndef _cstring_#define _cstring_#include <cstring>#endif#ifndef _cstdlib_#define _cstdlib_#include <cstdlib>#endifconst int THP_MAXLEN=1000;const int THP_MAXSTRLEN=4040;const int L0=10000;class thpfriend const thp operator-(const thp&,const thp&am

2、p;);friend const thp operator+(const thp&,const thp&);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&am

3、p;);public:const thp& operator=(const thp&);const thp& operator=(const char*);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 operat

4、or<(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:int _dataTHP_MAXLEN,_high,_sign;private:;/源代码thp.cpp#include "thp.h

5、"const int thp:thpstd() int i;for(i=0;i<=_high;i+) while(_datai<0)_datai+=L0;_datai+1-;高进度四则运算 C+版(By小贝))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 + ()(_data0+;if(_data0>=L0)this->thp

6、std();return(*this);)void thp:shl(const int n)( int i;for(i=_high;i>=0;i-)( _datai+n=_datai;)_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=_datai-o._datai;高进度四则运算 C+版(By小贝)if(tint<0)( tint+=L0;_datai+1-;)_datai=tint;)

7、for(len=this->_high;len>=0;len-)( if(_datalen!=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._data0=a._datai;while(b<=d)(d-=b;c._datai+;)if(i=0)bre

8、ak;)c._high=0;for(i=a._high;i>0;i-)( 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

9、;if(*this=o)return(false);( if(this->_high!=o._high)return(this->_high<o._high);for(i=this->_high;i>=0;i-)( if(this->_datai!=o._datai)return(this->_datai<o._datai); if(i=0)break;)return(false);) bool thp:operator > (const thp &o)const( if(*this=o)return(false);return(!

10、(*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,c

11、onst thp &b)( int i,len,tint;thp re;len=max(a._high,b._high);for(i=0;i<=len;i+)( tint=a._datai-b._datai+re._datai;if(tint<0)( tint+=L0;re._datai+1-;re._datai=tint;)while(len>0&&re._datalen<1)len-;re._high=len;return(re);)const thp operator/(const thp &a,const thp &b)

12、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._datai*b._dataj+re._datai+j;re._datai+j=tint%L0;re._datai+j+1+=tint/L0;)len=i+j;if(re._datalen+1!=0)len+;re._high=len;

13、return(re);) const thp operator+(const thp &a,const thp &b)int i,len,tint;高进度四则运算 C+版(By小贝)thp re;len=max(a._high,b._high);for(i=0;i<=len;i+) tint=a._datai+b._datai+re._datai;re._datai=tint%L0;re._datai+1=tint/L0;if(re._datalen+1!=0)len+;re._high=len;return(re);const char *thp:tostr()cons

14、t char *re;char ts5;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=_datai;for(j=0;j<4;j+)ts3-j=tint%10+'0'tint/=10;strcat(re,ts);while(strlen(re)>1&&*re='0')re+;return(re);高进度四则运算 C+版(By小贝)inl

15、ine void thp:makeempty()( memset(this,0,sizeof(thp);)const thp& thp:operator = (const char *s)( char ts5;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,

16、4);s+=4;_datai=atoi(ts);)return(*this);)const thp& thp:operator = (const thp &hp)( memcpy(this,&hp,sizeof(thp);return(*this);)测试代码cpp.cpp#ifndef _iostream_#define _iostream_#include <iostream>#endif#ifndef _ctime_#define _ctime_#include <ctime>#endif#ifndef _conio_h_#define _

17、conio_h_#include <conio.h>#endif#include "thp.h"using std:cout;using std:endl;int main() clock_t t1=clock();thpa="123123122344444444444234444444444444411222222333333333333333333333333333333333333333333342342333333333333333333333333333333333333333333333333333333333333333333333333

18、3333333333333235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666 6342342333333333333333333333333333333333333333333333

19、3333333333333333333333333333333333333333235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666 6342342333333333333333333

20、3333333333333333333333333333333333333333333 333333333333333333333333235234534563452342342344444987724352345234234234000000000000000000000 000000000000000000000000006666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666

21、66 63423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666 63423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333235234534563452342342344444987724352345234234234000000000000000000000 0000000000000000000000000066666666666666666666666666666666666666666666666666666666666666666666

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论