-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigInteger.cpp
178 lines (140 loc) · 3.44 KB
/
BigInteger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "BigInteger.h"
#include <iostream>
using namespace std;
static void Add(int n, int m, int &carry, int &result);
static vector<int> Add(const vector<int> &vn, const vector<int> &vm);
BigInteger::BigInteger()
{
}
BigInteger::BigInteger(const string &number)
{
// Convert digits to integers and enter them into the vector
int size = (int) number.size();
for(int i = size - 1; i >= 0 ; i--)
m_number.push_back((int)number[i] - 48);
}
string BigInteger::ToString() const
{
vector<int>::const_reverse_iterator i = m_number.rbegin();
string number = "";
while(i != m_number.rend())
{
number += (char)(*i + 48);
i++;
}
return number;
}
int BigInteger::Digit(int index)
{
return m_number[index];
}
vector<int> BigInteger::GetNumber()
{
return m_number;
}
void BigInteger::Add(BigInteger &n)
{
vector<int> add = n.GetNumber();
m_number = ::Add(m_number, add);
}
void BigInteger::Multiply(BigInteger &n)
{
BigInteger current = *this;
BigInteger i("1");
BigInteger one("1");
while (i < n)
{
this->Add(current);
i.Add(one);
}
}
void BigInteger::Multiply(int n)
{
BigInteger current = *this;
for (int i = 1; i < n; i++)
this->Add(current);
}
void BigInteger::Pow(int n)
{
BigInteger current = *this;
for (int i = 1; i < n; i++)
this->Multiply(current);
}
static void Add(int n, int m, int &carry, int &result)
{
result = n + m + carry;
carry = result / 10;
if(carry)
result %= 10;
}
static vector<int> Add(const vector<int> &vn, const vector<int> &vm)
{
vector<int> r;
int n, m;
int column = 0;
int result = 0;
int carry = 0;
int vnSize = (int) vn.size();
int vmSize = (int) vm.size();
while (true)
{
n = column < vnSize ? (int) vn[column] : 0;
m = column < vmSize ? (int) vm[column] : 0;
if(n == 0 && m == 0 && carry == 0 && column >= vnSize && column >= vmSize) break;
Add(n, m, carry, result);
r.push_back(result);
column++;
}
return r;
}
bool operator <(const BigInteger &a, const BigInteger &b) {
if (a.m_number.size() < b.m_number.size()) {
return true;
}
if (a.m_number.size() > b.m_number.size()) {
return false;
}
for (int i = ((int)(a.m_number.size())) - 1; i >= 0; i--)
{
if (a.m_number[i] == b.m_number[i]) continue;
if (a.m_number[i] < b.m_number[i])
return true;
else
return false;
}
return false;
}
std::ostream& operator<< (std::ostream& os, const BigInteger& i)
{
os << i.ToString();
return os;
}
void BigIntegerTest()
{
cout << "Testing BigInteger class" << endl;
int n = 1, m = 2, carry = 0, result = 0;
Add(n, m, carry, result);
cout << (result == 3);
cout << (carry == 0);
n = 5, m = 5, carry = 0, result = 0;
Add(n, m, carry, result);
cout << (result == 0);
cout << (carry == 1);
n = 9, m = 9, carry = 0, result = 0;
Add(n, m, carry, result);
cout << (result == 8);
cout << (carry == 1);
BigInteger a("123456789123456789123456789");
cout << (a.ToString() == "123456789123456789123456789");
BigInteger b("15");
BigInteger c("14");
b.Add(c);
cout << (b.ToString() == "29");
BigInteger d("55");
BigInteger e("45");
d.Add(e);
cout << (d.ToString() == "100");
a.Add(a);
cout << (a.ToString() == "246913578246913578246913578");
cout << endl;
}