无聊的大数

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
// 将整数转换为string
string turn_str(int num)
{
    string str = "";
    int flag = bool(num < 0);
    if (flag)num = -num;
    while (num / 10)
    {
        str = char(num % 10 + 48) + str;
        num /= 10;
    }
    str = char(num + 48) + str;
    if (flag)str = "-" + str;
    return str;
}
// 将string转换为整数
int turn_int(string num)
{
    int ret = 0;
    int flag = (num[0] == '-');
    for (int i = flag; i < num.size(); i++)ret += int(num[i] - 48) * pow(10, num.size() - i - 1);
    if (flag)ret = -ret;
    return ret;
}

// 为两个字符串添加前导零,使得这两个字符串长度相等
void add_z(string& lhs, string& rhs)
{
    int num = (((lhs.size() >= rhs.size()) ? (lhs.size() - rhs.size()) : (rhs.size() - lhs.size())));//Landry's torment 求lhs 和 rhs 中较长长度
    for (int i = 0; i < num; i++)
    {
        if (lhs.size() < rhs.size())lhs = '0' + lhs;
        else rhs = '0' + rhs;
    }
}

// 去除str中的前导零
void minus_z(string& str)
{
    int i = 0;
    if (str[0] != '0')return;
    while (str[i++] == '0');
    str = str.substr(i - 1, str.size() - i + 1);
}
class Bignums
{
private:
    string m_data;
    int m_lenth;
public:
    Bignums();
    Bignums(int num);
    Bignums(string num);
    friend ostream& operator<<(ostream& om, Bignums bm);
    Bignums operator+(Bignums& ahs);
    Bignums operator-(Bignums& ahs);
    string& get_data() { return m_data; };
};


// 一些构造函数
Bignums::Bignums() { m_data = "0"; m_lenth = 1; };
Bignums::Bignums(int num)
{
    m_data = turn_str(num);
    m_lenth = m_data.size();
}
Bignums::Bignums(string num)
{
    m_data = num;
    m_lenth = m_data.size();
}

ostream& operator<<(ostream& om, Bignums bm)
{
    cout << bm.m_data;
    return om;
}

// 核心逻辑
Bignums Bignums::operator+(Bignums& ahs)
{
    int len = ((m_data.size() >= ahs.m_data.size()) ? (m_data.size()) : (ahs.m_data.size())); // 求较大长度,以便后续开数组
    int* count = new int[len + 2]; // 进位数组。数组长度是两者较大长度加二
    count[len+1] = 0;
    count[1] = 0;
    int tem;
    string str = "";
    add_z((*this).get_data(), ahs.get_data());
    for (int i = len-1; i >= 0; --i)
    {
        tem = ((*this).get_data()[i] - 48) + (ahs.get_data()[i] - 48) + (count[i+2]);// 加上进位数组存储的数字。
        count[i+1] = tem / 10; // 处理进位 
        tem %= 10; // tem当然不能大于10
        str = char(tem + 48)+str; // 把tem接到答案前面去
    }
    if ((count[1]))str = char(count[1] + 48) + str;
    Bignums temp;
    temp.m_data = str;
    temp.m_lenth = str.size();
    return temp;
}
int main()
{
    string a = "111";
    string b = "111111";
    add_z(a, b);
    cout << a << endl << b;
    minus_z(a);
    cout << endl << a << endl;
    cout << "//" << endl;
    Bignums ba{ "35463546543635465436543635435466354" };
    Bignums bb{ "5349058349083549054354654363546"};
    cout << "ba = " << ba << " bb = " << bb << endl;
    cout << "ba+bb = " << ba + bb;
    return 0;
}



无聊 不如去看《深入理解计算机系统》

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值