c++ 高精度运算类 bign

  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstdlib>  
  4. #include <cstring>  
  5. #include <string>  
  6. #include <algorithm>  
  7. using namespace std;  
  8.   
  9. const int MAXN = 410;  
  10.   
  11. struct bign  
  12. {  
  13.     int len, s[MAXN];  
  14.     bign ()  
  15.     {  
  16.         memset(s, 0, sizeof(s));  
  17.         len = 1;  
  18.     }  
  19.     bign (int num) { *this = num; }  
  20.     bign (const char *num) { *this = num; }  
  21.     bign operator = (const int num)  
  22.     {  
  23.         char s[MAXN];  
  24.         sprintf(s, "%d", num);  
  25.         *this = s;  
  26.         return *this;  
  27.     }  
  28.     bign operator = (const char *num)  
  29.     {  
  30.         for(int i = 0; num[i] == '0'; num++) ;  //去前导0  
  31.         len = strlen(num);  
  32.         for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';  
  33.         return *this;  
  34.     }  
  35.     bign operator + (const bign &b) const //+  
  36.     {  
  37.         bign c;  
  38.         c.len = 0;  
  39.         for(int i = 0, g = 0; g || i < max(len, b.len); i++)  
  40.         {  
  41.             int x = g;  
  42.             if(i < len) x += s[i];  
  43.             if(i < b.len) x += b.s[i];  
  44.             c.s[c.len++] = x % 10;  
  45.             g = x / 10;  
  46.         }  
  47.         return c;  
  48.     }  
  49.     bign operator += (const bign &b)  
  50.     {  
  51.         *this = *this + b;  
  52.         return *this;  
  53.     }  
  54.     void clean()  
  55.     {  
  56.         while(len > 1 && !s[len-1]) len--;  
  57.     }  
  58.     bign operator * (const bign &b) //*  
  59.     {  
  60.         bign c;  
  61.         c.len = len + b.len;  
  62.         for(int i = 0; i < len; i++)  
  63.         {  
  64.             for(int j = 0; j < b.len; j++)  
  65.             {  
  66.                 c.s[i+j] += s[i] * b.s[j];  
  67.             }  
  68.         }  
  69.         for(int i = 0; i < c.len; i++)  
  70.         {  
  71.             c.s[i+1] += c.s[i]/10;  
  72.             c.s[i] %= 10;  
  73.         }  
  74.         c.clean();  
  75.         return c;  
  76.     }  
  77.     bign operator *= (const bign &b)  
  78.     {  
  79.         *this = *this * b;  
  80.         return *this;  
  81.     }  
  82.     bign operator - (const bign &b)  
  83.     {  
  84.         bign c;  
  85.         c.len = 0;  
  86.         for(int i = 0, g = 0; i < len; i++)  
  87.         {  
  88.             int x = s[i] - g;  
  89.             if(i < b.len) x -= b.s[i];  
  90.             if(x >= 0) g = 0;  
  91.             else  
  92.             {  
  93.                 g = 1;  
  94.                 x += 10;  
  95.             }  
  96.             c.s[c.len++] = x;  
  97.         }  
  98.         c.clean();  
  99.         return c;  
  100.     }  
  101.     bign operator -= (const bign &b)  
  102.     {  
  103.         *this = *this - b;  
  104.         return *this;  
  105.     }  
  106.     bign operator / (const bign &b)  
  107.     {  
  108.         bign c, f = 0;  
  109.         for(int i = len-1; i >= 0; i--)  
  110.         {  
  111.             f = f*10;  
  112.             f.s[0] = s[i];  
  113.             while(f >= b)  
  114.             {  
  115.                 f -= b;  
  116.                 c.s[i]++;  
  117.             }  
  118.         }  
  119.         c.len = len;  
  120.         c.clean();  
  121.         return c;  
  122.     }  
  123.     bign operator /= (const bign &b)  
  124.     {  
  125.         *this  = *this / b;  
  126.         return *this;  
  127.     }  
  128.     bign operator % (const bign &b)  
  129.     {  
  130.         bign r = *this / b;  
  131.         r = *this - r*b;  
  132.         return r;  
  133.     }  
  134.     bign operator %= (const bign &b)  
  135.     {  
  136.         *this = *this % b;  
  137.         return *this;  
  138.     }  
  139.     bool operator < (const bign &b)  
  140.     {  
  141.         if(len != b.len) return len < b.len;  
  142.         for(int i = len-1; i >= 0; i--)  
  143.         {  
  144.             if(s[i] != b.s[i]) return s[i] < b.s[i];  
  145.         }  
  146.         return false;  
  147.     }  
  148.     bool operator > (const bign &b)  
  149.     {  
  150.         if(len != b.len) return len > b.len;  
  151.         for(int i = len-1; i >= 0; i--)  
  152.         {  
  153.             if(s[i] != b.s[i]) return s[i] > b.s[i];  
  154.         }  
  155.         return false;  
  156.     }  
  157.     bool operator == (const bign &b)  
  158.     {  
  159.         return !(*this > b) && !(*this < b);  
  160.     }  
  161.     bool operator != (const bign &b)  
  162.     {  
  163.         return !(*this == b);  
  164.     }  
  165.     bool operator <= (const bign &b)  
  166.     {  
  167.         return *this < b || *this == b;  
  168.     }  
  169.     bool operator >= (const bign &b)  
  170.     {  
  171.         return *this > b || *this == b;  
  172.     }  
  173.     string str() const  
  174.     {  
  175.         string res = "";  
  176.         for(int i = 0; i < len; i++) res = char(s[i]+'0') + res;  
  177.         return res;  
  178.     }  
  179. };  
  180.   
  181. istream& operator >> (istream &in, bign &x)  
  182. {  
  183.     string s;  
  184.     in >> s;  
  185.     x = s.c_str();  
  186.     return in;  
  187. }  
  188.   
  189. ostream& operator << (ostream &out, const bign &x)  
  190. {  
  191.     out << x.str();  
  192.     return out;  
  193. }  
  194.   
  195. int main()  
  196. {  
  197.     bign a, b, c, d, e, f, g;  
  198.     while(cin>>a>>b)  
  199.     {  
  200.         a.clean(), b.clean();  
  201.         c = a+b;  
  202.         d = a-b;  
  203.         e = a*b;  
  204.         f = a/b;  
  205.         g = a%b;  
  206.         cout<<"a+b"<<"="<<c<<endl; // a += b  
  207.         cout<<"a-b"<<"="<<d<<endl; // a -= b;  
  208.         cout<<"a*b"<<"="<<e<<endl; // a *= b;  
  209.         cout<<"a/b"<<"="<<f<<endl; // a /= b;  
  210.         cout<<"a%b"<<"="<<g<<endl; // a %= b;  
  211.         if(a != b) printf("YES\n");  
  212.         else printf("NO\n");  
  213.     }  
  214.     return 0;  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
运算符重载是 C++ 中的一个重要特性,它允许我们对已有的运算符进行重新定义,以适应自定义型的操作。对于高精度计算,我们可以通过运算符重载来实现高精度整数的加减乘除等运算。 在 C++ 中,我们可以通过重载运算符来实现高精度整数的加减乘除等运算。例如,我们可以重载加法运算符 +,使其能够对两个高精度整数进行相加操作。具体实现方式可以参考以下代码: ```c++ class BigInt { public: // 构造函数 BigInt(string s) { reverse(s.begin(), s.end()); for (int i = 0; i < s.size(); i += 9) { int v = 0; for (int j = i; j < min(i + 9, (int)s.size()); j++) { v = v * 10 + (s[j] - '0'); } num.push_back(v); } } // 重载加法运算符 BigInt operator+(const BigInt& b) const { BigInt res = *this; res.num.resize(max(num.size(), b.num.size())); for (int i = 0; i < b.num.size(); i++) { res.num[i] += b.num[i]; if (res.num[i] >= BASE) { res.num[i] -= BASE; res.num[i+1]++; } } while (res.num.back() == 0 && res.num.size() > 1) { res.num.pop_back(); } return res; } private: static const int BASE = 1e9; vector<int> num; }; ``` 在上述代码中,我们定义了一个 BigInt ,其中包含一个字符串构造函数和一个重载加法运算符的函数。在重载加法运算符的函数中,我们首先将两个高精度整数的位数扩展到相同长度,然后逐位相加,并处理进位。最后,我们还需要去除结果中的前导零。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值