C++ std::list实现大整数加法运算

#pragma once

#include <iostream>
#include <list>
#include <string>

using namespace std;

class CBigInt
{
public:
    CBigInt(void);
    CBigInt(const string& _str);
    CBigInt& Set(const string& _str);
    ~CBigInt(void);
    friend std::ostream& operator<<(std::ostream& os, const CBigInt& _bigInt);
    friend std::istream& operator>>(std::istream& is, CBigInt& _bigInt);
    friend CBigInt operator+(const CBigInt& lhs, const CBigInt& rhs);
    friend CBigInt operator-(const CBigInt& lhs, const CBigInt& rhs);

private:
    static short add(short& carry, const short a=0, const short b=0);
private:
    std::list<short> m_list; 
};

 

 

#include "BigInt.h"

#include <string>
#include <streambuf>
#include <iterator>

CBigInt::CBigInt(void)
{
}

CBigInt::CBigInt(const string& _str)
{
    for (auto itr = _str.cbegin(); itr != _str.cend(); ++itr)
    {
        m_list.push_back(*itr-'0');
    }
}

CBigInt& CBigInt::Set(const string& _str)
{
    m_list.clear();
    for (auto itr = _str.cbegin(); itr != _str.cend(); ++itr)
    {
        m_list.push_back(*itr-'0');
    }
    return *this;
}

CBigInt::~CBigInt(void)
{
}

std::ostream& operator<<(std::ostream& os, const CBigInt& _bigInt)
{
    for (auto itr = _bigInt.m_list.cbegin(); itr != _bigInt.m_list.cend(); ++itr)
    {
        os<<*itr;
    }
    return os;
}

std::istream& operator>>(std::istream& is, CBigInt& _bigInt)
{
    std::string str;
    std::getline(is, str);
    for (std::size_t i = 0; i< str.length(); ++i)
    {
        short s = str[i]-'0';
        _bigInt.m_list.push_back(s);
    }
    return is;
}

short CBigInt::add(short& carry, const short a, const short b)
{
    short current = (a + b + carry)%10;
    carry = (a + b + carry)/10;
    return current;
}

CBigInt operator+(const CBigInt& lhs, const CBigInt& rhs)
{
    //如果长度不同,可以补0,那种方式比较好算,但是开辟的内存较多,不划算
    CBigInt retBinInt;
    short carry = 0;
    auto itrLhs = lhs.m_list.crbegin(), itrRhs = rhs.m_list.crbegin();
    for ( ;itrLhs != lhs.m_list.crend() && itrRhs != rhs.m_list.crend(); ++itrLhs, ++itrRhs)
    {
        retBinInt.m_list.push_front(CBigInt::add(carry, *itrLhs, *itrRhs));
    }

    if (itrLhs != lhs.m_list.crend())
    {
        for (; itrLhs != lhs.m_list.crend(); ++itrLhs)
        {
            retBinInt.m_list.push_front(CBigInt::add(carry, *itrLhs));
        }
    }

    if (itrRhs != rhs.m_list.crend())
    {
        for (; itrRhs != rhs.m_list.crend(); ++itrRhs)
        {
            retBinInt.m_list.push_front(CBigInt::add(carry, *itrRhs));
        }
    }

    if (carry !=0)
    {
        retBinInt.m_list.push_front(1);
    }
        
    return retBinInt;
}

CBigInt operator-(const CBigInt& lhs, const CBigInt& rhs)
{
    return CBigInt();
}

 

 

#include <iostream>
using namespace std;
#include <vector>
#include <utility>

#include "BigInt.h"

void AddTest(const std::string& _a, const std::string& _b)
{
    CBigInt a,b;
    cout<<"a="<<a.Set(_a)<<" ";
    cout<<"b="<<b.Set(_b)<<endl;
    cout<<"a+b="<<a+b<<endl;
}

int main(int , char**)
{
    AddTest("2", "3");
    AddTest("22", "333");
    AddTest("66", "777");
    AddTest("777", "666");
    AddTest("999", "1");
    AddTest("1", "999");
    AddTest("999908", "92");
    AddTest("123456789", "987654321");
    AddTest("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999908"
        , "92");
    std::cout<<"please input a,b:\n";
    CBigInt a,b;
    std::cin>>a>>b;
    cout<<"a+b=\n"<<a+b<<endl;

    return 0;

}



 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++程序员Carea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值