TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
large_number Class Reference

#include <large_number.h>

Collaboration diagram for large_number:
[legend]

Public Member Functions

 large_number ()
 
 large_number (int n)
 
 large_number (const large_number &a)
 
 large_number (std::vector< unsigned char > &vec)
 
 large_number (char const *number_str)
 
void add_digit (unsigned int value)
 
size_t num_digits () const
 
unsigned char & operator[] (size_t n)
 
const unsigned char & operator[] (size_t n) const
 
large_numberoperator++ ()
 
large_numberoperator++ (int)
 
large_numberoperator+= (large_number n)
 
large_numberoperator+= (int n)
 
large_numberoperator= (const large_number &b)
 
template<class T >
large_numberoperator*= (const T n)
 
char digit_char (size_t i) const
 

Static Public Member Functions

static bool test ()
 

Private Member Functions

template<class T >
void multiply (const T n)
 

Private Attributes

std::vector< unsigned char > _digits
 

Friends

std::ostream & operator<< (std::ostream &out, const large_number &a)
 
bool operator== (large_number const &a, large_number const &b)
 
bool operator!= (large_number const &a, large_number const &b)
 
template<class T >
large_numberoperator+ (const large_number &a, const T &b)
 

Detailed Description

Store large unsigned numbers as a C++ vector The class provides convenience functions to add a digit to the number, perform multiplication of large number with long unsigned integers.

Definition at line 24 of file large_number.h.

Constructor & Destructor Documentation

◆ large_number() [1/5]

large_number::large_number ( )
inline

< initializer with value = 1

Definition at line 27 of file large_number.h.

27{ _digits.push_back(1); }
std::vector< unsigned char > _digits

◆ large_number() [2/5]

large_number::large_number ( int n)
inlineexplicit

< initializer from an integer < initializer from an integer initializer from another large_number

Definition at line 39 of file large_number.h.

◆ large_number() [3/5]

large_number::large_number ( const large_number & a)
inline

initializer from a vector

Definition at line 48 of file large_number.h.

◆ large_number() [4/5]

large_number::large_number ( std::vector< unsigned char > & vec)
inlineexplicit

initializer from a string

Definition at line 51 of file large_number.h.

◆ large_number() [5/5]

large_number::large_number ( char const * number_str)
inlineexplicit

Definition at line 54 of file large_number.h.

54 {
55 for (size_t i = strlen(number_str); i > 0; i--) {
56 char a = number_str[i - 1] - '0';
57 if (a >= 0 && a <= 9)
58 _digits.push_back(a);
59 }
60 }

Member Function Documentation

◆ add_digit()

void large_number::add_digit ( unsigned int value)
inline

add a digit at MSB to the large number

Definition at line 118 of file large_number.h.

118 {
119 if (value > 9) {
120 std::cerr << "digit > 9!!\n";
121 exit(EXIT_FAILURE);
122 }
123
124 _digits.push_back(value);
125 }

◆ digit_char()

char large_number::digit_char ( size_t i) const
inline

returns i^th digit as an ASCII character

Definition at line 248 of file large_number.h.

248 {
249 return _digits[num_digits() - i - 1] + '0';
250 }
size_t num_digits() const

◆ multiply()

template<class T >
void large_number::multiply ( const T n)
inlineprivate

multiply large number with another integer and store the result in the same large number

Definition at line 258 of file large_number.h.

258 {
259 static_assert(std::is_integral<T>::value,
260 "Can only have integer types.");
261 // assert(!(std::is_signed<T>::value)); //, "Implemented only for
262 // unsigned integer types.");
263
264 size_t i;
265 uint64_t carry = 0, temp;
266 for (i = 0; i < this->num_digits(); i++) {
267 temp = static_cast<uint64_t>((*this)[i]) * n;
268 temp += carry;
269 if (temp < 10) {
270 carry = 0;
271 } else {
272 carry = temp / 10;
273 temp = temp % 10;
274 }
275 (*this)[i] = temp;
276 }
277
278 while (carry != 0) {
279 this->add_digit(carry % 10);
280 carry /= 10;
281 }
282 }
void add_digit(unsigned int value)

◆ num_digits()

size_t large_number::num_digits ( ) const
inline

Get number of digits in the number

Definition at line 130 of file large_number.h.

130{ return _digits.size(); }

◆ operator*=()

template<class T >
large_number & large_number::operator*= ( const T n)
inline

operator overload to increment

Definition at line 238 of file large_number.h.

238 {
239 static_assert(std::is_integral<T>::value,
240 "Must be integer addition unsigned integer types.");
241 this->multiply(n);
242 return *this;
243 }
void multiply(const T n)

◆ operator++() [1/2]

large_number & large_number::operator++ ( )
inline

operator overload to increment (prefix)

Definition at line 175 of file large_number.h.

175 {
176 (*this) += 1;
177 return *this;
178 }

◆ operator++() [2/2]

large_number & large_number::operator++ ( int )
inline

operator overload to increment (postfix)

Definition at line 183 of file large_number.h.

