2021-03-04

大整数类:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class Biglong : public vector<long long> {
public :
    Biglong();
    Biglong(long long);

    bool operator<(const Biglong &) const;
    bool operator<(const long long &) const;

    bool operator>(const Biglong &) const;
    bool operator>(const long long &) const;

    bool operator==(const Biglong &) const;
    bool operator==(const long long &) const;

    bool operator!=(const Biglong &) const;
    bool operator!=(const long long &) const;

    bool operator<=(const Biglong &) const;
    bool operator<=(const long long &) const;

    bool operator>=(const Biglong &) const;
    bool operator>=(const long long &) const;

    Biglong operator+(const Biglong &) const;
    Biglong operator-(const Biglong &) const;
    Biglong operator*(const Biglong &) const;
    Biglong operator/(const Biglong &) const;

    Biglong operator+(const long long &) const;
    Biglong operator-(const long long &) const;
    Biglong operator*(const long long &) const;
    Biglong operator/(const long long &) const;

    Biglong &operator+=(const Biglong &);
    Biglong &operator-=(const Biglong &);
    Biglong &operator*=(const Biglong &);
    Biglong &operator/=(const Biglong &);

    Biglong &operator+=(const long long &);
    Biglong &operator-=(const long long &);
    Biglong &operator*=(const long long &);
    Biglong &operator/=(const long long &);

    friend Biglong operator+(const long long &, const Biglong &);
    friend Biglong operator-(const long long &, const Biglong &);
    friend Biglong operator*(const long long &, const Biglong &);
    friend Biglong operator/(const long long &, const Biglong &);

    friend istream &operator>>(istream &, Biglong &);
    friend ostream &operator<<(ostream &, const Biglong &);
private :
    long long bit; //分辨整数是正数还是负数(0为正,1为负)
    void process_digit(Biglong &) const; //调整进位、负数位、删除前导0
    //四则运算的实现方法
     Biglong same_symbol_add(const Biglong &, const Biglong &) const;
    Biglong same_symbol_sub(const Biglong &, const Biglong &) const;
    Biglong same_symbol_mul(const Biglong &, const Biglong &) const;
    Biglong same_symbol_div(const Biglong &, const Biglong &) const;
};

//private : 
void Biglong::process_digit(Biglong &num) const{
    for (int i = 0; i < num.size(); ++i) {
        if (num[i] < 0) { // 调整负数位
            num[i + 1] -= 1;
            num[i] += 10;
        } else if (num[i] > 9) { // 调整进位
            if (i == num.size() - 1) num.push_back(0);
            num[i + 1] += num[i] / 10;
            num[i] %= 10;
        }
    }
    for (int i = num.size() - 1; i > 0; --i) { // 删除前导0
        if (num[i]) break;
        else num.pop_back();
    }
    return ;
}

//same_symbol :
Biglong Biglong::same_symbol_add(const Biglong &max_num, const Biglong &min_num) const {
    Biglong temp = max_num;
    for (int i = 0; i < min_num.size(); ++i) {
        temp[i] += min_num[i];
    }
    process_digit(temp);
    return temp;
}
Biglong Biglong::same_symbol_sub(const Biglong &max_num, const Biglong &min_num) const {
    Biglong temp = max_num;
    for (int i = 0; i < min_num.size(); ++i) {
        temp[i] -= min_num[i];
    }
    process_digit(temp);
    return temp;
}
Biglong Biglong::same_symbol_mul(const Biglong &max_num, const Biglong &min_num) const {
    Biglong temp;
    for (int i = 0; i < min_num.size(); ++i) {
        for (int j = 0; j < max_num.size(); ++j) {
            if (temp.size() <= i + j) temp.push_back(0);
            temp[i + j] += max_num[j] * min_num[i];
        }
    }
    process_digit(temp);
    return temp;
}
Biglong Biglong::same_symbol_div(const Biglong &max_num, const Biglong &min_num) const {
    Biglong temp, res;
    for (int i = max_num.size() - 1; i >= 0; --i) {
        temp *= 10;
        temp += max_num[i];
        long long val = 0;
        while (temp >= min_num) {
            temp -= min_num;
            val += 1;
        }
        res *= 10;
        res += val;
        if (!i && (temp * 2) > min_num) res += 1;
    }
    return res;
}

//构造函数
Biglong::Biglong() {
    bit = 0;
    push_back(0);
}
Biglong::Biglong(long long x) {
    bit = (x < 0);
    x = bit ? -x : x;
    push_back(x);
    process_digit(*this);
}

//重载"<"
bool Biglong::operator<(const Biglong &num) const {
    if (bit - num.bit) return bit > num.bit;
    if (size() - num.size()) return !bit ? (size() < num.size()) : (size() > num.size());
    for (int i = size() - 1; i >= 0; --i) {
        if (at(i) - num[i]) return !bit ? (at(i) < num[i]) : (at(i) > num[i]);
    }
    return false;
}
bool Biglong::operator<(const long long &num) const {
    Biglong temp(num);
    return *this < temp;
}

//重载">"
bool Biglong::operator>(const Biglong &num) const {
    return num < *this;
}
bool Biglong::operator>(const long long &num) const {
    Biglong temp(num);
    return temp < *this;
}

//重载"=="
bool Biglong::operator==(const Biglong &num) const {
    if ((bit - num.bit) || (size() - num.size())) return false;
    for (int i = 0; i < size(); ++i) {
        if (at(i) - num[i]) return false;
    }
    return true;
}
bool Biglong::operator==(const long long &num) const {
    Biglong temp(num);
    return *this == temp;
}

