压位高精(万进制)//头文件: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 thp{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 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*);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:int _data[THP_MAXLEN],_high,_sign;private:};// 源代码 thp.cpp#include "thp.h"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);}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);//测试代码cpp.cpp#ifndef _iostream_#define _iostream_#include <iostream>#endif#ifndef _ctime_#define _ctime_#include <ctime>#endif#ifndef _conio_h_#define _conio_h_#include <conio.h>#endif#include "thp.h"using std::cout;using std::endl;int main(){ clock_t t1=clock();thpa="123123122344444444444234444444444444411222222333333333333333333333 333333333333333333333\ 342342333333333333333333333333333333333333333333333333333333333333333 3333333333333333333333\235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666 6\ 342342333333333333333333333333333333333333333333333333333333333333333 3333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3423423333333333333333333333333333333333333333333333333333333333333 333333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\ 444444444444444444444444444444444444444444444443423452234234342";thp b="23423433444444444444444444444444444444444423423423423423\ 342342333333333333333333333333333333333333333333333333333333333333333 3333333333333333333333\ 235234534563452342342344444987724352345234234234000000000000000000000 00000000000000000000000000\ 666666666666666666666666666666666666666666666666666666666666666666666 666666666666666666666666666666666666666666666666666666666666666666666 6\3333333333323423";thp c=a/b;thp d=c*b;thp e=d+a%b;cout<<e.tostr()<<endl;cout<<"Run time="<<(clock()-t1)<<"ms"<<endl;getch();return 0;}。