From 971f13a59b347343b637aff1d85e8d297535d34c Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 19 Jul 2020 02:06:36 +0200 Subject: [PATCH] Finished operator overloading --- include/gm3p_int.hpp | 18 ++++++++++- src/gm3p_int.cpp | 72 +++++++++++++++++++++++++++++++++++++++----- testapp/main.cpp | 12 ++++++-- 3 files changed, 91 insertions(+), 11 deletions(-) diff --git a/include/gm3p_int.hpp b/include/gm3p_int.hpp index 6d4e14f..b72e611 100644 --- a/include/gm3p_int.hpp +++ b/include/gm3p_int.hpp @@ -12,13 +12,20 @@ namespace gm3p // CONSTRUCTORS gInt(); gInt(mpz_t init); + gInt(mpq_t init); + gInt(mpf_t init); gInt(unsigned long int init); gInt(long int init); gInt(const char* init, int base); + gInt(double init); + + // DESTRUCTOR ~gInt(); - // ASSIGNMENT OPERATORS + // ASSIGNMENT/COPY OPERATORS gInt& operator=(const gInt& other); + gInt(const gInt& other); + gInt(gInt&& other); // ARITHMETIC OPERATORS friend gInt operator+(const gInt& left, const gInt& right); @@ -31,6 +38,15 @@ namespace gm3p gInt& operator*=(const gInt& right); gInt& operator/=(const gInt& right); + // CONVERSION OPERATORS + operator long int() const; + + unsigned long int ToUnsigned() const; + char* ToString(char* buf, int base) const; + double ToDouble() const; + double ToDouble(long int* exp) const; + + // STDIO OPERATORS friend std::ostream& operator<<(std::ostream& os, const gInt& value); private: diff --git a/src/gm3p_int.cpp b/src/gm3p_int.cpp index efd801a..347cf0b 100644 --- a/src/gm3p_int.cpp +++ b/src/gm3p_int.cpp @@ -8,29 +8,46 @@ namespace gm3p } gInt::gInt(mpz_t init) - : gInt::gInt() { - mpz_set(value, init); + mpz_init_set(value, init); + } + + gInt::gInt(mpq_t init) + { + mpz_init(value); + mpz_set_q(value, init); + } + + gInt::gInt(mpf_t init) + { + mpz_init(value); + mpz_set_f(value, init); } gInt::gInt(unsigned long int init) : gInt::gInt() { - mpz_set_ui(value, init); + mpz_init_set_ui(value, init); } gInt::gInt(long int init) : gInt::gInt() { - mpz_set_si(value, init); + mpz_init_set_si(value, init); } gInt::gInt(const char* init, int base) : gInt::gInt() { - mpz_set_str(value, init, base); + mpz_init_set_str(value, init, base); } + gInt::gInt(double init) + { + mpz_init_set_d(value, init); + } + + gInt::~gInt() { mpz_clear(value); @@ -40,6 +57,17 @@ namespace gm3p gInt& gInt::operator=(const gInt& other) + { + mpz_set(this->value, other.value); + return *this; + } + + gInt::gInt(const gInt& other) + { + mpz_set(this->value, other.value); + } + + gInt::gInt(gInt&& other) { mpz_set(this->value, other.value); } @@ -100,11 +128,39 @@ namespace gm3p return *this; } + gInt::operator long int() const + { + return mpz_get_si(value); + } + + unsigned long int gInt::ToUnsigned() const + { + return mpz_get_ui(value); + } + + char* gInt::ToString(char* buf, int base) const + { + return mpz_get_str(buf, base, value); + } + + double gInt::ToDouble() const + { + return mpz_get_d(value); + } + + double gInt::ToDouble(long int* exp) const + { + return mpz_get_d_2exp(exp, value); + } + std::ostream& operator<<(std::ostream& os, const gInt& value) { - static char* buf; - gmp_asprintf(&buf, "%Zd", value); - os << buf; + os << value.ToString(NULL, + (os.flags() & std::ios_base::dec) != 0 ? 10 : + (os.flags() & std::ios_base::hex) != 0 ? 16 : + 8 + ); + return os; } } \ No newline at end of file diff --git a/testapp/main.cpp b/testapp/main.cpp index b41a5a9..d51f8ac 100644 --- a/testapp/main.cpp +++ b/testapp/main.cpp @@ -1,6 +1,7 @@ #include "gm3p.hpp" -//#include +#include +#include using namespace gm3p; @@ -9,9 +10,16 @@ int main(int argc, char** argv) gInt a("12234234234095780349859034869034635235", 10); gInt b("123534622305820985092875207398734234753", 10); + a *= b; gInt c = a * b; - std::cout << a << " * " << b << " = " << c << std::endl; + std::cout << std::dec << a << " * " << b << " = " << c << std::endl; + std::cout << std::hex << a << " * " << b << " = " << c << std::endl; + std::cout << std::oct << a << " * " << b << " = " << c << std::endl << std::endl << std::dec; + + int d = c; + std::cout << d << std::endl; + std::cout << c.ToString(NULL, 2) << std::endl; return 0; } \ No newline at end of file