c++ My_task

#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
#define maxlen 10000
class HP
{
    public:
    friend ostream& operator<<(ostream &cout,const HP& x);

    public:
    HP() {(*this)=0;}
    HP(int inte) {(*this)=inte;}
    HP(const char* str) {(*this)=str;}
    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);
    int compare(const HP&b);

    int len;
    int s[maxlen];
};

ostream& operator<<(ostream& cout,const HP&x){
    for(int i=x.len;i>=1;i--)
        cout<<x.s[i];
    return cout;
}
HP HP::operator=(int inte){
    if(inte==0){
        len=1;
        s[len]=0;
        return (*this);
    }
    else{
        for(len=0;inte>0;){
            s[++len]=inte%10;
            inte/=10;
        }
        return *this;
    }
}
HP HP::operator=(const char* str){
    int len=strlen(str);
    for(int i=1;i<len;i++){
        s[i]=str[len-i]-'0';
    }
    return (*this);
}
HP HP::operator*(const HP&x){
    HP temp;
    temp.len=len+x.len;
    int i,j;
    for(i=1;i<=temp.len;i++)
        temp.s[i]=0;
    for(i=1;i<=x.len;i++)
        for(j=1;j<=len;j++){
            temp.s[i+j-1]+=x.s[i]*s[j];
        }
    for(i=1;i<x.len;i++){
        temp.s[i+1]+=temp.s[i]/10;
        temp.s[i]%=10;
    }
    while(i>1&&!temp.s[i]){
        i--;
    }
    temp.len=i;
    return temp;
}//19.3.10

HP HP::operator+(const HP&x){
    HP temp(0);
    int i=1;
    for(i=1;i<=x.len;i++)
        temp.s[i] += x.s[i];
    for(i=1;i<=len;i++)
        temp.s[i] += s[i];
    for(i=1;i<=len-1||i<=x.len-1;i++){
        temp.s[i+1]+=temp.s[i]%10;
        temp.s[i]/=10;
        len=i+1;
    }
    if(temp.s[i]>=10){
        len++;
        temp.s[i+1]=1;
        temp.s[i]/=10;
    }
    return temp;
}
//19.3.15
HP HP::operator-(const HP&x){
    int i,j=0;
    HP temp(0);
    for(i=1;i<=len;i++){
        temp.s[i]=s[i]-j;
        if(i<=x.len)
            temp.s[i]-=x.s[i];
        if(temp.s[i]<0){
            temp.s[i]+=10;
            j=1
        } 
        else
            j=0;
    }
    temp.len=len;
    while(temp.len>1&&!temp.s[temp.len]){
        temp.len--;
    }
    return temp;
}
HP HP::operator/(const HP&x){
    int i,j;
    HP temp(0),c(0);
    for(i=len;i>0;i--){
        if(!(temp.len==1&&temp.s[1]==0)){
            for(j=temp.len;j>0;j--){
                temp.s[j+1]=temp.s[j];
            }
            temp.len++;
        }
        temp.s[1]=s[i];
        c.s[i]=0;
        while((j=temp.compare(x))>=0){
            c.s[i]++;
            temp=temp-x;
            if(j==0)
                break;
        }
    }
    c.len=len;
    while((c.len>1)&&!(c.s[c.len])){
        c.len--;
    }
    return c;
}
HP HP::operator%(const HP&x){
    HP c(0),b(0);
    c=(*this)/x;
    b=(*this)-c*x;
    return b;
}
int HP::compare(const HP&x){
    if(len>x.len)
        return 1;
    else if(len<x.len)
        return -1;
    else{
        for(int i=len;i>=1;i++){
            if(s[i]==x.s[i])
                continue;
            else
                return s[i]>x.s[i]? 1:-1;
        }
        return 0;
    }
}//仅支持正数,负数需加入符号位
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值