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

总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空,输出指定结果

#include <iostream> 
#include <cstring> 
#include <cstdlib> 
#include <cstdio> 
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 12
6 6
样例输出
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
25
25
26
12
12
12
13
13
14
来源
Guo Wei

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int MAX = 110;
class CHugeInt
{

    char num[210];

public:
    //为了实现核心函数——加法,我们采取逆转数组以便进位运算。
    char *reverse(char *str)
    {
        for (int i = 0, j = strlen(str) - 1; i < j; i++, j--)
            swap(str[i], str[j]);
        return str;
    };

    //本题不需要自己实现复制构造函数来深拷贝,因为压根儿没有指针成员,
    //用默认赋值足矣(默认赋值会把数组元素一个个拷贝过去)。

    //采用逆序存储数字。记得给num先补零!
    // 虽然cin输入字符串到字符数组里会自动末位补零,但是其他位不是零在做加法运算时会出错。
    CHugeInt(const char *s = NULL) //类型转换构造: char* -> CHugeInt
    {
        memset(num, '\0', sizeof(num));
        strcpy(num, s);
        reverse(num);
    }
    CHugeInt(const int n = 0) //类型转换构造: int -> CHugeInt
    {
        memset(num, '\0', sizeof(num));
        sprintf(num, "%d", n); //很有用的函数,会将数字直接印到字符串里
        reverse(num);
    }

    //我们只需要重载两个新类对象的加法,而不需要考虑其他数据类型。
    //因为若有的语句中两个参数不全是此类对象,则编译器会自动采用类型转换构造函数。
    friend const CHugeInt operator+(const CHugeInt &obj1, const CHugeInt &obj2) //重载'+'
    {
        CHugeInt ret(0);
        int carry = 0; //进位
        for (int i = 0; i < 210; i++)
        {
            char a = (obj1.num)[i];
            char b = (obj2.num)[i];
            if (!a && !b && !carry) //所有位都处理完了
                break;
            if (!a)
                a = '0';
            if (!b)
                b = '0';
            int result = a + b - 2 * '0' + carry; //这位的值
            if (result >= 10)
            {
                carry = 1;
                result -= 10;
            }
            else
                carry = 0;
            (ret.num)[i] = result + '0';
        }
        return ret;
    }

    void operator+=(const CHugeInt &obj) //重载'+='
    {
        *this = *this + obj; //利用之前定义的'+'和默认的赋值函数
    }

    CHugeInt operator++(int) //重载后置'++'
    {
        CHugeInt temp(*this);
        (*this) += 1; //利用之前定义的'+='
        return temp;
    }
    CHugeInt operator++() //重载前置'++'
    {
        (*this) += 1; //利用之前定义的'+='
        return *this;
    }

    friend ostream &operator<<(ostream &o, const CHugeInt &obj) //重载cout<<...
    {

        for (int i = strlen(obj.num) - 1; i >= 0; i--)
            cout << obj.num[i];
        return o;
    }
};
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;
}

有史以来做的时间最长的一道题。。。注意点见注释。

补充:‘=’不能重载为全局函数(friend)。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值