Bign Class C ++ high-precision Template

Source: Internet
Author: User

A very good high-precision template, but this template can only calculate addition, subtraction, multiplication, division and other basic operations, but the operation subtraction can only reduce the number of large numbers, therefore, the most important operations are the basic operations: +, + =, *, * =,/,/=,-=, =,> =, recently, I found some bugs in my previous templates through a few questions I have posted on the page. Now I am posting the correct templates. You are welcome to point out my bugs. [Cpp] # include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <string> # include <algorithm> using namespace std; const int MAXN = 410; struct bign {int len, s [MAXN]; bign () {memset (s, 0, sizeof (s); len = 1 ;} bign (int num) {* this = num;} bign (const char * num) {* this = num;} bign operator = (const int num) {char s [MAXN]; sprintf (s, "% d", num); * this = s; retu Rn * this;} bign operator = (const char * num) {for (int I = 0; num [I] = '0'; num ++ ); // lead 0 len = strlen (num); for (int I = 0; I <len; I ++) s [I] = num [len-i-1]-'0'; return * this;} bign operator + (const bign & B) const // + {bign c; c. len = 0; for (int I = 0, g = 0; g | I <max (len, B. len); I ++) {int x = g; if (I <len) x + = s [I]; if (I <B. len) x + = B. s [I]; c. s [c. len ++] = x % 10; g = x/10 ;} Return c;} bign operator + = (const bign & B) {* this = * this + B; return * this;} void clean () {while (len> 1 &&! S [len-1]) len --;} bign operator * (const bign & B) // * {bign c; c. len = len + B. len; for (int I = 0; I <len; I ++) {for (int j = 0; j <B. len; j ++) {c. s [I + j] + = s [I] * B. s [j] ;}}for (int I = 0; I <c. len; I ++) {c. s [I + 1] + = c. s [I]/10; c. s [I] % = 10;} c. clean (); return c;} bign operator * = (const bign & B) {* this = * this * B; return * this ;} bign operator-(const bign & B) {bign c; c. len = 0; fo R (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;} c. s [c. len ++] = x;} c. clean (); return c;} bign operator-= (const bign & B) {* this = * this-B; return * this ;} bign operator/(const bign & B) {bign c, f = 0; for (int I = len-1; I> = 0; I --) {f = f * 10; f. s [0] = s [I]; while (f> = B) {f-= B; c. s [I] ++;} c. len = len; c. cl Ean (); return c;} bign operator/= (const bign & B) {* this = * this/B; return * this ;} bign operator % (const bign & B) {bign r = * this/B; r = * this-r * B; return r ;} bign operator % = (const bign & B) {* this = * this % B; return * this;} bool operator <(const bign & B) {if (len! = B. len) return len <B. len; for (int I = len-1; I> = 0; I --) {if (s [I]! = B. s [I]) return s [I] <B. s [I];} return false;} bool operator> (const bign & B) {if (len! = B. len) return len> B. len; for (int I = len-1; I> = 0; I --) {if (s [I]! = B. s [I]) return s [I]> B. s [I];} return false;} bool operator = (const bign & B) {return! (* This> B )&&! (* This <B);} bool operator! = (Const bign & B) {return! (* This = B);} bool operator <= (const bign & B) {return * this <B | * this = B ;} bool operator >=( const bign & B) {return * this> B | * this = B;} string str () const {string res = ""; for (int I = 0; I <len; I ++) res = char (s [I] + '0') + res; return res ;}}; istream & operator> (istream & in, bign & x) {string s; in> s; x = s. c_str (); return in;} ostream & operator <(ostream & out, const Bign & x) {out <x. str (); return out;} int main () {bign a, B, c, d, e, f, g; while (cin> a> B) {. clean (), B. clean (); c = a + B; d = a-B; e = a * B; f = a/B; g = a % B; cout <"a + B" <"=" <c <endl; // a + = B cout <"a-B" <"=" <d <endl; // a-= B; cout <"a * B" <"=" <e <endl; // a * = B; cout <"a/B" <"=" <f <endl; // a/= B; cout <"a % B" <"=" <g <endl; // a % = B; if (! = B) printf ("YES \ n"); else printf ("NO \ n");} 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.