//重载"!="
bool Biglong::operator!=(const Biglong &num) const {
    return !(*this == num);
}
bool Biglong::operator!=(const long long &num) const {
    return !(*this == num);
}

//重载"<="
bool Biglong::operator<=(const Biglong &num) const {
    return (*this < num) || (*this == num);
}
bool Biglong::operator<=(const long long &num) const {
    return (*this < num) || (*this == num);
}

//重载">="
bool Biglong::operator>=(const Biglong &num) const {
    return (*this > num) || (*this == num);
}
bool Biglong::operator>=(const long long &num) const {
    return (*this > num) || (*this == num);
}

//重载 Biglong long:"+、-、×、÷"
Biglong Biglong::operator+(const Biglong &num) const {
    if (bit == num.bit) {
        return same_symbol_add(max(*this, num), min(*this, num));
    } else {
        Biglong num1 = *this, num2 = num;
        num1.bit = 0, num2.bit = 0;
        Biglong temp = same_symbol_sub(max(num1, num2), min(num1, num2));
        temp.bit = (num1 < num2) ? num.bit : bit;
        return temp;
    }
}
Biglong Biglong::operator-(const Biglong &num) const {
    if (bit - num.bit) {
        Biglong num1 = *this, num2 = num;
        num1.bit = 0, num2.bit = 0;
        Biglong temp = num1 + num2;
        temp.bit = bit;
        return temp;
    } else {
        Biglong num1 = *this, num2 = num;
        num1.bit = 0, num2.bit = 0;
        Biglong temp = same_symbol_sub(max(num1, num2), min(num1, num2));
        temp.bit = (num1 < num2);
        return temp;
    }
}
Biglong Biglong::operator*(const Biglong &num) const {
    Biglong num1 = *this, num2 = num;
    num1.bit = 0, num2.bit = 0;
    Biglong temp = same_symbol_mul(max(num1, num2), min(num1, num2));
    temp.bit = !(bit == num.bit);
    return temp;
}
Biglong Biglong::operator/(const Biglong &num) const {
    if (*this < num) return 0;
    return same_symbol_div(max(*this, num), min(*this, num));
}

//重载 long long:"+、-、×、÷"
Biglong Biglong::operator+(const long long &num) const {
    Biglong temp(num);
    return (*this + temp);
}
Biglong Biglong::operator-(const long long &num) const {
    Biglong temp(num);
    return (*this - temp);
}
Biglong Biglong::operator*(const long long &num) const {
    Biglong temp(num);
    return (*this * temp);
}
Biglong Biglong ::operator/(const long long &num) const {
    if (*this < num) return 0;
    long long temp = 0;
    Biglong res = *this;
    for (int i = size() - 1; i >= 0; --i) {
        temp = temp * 10 + at(i);
        res[i] = temp / num;
        temp %= num;
    }
    return res;
}

//重载 Biglong long:"+=、-=、×=、÷="
Biglong & Biglong::operator+=(const Biglong &num) {
    *this = *this + num;
    return *this;
}
Biglong & Biglong::operator-=(const Biglong &num) {
    *this = *this - num;
    return *this;
}
Biglong& Biglong::operator*=(const Biglong &num) {
    *this = *this * num;
    return *this;
}
Biglong& Biglong::operator/=(const Biglong &num) {
    *this = *this / num;
    return *this;
}

//重载 long long:"+=、-=、×=、÷="
Biglong& Biglong::operator+=(const long long &num) {
    *this = *this + num;
    return *this;
}
Biglong& Biglong::operator-=(const long long &num) {
    *this = *this - num;
    return *this;
}
Biglong& Biglong::operator*=(const long long &num) {
    *this = *this * num;
    return *this;
}
Biglong& Biglong::operator/=(const long long &num) {
    *this = *this / num;
    return *this;
}

//重载 "long long '+、-、×、÷' Biglong"
Biglong operator+(const long long &num, const Biglong &big_num) {
    return (big_num + num);
}
Biglong operator-(const long long &num, const Biglong &big_num) {
    Biglong temp(num);
    return (temp - big_num);
}
Biglong operator*(const long long &num, const Biglong &big_num) {
    return (big_num * num);
}
Biglong operator/(const long long &num, const Biglong &big_num) {
    Biglong temp(num);
    return (temp / big_num);
}

//重载 "右移操作符>>"
istream &operator>>(istream &in, Biglong &num) {
    long long temp;
    in >> temp;
    num.clear();
    num.push_back(temp);
    num.process_digit(num);
    return in;
}

//重载 "左移操作符<<"
ostream &operator<<(ostream &out, const Biglong &num) {
    if (!num.size() && num[0] == 0) {
        out << 0;
        return out;
    }
    if (num.bit) out << "-";
    for (int i = num.size() - 1; i >= 0; --i) {
        out << num[i];
    }
    return out;
}

int main() {
    Biglong big1, big2, ans;
    char oper;
    while (1) {
        cin >> big1 >> oper >> big2;
        switch (oper) {
            case '+': ans = big1 + big2; break;
            case '-': ans = big1 - big2; break;
            case '*': ans = big1 * big2; break;
            case '/': ans = big1 / big2;break;
        }
        cout << ans << endl;
    }
    return 0;
}

在这里插入图片描述

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值