大数运算模板

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4

class BigInt
{
private:
    int a[500];    //可以控制大数的位数
    int len;       //大数长度
public:
    BigInt(){ len = 1;memset(a,0,sizeof(a)); }   //构造函数
    BigInt(const int);       //将一个int类型的变量转化为大数
    BigInt(const char*);     //将一个字符串类型的变量转化为大数
    BigInt(const BigInt &);  //拷贝构造函数
    BigInt &operator=(const BigInt &);   //重载赋值运算符,大数之间进行赋值运算

    friend istream& operator>>(istream&,  BigInt&);   //重载输入运算符
    friend ostream& operator<<(ostream&,  BigInt&);   //重载输出运算符

    BigInt operator+(const BigInt &) const;   //重载加法运算符,两个大数之间的相加运算
    BigInt operator-(const BigInt &) const;   //重载减法运算符,两个大数之间的相减运算
    BigInt operator*(const BigInt &) const;   //重载乘法运算符,两个大数之间的相乘运算
    BigInt operator/(const int   &) const;    //重载除法运算符,大数对一个整数进行相除运算

    BigInt operator^(const int  &) const;    //大数的n次方运算
    int    operator%(const int  &) const;    //大数对一个int类型的变量进行取模运算
    bool   operator>(const BigInt & T)const;   //大数和另一个大数的大小比较
    bool   operator<(const BigInt & T) const;
    bool   operator==(const BigInt & T) const;
    bool   operator>(const int & t)const;      //大数和一个int类型的变量的大小比较
    bool   operator<(const int &t) const;
    bool   operator==(const int &t) const;

    void print();       //输出大数
};

bool BigInt::operator==(const BigInt & T) const {
    return !(*this > T) && !(T > *this);
}
bool BigInt::operator==(const int &t) const {
    BigInt T = BigInt(t);
    return *this == T;
}
bool BigInt::operator<(const BigInt & T) const {
    return T > *this;
}
bool BigInt::operator<(const int &t) const {
    return BigInt(t) > *this;
}
BigInt::BigInt(const int b)     //将一个int类型的变量转化为大数
{
    int c,d = b;
    len = 0;
    memset(a,0,sizeof(a));
    while(d > MAXN)
    {
        c = d - (d / (MAXN + 1)) * (MAXN + 1);
        d = d / (MAXN + 1);
        a[len++] = c;
    }
    a[len++] = d;
}
BigInt::BigInt(const char*s)     //将一个字符串类型的变量转化为大数
{
    int t,k,index,l,i;
    memset(a,0,sizeof(a));
    l=strlen(s);
    len=l/DLEN;
    if(l%DLEN)
        len++;
    index=0;
    for(i=l-1;i>=0;i-=DLEN)
    {
        t=0;
        k=i-DLEN+1;
        if(k<0)
            k=0;
        for(int j=k;j<=i;j++)
            t=t*10+s[j]-'0';
        a[index++]=t;
    }
}
BigInt::BigInt(const BigInt & T) : len(T.len)  //拷贝构造函数
{
    int i;
    memset(a,0,sizeof(a));
    for(i = 0 ; i < len ; i++)
        a[i] = T.a[i];
}
BigInt & BigInt::operator=(const BigInt & n)   //重载赋值运算符,大数之间进行赋值运算
{
    int i;
    len = n.len;
    memset(a,0,sizeof(a));
    for(i = 0 ; i < len ; i++)
        a[i] = n.a[i];
    return *this;
}
istream& operator>>(istream & in,  BigInt & b)   //重载输入运算符
{
    char ch[MAXSIZE*4];
    int i = -1;
    in>>ch;
    int l=strlen(ch);
    int count=0,sum=0;
    for(i=l-1;i>=0;)
    {
        sum = 0;
        int t=1;
        for(int j=0;j<4&&i>=0;j++,i--,t*=10)
        {
            sum+=(ch[i]-'0')*t;
        }
        b.a[count]=sum;
        count++;
    }
    b.len =count++;
    return in;

}
ostream& operator<<(ostream& out,  BigInt& b)   //重载输出运算符
{
    int i;
    cout << b.a[b.len - 1];
    for(i = b.len - 2 ; i >= 0 ; i--)
    {
        cout.width(DLEN);
        cout.fill('0');
        cout << b.a[i];
    }
    return out;
}

