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
    评论
C++中,std::vector是一个动态数组容器,它可以自动调整大小以适应元素的添加和删除。当使用拷贝构造函数或赋值运算符对std::vector进行深拷贝时,会创建一个新的std::vector对象,并将原始对象的元素逐个复制到新对象中。 在拷贝构造函数中,通常会使用另一个std::vector对象作为参数,将其元素复制到新对象中。例如,可以使用以下代码进行深拷贝: Vector(const Vector<T>& v) : _start(nullptr), _finish(nullptr), _endofstorage(nullptr) { Vector<T> tmp(v.begin(), v.end()); swap(tmp); } 在赋值运算符中,也可以使用另一个std::vector对象作为参数进行深拷贝。例如,可以使用以下代码进行深拷贝: Vector<T>& operator=(Vector<T> v) { swap(v); return *this; } 这种深拷贝的方式可以确保新对象和原始对象是完全独立的,它们拥有各自的内存空间,并且对一个对象的修改不会影响到另一个对象。 除了拷贝构造函数和赋值运算符,std::vector还提供了其他构造函数,如默认构造函数和填充构造函数,它们也可以用于进行深拷贝。例如,可以使用以下代码进行填充构造: Vector(int n, const T& val = T()) : _start(nullptr), _finish(nullptr), _endofstorage(nullptr) { reserve(n); for (size_t i = 0; i < n; ++i) { push_back(val); } } 总结来说,当使用std::vector进行深拷贝时,可以使用拷贝构造函数、赋值运算符或其他构造函数来创建一个新的std::vector对象,并将原始对象的元素逐个复制到新对象中,以确保新对象和原始对象是完全独立的。 #### 引用[.reference_title] - *1* *2* *3* [[C++](11)vector的使用与模拟实现:迭代器失效,深浅拷贝详细剖析](https://blog.csdn.net/CegghnnoR/article/details/125712783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

C++程序员Carea

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

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

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

打赏作者

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

抵扣说明:

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

余额充值