Big Data Template-from jxy Senior Engineer

Source: Internet
Author: User
# Include <iostream> # include <cstring> # include <cstdio> # define Max 1300 # define base 10000 # define baselen 4 using namespace STD; // int countnub = 0; // This big integer class uses the array digital [Max] to represent a big integer; // a digital value indicates the maximum value of 9999; // Len indicates that the current integer uses the maximum number of digital digits, sign indicates the symbol; Class int {public: // constructor; int (); // comparison function. If the second parameter is 0, it indicates absolute value comparison; int CMP (INT, INT); // determines whether it is 0; bool zero (); // determines parity; bool odd (); // shifts one binary int move () to the right; // assigns a value; int operator = (I NT); int operator = (INT); int operator = (char *); // binary computation; int operator + (INT); int operator-(INT ); int operator * (INT); int operator/(INT); int operator % (INT); // input/output; friend ostream & operator <(ostream &, INT ); friend istream & operator> (istream &, Int &); Private: int digital [Max]; int sign; int Len; // decimal shift int shift (int K);}; INT: int () {digital [Len = 0] = 0, Sign = 1;} int INT :: CMP (int OBJ, int sel = 1) // if the initial value of SEL is 0, it is an absolute value comparison {If (SEL & obj. sign + Sign = 0) return sign-obj. sign; // compare positive and negative numbers; int K = len-obj. len; // compare length; If (k) return Sel? Sign * K: K; For (k = Len; k> 0 & obj. digital [k] = digital [k]; k --); // compare digits; return Sel? Sign * (digital [k]-obj. digital [k]): digital [k]-obj. digital [k];} bool INT: zero () {return digital [0] + Len = 0;} bool INT: Odd () {return digital [0] & 1;} int INT: Move () {If (digital [0] <= 1 & Len = 0) digital [0] = 0; else {int K = Len, T, carry = 0; If (digital [Len] = 1) Len --; while (k> = 0) {T = digital [k] & 1; digital [k] = digital [k]> 1; if (carry) digital [k] + = base/2; k --; carry = T ;}}if (this-> zero () Sign = 1; return * This ;} //////////////////////////////////////// //////////////////////////////////////// /// int INT:: Operator = (int obj) {for (LEN = 0, Sign = obj. sign; Len <= obj. len; Len ++) digital [Len] = obj. digital [Len]; Len --; return * This;} int INT: Operator = (int obj) {If (OBJ <0) Sign =-1, OBJ =-OBJ; else Sign = 1; digital [0] = OBJ % base; If (OBJ/= base) {digital [1] = OBJ % base, Len = 1; if (OBJ/= base) di Gital [2] = OBJ % base, Len = 2;} else Len = 0; return * This;} int INT: Operator = (char * s) {int I, j, L, K; If (s [0] = '-') L = 1, Sign =-1; else l = 0, Sign = 1; I = L; while (s [I]) I ++; I --; k = 0; while (I-baselen + 1> = L) {for (j = 1, digital [k] = 0; j <= baselen; j ++) digital [k] = digital [k] * 10 + s [I-baselen + J]-'0'; I = I-baselen, K ++ ;} digital [k] = 0; while (I> = L) digital [k] = digital [k] * 10 + s [L ++]-'0 '; if (k) Len = K-(digital [k] = 0); else Len = 0; return * This ;} //////////////////////////////////////// /// // INT:: Operator + (int obj) {int sum; If (obj. sign = sign) // Add the same number; {int carry; int I; for (I = carry = 0; I <= Len & I <= obj. len; I ++) carry = carry + digital [I] + obj. digital [I], Sum. digital [I] = carry % base, carry = carry/base; For (; I <= Len; I ++) carry = carry + digital [I], Sum. diGi Tal [I] = carry % base, carry = carry/base; For (; I <= obj. len; I ++) carry = carry + obj. digital [I], Sum. digital [I] = carry % base, carry = carry/base; sum. len = I -! (Sum. digital [I] = carry); sum. sign = sign; return sum;} else // returns the same-number subtraction from a different sign; {sum = OBJ; sum. sign =-sum. sign; return * This-sum;} int INT: Operator-(int obj) {int * sub1, * sub2, quotient; If (Sign = obj. sign) // minus the same number; {int I, carry; I = This-> CMP (OBJ, 0); // absolute value comparison; if (I = 0) return quotient; else if (I <0) sub1 = & OBJ, sub2 = This, quotient. sign =-sign; else sub1 = This, sub2 = & OBJ, quotient. sign = sign; for (I = carry = 0; I <= sub2-> Len; I ++) if (quotient. digital [I] = sub1-> digital [I]-carry-sub2-> digital [I]) <0) carry = 1, quotient. digital [I] + = base; // borrow space; else carry = 0; For (; I <= sub1-> Len; I ++) if (quotient. digital [I] = sub1-> digital [I]-carry) <0) carry = 1, quotient. digital [I] + = base; // borrow space; else carry = 0; I --; while (I & "ient. digital [I] = 0) I --; quotient. len = I; return quotient;} else // change the same number and add: {quotient = OBJ, quotient. sign =-obj. sign; return * This + quotient;} int INT: Operator * (int obj) {int carry, I, j, maxlen; int product; maxlen = obj. len + 2; memset (product. digital, 0, sizeof (INT) * maxlen); for (I = 0; I <= obj. len; I ++) {for (j = 0, carry = 0; j <= Len; j ++) {carry + = obj. digital [I] * digital [J] + product. digital [J + I]; product. digital [J + I] = carry % base; carry/= base;} while (carry) product. digital [I + J ++] = carry % base, carry/= base;} I = maxlen-1; while (I & product. digital [I] = 0) I --; product. len = I; If (product. zero () product. sign = 1; // determine the else product. sign = sign * obj. sign; return product;} int INT: Operator/(int obj) {int Div, K, flag; int x, y, z; X = * This; flag = obj. sign * sign; obj. sign = x. sign = 1; while (X. CMP (OBJ)> 0) {k = x. len-obj.len; If (X. digital [X. len]> obj. digital [obj. len]) DIV = x. digital [X. len]/(obj. digital [obj. len] + 1); else if (X. len> obj. len) k --, DIV = (X. digital [X. len] * base + X. digital [X. len-1])/(obj. digital [obj. len] + 1); else break; X = x-(OBJ * (Z = div )). shift (k); y = Y + Z. shift (k);} If (X. CMP (OBJ)> = 0) y = Y + (Z = 1); If (Y. zero () Y. sign = 1; else y. sign = flag; return y;} int INT: Operator % (int obj) {int Div, K; int x, y, z; X = * This; obj. sign = x. sign = 1; while (X. CMP (OBJ)> 0) {k = x. len-obj.len; If (X. digital [X. len]> obj. digital [obj. len]) DIV = x. digital [X. len]/(obj. digital [obj. len] + 1); else if (X. len> obj. len) k --, DIV = (X. digital [X. len] * base + X. digital [X. len-1])/(obj. digital [obj. len] + 1); else break; X = x-(OBJ * (Z = div )). shift (k);} If (X. CMP (OBJ)> = 0) x = x-OBJ; If (X. zero () X. sign = 1; else X. sign = sign; return X;} int INT: shift (int K) {int temp; int I; temp = * this; for (I = 0; I <= Len; I ++) temp. digital [I + k] = digital [I]; for (I = 0; I <K; I ++) temp. digital [I] = 0; temp. sign = sign; temp. len = Len + k; return temp ;} //////////////////////////////////////// /// // ostream & operator <(ostream & out, int OBJ) {int I = obj. len; If (obj. sign =-1) out <'-'; out <obj. digital [I --]; out. fill ('0'); out. SETF (IOs: right); While (I> = 0) {out. width (baselen); out <obj. digital [I --];} return out;} istream & operator> (istream & in, Int & OBJ) {char s [baselen * max]; In> S; OBJ = s; Return in;} int main () {int A, B; while (CIN> A> B) {cout <a + B <Endl; cout <a-B <Endl; cout <a * B <Endl; cout <a/B <Endl;} return 0 ;}
 
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.