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

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

public:
    int a[2 * MAX];
    int len;

    CHugeInt():len(0) { memset(a, 0, sizeof(a)); }
    CHugeInt(char c[]){
        memset(a, 0, sizeof(a));
        len = strlen(c);
        for (int i = 0;i < len; ++i) {
            a[i] = c[len - 1 - i] - '0';
        }
    }
    CHugeInt(int n){
        memset(a, 0, sizeof(a));
        if (n == 0) {
            len = 1;
        }
        else {
            int wei = 0;
            while (n > 0) {
                wei++;
                a[wei - 1] = n % 10;
                n = n / 10;
            }
            len = wei;
        }

    }
    CHugeInt operator+ (CHugeInt & b) {
        CHugeInt c;
        int temp;
        int ci = 0;
        for (int i = 0;i < len || i < b.len; ++i) {
            temp = a[i] + b.a[i] + ci;
            ci = temp / 10;
            c.a[c.len++] = temp % 10;
        }
        if (ci != 0) {
            c.a[c.len++] = ci;
        }
        return c;
    }

    CHugeInt operator+ (int n) {
        CHugeInt b = CHugeInt(n);
        CHugeInt c;
        int temp;
        int ci = 0;
        for (int i = 0;i < len || i < b.len; ++i) {
            temp = a[i] + b.a[i] + ci;
            ci = temp / 10;
            c.a[c.len++] = temp % 10;
        }
        if (ci != 0) {
            c.a[c.len++] = ci;
        }
       /* memcpy(a, c.a, sizeof(a));
        len = c.len;*/
        return c;
    }

    friend CHugeInt operator+ (int a, CHugeInt & b) {
        return  b + a;
    }
    operator char* () {
        static char res[2 * MAX];
        for (int i = 0;i <len;++i) {
            res[i] = a[len - i - 1] + '0';
        }
        res[len] = '\0';
        return res;
    }

    CHugeInt& operator+= (int n) {
        CHugeInt b = CHugeInt(n);
        CHugeInt c;
        int temp;
        int ci = 0;
        for (int i = 0;i < len || i < b.len; ++i) {
            temp = a[i] + b.a[i] + ci;
            ci = temp / 10;
            c.a[c.len++] = temp % 10;
        }
        if (ci != 0) {
            c.a[c.len++] = ci;
        }
         memcpy(a, c.a, sizeof(a));
         len = c.len;
        return * this;
    }

    CHugeInt & operator++ () {
        CHugeInt b = CHugeInt(1);
        CHugeInt c;
        int temp;
        int ci = 0;
        for (int i = 0;i < len || i < b.len; ++i) {
            temp = a[i] + b.a[i] + ci;
            ci = temp / 10;
            c.a[c.len++] = temp % 10;
        }
        if (ci != 0) {
            c.a[c.len++] = ci;
        }
        memcpy(a, c.a, sizeof(a));
        len = c.len;
        return *this;
    }

    CHugeInt operator++ (int ff) {
        CHugeInt res(*this);
        CHugeInt b = CHugeInt(1);
        CHugeInt c;
        int temp;
        int ci = 0;
        for (int i = 0;i < len || i < b.len; ++i) {
            temp = a[i] + b.a[i] + ci;
            ci = temp / 10;
            c.a[c.len++] = temp % 10;
        }
        if (ci != 0) {
            c.a[c.len++] = ci;
        }
        memcpy(a, c.a, sizeof(a));
        len = c.len;
        return res;
    }
};
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;
}

posted @ 2022-02-20 17:24  icefield817  阅读(180)  评论(0编辑  收藏  举报