diff --git a/big-int/src/bigint.hpp b/big-int/src/bigint.hpp index 9d8f030..94fb571 100644 --- a/big-int/src/bigint.hpp +++ b/big-int/src/bigint.hpp @@ -33,6 +33,13 @@ namespace libbig { +// sign = 1: positive number +// sign = 0: negative number +// flip if its reverse + +constexpr bool NEGATIVE = 0; +constexpr bool POSITIVE = 1; + class largeInt { private: @@ -125,7 +132,7 @@ class largeInt bool operator!=(int); bool operator!=(int64_t); - bool operator<(largeInt); + bool operator<(const largeInt&); bool operator<(int); bool operator<(int64_t); bool operator<(long long); @@ -150,4 +157,4 @@ class largeInt }; } // namespace libbig -#endif \ No newline at end of file +#endif diff --git a/big-int/src/operators/less_than.cpp b/big-int/src/operators/less_than.cpp index 48d792b..47b8140 100644 --- a/big-int/src/operators/less_than.cpp +++ b/big-int/src/operators/less_than.cpp @@ -32,5 +32,40 @@ namespace libbig { - + bool largeInt:: operator < (const largeInt& z) { + if (this->sign == NEGATIVE) { + if (z.sign == POSITIVE){ + return true; + } else if (z.sign == NEGATIVE){ + if (this->number.length() > z.number.length()){ + return true; + } + if (this->number.length() < z.number.length()){ + return false; + } + for(int i = 0; i < this->number.length(); i++) { + if (this->number[i] < z.number[i]) { + return false; + } + } + } + } else if (this->sign == POSITIVE) { + if (z.sign == NEGATIVE){ + return false; + } else if (z.sign == POSITIVE){ + if (this->number.length() < z.number.length()){ + return true; + } + if (this->number.length() > z.number.length()){ + return false; + } + for(int i = 0; i < this->number.length(); i++) { + if (this->number[i] > z.number[i]) { + return false; + } + } + } + } + return false; + } } // namespace libbig