-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhp_uint_manip.cpp
166 lines (131 loc) · 2.98 KB
/
hp_uint_manip.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
#include "hp_uint.h"
using namespace std;
namespace hpa
{
hp_uint::hp_uint()
{
base = 10;
digits.push_back(0);
}
hp_uint::hp_uint(string s)
{
base = 10;
this->from_string(s);
}
void hp_uint::set_base(usint b)
{
if (2 <= b && b <= 36)
base = b;
}
void hp_uint::from_string(string s)
{
// Clear current digits
digits.clear();
// Ingnore trailing zeroes
int i = 0;
while (i < s.size() && s[i] == '0')
i++;
if (i == s.size()) {
// The number is zero
digits.push_back(0);
return;
}
// Process the string
for (i; i < s.size(); i++) {
usint digit;
if ('0' <= s[i] && s[i] <= '9')
digit = s[i] - '0';
else
digit = s[i] - 'a' + 10;
ulint carry = 0, tmp;
// Multiply the current number by 'base'
digits.push_back(0);
for (int j = 0; j < digits.size(); j++) {
tmp = (ulint) digits[j];
tmp = tmp * ((ulint) base) + carry;
digits[j] = (usint) tmp;
carry = (tmp >> ((ulint) (sizeof(usint) * 8)));
}
// To the current number add 'digit'
carry = 0;
digits.push_back(0);
tmp = (ulint) digits[0];
tmp += (ulint) digit;
digits[0] = (usint) tmp;
carry = (tmp >> ((ulint) (sizeof(usint) * 8)));
int j = 1;
while (carry > 0) {
tmp = (ulint) digits[j];
tmp += carry;
digits[j] = (usint) tmp;
carry = (tmp >> ((ulint) (sizeof(usint) * 8)));
j++;
}
// Clear leading zeroes
for (int k = digits.size() - 1; digits[k] == 0; k--)
digits.pop_back();
}
}
string hp_uint::to_string()
{
if (digits[0] == 0 && digits.size() == 1)
return "0";
string rs;
vector<usint> digits_copy = digits;
// Process the number
while (!(digits_copy[0] == 0 && digits_copy.size() == 1)) {
ulint carry = 0;
// New digit will be stored in carry
for (int i = digits_copy.size() - 1; i >= 0; i--) {
ulint tmp = (carry * ((ulint) USINT_MAX + 1)) + ((ulint) digits_copy[i]);
digits_copy[i] = (usint) (tmp / base);
carry = (ulint) (tmp % base);
}
// Pop the created zeros
for (int k = digits_copy.size() - 1; digits_copy[k] == 0 && k > 0; k--)
digits_copy.pop_back();
usint digit = (usint) carry;
// Append the'/usr/share/applications/skype.desktop' digit to the string
if (0 <= digit && digit <= 9)
rs.push_back('0' + digit);
else
rs.push_back('a' + digit - 10);
}
// Return it reversed
return string(rs.rbegin(), rs.rend());
}
ostream& operator<<(ostream& os, const hp_uint& rhs)
{
string s;
s = rhs.to_string();
os << s;
return os;
}
istream& operator>>(istream& is, hp_uint& rhs)
{
string s;
is >> s;
rhs.from_string(s);
return is;
}
hp_uint& hp_uint::operator=(const hp_uint& rhs)
{
if (this != &rhs)
digits = rhs.digits;
return *this;
}
bool hp_uint::is_even()
{
if (digits[0] % 2 == 0)
return true;
else
return false;
}
bool hp_uint::is_zero()
{
if (digits.size() == 1 && digits[0] == 0)
return true;
else
return false;
}
}