非负的高精度整数类实现

来源:互联网 发布:网络红猫猫叔图片 编辑:程序博客网 时间:2024/05/18 04:00

在一些竞赛中,经常会使用到一些高精度的大数据类,在这里我送给大家一个高精度的整数类

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>


using namespace std;


#define MAX 2000
//非负的大整数类
class Bigint
{
public:
    int len;//长度
    int data[MAX];//存储大整数
    Bigint();
    Bigint(const char *S);
    Bigint(int num);
    //operator
    Bigint &operator =(const char *S);
    Bigint &operator =(int num);
    friend istream &operator >>(istream &in,Bigint &B);
    friend ostream &operator <<(ostream &out,Bigint &B);
    Bigint operator +(Bigint &B);
    Bigint operator +=(Bigint &B);
    Bigint operator -(Bigint &B);
Bigint operator -=(Bigint &B);
bool operator > (Bigint &B);
Bigint operator *(int n);
Bigint operator *(Bigint &B);
    //str
    string str();
    //赋值为n的阶乘
    void assignf(int n);
};
Bigint::Bigint()
{
    this->len = 0;
    memset(this->data,0,sizeof(this->data));
}
Bigint::Bigint(const char *S)
{
    this->len = strlen(S);//先给字符串长度赋值
    int i;
    for(i=0;i<len;i++)
    {
        this->data[i] = S[len-1-i]-'0';//从低位到高位赋值
    }
}
Bigint::Bigint(int num)
{
    char S[14];
    sprintf(S,"%d",num);//将整数变成字符串打印到内存中
    this->len = strlen(S);//同上
    int i;
    for(i=0;i<len;i++)
    {
        this->data[i] = S[len-1-i]-'0';
    }
}
Bigint& Bigint::operator =(const char *S)
{
    this->len = strlen(S);
    int i;
    for(i=0;i<len;i++)
    {
        this->data[i] = S[len-1-i]-'0';
    }
    return *this;
}
Bigint& Bigint::operator =(int num)
{
    char t[14];
    sprintf(t,"%d",num);
    *this = t;
    return *this;
}
//返回字符串形式的整形
string Bigint::str()
{
    string S;
    int i;
    for(i=0;i<this->len;i++)
    {
        S = (char)(this->data[i]+'0')+ S;
    }
    return S;
}
istream &operator >>(istream &in,Bigint &B)
{
    string S;
    in>>S;
    B = S.c_str();
    return in;
}
ostream &operator <<(ostream &out,Bigint &B)
{
    out<<B.str();
    return out;
}
//整数的加操作
//首先从低位开始加,如果有进位就加到上一位,直到超出证书长度并且进位==0
Bigint Bigint::operator +(Bigint &B)
{
    int maxlen = this->len>B.len?this->len:B.len;
    Bigint T;//返回的大整数类型
    T.len = 0;
    int i,j;
    int t=0;
    for(i=0,j=0;j||i<maxlen;i++)
    {
        //j是进位
        t = j;//本位
        if(i<B.len)
            t+=B.data[i];
        if(i<this->len)
            t+=this->data[i];
        T.data[T.len++] = t%10;//本位的值
        j = t / 10;//进位
    }
    return T;
}
Bigint Bigint::operator +=(Bigint &B)
{
    *this = *this+B;
    return *this;
}
//a>b的情况,这里是一个计算非负数计算的类
//就像减法运算法则一样
//低位开始相减去,如果不够的话就向高位去借
Bigint Bigint::operator -(Bigint &B)
{
    //返回的Bigint类
    Bigint T;
    T.len = 0;
    int maxlen = this->len>B.len?this->len:B.len;
    int i,j;
    for(j=0,i=0;i<maxlen||j;i++)
    {
        //j是借位
        //temp是本位
        int temp = this->data[i] - B.data[i] - j;
        if(temp>=0)
        {
            T.data[T.len++] = temp;
j=0;
        }
        else
        {
            T.data[T.len++] = temp+10;
            j = 1;
        }
    }
return T;
}
Bigint Bigint::operator -=(Bigint &B)
{
Bigint T;
T.len = 0;
int i,j;
int maxlen = this->len>B.len?this->len:B.len;
for(i=0,j=0;j||i<maxlen;i++)
{
int temp = this->data[i]-B.data[i]-j;
if(temp>=0)
{
this->data[i] = temp;
j=0;
}
else
{
this->data[i] = temp + 10;
j=1;
}
}
return *this;
}
//判断大小
//如果长度不一样的话,直接判断
//否则从高位开始比较
bool Bigint::operator > (Bigint &B)
{
int i;
if(this->len>B.len)
return true;
else if(this->len<B.len)
return false;
else
{
for(i=this->len-1;i>=0;i--)
{
if(this->data[i] != B.data[i])
return this->data[i] > B.data[i];
}
}
return false;
}
void Bigint::assignf(int n)
{
    int temp;
    int c;
    int i,j;
    memset(this->data,0,sizeof(this->data));
    this->len = 0;
    this->data[0] = 1;//0或者1的阶乘是1
    for(i=2;i<=n;i++)
    {
        //将这个整数乘以i
        c = 0;//进位
        for(j=0;j<MAX;j++)
        {
            temp = this->data[j]*i+c;
            this->data[j] =temp%10;
            c = temp/10;
        }
    }
    for(i=MAX-1;;i--)
    {
        if(this->data[i])
        {
            this->len = i+1;
            break;
        }
    }
}
Bigint Bigint::operator *(int n)
{
    int temp;
    int c;//进位
    Bigint T = *this;
    int i;
    for(i=0,c=0;i<this->len||c;i++)
    {
        temp = T.data[i]*n+c;
        T.data[i] = temp%10;
        c = temp/10;
    }
    for(i=MAX-1;;i--)
    {
        if(T.data[i])
        {
            T.len = i+1;
            break;
        }
    }
    return T;
}
Bigint Bigint::operator *(Bigint &B)
{
    int i,j;
    Bigint T;
    Bigint R;
    for(i=0;i<B.len;i++)
    {
        T = ((*this) * (B.data[i]));
        R+=T;
    }
    return T;
}
int main()
{
    Bigint B2 = "2";
    Bigint B3;
    B3.assignf(4);
    B3 = B3*B2;
cout<<B3.str();
    return 0;
}

0 0
原创粉丝点击