首页 > 代码库 > CBigInt大整数加法

CBigInt大整数加法

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <list>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 class CBigInt
10 {
11 public:
12     CBigInt(void);
13     CBigInt(const string& _str);
14     CBigInt& Set(const string& _str);
15     ~CBigInt(void);
16     friend std::ostream& operator<<(std::ostream& os, const CBigInt& _bigInt);
17     friend std::istream& operator>>(std::istream& is, CBigInt& _bigInt);
18     friend CBigInt operator+(const CBigInt& lhs, const CBigInt& rhs);
19     friend CBigInt operator-(const CBigInt& lhs, const CBigInt& rhs);
20 
21 private:
22     static short add(short& carry, const short a=0, const short b=0);
23 private:
24     std::list<short> m_list; 
25 };
 1 #include "BigInt.h"
 2 
 3 #include <string>
 4 #include <streambuf>
 5 #include <iterator>
 6 
 7 CBigInt::CBigInt(void)
 8 {
 9 }
10 
11 CBigInt::CBigInt(const string& _str)
12 {
13     for (auto itr = _str.cbegin(); itr != _str.cend(); ++itr)
14     {
15         m_list.push_back(*itr-0);
16     }
17 }
18 
19 CBigInt& CBigInt::Set(const string& _str)
20 {
21     m_list.clear();
22     for (auto itr = _str.cbegin(); itr != _str.cend(); ++itr)
23     {
24         m_list.push_back(*itr-0);
25     }
26     return *this;
27 }
28 
29 CBigInt::~CBigInt(void)
30 {
31 }
32 
33 std::ostream& operator<<(std::ostream& os, const CBigInt& _bigInt)
34 {
35     for (auto itr = _bigInt.m_list.cbegin(); itr != _bigInt.m_list.cend(); ++itr)
36     {
37         os<<*itr;
38     }
39     return os;
40 }
41 
42 std::istream& operator>>(std::istream& is, CBigInt& _bigInt)
43 {
44     std::string str;
45     std::getline(is, str);
46     for (std::size_t i = 0; i< str.length(); ++i)
47     {
48         short s = str[i]-0;
49         _bigInt.m_list.push_back(s);
50     }
51     return is;
52 }
53 
54 short CBigInt::add(short& carry, const short a, const short b)
55 {
56     short current = (a + b + carry)%10;
57     carry = (a + b + carry)/10;
58     return current;
59 }
60 
61 CBigInt operator+(const CBigInt& lhs, const CBigInt& rhs)
62 {
63     //如果长度不同,可以补0,那种方式比较好算,但是开辟的内存较多,不划算
64     CBigInt retBinInt;
65     short carry = 0;
66     auto itrLhs = lhs.m_list.crbegin(), itrRhs = rhs.m_list.crbegin();
67     for ( ;itrLhs != lhs.m_list.crend() && itrRhs != rhs.m_list.crend(); ++itrLhs, ++itrRhs)
68     {
69         retBinInt.m_list.push_front(CBigInt::add(carry, *itrLhs, *itrRhs));
70     }
71 
72     if (itrLhs != lhs.m_list.crend())
73     {
74         for (; itrLhs != lhs.m_list.crend(); ++itrLhs)
75         {
76             retBinInt.m_list.push_front(CBigInt::add(carry, *itrLhs));
77         }
78     }
79 
80     if (itrRhs != rhs.m_list.crend())
81     {
82         for (; itrRhs != rhs.m_list.crend(); ++itrRhs)
83         {
84             retBinInt.m_list.push_front(CBigInt::add(carry, *itrRhs));
85         }
86     }
87 
88     if (carry !=0)
89     {
90         retBinInt.m_list.push_front(1);
91     }
92         
93     return retBinInt;
94 }
95 
96 CBigInt operator-(const CBigInt& lhs, const CBigInt& rhs)
97 {
98     return CBigInt();
99 }
 1 #include <iostream>
 2 using namespace std;
 3 #include <vector>
 4 #include <utility>
 5 
 6 #include "BigInt.h"
 7 
 8 void AddTest(const std::string& _a, const std::string& _b)
 9 {
10     CBigInt a,b;
11     cout<<"a="<<a.Set(_a)<<" ";
12     cout<<"b="<<b.Set(_b)<<endl;
13     cout<<"a+b="<<a+b<<endl;
14 }
15 
16 int main(int , char**)
17 {
18     AddTest("2", "3");
19     AddTest("22", "333");
20     AddTest("66", "777");
21     AddTest("777", "666");
22     AddTest("999", "1");
23     AddTest("1", "999");
24     AddTest("999908", "92");
25     AddTest("123456789", "987654321");
26     AddTest("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999908"
27         , "92");
28     std::cout<<"please input a,b:\n";
29     CBigInt a,b;
30     std::cin>>a>>b;
31     cout<<"a+b=\n"<<a+b<<endl;
32 
33     return 0;
34 
35 }

 技术分享

CBigInt大整数加法