• 高精度运算类bign


     
    Bign
      1  #include<iostream>
      2  #include<string>
      3  #include<cstring>
      4  #include<cstdio>
      5  using namespace std;
      6  const int maxn = 1000 + 5;
      7  
      8  class bign
      9  {
     10  public:
     11      int len, s[maxn];
     12      bign()
     13      {
     14          len = 0;
     15          memset(s, 0, sizeof(s));
     16      }
     17      bign operator = (const char *num)
     18      {
     19          //支持对字符数组的赋值
     20          len = strlen(num);
     21          for(int i = 0; i < len; ++i)
     22              s[i] = num[len-i-1] - '0';
     23          return *this;
     24      }
     25      bign(const char *num)
     26      {
     27          //初始化
     28          *this = num;
     29      }
     30      bign operator = (int num)
     31      {
     32          //支持对整型数的赋值
     33          char s[maxn];
     34          sprintf(s, "%d", num);
     35          *this = s;
     36          return *this;
     37      }
     38      bign(int num)
     39      {
     40          //初始化
     41          *this = num;
     42      }
     43      string str() const
     44      {
     45          //x.str()不会改变x
     46          string str = "";
     47          for(int i = 0; i < len; ++i)
     48              str += (char)(s[i] + '0');//str = str + s[i] +'0'也可以
     49          if(str == "")
     50              str = "0";
     51          return str;
     52      }
     53      friend istream& operator>>(istream &in, bign& b);/*
     54      {
     55          string str;
     56          in >> str;
     57          b = str.c_str();
     58          return in;
     59      }*/
     60      friend ostream& operator<<(ostream &out, bign& b);/*
     61      {
     62          out << b.str();
     63          return out;
     64      }*/
     65      bign operator +(const bign& b) const
     66      {
     67          bign c;
     68          c.len = 0;
     69          /*
     70          int d = 0, i = 0;
     71          for(; d || i < max(len, b.len); ++i)
     72          {
     73          int x = d;
     74              if(i < len)
     75                  x += s[i];
     76              if(i < b.len)
     77                  x += b.s[i];
     78              c.s[c.len++] = x % 10;
     79              g = x % 10;
     80          }
     81          */
     82          // memset(c, 0, sizeof(c));
     83          int i = 0, d = 0;
     84          for(; i < max(len, b.len)+1; ++i)
     85          {
     86              int x = s[i] + b.s[i] + d;
     87              d = c.s[i] / 10;
     88              x %= 10;
     89              c.s[c.len++] = x;
     90          }
     91          if(d == 0)
     92              --c.len;
     93          return c;
     94      }
     95      bign operator +=(const bign& b)
     96      {
     97          *this = *this + b;
     98          return *this;
     99      }
    100      bool operator <(const bign& b) const
    101      {
    102          if(len != b.len)
    103              return len < b.len;
    104          for(int i = len-1; i >= 0; --i)
    105              if(s[i] != b.s[i])
    106                  return s[i] < b.s[i];
    107          return false;
    108      }
    109      bool operator >(const bign& b) const
    110      {
    111          return b < *this;
    112      }
    113      bool operator <= (const bign& b) const
    114      {
    115          return !(*this > b);
    116      }
    117      bool operator >=(const bign& b) const
    118      {
    119          return !(*this < b);
    120      }
    121      bool operator ==(const bign& b) const
    122      {
    123          return !(*this < b) && (*this > b);
    124      }
    125  };
    126  
    127  inline istream& operator>>(istream &in, bign& b)
    128  {
    129      string str;
    130      in >> str;
    131      b = str.c_str();
    132      return in;
    133  }
    134  inline    ostream& operator<<(ostream &out, bign& b)
    135  {
    136      out << b.str();
    137      return out;
    138  }
    139  int main()
    140  {
    141      return 0;
    142  }

    下面是一些实验:

    Test
     1 #include<iostream>
     2 #include<sstream>
     3 #include<cstring>
     4 #include<cstdio>
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     __int64 n;
    10     char a[1000];
    11     while(cin >> n)
    12     {
    13         //注意整数不能溢出,cin返回错误直接退出
    14         stringstream ss;
    15         ss << n;
    16         string str;
    17         ss >> str;
    18         cout << str;
    19         cout << endl;
    20         /*
    21         //还是用C++的stringstream好,C的__int64依然是当做普通int操作的,容易出错
    22         sprintf(a, "%d", n);
    23         for(int i = 0; i < strlen(a); ++i)
    24             cout << a[i];
    25             */
    26         cout << endl << "**********************String****************************" << endl;
    27         string str = "";
    28         //对string串字符方式的赋值
    29         for(int i = 0; i < strlen(a); ++i)
    30             str += (char)(a[i]);
    31         if(str == "")
    32             str = "0";
    33         cout << str;
    34         cout << endl << "**********************Input****************************" << endl;
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    [转]mysql的查询、子查询及连接查询
    [转]Mysql之Union用法
    [转]Mysql Join语法解析与性能分析
    【转】mysql中select用法
    [转]mysql update操作
    【转】mysql INSERT的用法
    框架:NHibernate学习目录
    NET基础篇——Entity Framework 数据转换层通用类
    MVC5 + EF6 + Bootstrap3
    MVC5+EF6 入门完整教程
  • 原文地址:https://www.cnblogs.com/sanghai/p/2999555.html
Copyright © 2020-2023  润新知