我是靠谱客的博主 故意冬天,最近开发中收集的这篇文章主要介绍BigInteger大整数类的加、减、乘、输入、输出,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include<iostream>
#include<vector>
#include<string>
#define max(x,y) (x>y? x:y)
using namespace std;
const int BASE = 1e4;
const int WIDTH = 4;
#pragma warning(disable:4996)
struct BigInteger {
    int flag = 1;//代表正负
    vector<int> s;
    BigInteger(long long int num = 0) { *this = num; }
    BigInteger operator=(long long int num) {
        s.clear();
        do {
            s.push_back(num%BASE);
            num /= BASE;
        } while (num);
        return *this;
    }
    BigInteger operator=(const string& str) {
        s.clear();
        int x, len = (str.length() - 1) / WIDTH + 1;
        for (int i = 0; i < len; ++i) {
            int end = str.length() - i * WIDTH;
            int start = max(0, end - WIDTH);
            sscanf(str.substr(start, end - start).c_str(), "%d", &x);
            s.push_back(x);
        }
        return *this;
    }
    bool operator<(const BigInteger& x) const {//绝对值的比较
        if (s.size() != x.s.size()) return s.size() < x.s.size();
        for (int i = s.size() - 1; i >= 0; --i)
            if (s[i] != x.s[i]) return s[i] < x.s[i];
        return false;
    }
    BigInteger operator+(const BigInteger& b)const;
    BigInteger operator-(const BigInteger& b)const;
    BigInteger operator*(const BigInteger& b)const;
};
BigInteger add(const BigInteger& a, const BigInteger& b) {//都是正
    BigInteger ans;
    ans.s.clear();
    int L1 = a.s.size(), L2 = b.s.size();
    int jin = 0, i = 0, T;
    while (i < L1 && i < L2) {
        T = a.s[i] + b.s[i] + jin;
        ans.s.push_back(T%BASE);
        jin = T > BASE ? 1 : 0;
        ++i;
    }
    while (i < L1) {
        T = a.s[i] + jin;
        ans.s.push_back(T%BASE);
        jin = T > BASE ? 1 : 0;
        ++i;
    }
    while (i < L2) {
        T = b.s[i] + jin;
        ans.s.push_back(T%BASE);
        jin = T > BASE ? 1 : 0;
        ++i;
    }
    if (jin)
        ans.s.push_back(jin);
    return ans;
}
BigInteger sub(const BigInteger& a, const BigInteger& b) {//都是正且a大于b
    BigInteger ans;
    ans.s.clear();
    int L1 = a.s.size(), L2 = b.s.size();
    int jie = 0, i = 0, T;
    while (i < L2) {
        T = a.s[i] - jie - b.s[i];
        if (T < 0)
            T += BASE, jie = 1;
        else
            jie = 0;
        ans.s.push_back(T);
        ++i;
    }
    while (i < L1) {
        T = a.s[i] - jie;
        if (T < 0)
            T += BASE, jie = 1;
        else
            jie = 0;
        ans.s.push_back(T);
        ++i;
    }
    while (ans.s.size() > 1 && !ans.s.back()) ans.s.pop_back();
    return ans;
}
BigInteger mult(const BigInteger& a, int x,int n) {//x的后面有n*WIDTH个0
    BigInteger ans;
    ans.s.clear();
    int L = a.s.size();
    while (n--) ans.s.push_back(0);
    int jin = 0, i = 0, T;
    while (i < L) {
        T = a.s[i]*x + jin;
        ans.s.push_back(T%BASE);
        jin = T / BASE;
        ++i;
    }
    if (jin) ans.s.push_back(jin);
    return ans;

}
ostream& operator<<(ostream& out, const BigInteger& x) {
    if (x.flag == -1)printf("-");
    printf("%d",x.s.back());
    for (int i = x.s.size() - 2; i >= 0; --i)
        printf("%04d", x.s[i]);
    return out;
}
istream& operator>>(istream& in, BigInteger& x) {
    string s;
    if (!(in >> s)) return in;
    if (s[0] == '-') x.flag = -1, s = string(s.begin() + 1, s.end());
    x = s;
    return in;
}
BigInteger BigInteger::operator+(const BigInteger& b)const {
    BigInteger ans;
    if (flag == b.flag) ans.flag = flag, ans = add(*this, b);
    else {
        if (*this < b)ans = sub(b, *this),ans.flag = b.flag;
        else if (b < *this) ans = sub(*this, b), ans.flag = flag;
        else ans = 0;
    }
    return ans;
}
BigInteger BigInteger::operator-(const BigInteger& b)const {
    BigInteger ans = b;
    ans.flag *= -1;
    return *this + ans;
}
BigInteger BigInteger::operator*(const BigInteger& b)const {
    BigInteger ans,tt;
    ans.flag = flag * b.flag;
    int L = b.s.size();
    for (int i = 0; i < L; ++i)
        ans = ans + mult(*this, b.s[i], i);
    return ans;
}
int main() {
    BigInteger a, b;
    while (cin >> a >> b)
        cout << a + b << endl << a - b << endl << a * b << endl;
}

最后

以上就是故意冬天为你收集整理的BigInteger大整数类的加、减、乘、输入、输出的全部内容,希望文章能够帮你解决BigInteger大整数类的加、减、乘、输入、输出所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部