From 50e78e1a897574ee1d5974e979b44e864be947dc Mon Sep 17 00:00:00 2001 From: Faheel Ahmad Date: Tue, 10 Oct 2017 20:33:39 +0530 Subject: [PATCH] Add increment and decrement operators --- include/BigInt.hpp | 8 +-- include/operators/increment_decrement.hpp | 70 +++++++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 include/operators/increment_decrement.hpp diff --git a/include/BigInt.hpp b/include/BigInt.hpp index 0313c27..d79414a 100644 --- a/include/BigInt.hpp +++ b/include/BigInt.hpp @@ -65,10 +65,10 @@ class BigInt { BigInt& operator%=(const std::string&); // TODO // Increment and decrement operators: - BigInt& operator++(); // pre-increment TODO - BigInt& operator--(); // pre-decrement TODO - BigInt operator++(int); // post-increment TODO - BigInt operator--(int); // post-decrement TODO + BigInt& operator++(); // pre-increment + BigInt& operator--(); // pre-decrement + BigInt operator++(int); // post-increment + BigInt operator--(int); // post-decrement // Relational operators: bool operator<(const BigInt&) const; diff --git a/include/operators/increment_decrement.hpp b/include/operators/increment_decrement.hpp new file mode 100644 index 0000000..1037820 --- /dev/null +++ b/include/operators/increment_decrement.hpp @@ -0,0 +1,70 @@ +/* + =========================================================================== + Increment and decrement operators + =========================================================================== +*/ + +#ifndef BIG_INT_INCREMENT_DECREMENT_OPERATORS_HPP +#define BIG_INT_INCREMENT_DECREMENT_OPERATORS_HPP + +#include "BigInt.hpp" +#include "constructors/constructors.hpp" +#include "operators/assignment.hpp" +#include "operators/arithmetic_assignment.hpp" +#include "operators/binary_arithmetic.hpp" + + +/* + Pre-increment + ------------- + ++BigInt +*/ + +BigInt& BigInt::operator++() { + *this += 1; + + return *this; +} + + +/* + Pre-decrement + ------------- + --BigInt +*/ + +BigInt& BigInt::operator--() { + *this -= 1; + + return *this; +} + + +/* + Post-increment + -------------- + BigInt++ +*/ + +BigInt BigInt::operator++(int) { + BigInt temp = *this; + *this += 1; + + return temp; +} + + +/* + Post-decrement + -------------- + BigInt-- +*/ + +BigInt BigInt::operator--(int) { + BigInt temp = *this; + *this -= 1; + + return temp; +} + +#endif // BIG_INT_INCREMENT_DECREMENT_OPERATORS_HPP