CBigInt大整数加法
2018-06-17 23:35:19来源:未知 阅读 ()
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 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:HashTable 简述
- 重载矩阵加法运算 代码参考 2020-04-29
- 重载加法运算符的复数运算 代码参考 2020-04-29
- [题记]字符串转换整数-leetcode 2020-04-03
- L1-008 求整数段和 (10分) 2020-03-22
- 用C++实现:高精度加法 2020-03-18
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash