Project name Large number Calculator
*************************************************
The lower layer of the large number computation uses the string object storage, converts the integral type data to the character type to carry on the storage operation the addition and subtraction, uses the bitwise to carry on the subtraction, the design mark bit, the marking carries on and borrow the way; multiplication control the number of cycles on the basis of the addition and subtraction of data processing
#include <iostream> #include <cassert> #include <string>using namespace std; #define int_64 long long#define un_init 0xcccccccccccccccc//can't separate #define min_int 0xffffffffffffffff#define max_int 0x7fffffffffffffffclass bigdata{ Public:bigdata (): _data (0), _pdata ("") {}bigdata (Int_64 data): _data (data) {int_64 tmp = _data;char csymbol = ' + ';if (tmp<0) {tmp = 0 - tmp;csymbol = '-';} _pdata.append (1, csymbol);while (TMP) {_pdata.append (1, tmp % 10 + ' 0 '); TMP /= 10;} char* left = (char*) (_pdata.c_str () + 1);char* right = (char*) (_ Pdata.c_str () + _pdata.size () - 1);char temp;while (left < right) { Temp = *left;*left++ = *right;*right-- = temp;}} Bigdata (Const char* pdata) {assert (PData);int_64 ret = 0;char* source = (char*) Pdata;char csymbol = ' + ';if (*source == '-' | | *source == ' + ') csymbol = *source;//while (*source == ' 0 ')//source++;_ Pdata.append (1, csymbol);while (*source <= ' 9 ' &&*source >= ' 0 ') {if (Ret <= max_int) ret = ret * 10 + *source;_pdata.append (1, *source); source++;} if (*source = '-') Ret = 0 - ret;_data = ret;} Bigdata (const bigdata& bigdata) {_data = bigdata._data;_pdata = bigdata._ pdata;} bigdata operator+ (Bigdata& bigdata) {if (! Isint64overflow () && !bigdata.isint64overflow ()) {if (_pdata[0] != bigdata._ Pdata[0]) return bigdata (_data + bigdata._data);else{if (' + ' == _pdata[0] && max_int - _data <= bigdata._data | | '-' == _pdata[0] && _data >= min_int - bigdata._data ') Return bigdata (_data + bigdata._data);}} Return bigdata (ADD (_pdata, bigdata._pdata). C_STR ());} bigdata operator-(Bigdata& bigdata) {if (! Isint64overflow () && !bigdata.isint64overflow ()) {if (_pdata[0] == bigdata._ Pdata[0]) return bigdata (_data - bigdata._data);else{if (' + ' == _pdata[0] && _data <= max_int + bigdata._data | | '-' == _pdata[0] && _data >= min_int + bigdata._data ') Return bigdata (_data - bigdata._data);}} string ret;if (_pdata[0] == bigdata._pdata[0]) ret = sub (_pdata, bigData._ pdata); Else{ret = add (_pdata, bigdata._pdata); ret[0] = _pdata[0];} Return bigdata (Ret.c_str ());} bigdata operator* (Bigdata& bigdata) {if (! Isint64overflow () && !bigdata.isint64overflow ()) {if (_pdata[0] == bigdata._ Pdata[0]) {if (' + ' == _pdata[0] && _data <= MAX_INT / bigdata._data | | '-' == _pdata[0] && _data >= max_int / bigdata._data ') Return bigdata (_data *bigdata._data);} else{if (' + ' == _pdata[0] && _data <= MIN_INT / bigdata._data | | '-' == _pdata[0] && _data >= min_int / bigdata._data ') Return bigdata (_data * bigdata._data);}} Return bigdata (Mul (_pdata, bigdata._pdata). C_STR ());} bigdata operator/(Bigdata& bigdata) {if (_data == 0 | | bigdata._data == 0) Return bigdata (int_64 (0));if (! Isint64overflow () && !bigdata.isint64overflow ()) {Return bigdata (_data / Bigdata._data);} Return bigdata (Div (_pdata, bigdata._pdata). C_STR ());} friend ostream& operator<< (ostream& out, const bigdata& b) {out << b._pdata;return out;} Friend istream& operator>> (istream& in, bigdata& b) {in >> b._pdata;return in;} Protected:bool isint64overflow () {if (_data <= max_int&&_data >= min_int) Return false;return true;} String add (STRING&NBSP;S1,&NBSP;STRING&NBSP;S2) {int leftsize = s1.size ();int Rightsize = s2.size ();string ret;char csymbol = ' + ';if (s1[0] == S2[0]) {if (s1[0] == '-') csymbol = '-';} else{if (S1[0] == ' + ' &&strcmp (S1.c_str () + 1, s2.c_str () + 1) < 0 | | s1[0] == '-' &&strcmp (S1.c_str () + 1, s2.c_str () + 1) >0) csymbol = '-';} if (leftsize < rightsize) {swap (S1,&NBSP;S2); swap (leftsize, rightsize);} Ret.resize (leftsize + 1); ret[0] = csymbol;char cres, cstep = 0;for (INT&NBSP;IDX&NBSP;=&NBSP;1;&NBSP;IDX&NBSP;<&NBSP;LEFTSIZE;&NBSP;++IDX) {cres = s1[leftsize - idx] - ' 0 ' + cStep;if (idx<rightsize) cres += (s2[rightsize - idx] - ' 0 '); cstep = cres / 10;ret[leftsize - idx + 1] = cRes % 10 + ' 0 ';} ret[1] = cstep + ' 0 '; return ret;} String sub (STRING&NBSP;S1,&NBSP;STRING&NBSP;S2) {int leftsize = s1.size (); Int rightSIze = s2.size ();char csymbol = s1[0];//leftsize == rightsize & & (csymbol == ' + ' &&strcmp (S1.c_str () + 1, s2.c_str () + 1) < 0 | | cSymbol == '-' &&strcmp (S1.c_str () + 1, s2.c_str () + 1) > 0) if (leftsize < rightsize | | &NBSP;LEFTSIZE&NBSP;==&NBSP;RIGHTSIZE&NBSP;&&STRCMP (S1.c_str () + 1, s2.c_str () + 1) < 0) {swap (S1,&NBSP;S2); swap (leftsize, rightsize);if (' + ' == csymbol) csymbol = '-';elsecsymbol = ' + ';} String ret;ret.resize (leftsize);ret[0] = csymbol;char cret;for (int idx = &NBSP;1;&NBSP;IDX&NBSP;<&NBSP;LEFTSIZE;&NBSP;++IDX) {cret = s1[leftsize - idx] - ' 0 ';if (idx < rightsize) cret -= (s2[rightsize - idx] - ' 0 ');if (cret < 0) {s1[leftsize - idx - 1] -= 1;cret += 10;} ret[leftsize - idx] = cret + ' 0 ';} Return ret;} String mul (STRING&NBSP;S1,&NBSP;STRING&NBSP;S2) {int leftsize = s1.size ();int Rightsize = s2.size ();if (leftsize < rightsize) {swap (leftsize, rightsize); Swap (S1,&NBSP;S2);} char csymbol = ' + ';if (s1[0] != s2[0]) csymbol = '-';string ret; Ret.resize (leftsize + rightsize - 1); ret[0] = csymbol;int idatalen = ret.size ();int offset = 0;for (int idx = 1; idx < RIGHTSIZE;&NBSP;++IDX) {char cleft = s2[rightsize - idx] - ' 0 ';char cstep = 0;if (cleft == 0) {++offset;continue;} for (int ildx = 1; ildx < leftsize; ++ildx) {char cret = cleft* (s1[leftsize - ildx] - ' 0 '); cRet += (cstep + ret[idatalen - ildx - offset] - ' 0 ');ret[iDataLen - ildx - offset] = cret % 10 + ' 0 '; cStep = cRet / 10;} Ret[idatalen - offset - leftsize] += cstep;++offset;} Return ret;} String div (STRING&NBSP;S1,&NBSP;STRING&NBSP;S2) {int leftsize = s1.size ();int Rightsize = s2.size ();char csymbol = ' + ';if (S1[0] != s2[0]) {CSymbol = '-';} if (leftsize < rightsize | | (LEFTSIZE&NBSP;==&NBSP;RIGHTSIZE&&STRCMP (S1.c_str () + 1, s2.c_str () + 1 )) {return "0";} else{if ("1" == s2 | | "-1" == s2) {s1[0] = csymbol;return s1;}} char *left = (char*) (s1.c_sTR () + 1);char* right = (char*) (S2.c_str () + 1); int idatalen = rightSize - 1;string ret;for (int idx = 0; idx < LEFTSIZE&NBSP;-&NBSP;1;&NBSP;++IDX) {if (! Isleftstrright (left, idatalen, right, rightsize - 1)) {iDataLen++;ret.append (1, ') 0 '); continue;} Else{ret.append (1, subloop (left, idatalen, right, rightsize - 1));iDataLen = rightsize - 1;}}} Char subloop (char*& left, int lsize, char* right, int rsize) { ASSERT (left&&right);char cret = ' 0 ';while (1) {if (! Isleftstrright (left, lsize, right, rsize)) break;int ldatalen = lsize;int rdatalen = rsize;while (ldatalen >= 0 && rdatalen >= 0) {if (Left[ldatalen] < right[rdatalen]) {left[ldatalen - 1] -= 1;left[ldatalen] += 10;} left[ldatalen] -= right[rdatalen];ldatalen--;rdatalen--;} while (' 0 ' == *left) {left++;lsize--;} cret++;}} Bool isleftstrright (char* left, int lsize, char* right, const Int rsize) {assert (Left&&right);if (lsize>rsize | | &NBSP;LSIZE&NBSP;==&NBSP;RSIZE&&STRNCMP (left, right, lsize) >= 0) return True;return false;} private:int_64 _data;string _pdata;}; Void test1 () {BIGDATA&NBSP;B1 (1234); BIGDATA&NBSP;B2 (123456789); Bigdata b3= b1 + b2;cout << b3 << endl;}
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1767620
Simulate the basic calculation of big data to solve the limitation of the time-limit of calculating data by conventional calculator