P1601一道高精度的题

#include <iostream> 
#include <cstring>    // use strlen
#include <string> 
using namespace std; 
const int Max = 5e2+10; 

class st {
private: 
    char num[Max]; 
    int n; 
public:
    st();
    void Reset(char * a, int len);
    st operator+(const st & b); 
    friend ostream & operator<<(ostream & os, st ans); 
};

st::st() { 
    n = 0;
    for (int i = 0; i < Max; ++i) 
        num[i] = '\0';
}

void st::Reset(char * a, int len) {
    for (int i = 0; i < len; ++i) 
        num[len-i-1] = a[i]; 
    n = len;     
}

st st::operator+(const st & b) {
    st c;
    int i;
    int getin = 0;
    for (i = 0; num[i] || b.num[i]; ++i) {
        int a1 = 0, b1 = 0; 
        if (i < strlen(num)) a1 = num[i] - '0';
        if (i < strlen(b.num)) b1 = b.num[i] - '0';
        getin += a1 + b1;
        c.num[i] = getin % 10 + '0';
        getin /= 10;
    }
    if (getin) 
        c.num[i++] = getin + '0'; 
    c.n = i; 
    return c;     
}

ostream & operator<<(ostream & os, st ans) {
    for (int i = ans.n-1; i >= 0; --i) 
        os << ans.num[i]; 
    return os;     
}

int main() { 
     char a[Max], b[Max];
    st A, B; 
    cin >> a >> b; 
    A.Reset(a, strlen(a)); 
    B.Reset(b, strlen(b));  
    cout << A+B << endl;  
    return 0;
}

 https://www.luogu.org/problemnew/show/P1601

转载于:https://www.cnblogs.com/yifeiWa/p/10713202.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值