fixed code for lint and copy constructor

This commit is contained in:
Krishna Vedala 2020-05-26 18:07:41 -04:00
parent 251c32d8b3
commit c892cbadc6
2 changed files with 244 additions and 233 deletions

View File

@ -18,11 +18,12 @@ large_number fib(uint64_t n) {
large_number f1(1); large_number f1(1);
do { do {
large_number f2 = f0 + f1; large_number f2 = f1;
f0 = f1; f1 += f0;
f1 = f2; f0 = f2;
n--; n--;
} while (n > 2); // since we start from 2 } while (n > 2); // since we start from 2
return f1; return f1;
} }

View File

@ -1,4 +1,5 @@
/** /**
* @file
* @author Krishna Vedala * @author Krishna Vedala
* @email krishna (dot) vedala (at) ieee (dot) org * @email krishna (dot) vedala (at) ieee (dot) org
**/ **/
@ -24,9 +25,18 @@ class large_number {
/**< initializer with value = 1 */ /**< initializer with value = 1 */
large_number() { _digits.push_back(1); } large_number() { _digits.push_back(1); }
// /**< initializer from an integer */
// explicit large_number(uint64_t n) {
// uint64_t carry = n;
// do {
// add_digit(carry % 10);
// carry /= 10;
// } while (carry != 0);
// }
/**< initializer from an integer */ /**< initializer from an integer */
explicit large_number(uint64_t n) { explicit large_number(int n) {
uint64_t carry = n; int carry = n;
do { do {
add_digit(carry % 10); add_digit(carry % 10);
carry /= 10; carry /= 10;
@ -34,15 +44,13 @@ class large_number {
} }
/**< initializer from another large_number */ /**< initializer from another large_number */
explicit large_number(const large_number &a) { _digits = a._digits; } large_number(const large_number &a) : _digits(a._digits) {}
/**< initializer from a vector */ /**< initializer from a vector */
explicit large_number(const std::vector<unsigned char> &vec) { explicit large_number(std::vector<unsigned char> &vec) : _digits(vec) {}
_digits = vec;
}
/**< initializer from a string */ /**< initializer from a string */
explicit large_number(const char *number_str) { explicit large_number(char const *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) _digits.push_back(a); if (a >= 0 && a <= 9) _digits.push_back(a);
@ -94,8 +102,8 @@ class large_number {
// 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 << "!=7000000000000000000000000005202)" std::cerr << "\tFailed 6/6 (" << a
<< std::endl; << "!=7000000000000000000000000005202)" << std::endl;
return false; return false;
} }
std::cout << "\tPassed 6/6..." << std::endl; std::cout << "\tPassed 6/6..." << std::endl;
@ -168,7 +176,7 @@ class large_number {
/** /**
* operator overload to increment (postfix) * operator overload to increment (postfix)
**/ **/
large_number operator++(int) { large_number &operator++(int) {
large_number tmp(_digits); large_number tmp(_digits);
++(*this); ++(*this);
return tmp; return tmp;
@ -177,11 +185,9 @@ class large_number {
/** /**
* operator overload to add * operator overload to add
**/ **/
template <class T> large_number &operator+=(large_number n) {
large_number &operator+=(T n) {
// if adding with another large_number // if adding with another large_number
if (typeid(T) == typeid(large_number)) { large_number *b = reinterpret_cast<large_number *>(&n);
large_number *b = static_cast<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;
@ -194,28 +200,30 @@ class large_number {
this->add_digit(carry % 10); this->add_digit(carry % 10);
carry /= 10; carry /= 10;
} }
} else if (std::is_integral<T>::value) {
return (*this) += large_number(n);
} else {
std::cerr << "Must be integer addition unsigned integer types."
<< std::endl;
}
return *this; return *this;
} }
large_number &operator+=(int n) { return (*this) += large_number(n); }
// large_number &operator+=(uint64_t n) { return (*this) += large_number(n);
// }
/** /**
* 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; large_number c = a;
return a; c += b;
return c;
} }
/** /**
* assignment operator * assignment operator
**/ **/
void operator=(const large_number &b) { _digits = b._digits; } large_number &operator=(const large_number &b) {
this->_digits = b._digits;
return *this;
}
/** /**
* operator overload to increment * operator overload to increment
@ -242,7 +250,8 @@ class large_number {
**/ **/
template <class T> template <class T>
void multiply(const T n) { void multiply(const T n) {
static_assert(std::is_integral<T>::value, "Can only have integer types."); static_assert(std::is_integral<T>::value,
"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.");
@ -266,7 +275,8 @@ class large_number {
} }
} }
std::vector<unsigned char> _digits; /**< where individual digits are stored */ std::vector<unsigned char>
_digits; /**< where individual digits are stored */
}; };
#endif // OTHERS_LARGE_NUMBER_H_ #endif // OTHERS_LARGE_NUMBER_H_