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

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

这里运算符的重载比较难

5:别叫,这个大整数已经很简化了!

总时间限制: 

1000ms

 

内存限制: 

65536kB

// 在此处补充你的代码

描述

程序填空,输出指定结果

#include 
#include
#include
#include
using namespace std;const int MAX = 110; class CHugeInt {
};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;}

输入

多组数据,每组数据是两个非负整数s和 n。s最多可能200位, n用int能表示

输出

对每组数据,输出6行,内容分别是:

样例输入

99999999999999999999999999888888888888888812345678901234567789 126 6

样例输出

999999999999999999999999998888888888888888123456789012345678019999999999999999999999999988888888888888881234567890123456780199999999999999999999999999888888888888888812345678901234567801252526121212131314

来源

Guo Wei

 

#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); // 把c全部置为0 strcpy(c, a); len = strlen(c); reverse(c); } CHugeInt(int n) { int len; memset(c, 0, 200); // 把c全部置为0 sprintf(c, "%d", n);//把n转换为char存在c 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) { // b为0的情况 return *this; } if (c[0] == '0' && len == 1) { // a为0的情况 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'; // 注意前面处理中有的字符串位是用数字0填充的 if (b.c[i] == 0) c2 = '0'; x = c1 - '0' + c2 - '0' + j; // j是进位 a.c[i] = (x % 10) + '0'; // 处理进位 j = x / 10; // j是进位 } // 将字符串长度较大值,给新的字符串 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);//直接调用默认复制构造函数,因为char c[200]是数组不是指针 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) { cout << 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;}

 

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

你可能感兴趣的文章
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>