C++ std::list實現大整數加法運算

CalmReason發表於2016-11-18
#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;

}



相關文章