diff options
author | Olivier Gayot <og@satcom1.com> | 2017-08-26 00:15:58 +0200 |
---|---|---|
committer | Olivier Gayot <og@satcom1.com> | 2017-11-19 14:00:12 +0100 |
commit | 671f3f8ca44382386daf63c70140742e61200c2e (patch) | |
tree | 4a99e06fc2ec3604db7348aab545fdb070866b8f /number.cpp | |
parent | 59962a745c0ca1e3237c8634005787eb1c8bbfde (diff) |
Added a class "number" with comparison operators
Signed-off-by: Olivier Gayot <og@satcom1.com>
Diffstat (limited to 'number.cpp')
-rw-r--r-- | number.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/number.cpp b/number.cpp new file mode 100644 index 0000000..e942cf2 --- /dev/null +++ b/number.cpp @@ -0,0 +1,58 @@ +#include "number.h" + +/* Comparison operators {{{ */ + +bool +number::operator<(const number &n) const +{ + const auto size = this->_operands.size(); + + if (size < n._operands.size()) { + return true; + } + + if (size > n._operands.size()) { + return false; + } + + /* Because first item is the least significant. */ + auto l1(this->_operands); + auto l2(n._operands); + + l1.reverse(); + l2.reverse(); + + return l1 < l2; +} + +bool +number::operator>(const number &n) const +{ + return n.operator<(*this); +} + +bool +number::operator<=(const number &n) const +{ + return operator<(n) || operator==(n); +} + +bool +number::operator>=(const number &n) const +{ + return operator>(n) || operator==(n); +} + +bool +number::operator==(const number &n) const +{ + return _operands == n._operands; +} + +bool +number::operator!=(const number &n) const +{ + return _operands != n._operands; +} + +/* }}} */ |