code cleanup

This commit is contained in:
Krishna Vedala 2020-05-24 16:24:41 -04:00
parent 6276e4ec06
commit b802dbe077

View File

@ -5,6 +5,7 @@
#ifndef OTHERS_LARGE_NUMBER_H_ #ifndef OTHERS_LARGE_NUMBER_H_
#define OTHERS_LARGE_NUMBER_H_ #define OTHERS_LARGE_NUMBER_H_
#include <algorithm>
#include <cassert> #include <cassert>
#include <cinttypes> #include <cinttypes>
#include <cstring> #include <cstring>
@ -20,10 +21,11 @@
**/ **/
class large_number { class large_number {
public: public:
large_number() /**< initializer with value = 1 */ { _digits.push_back(1); } /**< initializer with value = 1 */
large_number() { _digits.push_back(1); }
large_number(uint64_t n) /**< initializer from an integer */ /**< initializer from an integer */
{ explicit large_number(uint64_t n) {
uint64_t carry = n; uint64_t carry = n;
do { do {
add_digit(carry % 10); add_digit(carry % 10);
@ -31,24 +33,19 @@ public:
} while (carry != 0); } while (carry != 0);
} }
large_number( /**< initializer from another large_number */
const large_number &a) /**< initializer from another large_number */ explicit large_number(const large_number &a) { _digits = a._digits; }
{
_digits = a._digits;
}
large_number( /**< initializer from a vector */
const std::vector<unsigned char> &vec) /**< initializer from a vector */ explicit large_number(const std::vector<unsigned char> &vec) {
{
_digits = vec; _digits = vec;
} }
large_number(const char *number_str) /**< initializer from a string */ /**< initializer from a string */
{ explicit large_number(const char *number_str) {
for (size_t i = strlen(number_str); i > 0; i--) { for (size_t i = strlen(number_str); i > 0; i--) {
unsigned char a = number_str[i - 1] - '0'; unsigned char a = number_str[i - 1] - '0';
if (a >= 0 && a <= 9) if (a >= 0 && a <= 9) _digits.push_back(a);
_digits.push_back(a);
} }
} }
@ -97,8 +94,8 @@ public:
// 6. test addition with another large number // 6. test addition with another large number
a = a + large_number("7000000000000000000000000000000"); a = a + large_number("7000000000000000000000000000000");
if (a != large_number("7000000000000000000000000005202")) { if (a != large_number("7000000000000000000000000005202")) {
std::cerr << "\tFailed 6/6 (" << a std::cerr << "\tFailed 6/6 (" << a << "!=7000000000000000000000000005202)"
<< "!=7000000000000000000000000005202)" << std::endl; << std::endl;
return false; return false;
} }
std::cout << "\tPassed 6/6..." << std::endl; std::cout << "\tPassed 6/6..." << std::endl;
@ -147,11 +144,9 @@ public:
**/ **/
friend bool operator==(large_number const &a, large_number const &b) { friend bool operator==(large_number const &a, large_number const &b) {
size_t N = a.num_digits(); size_t N = a.num_digits();
if (N != b.num_digits()) if (N != b.num_digits()) return false;
return false;
for (size_t i = 0; i < N; i++) for (size_t i = 0; i < N; i++)
if (a[i] != b[i]) if (a[i] != b[i]) return false;
return false;
return true; return true;
} }
@ -184,29 +179,27 @@ public:
**/ **/
template <class T> template <class T>
large_number &operator+=(T n) { large_number &operator+=(T n) {
if (typeid(T) == // if adding with another large_number
typeid(large_number)) // if adding with another large_number if (typeid(T) == typeid(large_number)) {
{ large_number *b = static_cast<large_number *>(&n);
large_number *b = (large_number *)&n;
const size_t max_L = std::max(this->num_digits(), b->num_digits()); const size_t max_L = std::max(this->num_digits(), b->num_digits());
unsigned int carry = 0; unsigned int carry = 0;
size_t i; size_t i;
for (i = 0; i < max_L || carry != 0; i++) { for (i = 0; i < max_L || carry != 0; i++) {
if (i < b->num_digits()) if (i < b->num_digits()) carry += (*b)[i];
carry += (*b)[i]; if (i < this->num_digits()) carry += (*this)[i];
if (i < this->num_digits())
carry += (*this)[i];
if (i < this->num_digits()) if (i < this->num_digits())
(*this)[i] = carry % 10; (*this)[i] = carry % 10;
else else
this->add_digit(carry % 10); this->add_digit(carry % 10);
carry /= 10; carry /= 10;
} }
} else if (std::is_integral<T>::value) } else if (std::is_integral<T>::value) {
return (*this) += large_number(n); return (*this) += large_number(n);
else } else {
std::cerr << "Must be integer addition unsigned integer types." std::cerr << "Must be integer addition unsigned integer types."
<< std::endl; << std::endl;
}
return *this; return *this;
} }
@ -214,7 +207,7 @@ public:
* operator overload to perform addition * operator overload to perform addition
**/ **/
template <class T> template <class T>
friend large_number &operator+(large_number &a, const T &b) { friend large_number &operator+(const large_number &a, const T &b) {
a += b; a += b;
return a; return a;
} }
@ -249,19 +242,18 @@ private:
**/ **/
template <class T> template <class T>
void multiply(const T n) { void multiply(const T n) {
static_assert(std::is_integral<T>::value, static_assert(std::is_integral<T>::value, "Can only have integer types.");
"Can only have integer types.");
// assert(!(std::is_signed<T>::value)); //, "Implemented only for // assert(!(std::is_signed<T>::value)); //, "Implemented only for
// unsigned integer types."); // unsigned integer types.");
size_t i; size_t i;
unsigned long long carry = 0, temp; uint64_t carry = 0, temp;
for (i = 0; i < this->num_digits(); i++) { for (i = 0; i < this->num_digits(); i++) {
temp = (*this)[i] * n; temp = (*this)[i] * n;
temp += carry; temp += carry;
if (temp < 10) if (temp < 10) {
carry = 0; carry = 0;
else { } else {
carry = temp / 10; carry = temp / 10;
temp = temp % 10; temp = temp % 10;
} }
@ -272,10 +264,9 @@ private:
this->add_digit(carry % 10); this->add_digit(carry % 10);
carry /= 10; carry /= 10;
} }
}; }
std::vector<unsigned char> std::vector<unsigned char> _digits; /**< where individual digits are stored */
_digits; /**< where individual digits are stored */
}; };
#endif // OTHERS_LARGE_NUMBER_H_ #endif // OTHERS_LARGE_NUMBER_H_