高精度模板

本文介绍了一种使用C++实现高精度算术运算的方法,包括加、减、乘、除等基本运算,并提供了一个完整的类实现示例,能够处理大整数运算,避免了传统数据类型在大数值计算中的溢出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

High Precision in C Plus Plus

不排除有bug的可能

#include<iostream>
#include<cstring>

using namespace std;

const int maxlen=1e4;

class HP {
public:
    int len,s[maxlen]; 
    HP(){(*this)=0;};
    HP(int inte){(*this)=inte;};
    HP(const char *str){(*this)=str;};
    friend ostream& operator<<(ostream &cout ,const HP &x);
    HP operator=(int inte);
    HP operator=(const char *str);
    HP operator=(const HP &b);
    HP operator*(const HP &b);
    HP operator+(const HP &b);
    HP operator-(const HP &b);
    HP operator/(const HP &b);
    HP operator%(const HP &b);
    int Compare(const HP &b);
};

ostream & operator<<(ostream &cout,const HP &x){
    for(int i=x.len;i>=1;i--) cout<<x.s[i];
    return cout;
}
istream& operator>>(istream& in, HP& x) {
    string s; in>>s;x=s.c_str();return in;
}

HP HP::operator =(const char *str){
    len=strlen(str);
    for(int i=1;i<=len;i++) s[i]=str[len-i]-'0';
    return *this;
}

HP HP::operator =(int inte){
    if(inte==0){len=1,s[1]=0;return (*this);}
    for(len=0;inte>0;) s[++len]=inte%10,inte/=10;
    return (*this);
}

HP HP::operator =(const HP &b){
    len=b.len; for(int i=0;i<=len;i++) s[i]=b.s[i];
    return (*this);
}

HP HP::operator *(const HP &b){
    int i,j;HP c;c.len=len+b.len;
    for(i=1;i<=c.len;i++) c.s[i]=0;
    for(i=1;i<=len;i++) for(j=1;j<=b.len;j++) c.s[i+j-1]+=s[i]*b.s[j];
    for(i=1;i<c.len;i++) c.s[i+1]+=c.s[i]/10,c.s[i]%=10;
    while(c.s[i]) c.s[i+1]+=c.s[i]/10,c.s[i]%=10,i++;
    while(i>1&&!c.s[i]) i--,c.len=i;
    return c;
}

HP HP::operator +(const HP &b){
    int i;HP c;c.s[1]=0;
    for(i=1;i<=len||i<=b.len||c.s[i];i++){
        if(i<=len) c.s[i]+=s[i];
        if(i<=b.len) c.s[i]+=b.s[i];
        c.s[i+1]=c.s[i]/10;
        c.s[i]%=10;
    }
    c.len=i-1;if(c.len==0) c.len=1;
    return c;
}

HP HP::operator -(const HP &b){
    int i,j;HP c;
    for(i=1,j=0;i<=len;i++){
        c.s[i]=s[i]-j; if(i<=b.len) c.s[i]-=b.s[i];
        if(c.s[i]<0) j=1,c.s[i]+=10; else j=0;
    }
    c.len=len; while(c.len>1&&!c.s[c.len]) c.len--;
    return c;
}

int HP::Compare(const HP &y){
    if(len>y.len) return 1;
    if(len<y.len) return -1;
    int i=len;
    while((i>1)&&(s[i]==y.s[i])) i--;
    return s[i]-y.s[i];
}

HP HP::operator /(const HP &b){
    int i,j;HP d(0),c;
    for(i=len;i>0;i--){
        if(!(d.len==1&&d.s[1]==0)) for(j=d.len;j>0;j--) d.s[j+1]=d.s[j],++d.len;
        d.s[1]=s[i]; c.s[i]=0;
        while((j=d.Compare(b))>=0){
            d=d-b;c.s[i]++;
            if(j==0) break;
        }
    }
    c.len=len; while((c.len>1)&&(c.s[c.len]==0)) c.len--;
    return c;
}

HP HP::operator %(const HP &b){
    int i,j; HP d(0);
    for(i=len;i>0;i--){
        if(!(d.len==1&&d.s[1]==0)) for(j=d.len;j>0;j--) d.s[j+1]=d.s[j],++d.len;
        d.s[1]=s[i];
        while((j=d.Compare(b))>=0){d=d-b;if(j==0) break;}
    }
    return d;
}

int main()
{
    HP a,b,c;char op;
    while(cin>>a>>op>>b){
        switch(op){
            case '+': c=a+b; break;
            case '-': c=a-b; break;
            case '*': c=a*b; break;
            case '/': c=a/b; break;
            case '%': c=a%b; break;
        }
        cout<<c<<'\n';
    }
    return 0;
}

看到关于高精度的不错的一篇文章
http://blog.youkuaiyun.com/code4101/article/details/23020525

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值