大整数加减乘除模板

模板只适用于正整数

大整数乘法:

an an-1 ... a1 a0和

bm bm-1 ... b1 b0相乘。

则对应结果的第k位就应该是a[i] * a[j] (i + j = k,  0 <= i,j <= k)

不考虑进位的情况下。

所以最后只要从低位往高位进位就解决了。

除法:模拟除法

加法:对应位相加,进位相加

减法:转换成大数减小数,借位相减

参考至代号4101

代码:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <climits>
using namespace std;
class bign
{
private:
    static const int maxn = 5000;
    int op[maxn];
    int len;//数长
    void clean()
    {
        while (len > 0 && !op[len])//去除前导0
            len--;
        len++;
    }

    void sum(bign &a, const bign &b)//大数减小数,结果在a中
    {
        for (int i = 0; i < a.len; i++)
        {
            a.op[i] -= b.op[i];
            if (a.op[i] < 0)
            {
                a.op[i] += 10;
                a.op[i + 1]--;
            }
        }
        a.clean();
    }
public:
    bign(const string &s)
    {
        *this = s;
    }

    bign()
    {
        memset(op, 0, sizeof(op));
        len = 1;//默认为0
    }

    bign(int num)
    {
        *this = num;
    }

    bign operator = (int num)
    {
        char s[20]; sprintf(s, "%d", num);
        *this = s;
        return *this;
    }

    bign operator = (const string &s)
    {
        memset(op, 0, sizeof(op));
        len = s.length();
        for (int i = 0, j = len - 1; j >= 0; j--, i++)
            op[i] = s[j] - '0';
        clean();
        return *this;
    }

    bign operator + (const bign &b)
    {
        bign c = *this;
        int i;
        c.len = max(c.len, b.len);//取长度高的
        for (i = 0; i < c.len; i++)//对应位相加
        {
            c.op[i] += b.op[i];
            if (c.op[i] > 9)//处理进位
            {
                c.op[i + 1]++;
                c.op[i] -= 10;
            }
        }
        c.clean();
        return c;
    }

    bign operator - (const bign &b)
    {
        if (*this < b)
        {
            bign c = b;
            sum(c, *this);
            return -c;
        }
        bign c = *this;
        sum(c, b);
        c.clean();
        return c;
    }

    bign operator * (const bign &b)const
    {
        int i, j;
        bign c;
        for (i = 0; i < this->len; i++)
            for (j = 0; j < b.len; j++)
            {
                c.op[i + j] += this->op[i] * b.op[j];
            }
        c.len = this->len + b.len;//长度最多为两数长度之和
        for (i = 0; i < c.len; i++)
        {
            c.op[i + 1] += (c.op[i] / 10);
            c.op[i] %= 10;
        }
        c.clean();
        return c;
    }

    bign operator / (const bign &b)//模拟除法
    {
        int i, j;
        bign c = *this, a;
        for (i = c.len - 1; i >= 0; i--)
        {
            a = a * 10 + c.op[i];
            for (j = 1; j < 10; j++)//a是b的多少倍
                if (a < b * j)
                    break;
            j--;
            a = a - b * j;//余数
            c.op[i] = j;//对应位的倍数
        }
        c.clean();
        return c;
    }

    bign operator % (const bign &b)
    {
        int i, j;
        bign c = *this, a;
        for (i = c.len - 1; i >= 0; i--)
        {
            a = a * 10 + c.op[i];
            for (j = 1; j < 10; j++)//a是b的多少倍
                if (a < b * j)
                    break;
            j--;
            a = a - b * j;//余数
        }
        return a;
    }

    bign operator ^ (int n)
    {
        bign c = 1;
        bign d = *this;
        while (n)
        {
            if (n & 1)
                c = c * d;
            d = d * d;
            n >>= 1;
        }
        return c;
    }

    bign operator - ()//取反
    {
        bign c = *this;
        c.op[len - 1] = -c.op[len - 1];//高位取反
        return c;
    }

    bool operator < (const bign &b)
    {
        if (len == b.len)
        {
            for (int i = len - 1; i >= 0; i--)
            {
                if (op[i] != b.op[i])
                    return op[i] < b.op[i];
            }
        }
        return len < b.len;
    }
    friend ostream &operator << (ostream &out, const bign &res)
    {
        for (int i = res.len - 1; i >= 0; i--)
            out<<res.op[i];
        //out<<endl;
        return out;
    }

    friend istream &operator >> (istream &in, bign &res)
    {
        string s;
        in>>s;
        res = s;
        return in;
    }

    string to_str()
    {
        string s;
        for (int i = 0; i < len; i++)
            s += (op[i] + '0');
        return s;
    }

    int length()
    {
        return len;
    }
};

int main()
{
    bign a(9998);
    bign b(11);
    bign c =  a % b;
    cout<<c<<endl;
    return 0;
}
View Code

 

 

 

 

posted on 2014-04-17 00:53  jec  阅读(330)  评论(1编辑  收藏  举报

导航