博客
关于我
C++ 实现大数加法类
阅读量:243 次
发布时间:2019-03-01

本文共 3324 字,大约阅读时间需要 11 分钟。

CHugeInt 类实现大整数运算

代码结构说明

#include 
#include
#include
#include
using namespace std;const int MAX = 110;class CHugeInt {private: char c[200]; public: // 将字符串反转 void reverse(char *a) { int i = 0, j = strlen(a) - 1; while (i < j) { swap(a[i], a[j]); i++; j--; } } CHugeInt(const char* a) { int len; memset(c, 0, 200); strcpy(c, a); len = strlen(c); reverse(c); } CHugeInt(int n) { int len; memset(c, 0, 200); sprintf(c, "%d", n); len = strlen(c); reverse(c); } CHugeInt operator +(const CHugeInt &b) const { CHugeInt a(0); int lenb = strlen(b.c); int len = strlen(c); int lena; if (b.c[0] == '0' && lenb == 1) { return *this; } if (c[0] == '0' && len == 1) { strcpy(a.c, b.c); return a; } int j = 0; for (int i = 0; i < len || i < lenb; i++) { int x = 0; char c1 = c[i], c2 = b.c[i]; if (c[i] == 0) c1 = '0'; if (b.c[i] == 0) c2 = '0'; x = c1 - '0' + c2 - '0' + j; a.c[i] = (x % 10) + '0'; j = x / 10; } lena = len > lenb ? len : lenb; if (j != 0) { int a0 = len > lenb ? len : lenb; a.c[a0] = j + '0'; lena = a0 + 1; } return a; } CHugeInt operator +(int n) { return *this + CHugeInt(n); } friend CHugeInt operator +(int n, const CHugeInt &h) { return h + n; } CHugeInt& operator +=(int n) { *this = *this + CHugeInt(n); return *this; } friend CHugeInt& operator ++(CHugeInt &a) { a = a + 1; return a; } friend ostream& operator<<(ostream& o, const CHugeInt &a) { int len = strlen(a.c); for (int i = len - 1; i >= 0; --i) { o << a.c[i]; } return o; } friend CHugeInt operator ++(CHugeInt &a, int) { CHugeInt b = a; a = (b + 1); return b; }};int main() { char s[210]; int n; while (cin >> s >> n) { CHugeInt a(s); CHugeInt b(n); cout << a + b << endl; cout << n + a << endl; cout << a + n << endl; b += n; cout << ++b << endl; cout << b++ << endl; cout << b << endl; } return 0;}

功能说明

  • 类定义与成员变量

    CHugeInt 类定义了一个字符数组 c 用于存储大整数的每一位数字。

  • 构造函数

    • CHugeInt(const char* a):将输入字符串反转后赋值给 c。
    • CHugeInt(int n):将输入整数转换为字符串并反转后赋值给 c。
  • 加法运算

    operator + 方法实现了两数相加功能,支持 CHugeInt 与 CHugeInt 以及 CHugeInt 与 int 的加法运算。

  • 进位处理

    在加法运算中,逐位处理每一位数字并处理进位,确保结果的正确性。

  • 字符串反转

    reverse 方法用于对字符串进行反转处理,确保数字存储的高低位正确。

  • 输入输出操作

    定义了 operator<< 方法,用于将 CHugeInt 对象输出为字符串,确保输出顺序正确无误。

  • 操作符重载

    定义了 += 和 ++ 操作符,支持大整数的自增操作。

  • 使用示例

    程序通过 cin 读取输入的两个非负整数 s 和 n,其中 s 最多可以有 200 位,n 则可以用 int 型表示。对于每组输入,程序输出六行结果,分别是:

  • a + b 的结果
  • n + a 的结果
  • a + n 的结果
  • b += n 后的结果
  • ++b 的结果
  • b 的最终结果
  • 样例输入

    99999999999999999999999999888888888888888812345678901234567789 126 6

    样例输出

    999999999999999999999999998888888888888888123456789012345678019999999999999999999999999988888888888888881234567890123456780199999999999999999999999999888888888888888812345678901234567801252526121212131314

    程序能够正确处理大整数的加法运算和自增操作,并输出预期结果。

    转载地址:http://lvhv.baihongyu.com/

    你可能感兴趣的文章
    Python:生成“元组集“;来自“元组列表“;不考虑顺序
    查看>>
    Python:生成具有趋势的随机时间序列数据(例如周期性、指数衰减等)
    查看>>
    Python:用渐进式数字重命名列表中的重复项而不对列表进行排序
    查看>>
    Python:矩形参数无效
    查看>>
    Python:语法进阶
    查看>>
    Python:调用复制函数时调用 Python 对象时超出了最大递归深度
    查看>>
    Python:这两个 import 语句有什么区别?
    查看>>
    Python:通过串口从 Velleman k8090 读取数据
    查看>>
    python:遍历具有列表值的字典
    查看>>
    Python:降低精度 pandas 时间戳数据帧
    查看>>
    python:非阻塞子进程,检查标准输出
    查看>>
    PythonABC多重继承
    查看>>
    Pythonargparse中的多个参数
    查看>>
    Python关于 *args 和 **kwargs 参数的详解(全)
    查看>>
    Pythonfrozenset散列算法/实现
    查看>>
    PythonGTK+3:通过单击列对树视图进行排序
    查看>>
    pythonnumpy生成二进制流_python/numpy生成的二进制文件,由C读取
    查看>>
    pythonsign函数_对python函数签名的方法详解 用python怎么写一个自签名证书
    查看>>
    PythonStudy
    查看>>
    Pythons__IMPORT__未按预期工作
    查看>>