"Reprint" C + + high-precision operation class Bign (overloaded operator)

Source: Internet
Author: User

#include <cstdio> #include <cstring> #include <iostream> using namespace std;  const int MAXN = 200;        struct bign{int len, S[MAXN];    /* The following constructors are unique in C + + and are initialized.    In fact, when defining bign X, this function is executed, the X.S is zeroed, and X.len=1 is assigned. It should be noted that in C + +, there is no need for typedef to be directly defined by the struct name, but also provide "automatic initialization" function, in this sense, C + + is more convenient than c * * Bign () {memset (s, 0, sizeof (s)      ));    len = 1;    } bign (int num) {*this = num;    }//is defined as a const parameter, which does not modify the value of the const parameter bign (const char* num) {*this = num;      }/* Above is the constructor method, the corresponding method is executed when initializing */bign operator = (int num) {char S[MAXN];      sprintf (S, "%d", num);      *this = s;    return *this;      }//function defines the CONST keyword after it indicates that "x.str () does not change X" string str () const {String res = "";      for (int i = 0; i < len; i++) res = (char) (s[i] + ' 0 ') + res;      if (res = = "") res = "0";    return res;    } void Clean () {while (len > 1 &&!s[len-1]) len--; }/* The following are overloaded operators */bign operator =(const char* num)      {//reverse storage, easy to calculate 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) {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-1; i++) {c.s[i+1] + c.s[i]/10;      C.s[i]%= 10;      } C.clean ();    return C;      } bign Operator-(const bign& b) {bign c; c.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;      } c.s[c.len++] = x;      } C.clean ();    return C;      } BOOL operator < (const bign& B) const{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) const{return B < *this; } BOOL operator <= (const bign& b) {return!    b > *this); } bool operator = = (Const bign& b) {return! ( b < *this) &&!    (*this < b);      } bign operator + = (Const bign& b) {*this = *this + b;    return *this;    }  };    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;    Cin >> A; A + = "123456789123456789000000000 ";    cout << a*2 << Endl;  return 0;   }

  

"Reprint" C + + high-precision operation class Bign (overloaded operator)

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.