C++大数运算(加减乘除)

// C = A + B, A >= 0, B >= 0
string add(const string &A, const string &B)
{
    vector a, b, c;
    for (auto iter = A.end() - 1; iter != A.begin() - 1; iter--)
        a.push_back(*iter - '0');
    for (auto iter = B.end() - 1; iter != B.begin() - 1; iter--)
        b.push_back(*iter - '0');
    int t = 0;
    for (size_t i = 0; i < a.size() || i < b.size(); i++)
    {
        if (i < a.size())
            t += a[i];
        if (i < b.size())
            t += b[i];
        c.push_back(t % 10);
        t /= 10;
    }
    if (t)
        c.push_back(1);
    string C;
    for (auto iter = c.end() - 1; iter != c.begin() - 1; iter--)
        C.push_back(*iter + '0');
    return C;
}

// C = A - B, A >= 0, B >= 0
string sub(const string &A, const string &B)
{
    bool flag = A.size() < B.size(); //判断A a, b, c;
        for (auto iter = A.end() - 1; iter != A.begin() - 1; iter--)
            a.push_back(*iter - '0');
        for (auto iter = B.end() - 1; iter != B.begin() - 1; iter--)
            b.push_back(*iter - '0');
        int t = 0;
        for (size_t i = 0; i < a.size(); i++)
        {
            t += a[i];
            if (i < b.size())
                t -= b[i];
            c.push_back((t + 10) % 10);
            t = t < 0 ? -1 : 0;
        }
        while (c.size() > 1 && c.back() == 0)
            c.pop_back();
        string C;
        for (auto iter = c.end() - 1; iter != c.begin() - 1; iter--)
            C.push_back(*iter + '0');
        return C;
    }
}

乘(大数乘小数)

// C = A * b, A >= 0, b >= 0
string mul(const string &A, int b)
{
    vector a, c;
    for (auto iter = A.end() - 1; iter != A.begin() - 1; iter--)
        a.push_back(*iter - '0');
    int t = 0;
    for (size_t i = 0; i < a.size() || t; i++)
    {
        if (i < a.size())
            t += a[i] * b;
        c.push_back(t % 10);
        t /= 10;
    }
    while (c.size() > 1 && c.back() == 0)
        c.pop_back();

    string C;
    for (auto iter = c.end() - 1; iter != c.begin() - 1; iter--)
        C.push_back(*iter + '0');
    return C;
}

乘(大数乘大数)

// C = A * B, A >= 0, B >= 0
//需要调用add(string,string)和mul(string,int)
string mul(const string &A, const string &B)
{
    vector m; //将AB中较短的数转成int数组,长的保留string
    const string &n= A.size() <= B.size()?B:A;
    if (A.size() <= B.size())
        for (auto iter = A.end() - 1; iter != A.begin() - 1; iter--)
            m.push_back(*iter - '0');
    else
        for (auto iter = B.end() - 1; iter != B.begin() - 1; iter--)
            m.push_back(*iter - '0');
    string C="";
    string t;
    for(size_t i=0;i

除(大数除小数)

// A / b = C , A >= 0, b > 0
string div(const string &A,int b){
   vector a, c;
    for (auto iter = A.begin(); iter != A.end(); iter++)
        a.push_back(*iter - '0');
    int r = 0;
    for (size_t i = 0; i < A.size(); i ++ )
    {
        r = r * 10 + a[i];
        c.push_back(r / b);
        r %= b;
    }
    while (c.size() > 1 && c.front() == 0) c.erase(c.begin());
    string C;
    for (auto iter = c.begin(); iter != c.end(); iter++)
        C.push_back(*iter + '0');
    return C;
}

你可能感兴趣的:(c++,算法)