BigInt BigInt::operator+(const BigInt & T) const   //两个大数之间的相加运算
{
    BigInt t(*this);
    int i,big;      //位数
    big = T.len > len ? T.len : len;
    for(i = 0 ; i < big ; i++)
    {
        t.a[i] +=T.a[i];
        if(t.a[i] > MAXN)
        {
            t.a[i + 1]++;
            t.a[i] -=MAXN+1;
        }
    }
    if(t.a[big] != 0)
        t.len = big + 1;
    else
        t.len = big;
    return t;
}
BigInt BigInt::operator-(const BigInt & T) const   //两个大数之间的相减运算
{
    int i,j,big;
    bool flag;
    BigInt t1,t2;
    if(*this>T)
    {
        t1=*this;
        t2=T;
        flag=0;
    }
    else
    {
        t1=T;
        t2=*this;
        flag=1;
    }
    big=t1.len;
    for(i = 0 ; i < big ; i++)
    {
        if(t1.a[i] < t2.a[i])
        {
            j = i + 1;
            while(t1.a[j] == 0)
                j++;
            t1.a[j--]--;
            while(j > i)
                t1.a[j--] += MAXN;
            t1.a[i] += MAXN + 1 - t2.a[i];
        }
        else
            t1.a[i] -= t2.a[i];
    }
    t1.len = big;
    while(t1.a[t1.len - 1] == 0 && t1.len > 1)
    {
        t1.len--;
        big--;
    }
    if(flag)
        t1.a[big-1]=0-t1.a[big-1];
    return t1;
}

BigInt BigInt::operator*(const BigInt & T) const   //两个大数之间的相乘运算
{
    BigInt ret;
    int i,j,up;
    int temp,temp1;
    for(i = 0 ; i < len ; i++)
    {
        up = 0;
        for(j = 0 ; j < T.len ; j++)
        {
            temp = a[i] * T.a[j] + ret.a[i + j] + up;
            if(temp > MAXN)
            {
                temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
                up = temp / (MAXN + 1);
                ret.a[i + j] = temp1;
            }
            else
            {
                up = 0;
                ret.a[i + j] = temp;
            }
        }
        if(up != 0)
            ret.a[i + j] = up;
    }
    ret.len = i + j;
    while(ret.a[ret.len - 1] == 0 && ret.len > 1)
        ret.len--;
    return ret;
}
BigInt BigInt::operator/(const int & b) const   //大数对一个整数进行相除运算
{
    BigInt ret;
    int i,down = 0;
    for(i = len - 1 ; i >= 0 ; i--)
    {
        ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
        down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
    }
    ret.len = len;
    while(ret.a[ret.len - 1] == 0 && ret.len > 1)
        ret.len--;
    return ret;
}
int BigInt::operator %(const int & b) const    //大数对一个int类型的变量进行取模运算
{
    int i,d=0;
    for (i = len-1; i>=0; i--)
    {
        d = ((d * (MAXN+1))% b + a[i])% b;
    }
    return d;
}
BigInt BigInt::operator^(const int & n) const    //大数的n次方运算
{
    BigInt t,ret(1);
    int i;
    if(n<0)
        exit(-1);
    if(n==0)
        return 1;
    if(n==1)
        return *this;
    int m=n;
    while(m>1)
    {
        t=*this;
        for( i=1;i<<1<=m;i<<=1)
        {
            t=t*t;
        }
        m-=i;
        ret=ret*t;
        if(m==1)
            ret=ret*(*this);
    }
    return ret;
}
bool BigInt::operator>(const BigInt & T) const   //大数和另一个大数的大小比较
{
    int ln;
    if(len > T.len)
        return true;
    else if(len == T.len)
    {
        ln = len - 1;
        while(a[ln] == T.a[ln] && ln >= 0)
            ln--;
        if(ln >= 0 && a[ln] > T.a[ln])
            return true;
        else
            return false;
    }
    else
        return false;
}
bool BigInt::operator >(const int & t) const    //大数和一个int类型的变量的大小比较
{
    BigInt b(t);
    return *this>b;
}

void BigInt::print()    //输出大数
{
    int i;
    printf("%d", a[len-1]);
    for (int i = len-2; i >= 0; --i) {
        printf("%04d", a[i]);
    }
    puts("");
}
int main()
{
    int t;
    char a[1000], b[1000];
    BigInt ans;
    scanf("%s%s",a,b);
    BigInt A(a), B(b);

    printf("%s + %s = ", a, b);
    ans = A + B;
    ans.print();

    printf("%s - %s = ", a, b);
    ans = A - B;
    ans.print();

    scanf("%d",&t);
    printf("%s * %d = ", a, t);
    ans = A * t;
    ans.print();

    printf("%s / %d = ", a, t);
    ans = A / t;
    ans.print();

    printf("%s ^ %d = ", a, t);
    ans = A ^ t;
    ans.print();

    printf("%s %% %d = ", a, t);
    ans = A % t;
    ans.print();

    return 0;
}





下面是用string类写的模板

两个大整数相加:

/*
	大整数加法
	调用方式:add(a, b);
	返回类型:string
*/

string add(string a, string b)
{
    string s;
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    int i = 0;
    int m, k = 0;
    while(a[i] && b[i])
    {
        m = a[i] - '0' + b[i] - '0' + k;
        k = m / 10;
        s += (m % 10 + '0');
        i++;
    }
    if(i == a.size())
    {
        while(i != b.size())
        {
            m = k + b[i] - '0';
            k = m / 10;
            s += m % 10 + '0';
            i++;
        }
        if(k) s += k + '0';
    }
    else if(i == b.size())
    {
        while(i != a.size())
        {
            m = k + a[i] - '0';
            k = m / 10;
            s += m % 10 + '0';
            i++;
        }
        if(k) s += k + '0';
    }
    reverse(s.begin(), s.end());
    return s;
}

两个大整数相减:

/*
    大整数减法
*/

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
string sub(string a, string b)
{
    int i, j, k, s, flag = 1;
    int tmpa[10000], tmpb[10000], c[10000];
    string ans;
    if(a.size() < b.size() || (a.size() == b.size() && a.compare(b) < 0))
    {
        string tmp = a;
        a = b;
        b = tmp;
        flag = 0;
    }
    while(a.length() > b.length()) b = '0' + b;
    int len = a.length();
    for(i = 0; i < len; i++)
    {
        tmpa[i] = a[i] - '0';
        tmpb[i] = b[i] - '0';
    }
    for(i = len - 1; i >= 0; i--)
    {
        if(tmpa[i] >= tmpb[i])
            c[i] = tmpa[i] - tmpb[i];
        else
        {
            c[i] = 10 + tmpa[i] - tmpb[i];
            tmpa[i-1]--;
        }
    }
    for(i = 0; i < len - 1; i++)
        if(c[i] != 0)
            break;
    for(j = i; j < len; j++)
        ans = ans + (char)(c[j] + '0');
    if(!flag)
        ans = '-' + ans;
    return ans;
}

int main()
{
    string a, b;
    while(cin >> a >> b)
    {
        cout << sub(a, b) << endl;
    }
    return 0;
}

大整数乘以一个小整数:

/*
    大整数乘以小整数(可以用整型变量表示的整数)
*/
string multi(string a, int k)
{
    if(k == 0) return "0";
    int len = a.length(), carry = 0;
    reverse(a.begin(), a.end());
    for(int i = 0; i < len; i++)
    {
        int s = (a[i] - '0') * k + carry;
        a[i] = s % 10 + '0';
        carry = s / 10;
    }
    while(carry != 0)
    {
        a = a + (char)(carry % 10 + '0');
        carry /= 10;
    }
    reverse(a.begin(), a.end());
    return a;
}


两个不含前导0的大整数相乘:

/*
    两个不含前导0的大整数相乘
*/


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

string multi_string_int(string a, int k)
{
    if(k == 0) return "0";
    int len = a.length(), carry = 0;
    reverse(a.begin(), a.end());
    for(int i = 0; i < len; i++)
    {
        int s = (a[i] - '0') * k + carry;
        a[i] = s % 10 + '0';
        carry = s / 10;
    }
    while(carry != 0)
    {
        a = a + (char)(carry % 10 + '0');
        carry /= 10;
    }
    reverse(a.begin(), a.end());
    return a;
}

string add(string a, string b)
{
    string s;
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    int i = 0;
    int m, k = 0;
    while(a[i] && b[i])
    {
        m = a[i] - '0' + b[i] - '0' + k;
        k = m / 10;
        s += (m % 10 + '0');
        i++;
    }
    if(i == a.size())
    {
        while(i != b.size())
        {
            m = k + b[i] - '0';
            k = m / 10;
            s += m % 10 + '0';
            i++;
        }
        if(k) s += k + '0';
    }
    else if(i == b.size())
    {
        while(i != a.size())
        {
            m = k + a[i] - '0';
            k = m / 10;
            s += m % 10 + '0';
            i++;
        }
        if(k) s += k + '0';
    }
    reverse(s.begin(), s.end());
    return s;
}

string multi_string_string(string a, string b)
{
    string ans = "";
    for(int i = a.size() - 1; i >= 0; i--)
    {
        string tmp = multi_string_int(b, a[i] - '0');
        for(int j = 0; j < a.size() - 1 - i; j++)
            tmp += '0';
        ans = add(ans, tmp);
    }
    return ans;
}

int main()
{
    string a, b;
    while(cin >> a >> b)
    {
        cout << multi_string_string(a, b) << endl;
    }
    return 0;
}



求大整数除以一个小整数的商:

string division(string str, int x) {
    string ans = "";
    int len = str.length();
    int y = 0;
    for(int i = 0; i < len; i++) {
        ans += char((y * 10 + (str[i] - '0')) / x + '0');
        y = (y * 10 + (str[i] - '0')) % x;
    }
    while(*(ans.begin()) == '0' && ans.size() > 1) ans.erase(ans.begin());
    return ans;
}




求一个大整数对一个小整数的余数:

int Mod(string str, int x) {
    int len = str.length();
    int y = 0;
    for(int i = 0; i < len; i++)
        y = (y * 10 + (str[i] - '0')) % x;
    return y;
}


两个大数除法:

/* 大数除法 -- 高精度除高精度  */
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
#define N 2000

/*
1.  a.size < b.size 返回-1
2.  a.size == b.size && a - b < 0 返回-1
3.  a.size == b.size && a - b == 0 返回0
*/

// 判断a.size 与 b.size的关系,以及做减法
int Judge(char a[], int a1, char b[], int b1) {
    int i;
    if(a1 < b1) return -1; // a.size < b.size
    bool flag = false;
    if(a1 == b1) { //a.size == b.size && a < b
        for(i = a1 - 1; i >= 0; i--) {
            if(a[i] > b[i])
                flag = true;
            else if(a[i] < b[i]) {
                if(!flag) return -1;
            }
        }
    }
    for(i = 0; i < a1; i++) { //前提b中b1--a1部分必须为'0'
        a[i] = a[i] - b[i] + 48; //'0'的ASCII为48
        if((a[i] - '0') < 0) {
            a[i] = a[i] + 10;
            a[i+1] = a[i+1] - 1;
        }
    }
    for(i = a1 - 1; i >= 0; i--) { // 返回被除数的长度
        if(a[i] != '0')
            return (i + 1);
    }
    return 0; //a.size==b.size&&a==b的情况
}

string division(string a, string b) {
    char x1[N], x2[N];
    int ans[N];
    int a_len, b_len, i, j;
    a_len = a.length();
    b_len = b.length();

    // 初始化
    memset(x1, '0', sizeof(x1));
    memset(x2, '0', sizeof(x2));
    memset(ans, 0, sizeof(ans));

    for(i = a_len - 1, j = 0; i >= 0; i--)
        x1[j++] = a[i];
    for(i = b_len - 1, j = 0; i >= 0; i--)
        x2[j++] = b[i];

    // 分析部分
    if(a_len < b_len) return "0";
    int temp_len = Judge(x1, a_len, x2, b_len);
    if(temp_len < 0) return "0";
    if(temp_len == 0) return "1";
    ans[0]++; // 减掉1位, 商加1
    int ntimes = temp_len - b_len;
    if(ntimes < 0) return "1";

    // 扩充数位, 加快减法
    else if(ntimes > 0) {
        for(i = temp_len - 1; i >= 0; i--) {
            if(i >= ntimes)
                x2[i] = x2[i - ntimes];
            else
                x2[i] = '0';
        }
    }
    b_len = temp_len;

    // 加快除法的部分
    for(j = 0; j <= ntimes; j++) {
        int ntemp;
        while((ntemp = Judge(x1, temp_len, x2 + j, b_len - j)) >= 0) {
            temp_len = ntemp;
            ans[ntimes - j]++;
        }
    }

    for(i = 0; i < N; i++) {
        if(ans[i] >= 10) {
            ans[i+1] += ans[i] / 10;
            ans[i] %= 10;
        }
    }

    // 返回string类型
    int k = N - 1;
    string c = "";
    while(ans[k] == 0 && k > 0) k--;
    for(i = k; i >= 0; i--)
        c+= (ans[i] + '0');
    return c;
}

int main()
{
    string a, b;
    while(cin >> a >> b) {
        cout << division(a, b) << endl;
    }
    return 0;
}


你可能感兴趣的:(大数运算模板)