UVaOJ 465 - Overflow


—— by A Code Rabbit


Description

给一个加法或乘法表达式。

判断加数及和有没有超过int的能够表示的范围(2147483647)。


Type

Big Number


Analysis

要用高精度加法和乘法。

剩下的就是简单判断一下是否大于2147483647。


Solution

// UVaOJ 465
// Overflow
// by A Code Rabbit

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

const int LIMITS_INT = 2147483647;
const int MAXD = 10000;

struct BigUnsigned {
    int len, d[MAXD];
    BigUnsigned() { memset(d, 0, sizeof(d)); len = 0; }
    BigUnsigned(int num) { *this = num; }
    BigUnsigned(const char* str) { *this = str; }
    void Clean() { while (len > 1 && !d[len - 1]) len--; }
    BigUnsigned operator=(int num) {
        char str[MAXD];
        sprintf(str, "%d", num);
        *this = str;
        return *this;
    }
    BigUnsigned operator=(const char* str) {
        memset(d, 0, sizeof(d));
        len = strlen(str);
        for (int i = 0; i < len; i++) d[i] = str[len - i - 1] - '0';
        Clean();
        return *this;
    }

    BigUnsigned operator+(const BigUnsigned& one) const {
        BigUnsigned res;
        for (int i = 0, x = 0; i < max(len, one.len) + 1; i++) {
            int tmp = d[i] + one.d[i] + x;
            res.d[res.len++] = tmp % 10;
            x = tmp / 10;
        }
        res.Clean();
        return res;
    }
    BigUnsigned operator*(const BigUnsigned& one) {
        BigUnsigned res;
        res.len = len + one.len;
        for(int i = 0; i < len; i++)
            for(int j = 0; j < one.len; j++)
                res.d[i + j] += d[i] * one.d[j];
        for(int i = 0; i < res.len - 1; i++){
            res.d[i + 1] += res.d[i] / 10;
            res.d[i] %= 10;
        }
        res.Clean();
        return res;
    }

    bool operator<(const BigUnsigned& one) const {
        if (len != one.len) return len < one.len;
        for (int i = len - 1; i >= 0; i--)
            if (d[i] != one.d[i]) return d[i] < one.d[i];
        return false;
    }
    bool operator>(const BigUnsigned& one) const { return one < *this; }
    bool operator<=(const BigUnsigned& one) const { return !(one < *this); }
    bool operator>=(const BigUnsigned& one) const { return !(*this < one); }
    bool operator!=(const BigUnsigned& one) const { return one < *this || *this < one; }
    bool operator==(const BigUnsigned& one) const { return !(one < *this || *this < one); }
};

char str_a[MAXD];
char str_b[MAXD];
char optor;
BigUnsigned a, b, ans;

int main() {
    while (scanf("%s %c %s", str_a, &optor, str_b) != EOF) {
        a = str_a;
        b = str_b;
        cout << str_a << " " << optor << " " << str_b << endl;
        if (a > LIMITS_INT) printf("first number too big\n");
        if (b > LIMITS_INT) printf("second number too big\n");
        ans = optor == '+' ? a + b : a * b;
        if (ans > LIMITS_INT) printf("result too big\n");
    }

    return 0;
} 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值