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

本文共 3427 字,大约阅读时间需要 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 + 方法实现了两数相加功能,支持 CHugeIntCHugeInt 以及 CHugeIntint 的加法运算。

  • 进位处理

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

  • 字符串反转

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

  • 输入输出操作

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

  • 操作符重载

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

  • 使用示例

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

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

    99999999999999999999999999888888888888888812345678901234567789 126 6

    样例输出

    999999999999999999999999998888888888888888123456789012345678019999999999999999999999999988888888888888881234567890123456780199999999999999999999999999888888888888888812345678901234567801252526121212131314

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

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

    你可能感兴趣的文章
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错TypeError: this.getOptions is not a function
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm设置镜像如淘宝:http://npm.taobao.org/
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>
    NPM酷库052:sax,按流解析XML
    查看>>
    npm错误 gyp错误 vs版本不对 msvs_version不兼容
    查看>>
    npm错误Error: Cannot find module ‘postcss-loader‘
    查看>>
    npm,yarn,cnpm 的区别
    查看>>
    NPOI
    查看>>