mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fixed code for lint and copy constructor
This commit is contained in:
parent
251c32d8b3
commit
c892cbadc6
@ -18,11 +18,12 @@ large_number fib(uint64_t n) {
|
||||
large_number f1(1);
|
||||
|
||||
do {
|
||||
large_number f2 = f0 + f1;
|
||||
f0 = f1;
|
||||
f1 = f2;
|
||||
large_number f2 = f1;
|
||||
f1 += f0;
|
||||
f0 = f2;
|
||||
n--;
|
||||
} while (n > 2); // since we start from 2
|
||||
|
||||
return f1;
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
/**
|
||||
* @file
|
||||
* @author Krishna Vedala
|
||||
* @email krishna (dot) vedala (at) ieee (dot) org
|
||||
**/
|
||||
@ -24,9 +25,18 @@ class large_number {
|
||||
/**< initializer with value = 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 */
|
||||
explicit large_number(uint64_t n) {
|
||||
uint64_t carry = n;
|
||||
explicit large_number(int n) {
|
||||
int carry = n;
|
||||
do {
|
||||
add_digit(carry % 10);
|
||||
carry /= 10;
|
||||
@ -34,15 +44,13 @@ class 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 */
|
||||
explicit large_number(const std::vector<unsigned char> &vec) {
|
||||
_digits = vec;
|
||||
}
|
||||
explicit large_number(std::vector<unsigned char> &vec) : _digits(vec) {}
|
||||
|
||||
/**< 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--) {
|
||||
unsigned char a = number_str[i - 1] - '0';
|
||||
if (a >= 0 && a <= 9) _digits.push_back(a);
|
||||
@ -94,8 +102,8 @@ class large_number {
|
||||
// 6. test addition with another large number
|
||||
a = a + large_number("7000000000000000000000000000000");
|
||||
if (a != large_number("7000000000000000000000000005202")) {
|
||||
std::cerr << "\tFailed 6/6 (" << a << "!=7000000000000000000000000005202)"
|
||||
<< std::endl;
|
||||
std::cerr << "\tFailed 6/6 (" << a
|
||||
<< "!=7000000000000000000000000005202)" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "\tPassed 6/6..." << std::endl;
|
||||
@ -168,7 +176,7 @@ class large_number {
|
||||
/**
|
||||
* operator overload to increment (postfix)
|
||||
**/
|
||||
large_number operator++(int) {
|
||||
large_number &operator++(int) {
|
||||
large_number tmp(_digits);
|
||||
++(*this);
|
||||
return tmp;
|
||||
@ -177,11 +185,9 @@ class large_number {
|
||||
/**
|
||||
* operator overload to add
|
||||
**/
|
||||
template <class T>
|
||||
large_number &operator+=(T n) {
|
||||
large_number &operator+=(large_number n) {
|
||||
// if adding with another large_number
|
||||
if (typeid(T) == typeid(large_number)) {
|
||||
large_number *b = static_cast<large_number *>(&n);
|
||||
large_number *b = reinterpret_cast<large_number *>(&n);
|
||||
const size_t max_L = std::max(this->num_digits(), b->num_digits());
|
||||
unsigned int carry = 0;
|
||||
size_t i;
|
||||
@ -194,28 +200,30 @@ class large_number {
|
||||
this->add_digit(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;
|
||||
}
|
||||
|
||||
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
|
||||
**/
|
||||
template <class T>
|
||||
friend large_number &operator+(large_number &a, const T &b) {
|
||||
a += b;
|
||||
return a;
|
||||
friend large_number &operator+(const large_number &a, const T &b) {
|
||||
large_number c = a;
|
||||
c += b;
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -242,7 +250,8 @@ class large_number {
|
||||
**/
|
||||
template <class T>
|
||||
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
|
||||
// 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_
|
||||
|
Loading…
Reference in New Issue
Block a user