Also just wrote the addition, with 1 000 000 000 in the system, but found that for addition is not as easy as the most basic method. The convenience of using 1 000 000 000 is also to deal with multiplication and division. It is still time-consuming for the computer to do multiplication operations.
#include <random>#include <iostream>#include <fstream>#include <string>#include <vector>#include <utility>Using namespace Std;constintmaxnum=1000000000;//Binary class bignumcalcu{ Public: constStatic intn=9; BIGNUMCALCU (): Num_1 (""), Result (""), Numsize_1 (0), Numsize_2 (0){//initNum_2=""; Fopen.open ("Num.txt"); } String Patchzero (intM);//Complement 0 voidGetInput ();//Read Data voidSplitnum ();voidSum ();//AdditionString Sub ();//Subtraction voidMulti ();//MultiplicationString Div ();//Division voidRandomdata ();Private: FStream fopen; vector<int> bitnum_1,bitnum_2; String Num_1,num_2,result; UnsignedintNumsize_1; Unsignedintnumsize_2;};voidBignumcalcu::getinput () { for(intI=0; i!=5; ++i) {result=""; Bitnum_1.clear (); Bitnum_2.clear (); fopen>>num_1>>num_2; cout<<"Num_1:"<<num_1<<"Num_2:"<<num_2<<endl; Splitnum (); Sum (); }}voidBignumcalcu::splitnum () {numsize_1= (num_1.size ()%9==0) ? Num_1.size ()/9: Num_1.size ()/9+1; Numsize_2= (num_2.size ()%9==0) ? Num_2.size ()/9: Num_2.size ()/9+1;//split if(Numsize_1==num_1.size ()/9) for(int Index=1;Index<=numsize_1;++Index) Bitnum_1.push_back (Std::stoi (Num_1.substr (Num_1.size ()-Index*bignumcalcu::n,9)));Else for(int Index=1;Index<=numsize_1;++Index){if(Index==numsize_1) Bitnum_1.push_back (Std::stoi (Num_1.substr (0, Num_1.size ()-(Index-1)) (*bignumcalcu::n));ElseBitnum_1.push_back (Std::stoi (Num_1.substr (Num_1.size ()-Index*bignumcalcu::n,9))); }if(Numsize_2==num_2.size ()/9) for(int Index=1;Index<=numsize_2;++Index) Bitnum_2.push_back (Std::stoi (Num_2.substr (Num_2.size ()-Index*bignumcalcu::n,9)));Else for(int Index=1;Index<=numsize_2;++Index){if(Index==numsize_2) Bitnum_2.push_back (Std::stoi (Num_2.substr (0, Num_2.size ()-(Index-1)) (*bignumcalcu::n));ElseBitnum_2.push_back (Std::stoi (Num_2.substr (Num_2.size ()-Index*bignumcalcu::n,9))); }}string BIGNUMCALCU::p Atchzero (intm) {Auto it=std::to_string (m). Size (); String temp=std::to_string (M);if(IT<BIGNUMCALCU::N) { for(; it<bignumcalcu::n;it++) temp="0"+temp;returnResult=temp+result; }Else returnResult=std::to_string (M);}voidBignumcalcu::sum () {intMin=std::min (numsize_1,numsize_2);int Index=0;//Rounding int sum=0;//If two numbers have the same number of digits //If the number of two digits is different if(min==numsize_1) for(int Count=min;Count<numsize_2;++Count)if((sum=bitnum_2[Count]+Index) {>=maxnum) {Index=1; Result=patchzero (sum-maxnum); }Else{Index=0; Result=patchzero (sum); }Else for(int Count=min;Count<numsize_1;++Count)if((sum=bitnum_1[Count]+Index) {>=maxnum) {Index=1; Result=patchzero (sum-maxnum); }Else{Index=0; Result=patchzero (sum); }//If the highest bit also requires rounding if(Index==1) result="1"+result; Cout<<result<<endl;}intMainintargcChar**ARGV) {BIGNUMCALCU Calc; Calc.getinput ();return 0;}
Here is a Python test. Because Python already has its own large integer count.
__author__=‘MarkLiang‘#coding=utf-8fopen=open(‘num.txt‘,‘r‘)forinfopen: num2=fopen.readline(); print("result: ",int(num1)+int(num2))fopen.close()
As for multiplication and division, I'm free to write.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
In terms of a simple large number,