/* 文件名 :Integer.main.cpp 程序功能:测试大整数类Integer的相关功能 by: lauo date: 2009-9-26 */ #include<iostream> #include<ctime> #include"Integer.h" using namespace std; void main() { cout<<Integer("134783247")+5.78475<<endl; cout<<Integer(5.78475)+"134783247"<<endl; cout<<Integer(-8475884545757)*"453475424657"<<endl; cout<<Integer(-8475884545757)*"-453475424657"<<endl; cout<<Integer(8475884545757)*"-453475424657"<<endl; cout<<(Integer(454452457)<"+567542457")<<endl; cout<<(Integer(454452457)<"-567542457")<<endl; cout<<(Integer(454452457)>="+454452457")<<endl; cout<<(Integer(454452457)>"+454452457")<<endl; cout<<(Integer(454452457)>5)<<endl; cout<<(Integer(454452457)>>5)<<endl; cout<<(Integer(454452457)<<5)<<endl; Integer in=(Integer("8243758234753984657948576835768")*"2845739857684576873586782374586457645"); Integer ptr=(Integer("8243758234753984657948576835768")*="2845739857684576873586782374586457645"); cout<<in<<endl; cout<<ptr<<endl; (ptr*=10000)+=10000; int iiii=478; (iiii*=10000)+=10000; cout<<iiii<<endl; cout<<ptr<<endl; cout<<(in*=100000)<<endl; cout<<in<<endl; srand(time(0)); Integer i(18734945823713478); cout<<i.toString()<<endl; Integer num,n0,v; goto mul; goto div; cout<<"please input two num:"; cin>>num>>n0; //cout<<num<<" "<<n0; num=num+n0; cout<<"num0+num1="<<num<<endl; //cout<<Integer(num).toString()<<" "<<Inendl; cout<<(num>>4)<<""<<endl; cout<<(num<<4)<<" "<<(num>>4)<<"||"<<endl; mul: cout<<"please input two num:"; cin>>num>>n0; num=num*n0; cout<<"num0*num1="<<num<<endl; { string str1="",str2=""; str1+=rand()%10+1+'0',str2+=rand()%10+1+'0'; cout<<str2<<endl; for(int k=0;k<2560;k++) { // str1+='0'; // str2+='0'; str1+=rand()%10+'0',str2+=rand()%10+'0'; //cout<<str1[k]<<str2[k]; } cout<<str1<<" "+str2<<endl; Integer i1(str1),i2(str2); cout<<"calc.."<<endl; clock_t t=clock(); i1=i1*i2; cout<<"used time:"<<clock()-t<<endl; cout<<"end."<<endl; } div: cout<<"please input two num:"; cin>>num>>n0; v=num/n0; cout<<"n0/n1="<<v<<endl; { string str1="",str2=""; str1+=rand()%10+1+'0',str2+=rand()%10+1+'0'; cout<<str2<<endl; for(int k=0;k<2560;k++) { // str1+='0'; // str2+='0'; str1+=rand()%10+'0',str2+=rand()%10+'0'; //cout<<str1[k]<<str2[k]; } str2.erase(rand()%str2.length(),str2.length()); cout<<str1<<" /n"+str2<<endl; Integer i1(str1),i2(str2),rev,mn,m; cout<<"calc.."<<endl; //getchar();getchar(); clock_t t=clock(); rev=i1/i2; mn=i1%i2; m=mn+(rev*i2); cout<<"used time:"<<clock()-t<<endl; cout<<"end."<<endl; cout<<rev<<endl; cout<<mn<<endl; cout<<m<<endl; } //cout<<i1<<endl; } 执行结果: 134783252 134783252 -3843605343730859122530349 3843605343730859122530349 -3843605343730859122530349 1 0 1 0 524574544 4544 45445245700000 23459591385754862969643951731905959594060114875944008055438053046360 23459591385754862969643951731905959594060114875944008055438053046360 4790000 234595913857548629696439517319059595940601148759440080554380530463610000 2345959138575486296964395173190595959406011487594400805543805304636000000 2345959138575486296964395173190595959406011487594400805543805304636000000 18734945823713478 please input two num:3 2 5 1 4 1 num0*num1=6 7 :7460673116481332552422630864700726087038245688267672941873072480149793801150850 34948687387352572025660034631293142264890713580031326890787906181166741187567213 71845479882354362363375502487263213357813744088107678908231953614965503434036581 71353271932423170589333144044277481997108539171166730082494549826981123806444283 95787302801967084874869963240284482725981587309963737497752863331842571638916256 40043835180004422951730268073736264564281251209085215543742885433857319334448718 31621405502204296253856543369972529015354348277197611029856025908057269086054624 08719670105780542997664584957428098091569557107118109977782617818211950246333346 46677513173685102072478868591171517201488109986201782992400654593386842053415245 51713005060204871196022509026797406399126277838316729168272142055867260997777223 32081177086185587938638726685003058964678176350226622700110681744117743968605847 47872182366083576094040310100668875881103428092278088209464937049410315337746228 69576602778119439466850038373898286785414642227327077081520493188809224888876189 65561112686146922741991309076812980023571537681834826060643031689973483960369127 45077670692057059042627140189558365660045290870234635039353973636069844324067524 70028845518594110092911954039881228491562906803514061110242487175824228102953189 00064885252324056690798034124029697345880526853051469282070418910055559466909298 10597023148563387308688462843229123655257985614984214880979844405187456665634996 46541790650094603524645645300006384677317543553549525421593916843492109081649471 73944314334285041072114927030981202826196041920011740820722856184079436093780641 20385718495299907721978264141749911189878244634771164568155417100820575572906766 56314792311879409895113217222132809995675429121360659576592134979872861739056855 64357433006391651018320825745753488777529881260799204453973642371327462455043166 86978715622131389004269110995056030226381543114663169743250760392024281687252561 95137315683990944194942544050175422105971484267225737365045017340898683068689397 39354534863816628026607366259187892582159945454627009792350274539297316079285000 25885183156270686581142124542903806122451839579282985849839245937524140066672513 03295621523617204539227907937110858676475496991064006265536001387207889412240765 14900285414450955247736276058091328341480747324943638151227051759351849777043779 19172838617788317710517354013320706912047065553442980666977310940952932635285128 51455038184346738633773366267151896808324675383008088356222799657915482813858610 99643142671393182407598767483942050348314436505465797562920775292875594206898548 0 731811781051273635396332139781013688916365763398082182253044702668961799330439 47685030696901840176347571148601025584000212389085450007242340209998413964215744 08051965444352426239384182343874594276217746917294869622330599254845632408162031 47417529073373563346404645701845320002654028559186316641120739386237068623473528 41005466259521069599014853929924465312669872002039062126886324265408603121270145 83882507174428198560909835391338106899832431005842290836280670299099033425722996 21602083377688060557634161302374422240233558960020382791873344527671537060539967 24057190044827731245845716052360812506647557035789012995487443744110786647103098 77062540963169034797940417137887141822735053333182980051652834072063684672989722 06412816005770948892589300244101138024287881110641657919488255515229741007987862 55685323957976485882545366939835550106125143684521465724364494637446032499824713 85635507197569921647715568972252510131809136886175752207002598120083985443678533 29656457235411809454567868081220195370804224880887185146326496330429974663423570 98926374442106321200995401095842643647978006404132809048436905289952722234640041 76601700620440101379664177299343202553369763615343548622342377597035947613231393 34719950271471133622788495447693740603310837669216318631599200162943563835994843 95425593111876786046653086847752050729901429599967810819692314558360761798330770 05656383513394119961579736520361436616973062473501462902058107760504074418844544 45943885416111418144621248930383534838493826373845994095435091939950048620300553 91734570798837251864438000305959335530124455834164618523973358701395971317764574 05632340805762536451086315362364502355822194950787945935961114838002015390094822 14931615439991324154405410535286759069785682981476410591891437599941154131434701 44949684701898900922025851218533738529291288718813251703993694712368595469395293 67085584373377206267633735290914526218978769199823608117278098803373427439580661 43837580339985870489035347683636821524762595550115213089139100982901294253943603 53167287324097756579905767067239057633717496112488873595474234052560462157274861 98045561382773517378424630461192938483931661018588588357004442960688656934625917 61052516758390789227739163590786441288518381224495976829636317124935352575525562 60065911047872020948690496154467415966041924130939471373461304080268198414325909 33760741122616015905609487079440996717689445247096127506625986456841045468313287 27617842637839302218507083460542378657197021249552445037007815593858753456691412 31220465530349962970378655843767371144025276966810726735363258800013082366845676 694 calc.. used time:0 end. please input two num:n0/n1=5 2 97164037688121680416831360432067011473071255924312429302774213355387740749117877 22241789255273180560558792323171755241474590580952106602655733739381684157961126 92559043649499895503991316483854380778448249784431081295101244121327409522918493 73996525236356729609184312411146502178662972647808200589349901016723123796037888 94757035015702313898227820233029072693952286645960694810782939947791309297976371 07974911766974145001783732900840276645753395762357937964126764680585300273126509 74114657990731738682217876801107927110491295240656419969088583432626013942975384 79834772555131183682217333009439051373958390184528931013418654754447953571563890 15859214290574369676993655327009379875751474166814228242247275355254234296747243 96892583506148369092156503252966348970193557487495317611009154818920847892408673 29395296389941625090798320827021979069818304225249183904764311348001244351441448 79069841380928818410646863815450045892638823159499500356283606468295790528899664 92608953067264278808009901506604914099494345912009232184100861828386857834792667 57279417339955642143500492257582605856162797349177366528034619905602444319020201 43192883814840460549200043486758061843433125221287434364235268067246436054689883 75702950873477931339773978785761195356224001359157511379576505860994688981924753 48184996349112458493190174033919203232402685450739552996809675223483555613224155 20298752145501554478983503065680117103390215242447001333092770315100435900796317 20757120168889096130689938091061717574415676618841130741017296200841827527861423 34709698684508660029578002152528389628062198782936528934566050276530284753691459 32391494014221535526369227862984582077183313094388561232418009172151735589858917 26601414594688460967301208672183624559245515965020650407882279820078287638939992 26256258239046618615312461422130492791603742710229346357064511703140633634860280 30643408543657434994480088787797498234230032421791111382677531615830602510590386 46273009701275319403236592196474430200383645952854211261745811670618661753808768 72881395716456030756186695349081755646675019284763110690370043088723913609663005 33373308022898468229651795897798456040487678605611396784236363275533678869752145 97098762534828548162086502164314255904979700692926928288547347404104449367236805 63284093554678236183992006977634656274600158380287425738554809966482461685622709 52403445185133051451578836922226656124746416842968563786221412305635101596800214 38678194935979460728147954226907911264532876995477981492443084744626618128977120 59014900443467726721550509848069710448602960570808659392637609263867711568112508 5 2441376781358756510859 calc.. used time:1649 end. 39798870223564879803489486399418413146416325803938074054108414896910354881674931 48848814923421438317568347445490695731603143680071625305392621604362598758612973 90714909121669922953162444538582045642856244925636200366018503948912747661777423 61440082575660674443741851483993025184813971425827296981909477181176688891895405 24622832133329460982166879766173914344181995955363200742814956648517670340240213 51339187135726093422665913999696544256268202118278145708335179049689899694173722 02858315142685775708986102286332105524241367190207822343083791496799989790394736 86112642971134109645915596499116879261221049151093933417745915434154924928733314 24294753291234345970234004079940236598930505012998641807253705580145750098299020 51443436013855820996778524589840937792207293286026610372543619018649313925990619 87185762874633729808219465524372012285050383737040966192528254801823491915550719 87075469907363328398580908829724047004393067138125834639385082863447941384970716 13151836904088616047386106840385551533696605847036562646872755012162138900460311 41937141138303231040035599294139960330133123329616090685396593288615303106772472 79097082683245540296564753784307497545589441376061837827255225068070822262969676 69721741691421710703632293127213607286366553589465626761895125644509690833838172 39985840859437012384650558208007630659010180994043584161058883564473186429795825 05866887086659907973733477936546928247072396592033075147921586544288487769655648 27357471555241336149336965476954964647725292972689048672906101960748519964191789 26925136382992158215054728973822112259896144330224454936744009824802592089072737 64539836059163910767967490775892386085277057425398051281032326081318057328299177 72330996764157035143416730738446879484644209433231775199142272101990371231476191 74901297334926257217370415330726747114328573760096713425082088004164139073471418 71163058718822521455061377616055491860190719501851448718230657770454073923994514 61878049629417862259429181475839946878361415203629464294523537564203644924984419 83773270100115650309250899290728995708886889318581451266675926314912680553519670 09597667774883590479003042792785982471135622960748739099053788586913504559928073 22723531854925095516717062904654346637286007473463343556762138403021235900896878 68500836038752551499589566503196048551816386972430135060543951361522999991432176 86841347787180233939792317416905667396516262660176268360441078782721667733620119 93958210454583209930514230423965151211939700613902971037840059866251913940712171 350354283905770635682951277518332006128740276071214887384497 1835871241355592372162 97164037688121680416831360432067011473071255924312429302774213355387740749117877 22241789255273180560558792323171755241474590580952106602655733739381684157961126 92559043649499895503991316483854380778448249784431081295101244121327409522918493 73996525236356729609184312411146502178662972647808200589349901016723123796037888 94757035015702313898227820233029072693952286645960694810782939947791309297976371 07974911766974145001783732900840276645753395762357937964126764680585300273126509 74114657990731738682217876801107927110491295240656419969088583432626013942975384 79834772555131183682217333009439051373958390184528931013418654754447953571563890 15859214290574369676993655327009379875751474166814228242247275355254234296747243 96892583506148369092156503252966348970193557487495317611009154818920847892408673 29395296389941625090798320827021979069818304225249183904764311348001244351441448 79069841380928818410646863815450045892638823159499500356283606468295790528899664 92608953067264278808009901506604914099494345912009232184100861828386857834792667 57279417339955642143500492257582605856162797349177366528034619905602444319020201 43192883814840460549200043486758061843433125221287434364235268067246436054689883 75702950873477931339773978785761195356224001359157511379576505860994688981924753 48184996349112458493190174033919203232402685450739552996809675223483555613224155 20298752145501554478983503065680117103390215242447001333092770315100435900796317 20757120168889096130689938091061717574415676618841130741017296200841827527861423 34709698684508660029578002152528389628062198782936528934566050276530284753691459 32391494014221535526369227862984582077183313094388561232418009172151735589858917 26601414594688460967301208672183624559245515965020650407882279820078287638939992 26256258239046618615312461422130492791603742710229346357064511703140633634860280 30643408543657434994480088787797498234230032421791111382677531615830602510590386 46273009701275319403236592196474430200383645952854211261745811670618661753808768 72881395716456030756186695349081755646675019284763110690370043088723913609663005 33373308022898468229651795897798456040487678605611396784236363275533678869752145 97098762534828548162086502164314255904979700692926928288547347404104449367236805 63284093554678236183992006977634656274600158380287425738554809966482461685622709 52403445185133051451578836922226656124746416842968563786221412305635101596800214 38678194935979460728147954226907911264532876995477981492443084744626618128977120 59014900443467726721550509848069710448602960570808659392637609263867711568112508 5 请按任意键继续. . . /* 文件名 :Integer.h 程序功能:定义大整数类Integer by: lauo date: 2009-9-26 */ #include<iostream> #include<string> using namespace std; #ifndef ___INTEGER_CLASS___ #define ___INTEGER_CLASS___ //#include"num_str.h" /* 此类的数据保存在num字符串对象中, 顺序为从低位到高位,与一般数字表示正好相反 sign表示的是符号位,0为正,1为负 */ class Integer { private: /*数据成员变量*/ string num;//用于保存位数据,与传统的书写数据顺序相反,保存的是数字,不是char型内容 bool sign;//用于表示正负号,当bool==true为负,反之为正 public: /*构造函数*/ Integer():num("/0"),sign(false){}//初始化为0,长度为1 Integer(const long long int n); Integer(const long int n); Integer(const int n); Integer(const short n); Integer(const long double d); Integer(const double d); Integer(const float f); Integer(const string num_str); Integer(const char* num_str); Integer(const Integer& i); private: Integer& inc(int pos); Integer& dec(int pos); public: Integer& trim(); Integer abs(const Integer &i); Integer& div(int digit); int mod(int digit); /*大整数基本运算*/ Integer &operator=(const Integer &i); Integer operator*(const Integer &i); Integer operator/(const Integer &i); Integer operator%(const Integer &i); Integer operator+(const Integer &i); Integer operator-(const Integer &i); Integer operator<<(const int i);//左移i个位置,乘以基底的i次方 Integer operator>>(const int i);//右移i个位置 Integer operator>(const int i);//循环右移i个位置 Integer& operator*=(const Integer &i); Integer& operator/=(const Integer &i); Integer& operator%=(const Integer &i); Integer& operator+=(const Integer &i); Integer& operator-=(const Integer &i); Integer& operator<<=(const int i);//左移i个位置,乘以基底的i次方 Integer& operator>>=(const int i);//右移i个位置 Integer& operator>=(const int i);//循环右移i个位置 /*大整数比较运算*/ bool operator<(const Integer& i); bool operator==(const Integer& i); bool operator<=(const Integer& i); bool operator>(const Integer& i); bool operator>=(const Integer& i); bool operator!=(const Integer& i); bool isZero(); /*大整数对外输入输出函数*/ friend istream& operator>>(istream& cin,Integer& i); friend ostream& operator<<(ostream& cout,const Integer& i); string toString(); }; #endif /* 文件名 :Integer.cpp 程序功能:大整数类Integer的成员函数 by: lauo date: 2009-9-26 */ #include"Integer.h" #define ABS(x) ((x)>=0?(x):(-x))//取绝对值 Integer::Integer(const long long int n) { sign=n<0;///得到符号,当n<0时为true即为负数 long long int m=ABS(n); int digit; while(digit=m%10,num+=digit,m/=10);//得到num的值 } Integer::Integer(const long int n) { long long int m=n; *this=Integer(m); } Integer::Integer(const int n) { long long int m=n; *this=Integer(m); } Integer::Integer(const short n) { long long int m=n; *this=Integer(m); } Integer::Integer(const long double d) { long long int n=(long long int)d;//这里使用了四舍五入的方式 Integer i(n);//先得到一个临时的Integer 对象,再用赋值号取得其值 *this=i; } Integer::Integer(const double d) { long long int n=(long long int)d; *this=Integer(n); } Integer::Integer(const float f) { long long int n=(long long int)f; *this=Integer(n); } /* 十进制大整数字符串构造函数 */ Integer::Integer(const string str)//标准的大整数构造方法,从大整数字符串中获取相应Integer信息 { int len=str.length(),i=0,j; for(;i<len;i++) if(!(str[i]<='9'&&str[i]>='0' || str[i]=='-' && i==0 || str[i]=='+' && i==0)) { num+='/0',sign=0; //throw string("错误的数字字符串类型!"); return; } i=(str[0]=='-'||str[0]=='+')?1:0; for(j=len-1;j>=i;j--) num+=str[j]-'0'; sign=(str[0]=='-');//当为1时,表明为负数 } Integer::Integer(const char* num_str) { *this=Integer(string(num_str)); } /* Integer::Integer(const string str) { NUM_TYPE rev=kind_of_num_str(str); if(rev==ERRORKIND)//类型出错 num+='/0',sign=0; else { if(rev==_DOUBLE_)//浮点数 { long double d=atof(str.c_str()); Integer i(d); *this=i; } else //整数 { long long int n; n=rev==HEXINT? h_atoi(str.c_str()) : (rev==BININT ? b_atoi(str.c_str()):atoi(str.c_str())); Integer i(n); *this=i; } } } */ Integer::Integer(const Integer& i) { *this=i; } Integer &Integer::operator=(const Integer &i) { this->num=i.num; this->sign=i.sign; return *this; } string Integer::toString() { string str=""; str+=sign?"-":""; for(int i=num.length()-1;i>=0;i--) str+=num[i]+'0'; return str; } Integer& Integer::trim() { int l=num.length()-1,j=0; for(;l>=1;l--,j++) if(num[l]!='/0') break; num.erase(l+1,j); if(num.length()==0) num+='/0'; return *this; } Integer Integer::abs(const Integer& i) { Integer j(i); j.sign=false;//为正 return j; } Integer& Integer::inc(int pos) { int len=num.length(); if(pos>=len) { num+=1; return *this; } num[pos]++; if(num[pos]>=10) { num[pos]-=10; return inc(pos+1); } return (*this).trim(); } Integer& Integer::dec(int pos) { int len=num.length(); if(pos>=len) return *this; num[pos]--; if(num[pos]<0) { num[pos]+=10; return dec(pos+1); } return (*this).trim(); } Integer& Integer::div(int digit)//除以一个数字 { if(digit>9||digit<1) return *this; int len=num.length(); for(int i=len-1;i>=0;i--) { if(i>0) num[i-1]+=num[i]%digit*10; num[i]/=digit; } return (*this).trim(); } int Integer::mod(int digit)//除以一个数字 { if(digit>9||digit<1) return -1; int len=num.length(); for(int i=len-1;i>=0;i--) { if(i>0) num[i-1]+=num[i]%digit*10; else return num[i]%digit; num[i]/=digit; } } //输入输出函数 istream& operator>>(istream& cin,Integer& i) { string str; cin>>str; i=Integer(str); return cin; } ostream& operator<<(ostream& cout,const Integer& i) { string str=""; str+=i.sign?"-":""; //cout<<i.num.length()<<endl; for(int j=i.num.length()-1;j>=0;j--) str+=i.num[j]+'0'; cout<<str; return cout; } //比较运算 bool Integer::operator<(const Integer& i) { if(sign==i.sign)//同符号 { int l1=num.length(),l2=i.num.length(); if(l1<l2) return !sign;//当为flase,表明为正数之间的比较,则长度较短的为值较小 else if(l1>l2) return sign;//当为false,表明为正数之间的比较,则返回false,表明this>i else { for(int l=l1-1;l>=0;l--) if(num[l]!=i.num[l])//如果当前数小于另一数,则表明返回值为!sign return num[l]<i.num[l]? !sign : sign; return false; } } else//符号不一样 return sign;//如果 this为正数,则返回sign=false } bool Integer::operator==(const Integer& i) { if(sign==i.sign) { int l1=num.length(),l2=i.num.length(); if(l1!=l2) return false; else { for(int l=l1-1;l>=0;l--) if(num[l]!=i.num[l]) return false; return true; } } else return false; } bool Integer::operator<=(const Integer& i) { if(*this<i ||*this==i) return true; else return false; } bool Integer::operator>(const Integer& i) { return !(*this<=i); } bool Integer::operator>=(const Integer& i) { return !(*this<i); } bool Integer::operator!=(const Integer& i) { return !(*this==i); } bool Integer::isZero() { return (num.length()==1&& num[0]==0); } //移位运算 Integer Integer::operator<<(const int i)//左移i个位置,乘以基底的i次方 { Integer j(*this); string str=""; for(int j=i;j>0;j--) str+='/0'; j.num=str+num; return j.trim(); } Integer Integer::operator>>(const int i)//右移i个位置 { Integer j(*this); j.num.erase(0,i); return j.trim(); } Integer Integer::operator>(const int i)//循环右移i个位置 { Integer j(*this); int len=num.length(); int k=i%len; for(int l=0;l<len;l++) j.num[l]=num[(l+k)%len];//拷贝 return j.trim(); } //加减乘除运算区 #if 1 //采用了递归算法(分治的方法) Integer Integer::operator*(const Integer &i)//乘 { Integer maxv,minv; /*取得最大最小值*/ if(abs(*this)<abs(i))//值的判断 maxv=i,minv=*this; else maxv=*this,minv=i; int l1=maxv.num.length(),l2=minv.num.length(); if(l2!=1) { int b1=l2/2,b2=l2-b1;//取得分界 Integer i1=minv>>b1,i2=(minv>b1)>>b2;//得到两个整数 return (( maxv*i1 )<<b1) +( maxv*i2);//递归运算方式 } else { if(maxv.isZero()||minv.isZero()) return Integer("0"); int mv=minv.num[0],n,l; for(l=0;l<l1;l++) maxv.num[l]*=mv; for(l=l1-1;l>=0;l--) { n=maxv.num[l]/10; maxv.num[l]-=n*10; while(n) maxv.inc(l+1),n--; } maxv.sign=maxv.sign!=minv.sign;//为负数 } return maxv.trim(); } #endif #if 0 //采用了以循环化解递归的大整数乘算法 Integer Integer::operator*(const Integer &i)//乘 { Integer maxv,minv,rev=Integer("0"); /*取得最大最小值*/ if(abs(*this)<abs(i))//值的判断 maxv=i,minv=*this; else maxv=*this,minv=i; int l1=maxv.num.length(),l2=minv.num.length(); Integer m[10]={Integer("0"),maxv}; for(int k=2,l;k<10;k++) { m[k]=maxv; for(l=0;l<l1;l++) m[k].num[l]*=k; for(l=l1-1;l>=0;l--) { int n=m[k].num[l]/10; m[k].num[l]-=n*10; while(n) m[k].inc(l+1),n--; } //cout<<k<<endl; } for(int k=0;k<l2;k++) { if(minv.num[k]==0) continue; rev=rev+(m[minv.num[k]]<<k); //cout<<k<<endl; } rev.sign=maxv.sign!=minv.sign;//哪果符号不相等,则为1,即负数 return rev; } #endif //采用了类似人工手算的移位试探法 Integer Integer::operator/(const Integer &i) { if(Integer(i).isZero()) return Integer("-0"); Integer m, rev=Integer("0") , lev,th=*this; while(true) { if(abs(th)<abs(i)) break; int a=th.num.length(),b=i.num.length(),c=a-b,n; Integer absi=abs(i), abt=abs(th), absd=absi<<c; if(!(absd<=abt)) absd=absd>>1,n=(th.num[a-1]*10+th.num[a-2])/i.num[b-1],c--; else n=th.num[a-1]/i.num[b-1]; n+=1; n=n>9?9:n; for(lev.sign=1;lev.sign;n--)//试探法 { m=Integer(long long int(n)); lev=abt-m*absd; if(lev.sign==0) rev=rev+(m<<c); } th=lev; } rev.sign=sign!=i.sign; return rev; } //二次取中法求除数,过慢了 #if 0 Integer Integer::operator/(const Integer &i)//除 { if(abs(*this)<abs(i)) return Integer("0"); int a0,a1,b0,b1; a0=num.length(),a1=num.length()+1; b0=i.num.length(),b1=i.num.length()+1; Integer maxv("1"),minv("1"),mid("1"),old; Integer abt=abs(*this),absi=abs(i),abv; maxv=maxv<<(a1-b0);//上界 minv=minv<<(a0-b1);//下界 do { old=mid; mid=(maxv+minv).div(2);//取中值 abv=mid*absi; if(abv<=abt)//如果abv小于abt { if((abt-abv)<absi) return mid; minv=mid; } else maxv=mid; //cout<<maxv<<" "<<minv<<endl; } while(!(old==mid)); return maxv; } #endif Integer Integer::operator%(const Integer &i)//模 { if(Integer(i).isZero()) return Integer("-0"); Integer m, lev,th=*this; while(true) { if(abs(th)<abs(i)) return th; int a=th.num.length(),b=i.num.length(),c=a-b,n; Integer absi=abs(i), abt=abs(th), absd=absi<<c; if(!(absd<=abt)) absd=absd>>1,n=(th.num[a-1]*10+th.num[a-2])/i.num[b-1],c--; else n=th.num[a-1]/i.num[b-1]; n+=1; n=n>9?9:n; for(lev.sign=1;lev.sign;n--)//试探法 { m=Integer(long long int(n)); lev=abt-m*absd; } th=lev; } return th; } Integer Integer::operator+(const Integer &i)//加 { Integer maxv,minv; /*取得最大最小值*/ if(abs(*this)<abs(i))//值的判断 maxv=i,minv=*this; else maxv=*this,minv=i; int l=minv.num.length(),j=0; if(maxv.sign!=minv.sign)//符号不相同,则相减 { for(;j<l;j++) { maxv.num[j]-=minv.num[j]; if(maxv.num[j]<0) { maxv.num[j]+=10; maxv.dec(j+1);//补偿 } } } else//符号相同,则相加 { for(;j<l;j++) { maxv.num[j]+=minv.num[j]; if(maxv.num[j]>=10) { maxv.num[j]-=10;//补偿 maxv.inc(j+1); } } } return maxv.trim(); } Integer Integer::operator-(const Integer &i)//减 { Integer j(i); j.sign=!j.sign; j=*this+j; return j.trim(); } Integer& Integer::operator*=(const Integer &i) { return *this=(*this*i); } Integer& Integer::operator/=(const Integer &i) { return *this=(*this/i); } Integer& Integer::operator%=(const Integer &i) { return *this=(*this%i); } Integer& Integer::operator+=(const Integer &i) { return *this=(*this+i); } Integer& Integer::operator-=(const Integer &i) { return *this=(*this-i); } Integer& Integer::operator<<=(const int i)//左移i个位置,乘以基底的i次方 { return *this=(*this<<i); } Integer& Integer::operator>>=(const int i)//右移i个位置 { return *this=(*this>>i); } Integer& Integer::operator>=(const int i)//循环右移i个位置 { return *this=(*this>i); }