183 {
184 static large_number tmp(_digits);
185 ++(*this);
186 return tmp;
187 }

◆ operator+=() [1/2]

large_number & large_number::operator+= ( int n)
inline

Definition at line 212 of file large_number.h.

212{ return (*this) += large_number(n); }

◆ operator+=() [2/2]

large_number & large_number::operator+= ( large_number n)
inline

operator overload to add

Definition at line 192 of file large_number.h.

192 {
193 // if adding with another large_number
194 large_number *b = reinterpret_cast<large_number *>(&n);
195 const size_t max_L = std::max(this->num_digits(), b->num_digits());
196 unsigned int carry = 0;
197 size_t i;
198 for (i = 0; i < max_L || carry != 0; i++) {
199 if (i < b->num_digits())
200 carry += (*b)[i];
201 if (i < this->num_digits())
202 carry += (*this)[i];
203 if (i < this->num_digits())
204 (*this)[i] = carry % 10;
205 else
206 this->add_digit(carry % 10);
207 carry /= 10;
208 }
209 return *this;
210 }

◆ operator=()

large_number & large_number::operator= ( const large_number & b)
inline

assignment operator

Definition at line 229 of file large_number.h.

229 {
230 this->_digits = b._digits;
231 return *this;
232 }

◆ operator[]() [1/2]

unsigned char & large_number::operator[] ( size_t n)
inline

operator over load to access the i^th digit conveniently and also assign value to it

Definition at line 137 of file large_number.h.

137{ return this->_digits[n]; }

◆ operator[]() [2/2]

const unsigned char & large_number::operator[] ( size_t n) const
inline

Definition at line 139 of file large_number.h.

139 {
140 return this->_digits[n];
141 }

◆ test()

static bool large_number::test ( )
inlinestatic

Function to check implementation

Definition at line 65 of file large_number.h.

65 {
66 std::cout << "------ Checking `large_number` class implementations\t"
67 << std::endl;
68 large_number a(40);
69 // 1. test multiplication
70 a *= 10;
71 if (a != large_number(400)) {
72 std::cerr << "\tFailed 1/6 (" << a << "!=400)" << std::endl;
73 return false;
74 }
75 std::cout << "\tPassed 1/6...";
76 // 2. test compound addition with integer
77 a += 120;
78 if (a != large_number(520)) {
79 std::cerr << "\tFailed 2/6 (" << a << "!=520)" << std::endl;
80 return false;
81 }
82 std::cout << "\tPassed 2/6...";
83 // 3. test compound multiplication again
84 a *= 10;
85 if (a != large_number(5200)) {
86 std::cerr << "\tFailed 3/6 (" << a << "!=5200)" << std::endl;
87 return false;
88 }
89 std::cout << "\tPassed 3/6...";
90 // 4. test increment (prefix)
91 ++a;
92 if (a != large_number(5201)) {
93 std::cerr << "\tFailed 4/6 (" << a << "!=5201)" << std::endl;
94 return false;
95 }
96 std::cout << "\tPassed 4/6...";
97 // 5. test increment (postfix)
98 a++;
99 if (a != large_number(5202)) {
100 std::cerr << "\tFailed 5/6 (" << a << "!=5202)" << std::endl;
101 return false;
102 }
103 std::cout << "\tPassed 5/6...";
104 // 6. test addition with another large number
105 a = a + large_number("7000000000000000000000000000000");
106 if (a != large_number("7000000000000000000000000005202")) {
107 std::cerr << "\tFailed 6/6 (" << a
108 << "!=7000000000000000000000000005202)" << std::endl;
109 return false;
110 }
111 std::cout << "\tPassed 6/6..." << std::endl;
112 return true;
113 }

Friends And Related Symbol Documentation

◆ operator!=

bool operator!= ( large_number const & a,
large_number const & b )
friend

operator overload to compare two numbers

Definition at line 168 of file large_number.h.

168 {
169 return !(a == b);
170 }

◆ operator+

template<class T >
large_number & operator+ ( const large_number & a,
const T & b )
friend

operator overload to perform addition

Definition at line 220 of file large_number.h.

220 {
221 static large_number c = a;
222 c += b;
223 return c;
224 }

◆ operator<<

std::ostream & operator<< ( std::ostream & out,
const large_number & a )
friend

operator overload to compare two numbers

Definition at line 146 of file large_number.h.

146 {
147 for (size_t i = a.num_digits(); i > 0; i--)
148 out << static_cast<int>(a[i - 1]);
149 return out;
150 }

◆ operator==

bool operator== ( large_number const & a,
large_number const & b )
friend

operator overload to compare two numbers

Definition at line 155 of file large_number.h.

155 {
156 size_t N = a.num_digits();
157 if (N != b.num_digits())
158 return false;
159 for (size_t i = 0; i < N; i++)
160 if (a[i] != b[i])
161 return false;
162 return true;
163 }
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....

Member Data Documentation

◆ _digits

std::vector<unsigned char> large_number::_digits
private

where individual digits are stored

Definition at line 285 of file large_number.h.


The documentation for this class was generated from the following file: