大数运算:支持加减乘运算

本程序仅为演示,仅供参考,没有考虑负数情况。

优点:

1、支持任意数量的数字,直到内存不足;

2、C++运算符重载,方便使用。

不足:

1、没有实现除法运算

 1 #pragma once
 2 #include 
 3 #include 
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 class BigNum
 9 {
10 public:
11     BigNum();
12     BigNum(int num);
13     BigNum(const char* num);
14 
15     BigNum operator=(int num);
16     BigNum operator=(const char* num);
17 
18     BigNum operator+(const BigNum& num) const;
19     BigNum operator-(const BigNum& num) const;
20     BigNum operator*(const BigNum& num) const;
21 
22     friend istream& operator>>(istream &in, BigNum& num);
23     friend ostream& operator<<(ostream &out, const BigNum& num);
24 
25     int size() const;
26     int get(int index) const;
27 
28 private:
29     string str() const;
30     /************************************************************************/
31     /*移除前导0                                                             */
32     /************************************************************************/
33     void rmLeadingZero();
34 
35 private:
36     vector<int> m_vNumber;
37 };
  1 #include "BigNum.h"
  2 
  3 BigNum::BigNum()
  4 {
  5 }
  6 
  7 BigNum::BigNum(int num)
  8 {
  9     *this = num;
 10 }
 11 
 12 BigNum::BigNum(const char* num)
 13 {
 14     *this = num;
 15 }
 16 
 17 BigNum BigNum::operator=(int num)
 18 {
 19     char s[256] = { 0 };  
 20     
 21     sprintf(s, "%d", num);  
 22     *this = s;
 23 
 24     return *this;  
 25 }
 26 
 27 BigNum BigNum::operator=(const char* num)
 28 {
 29     int len = strlen(num);
 30 
 31     for(int i = 0; i < len; i++)
 32     {
 33         m_vNumber.push_back(num[len - i - 1] - '0');  
 34     }
 35 
 36     return *this;  
 37 }
 38 
 39 BigNum BigNum::operator+(const BigNum& num) const
 40 {
 41     BigNum tmp;
 42     int    carry = 0;
 43     int    maxLen = max(size(), num.size());
 44 
 45     for(int i = 0; carry || i < maxLen; i++)
 46     {  
 47         int x = carry;  
 48         
 49         if(i < size()) 
 50         {
 51             x += get(i);  
 52         }
 53 
 54         if(i < num.size())
 55         {
 56             x += num.get(i);  
 57         }
 58         
 59         carry = x / 10;  
 60 
 61         tmp.m_vNumber.push_back(x % 10);  
 62     }  
 63 
 64     return tmp;  
 65 }
 66 
 67 BigNum BigNum::operator-(const BigNum& num) const
 68 {
 69     BigNum tmp;
 70     int    carry = 0;
 71 
 72     for(int i = 0; i < size(); i++) 
 73     {  
 74         int x = get(i) - carry;
 75 
 76         if(i < num.size()) 
 77         {
 78             x -= num.get(i);  
 79         }
 80 
 81         if(x >= 0) 
 82         {
 83             carry = 0;  
 84         }
 85         else 
 86         {  
 87             carry = 1;  
 88             x += 10;  
 89         }  
 90         tmp.m_vNumber.push_back(x);
 91     }
 92 
 93     tmp.rmLeadingZero();
 94 
 95     return tmp;
 96 }
 97 
 98 BigNum BigNum::operator*(const BigNum& num) const
 99 {
100     BigNum tmp;
101 
102     for(int i = 0; i < size(); i++)
103     {
104         for(int j = 0; j < num.size(); j++)
105         {
106             int k = get(i) * num.get(j);
107 
108             if (i + j + 1 > tmp.size())
109             {
110                 tmp.m_vNumber.push_back(k);
111             }
112             else
113             {
114                 tmp.m_vNumber[i + j] += k;
115             }
116         }
117     }
118 
119     for(int i = 0; i < tmp.size() - 1; i++)
120     {  
121         tmp.m_vNumber[i+1] += tmp.m_vNumber[i] / 10;  
122         tmp.m_vNumber[i] %= 10;  
123     }  
124 
125     return tmp;
126 }
127 
128 std::string BigNum::str() const
129 {
130     string                      res = "";
131     vector<int>::const_iterator it;
132 
133     for(it = m_vNumber.begin(); it != m_vNumber.end(); it++)
134     {
135         res = (char)(*it + '0') + res;  
136     }
137 
138     return res; 
139 }
140 
141 int BigNum::size() const
142 {
143     return m_vNumber.size();
144 }
145 
146 int BigNum::get(int index) const
147 {
148     if (index >= m_vNumber.size())
149     {
150         throw new exception("range error");
151     }
152 
153     return m_vNumber[index];
154 }
155 
156 void BigNum::rmLeadingZero()
157 {
158     vector<int>::const_iterator it;
159 
160     for (int i = size() - 1; i >= 0; i--)
161     {
162         if (get(i) == 0) 
163         {
164             m_vNumber.pop_back();
165         }
166         else
167         {
168             break;
169         }
170     }
171 }
172 
173 istream& operator>>(istream &in, BigNum& num)
174 {
175     string s;
176 
177     in >> s;
178     num = s.c_str(); 
179 
180     return in;  
181 }
182 
183 ostream& operator<<(ostream &out, const BigNum& num)
184 {
185     out << num.str();
186 
187     return out;  
188 }

调用情况

 1 #include 
 2 
 3 #include "BigNum.h"
 4 
 5 using namespace std;
 6 
 7 int main(int argc, char **argv)
 8 {
 9     BigNum num1("2111119");
10     BigNum num2(1222222);
11 
12     cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
13 
14     cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
15 
16     cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
17 
18     return 0;
19 }

运算结果

大数运算:支持加减乘运算_第1张图片

参考代码:https://git.oschina.net/zhujf21st/BigNumber.git

 

你可能感兴趣的:(大数运算:支持加减乘运算)