Operation Subtraction of Big data

Source: Internet
Author: User

BIGDATA.H:

#ifndef  __BIG_DATA_H__#define __BIG_DATA_H__#include <assert.h> #include  <string >typedef long long int64;//#define  MININT64 -9223372036854775808 //  compiler checks for errors////#define  maxint64 +9223372036854775807 const int64 minint64 = + 9223372036854775807 + 1;const int64 maxint64 = +9223372036854775807;class  bigdata{friend std::ostream& operator<< (std::ostream& _cout, const  Bigdata& bigdata);p ublic:bigdata (INT64&NBSP;VALUE&NBSP;=&NBSP;0XCCCCCCCCCCCCCCCC); Bigdata (CONST&NBSP;CHAR*&NBSP;STR); bigdata operator+ (Const bigdata& bigdata); bigdata operator-(Const bigdata& bigdata); bigdata operator* (Const bigdata& bigdata); bigdata operator/(Const bigdata& bigdata);p rotected:void _int64tostr (); bool _ Isint64overflow () Const;std::string addstr (cOnst bigdata& bigdata) CONST;STD::STRING&NBSP;SUBSTR (const bigdata& bigdata) const; Std::string mulstr (Const bigdata& bigdata) const;std::string divstr (const BigData & bigdata) Const;bool isleftstrbig (const char* pleft, int ilsize, const  char* pright, int irsize) const;char loopsub (Char* pleft, int ilsize,  const char* pright, int irsize) const;private:int64 _value;std::string _ PData;};  std::ostream& operator<< (std::ostream& _cout, const bigdata&  Bigdata); #endif  /*__big_data_h__*/

BigData.cpp:

#define  _CRT_SECURE_NO_WARNINGS 1#include  "BigData.h" Bigdata::bigdata (int64 value)  :_ Value (value) {this->_int64tostr ();} Bigdata::bigdata (CONST&NBSP;CHAR*&NBSP;STR)  : _value (0) {assert (str);char* temp =  ( char*) str;char csign =  ' + ';while  (isspace (*temp))  //*temp ==  '   ' Cannot eliminate tab{++temp;} if  ((*temp ==  '-')  | |   (*temp ==  ' + ')) {csign = *temp;++temp;} This->_pdata.resize (strlen (str)  + 1); this->_pdata[0] = csign;int icount =  1;while  (*temp ==  ' 0 ')//processing 000123{++temp;} while  ((*temp >=  ' 0 ')  &&  (*temp <=  ' 9 ')) {This->_value  = this->_value * 10 +  (*temp -  ' 0 '); this->_pdata[icount++]  = *temp++;} This->_pdata.resize (ICount);if  (csign ==  '-') {this->_value = 0 -  This->_vaLue;} if  (!this->_isint64overflow ())//overflow {THIS-&GT;_VALUE&NBSP;=&NBSP;0XCCCCCCCCCCCCCCCC;}} /*bigdata (CONST&NBSP;CHAR*&NBSP;STR) */bigdata bigdata::operator+ (const bigdata& bigdata) {if   (This->_isint64overflow ()  && bigdata._isint64overflow ())  //two no overflow {if  ( THIS-&GT;_PDATA[0]&NBSP;!=&NBSP;BIGDATA._PDATA[0])//XOR, add certainly no overflow {return bigdata (this->_value +  bigdata._value);} else //of two {if  (This->_value>0 && maxint64 - this->_value  >= bigdata._value)  | |   //results are still not overflowing (this->_value<0 && minint64 - this->_value < = bigdata._value)) {return bigdata (This->_value + bigdata._value);}}} if  (This->_pdata[0] == bigdata._pdata[0])  //Two of the same number {Return bigdata (THIS-&GT;ADDSTR ( Bigdata). C_STR ());} BIGDATA&NBSP;TEMP1 ((this->_pdata). C_STR ()),  //overflow, XOR, call minus BIGDATA&NBSP;TEMP2 ((Bigdata._pdata). C_STR ());if  (this->_pdata[0] ==  ' + ') {temp2._pdata[0] =  ' + '; temp2. _value = 0 - temp2._value;return  (Temp1. SUBSTR (TEMP2)). C_STR ();} else{temp1._pdata[0] =  ' + ';temp1._value = 0 - temp1._value;return  (temp2. SUBSTR (TEMP1)). C_STR ();}} Std::string bigdata::addstr (Const bigdata& bigdata) const{std::string left =  this->_pdata;std::string right = bigdata._pdata;char csign = this->_pdata[ 0];size_t ilsize = left.size (); Size_t irsize = right.size ();if  (iLSize  < irsize) {Std::swap (left, right); Std::swap (ilsize, irsize);} Std::string ret;ret.resize (ilsize + 1); ret[0] = csign;int step = 0;// Carry for  (size_t icount = 1; icount < ilsize; ++icount) {Char ch  = left[ilsize - icount] -  ' 0 '  + step;if  (icount < irsize) {ch +=  (right[irsize  - icount] -  ' 0 ');} ret[ilsize - icount + 1] = ch % 10 +  ' 0 '; step = ch  / 10;} ret[1] = step +  ' 0 '; return ret;} bigdata bigdata::operator-(Const bigdata& bigdata) {if  (This->_IsINT64OverFlow ()  && bigdata._isint64overflow ())//No overflow {if  (this->_pdata[0] == bigdata._pdata [0])  //the same number, after subtracting certainly no overflow {return this->_value - bigdata._value;} else{if  ((this->_value > 0 && maxint64 + bigdata._value  >= this->_value) | | (this->_value < 0 && minint64 + bigdata._value <=  This->_value)//Result no overflow {return this->_value - bigdata._value;}}} if  (This->_pdata[0] == bigdata. _pdata[0]) {Return bigdata (This->substr (bigdata). C_STR ());} Bigdata temp ((bigdata._pdata). C_STR ())  //, overflow  -3-5=-3+ ( -5)  3-( -5) =3+5temp._pdata[0] &NBSP;=&NBSP;THIS-&GT;_PDATA[0];TEMP._VALUE&NBSP;=&NBSP;0&NBSP;-&NBSP;TEMP._VALUE;RETURN&NBSP;THIS-&GT;ADDSTR ( temp). C_str ();} /*operator-*/std::string bigdata::substr (Const bigdata& bigdata) const{std::string ret; std::string left = this->_pdata;std::string right = bigdata._pdata;char  Csign = this->_pdata[0];size_t ilsize = left.size (); size_t iRSize =  right.size ();if  (csign ==  '-') {Std::swap (left, right); Std::swap (iLSize, iRSize) ;} if  ((ilsize < irsize)  | |   (Ilsize == irsize && left < right)) {Std::swap (left, right) ; Std::swap (ilsize, irsize);csign =  '-';} else{csign =  ' + ';} Ret.resize (ilsize); Ret[0] = cSign;char step = 0;for  (size_t icount = 1; icount  < ilsize; ++icount) {int ch = left[ilsize - icount] -  ' 0 '   - step;if  (icount < irsize) {ch -=  (right[irsize - icount] -   ' 0 ');} if  (ch < 0) {step = 1;ch = ch + 10;} else{step = 0;} ret[ilsize - icount] = ch +  ' 0 ';} Return ret;} /*substr (Const bigdata& bigdata) */bigdata bigdata::operator* (const BigData&  Bigdata) {if  (this->_pdata[1] ==  ' 0 '  | |  bigData._pData[1] ==  ' 0 ') {return int64 (0);} char csign =  ' + ';if  (this->_pdata[0] != bigdata._pdata[0]) {cSign =  ‘-‘;} if  (this->_pdata[1] ==  ' 1 ') {std::string ret = bigdata._pdata;ret[0] =  csign;return ret.c_str ();} if  (bigdata._pdata[1] ==  ' 1 ') {std::string ret = this->_pdata;ret[0] =  csign;return ret.c_str ();} if  (This->_isint64overflow ()  && bigdata._isint64overflow ())  //no Overflow {if  (( csign ==  ' + ')  &&  //the same number and the result does not overflow ((this->_value > 0 & & maxint64 / this->_value >= bigdata._value)  | | (this->_value < 0 && maxint64 / this->_value <=  Bigdata._value))) {Return this->_value * bigdata._value;} The if  ((csign ==  '-')  &&  //and the result did not overflow ((this->_value > 0  && minint64 / this->_value <= bigdata._value)  | | (this->_value < 0 && minint64 / this->_value >=  Bigdata._value))) {Return this->_valuE * bigdata._value;}} Return this->mulstr (Bigdata). C_STR ();} Std::string bigdata::mulstr (const bigdata& bigdata) const{char csign =  ' + ';   (this->_pdata[0] != bigdata._pdata[0]) {csign =  '-';} std::string left = this->_pdata;std::string right = bigdata._pdata;size_t  ilsize = left.size (); Size_t irsize = right.size ();std::string ret; Ret.resize (ilsize + irsize - 1);ret[0] = csign;if  (iLSize <  Irsize) {Std::swap (left, right); Std::swap (ilsize, irsize);} int step = 0;for  (int ircount = 0; ircount < irsize  - 1; ++ircount) {step = 0;int chr = right[irsize - 1 -  ircount] -  ' 0 ';if  (0&NBSP;==&NBSP;CHR) {continue;} for  (int ilcount = 1; ilcount <  (int) ilsize; ++ilcount) {int ch = chr *  (Left[ilsize - ilcount]  -  ' 0 ') + step + ret[ilsize + irsize - 1 - ilcount  - ircount];ret[ilsize + irsize - 1 - ilcount - ircount] =  ch % 10;step = ch / 10;} Ret[irsize - ircount - 1] += step;} for  (int icount = 1; icount < ilsize + irsize - 1;  ++icount) {ret[icount] +=  ' 0 ';} Return ret.c_str ();} bigdata bigdata::operator/(Const bigdata& bigdata) {//right == 0if  (bigData._ pdata[1] ==  ' 0 ') assert (false);//left == 0if  (this->_pdata[1] ==  ' 0 ') { Return int64 (0);} No overflow, the result is definitely no overflow if  (This->_isint64overflow ()  && bigdata._isint64overflow ()) {return  this->_value / bigdata._value;} char csign =  ' + ';if  (this->_pdata[0] != bigdata._pdata[0]) {cSign =  ‘-‘;} Size_t ilsize = this->_pdata.size (); size_t irsize = bigdata._pdata.size ();// left < rightif  ((ilsize < irsize)  | |   (ILSIZE&NBSP;==&NBSP;IRSIZE&NBSP;&AMP;&AMP;&NBSP;STRCMP (This->_pdata.c_str ()  + 1,  Bigdata._pdata.c_str ()  + 1) ( < 0)) {return int64 (0);} right == +1 or -1if  (Irsize == 2 && bigdata._pdata[1]  ==  ' 1 ') {std::string ret = this->_pdata;ret[0] = csign;return ret.c _str ();} left == rightif  (strcmp (This->_pdata.c_str ()  + 1, bigdata._pdata.c_str ()  + 1)  == 0) {std::string ret =  "+1";ret[0] = csign;return  Ret.c_str ();} There is overflow, the general situation RETURN&NBSP;THIS-&GT;DIVSTR (Bigdata). C_str ();} Std::string bigdata::D ivstr (const bigdata& bigdata) const {char csign =  ' + ';if  (this->_pdata[0] != bigdata._pdata[0]) {csign =  '-';} Size_t ilsize = this->_pdata.size (); Size_t irsize = bigdata._pdata.size (); STD :: String ret;ret.append (1, csign); Std::string leftstr = this->_pdata.c_str ();  //otherwise will change Thisstd::string rightstr = bigdata._pdata.c_str ();char* left =  ( char*) (Leftstr.c_str ()  + 1);char* right =  (char*) (Rightstr.c_str ()  + 1); int  iCount = iRSize - 1;while  (True) {Int slenleft = strlen (left); int  slenright = strlen (right);if  ((slenleft < slenright)  | |   (SLENLEFT&NBSP;==&NBSP;SLENRIGHT&AMP;&AMP;STRCMP (left, right)  < 0)) {break;} if  (! Isleftstrbig (LEFT,&NBSP;ICOUNT,&NBSP;RIGHT,&NBsp;irsize - 1)  //left < right{ret.append (1,  ' 0 '); ++iCount;continue;} Char ch = loopsub (left, icount, right, irsize - 1);while  (*left  ==  ' 0 ') {++left;--icount;} ++icount; //get the next ret.append (1, ch);} Return ret.c_str ();} Char bigdata::loopsub (Char* pleft, int ilsize, const char* pright, int  irsize) const{char ret =  ' 0 ';while  (isleftstrbig (Pleft, ilsize, pright,  irsize) {ret++;for  (int i = ilsize - 1;  (i >= 0)   &&  (irsize + i - ilsize >= 0);  --i) {pleft[i] -=  ( pright[irsize + i - ilsize] -  ' 0 ');if  (pleft[i] <  ' 0 ') {pLeft[i ]&NBSP;+=&NBSP;10;--PLEFT[I&NBSP;-&NBSP;1];}} for//to 0 eg:0334while  (*pleft ==  ' 0 ') {++pleft;--ilsize;}} Whilereturn&nbSp;ret;} Bool bigdata::isleftstrbig (const char* pleft, int ilsize, const char*  Pright, int irsize) const{if  ((ilsize > irsize)  | | (ILSIZE&NBSP;==&NBSP;IRSIZE&NBSP;&AMP;&AMP;&NBSP;STRCMP (Pleft, pright)  >= 0)) {return true;} Return false;} Void bigdata::_int64tostr () {char csign =  ' + ';if  (this->_value < 0) { csign =  '-';} This->_pdata.append (1, csign);if  (this->_value == 0) {this->_pdata.append (1,  ' 0 '); return;} int64 temp = this->_value;while  (temp) {this->_pdata.append (1,  (temp %  10 +  ' 0 '); temp /= 10;} char* left =  (char*) (This->_pdata.c_str ()  + 1); char* right = left  + this->_pdata.size ()  - 2;while  (left < right) {char ch =  *left;*left++ = *right;*rigHt-- = ch;}} /*void bigdata::_int64tostr () */bool bigdata::_isint64overflow () const{std::string max =   "+9223372036854775807";std::string min =  " -9223372036854775808"; size_t size =  this->_pdata.size ();if  (Size < max.size ()) {return true;} if  (this->_pdata[0] ==  ' + ')  &&  (Size == max.size ())  & &  (This->_pdata <= max)) {return true;//returns true without overflow}if  (this->_pdata[0]  ==  '-')  &&  (Size == min.size ())  &&  (this->_pdata  <= min)) {return true;} Return false;} /*bool bigdata::_isint64overflow () const*/std::ostream& operator<< (std::ostream& _ Cout, const bigdata& bigdata) {if  (Bigdata._isint64overflow ())//No overflow {_cout < < bigdata._value << std::endl;} else{if  (Bigdata._pdata[0] ==  ' + ') {_cout << bigdata._pdata.c_str ()  + 1 <<  std::endl;} Else{_cout << bigdata._pdata << std::endl;}} Return _cout;}


Operation Subtraction of Big data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.