C++高精度运算类bign (重载操作符)

大数据操作,有例如以下问题:

计算:456789135612326542132123+14875231656511323132

  456789135612326542132123*14875231656511323132

比較:7531479535511335666686565>753147953551451213356666865 ?

long long类型存储不了,存储不了就实现不成计算,怎么办???


为了解决以上问题,所以得定义一种结构类型以存储这些数据,并重载运算符支持这些数据的操作。为了方便代码的复用因此有了例如以下代码:

#include<cstdio>
#include<cstring> 
#include<iostream>
using namespace std;

const int maxn = 200;
struct bign{
  int len, s[maxn];
  
  /*下面的构造函数是C++中特有的,作用是进行初始化。
   其实,当定义bign x时,就会运行这个函数,把x.s清零。并赋x.len=1 。
   须要说明的是,在C++中,并不须要typedef就能够直接用结构体名来定义,并且
   还提供“自己主动初始化”的功能,从这个意义上说。C++比C语言方便	
  */ 
  bign() {
    memset(s, 0, sizeof(s));
    len = 1;
  }

  bign(int num) {
    *this = num;
  }

  //定义为const參数,作用是 不能对const參数的值做改动 
  bign(const char* num) {
    *this = num;
  }
  /*以上是构造方法。初始化时对运行对应的方法*/

  bign operator = (int num) {
    char s[maxn];
    sprintf(s, "%d", num);
    *this = s;
    return *this;
  } 

  //函数定义后的constkeyword,它表明“x.str()不会改变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--;
  }
  
  
  /* 下面是重载操作符 */ 
  bign operator = (const char* num) {
  	//逆序存储,方便计算 
    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;
}


posted @ 2016-02-22 09:24  blfshiye  阅读(762)  评论(0编辑  收藏  举报