Big integer BIGN Design and Implementation of C ++ high-precision templates, bign high-precision

Source: Internet
Author: User

Big integer BIGN Design and Implementation of C ++ high-precision templates, bign high-precision

First of all, I would like to thank Liu rujia for his book "algorithm competition getting started classic".


As we all know, the unsigned long with the largest storage capacity in C ++ also has an upper limit. If we want to calculate a very large integer, we will be overwhelmed, I wrote a high-precision class that allows four arithmetic operations of big integers.


This class uses strings for input and output, and uses Arrays for storage and processing. By simulating four arithmetic operations, you can calculate the addition, subtraction, multiplication, division ratio of a large integer.


Supports negative numbers, leading zeros, string and integer values, and stream input and output.


Paste my code:

# Include <string> # include <iostream> # include <iosfwd> # include <cmath> # include <cstring> # include <stdlib. h> # include <stdio. h ># include <cstring> # define MAX_L 2005 // maximum length. You can modify using namespace std; class bign {public: int len, s [MAX_L]; // number length, record array // constructor bign (); bign (const char *); bign (int); bool sign; // Sign 1 positive number 0 negative string toStr () const; // convert to string, mainly to facilitate the output of friend istream & operator> (istream &, bign &); // reload the input stream friend ost Ream & operator <(ostream &, bign &); // reload output stream // reload copy bign operator = (const char *); bign operator = (int ); bign operator = (const string); // reload compare bool operator> (const bign &) const; bool operator >=( const bign &) const; bool operator <(const bign &) const; bool operator <= (const bign &) const; bool operator! = (Const bign &) const; // returns the bign operator + (const bign &) const; bign operator ++ (); bign operator ++ (int ); bign operator + = (const bign &); bign operator-(const bign &) const; bign operator -- (); bign operator -- (int ); bign operator-= (const bign &); bign operator * (const bign &) const; bign operator * (const int num) const; bign operator * = (const bign &); bign operator/(const bign &) const; bign operator/= (const Bign &); // derivative operation of the four arithmetic operations bign operator % (const bign &) const; // modulo (remainder) bign factorial () const; // factorial bign Sqrt () const; // integer open root (rounded down) bign pow (const bign &) const; // power/some messy functions void clean ();~ Bign () ;};# define max (a, B) a> B? A: B # define min (a, B) a <B? A: bbign: bign () {memset (s, 0, sizeof (s); len = 1; sign = 1 ;}bign: bign (const char * num) {* this = num;} bign: bign (int num) {* this = num;} string bign: toStr () const {string res; res = ""; for (int I = 0; I <len; I ++) res = (char) (s [I] + '0') + res; if (res = "") res = "0"; if (! Sign & res! = "0") res = "-" + res; return res;} istream & operator> (istream & in, bign & num) {string str; in> str; num = str; return in;} ostream & operator <(ostream & out, bign & num) {out <num. toStr (); return out;} bign: operator = (const char * num) {memset (s, 0, sizeof (s )); char a [MAX_L] = ""; if (num [0]! = '-') Strcpy (a, num); else for (int I = 1; I <strlen (num); I ++) a [I-1] = num [I]; sign =! (Num [0] = '-'); len = strlen (a); for (int I = 0; I <strlen (a); I ++) s [I] = a [len-I-1]-48; return * this;} bign: operator = (int num) {if (num <0) sign = 0, num =-num; else sign = 1; char temp [MAX_L]; sprintf (temp, "% d", num); * this = temp; return * this;} bign: operator = (const string num) {const char * tmp; tmp = num. c_str (); * this = tmp; return * this;} bool bign: operator <(const bign & Num) const {if (sign ^ num. sign) return num. sign; if (len! = Num. len) return len <num. len; for (int I = len-1; I> = 0; I --) if (s [I]! = Num. s [I]) return sign? (S [I] <num. s [I]): (! (S [I] <num. s [I]); return! Sign;} bool bign: operator> (const bign & num) const {return num <* this;} bool bign: operator <= (const bign & num) const {return! (* This> num);} bool bign: operator >=( const bign & num) const {return! (* This <num);} bool bign: operator! = (Const bign & num) const {return * this> num | * this <num;} bool bign: operator = (const bign & num) const {return! (Num! = * This);} bign: operator + (const bign & num) const {if (sign ^ num. sign) {bign tmp = sign? Num: * this; tmp. sign = 1; return sign? * This-tmp: num-tmp;} bign result; result. len = 0; int temp = 0; for (int I = 0; temp | I <(max (len, num. len); I ++) {int t = s [I] + num. s [I] + temp; result. s [result. len ++] = t % 10; temp = t/10;} result. sign = sign; return result;} bign: operator ++ () {* this = * this + 1; return * this;} bign :: operator ++ (int) {bign old = * this; ++ (* this); return old;} bign: operator ++ = (const bi Gn & num) {* this = * this + num; return * this;} bign: operator-(const bign & num) const {bign B = num, a = * this; if (! Num. sign &&! Sign) {B. sign = 1; a. sign = 1; return B-a;} if (! B. sign) {B. sign = 1; return a + B;} if (! A. sign) {. sign = 1; B = bign (0)-(a + B); return B;} if (a <B) {bign c = (B-a); c. sign = false; return c;} bign result; result. len = 0; for (int I = 0, g = 0; I <. len; I ++) {int x =. s [I]-g; if (I <B. len) x-= B. s [I]; if (x> = 0) g = 0; else {g = 1; x + = 10;} result. s [result. len ++] = x;} result. clean (); return result;} bign: operator * (const bign & num) const {bign result; result. len = len + Num. len; for (int I = 0; I <len; I ++) for (int j = 0; j <num. len; j ++) result. s [I + j] + = s [I] * num. s [j]; for (int I = 0; I <result. len; I ++) {result. s [I + 1] + = result. s [I]/10; result. s [I] % = 10;} result. clean (); result. sign =! (Sign ^ num. sign); return result;} bign: operator * (const int num) const {bign x = num; bign z = * this; return x * z;} bign:: operator * = (const bign & num) {* this = * this * num; return * this;} bign: operator/(const bign & num) const {bign ans; ans. len = len-num. len + 1; if (ans. len <0) {ans. len = 1; return ans;} bign divisor = * this, divid = num; divisor. sign = divid. sign = 1; int k = ans. len -1; int j = len-1; while (k> = 0) {while (divisor. s [j] = 0) j --; if (k> j) k = j; char z [MAX_L]; memset (z, 0, sizeof (z )); for (int I = j; I> = k; I --) z [j-I] = divisor. s [I] + '0'; bign dividend = z; if (dividend <divid) {k --; continue;} int key = 0; while (divid * key <= dividend) key ++; key --; ans. s [k] = key; bign temp = divid * key; for (int I = 0; I <k; I ++) temp = temp * 10; divisor = Divisor-temp; k --;} ans. clean (); ans. sign =! (Sign ^ num. sign); return ans;} bign: operator/= (const bign & num) {* this = * this/num; return * this;} bign :: operator % (const bign & num) const {bign a = * this, B = num;. sign = B. sign = 1; bign result, temp = a/B * B; result = a-temp; result. sign = sign; return result;} bign: pow (const bign & num) const {bign result = 1; for (bign I = 0; I <num; I ++) result = result * (* this); return result;} Bign: factorial () const {bign result = 1; for (bign I = 1; I <= * this; I ++) result * = I; return result;} void bign: clean () {if (len = 0) len ++; while (len> 1 & s [len-1] = '\ 0') len --;} bign: Sqrt () const {if (* this <0) return-1; if (* this <= 1) return * this; bign l = 0, r = * this, mid; while (r-l> 1) {mid = (l + r)/2; if (mid * mid> * this) r = mid; else l = mid;} return l;} bign ::~ Bign () {} bign num0, num1, res; int main () {cin> num0> num1; res = num0 + num1; cout <res <endl; num0 = 5; num1 = "71"; res = num0-num1; cout <res <endl; res = num0.Sqrt (); cout <res <endl; res = num0.pow (5); cout <res <endl; return 0 ;}


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.