大數操作-加/減/乘/除/取模

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

int bigDataCmp(const string &left, const string &right)
{
    int left_len = left.size();
    int right_len = right.size();
    if (left_len > right_len) return 1;
    if (right_len > left_len) return -1;
    for (int i = 0; i < left_len; i++)
    {
        if (left[i] > right[i]) return 1;
        if (left[i] < right[i]) return -1;
    }
    return 0;
}

void bigDataAdd(const string &left, const string &right, string &result)
{
    int left_len = left.size();
    int right_len = right.size();
    int l_index, r_index;
    int carry = 0;
    for (l_index = left_len - 1, r_index = right_len - 1; l_index >= 0 && r_index >= 0; l_index--, r_index--)
    {
        int res = left[l_index] - '0' + right[r_index] - '0' + carry;
        if (res >= 10)
            carry = 1;
        else
            carry = 0;
        result.append(1, res % 10 + '0');
    }
    while (l_index >= 0)
    {
        int res = left[l_index] - '0' + carry;
        if (res >= 10)
            carry = 1;
        else
            carry = 0;
        result.append(1, res % 10 + '0');
        l_index--;
    }
    while (r_index >= 0)
    {
        int res = right[r_index] - '0' + carry;
        if (res >= 10)
            carry = 1;
        else
            carry = 0;
        result.append(1, res % 10 + '0');
        r_index--;
    }
    if (carry)
        result.append(1, '1');
    reverse(result.begin(), result.end());
}

void bigDataDec(const string &left, const string &right, string &result)
{
    int cmp = bigDataCmp(left, right);
    if (cmp == 0)
    {
        result = "0";
    }
    else if (cmp < 0)
    {
        bigDataDec(right, left, result);
        result = '-' + result;
    } 
    else
    {
        int left_len = left.size();
        int right_len = right.size();
        int l_index, r_index;
        int carry = 0;
        for (l_index = left_len - 1, r_index = right_len - 1; l_index >= 0 && r_index >= 0; l_index--, r_index--)
        {
            int res = left[l_index] - carry - right[r_index];
            if (res < 0)
            {
                carry = 1;
                res += 10;
            }
            else
                carry = 0;
            result.append(1, res + '0');
        }
        while (l_index >= 0)
        {
            int res = left[l_index] - '0' - carry;
            if (res < 0)
            {
                carry = 1;
                res += 10;
            }
            else
                carry = 0;
            result.append(1, res + '0');
            l_index--;
        }
        int pos = result.find_last_not_of('0');
        string tmp = result.substr(0, pos + 1);
        swap(tmp, result);
        reverse(result.begin(), result.end());
    }
}

void bigDataMul(const string &left, const string &right, string &result)
{
    int left_len = left.size();
    int right_len = right.size();
    vector<string> mid;
    int mul_count = 0;
    for (int r_index = right_len - 1; r_index >= 0; r_index--, mul_count++)
    {
        string mid_tmp;
        int carry = 0;
        for (int l_index = left_len - 1; l_index >= 0; l_index--)
        {
            int res = (right[r_index] - '0') * (left[l_index] - '0') + carry;
            if (res >= 10)
                carry = res / 10;
            else
                carry = 0;
            mid_tmp.append(1, res % 10 + '0');
        }
        if (carry)
            mid_tmp.append(1, carry + '0');
        reverse(mid_tmp.begin(), mid_tmp.end());
        mid_tmp.append(mul_count, '0');
        mid.push_back(mid_tmp);
    }

    string res_tmp = mid[0];
    for (int index = 1; index < mid.size(); index++)
    {
        string tmp;
        bigDataAdd(res_tmp, mid[index], tmp);
        res_tmp = tmp;
    }
    int pos = res_tmp.find_first_not_of('0');
    if (pos == string::npos)
        result = "0";
    else
    {
        string tmp = res_tmp.substr(pos);
        result = tmp;
    }
}

void bigDataDiv(const string &left, const string &right, string &result, string &remainder)
{
    int cmp = bigDataCmp(left, right);
    if (cmp == 0)
    {
        result = "1";
        remainder = "0";
    }
    else if (cmp < 0)
    {
        result = "0";
        remainder = left;
    }
    else
    {
        int left_len = left.size();
        int right_len = right.size();
        string left_substr;
        bool first_zero = true;
        for (int l_index = 0; l_index < left_len; l_index++)
        {
            left_substr += left[l_index];
            if (bigDataCmp(left_substr, right) < 0)
            {
                if (!first_zero)
                    result.append(1, '0');
            }
            else
            {
                string res_tmp = "1";
                string mul_tmp, sub_tmp;
                while (true)
                {
                    bigDataMul(right, res_tmp, mul_tmp);
                    bigDataDec(left_substr, mul_tmp, sub_tmp);
                    int cmp_tmp = bigDataCmp(sub_tmp, right);
                    if (cmp_tmp >= 0)
                    {
                        res_tmp[0]++;
                        mul_tmp.clear();
                        sub_tmp.clear();
                        continue;
                    }
                    result.append(res_tmp);
                    left_substr.clear();
                    if (cmp_tmp < 0 && sub_tmp != "0")
                        left_substr = sub_tmp;
                    first_zero = false;
                    break;
                }
            }
        }
        int pos = left_substr.find_first_not_of('0');
        if (pos == string::npos)
            remainder = "0";
        else
            remainder = left_substr.substr(pos);
    }
}

int main()
{
    string a, b;
    cin >> a >> b;
    string add_res, minus_res, mul_res, div_res, div_remainder;
    bigDataAdd(a, b, add_res);
    bigDataDec(a, b, minus_res);
    bigDataMul(a, b, mul_res);
    bigDataDiv(a, b, div_res, div_remainder);
    cout << "Add Result: " << add_res << endl << "Dec Result: " << minus_res << endl << "Mul Result: " << mul_res << endl << "Div Result: " << div_res << " " << div_remainder << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章