Finished operator overloading
This commit is contained in:
parent
97125eaf07
commit
971f13a59b
3 changed files with 91 additions and 11 deletions
|
@ -12,13 +12,20 @@ namespace gm3p
|
||||||
// CONSTRUCTORS
|
// CONSTRUCTORS
|
||||||
gInt();
|
gInt();
|
||||||
gInt(mpz_t init);
|
gInt(mpz_t init);
|
||||||
|
gInt(mpq_t init);
|
||||||
|
gInt(mpf_t init);
|
||||||
gInt(unsigned long int init);
|
gInt(unsigned long int init);
|
||||||
gInt(long int init);
|
gInt(long int init);
|
||||||
gInt(const char* init, int base);
|
gInt(const char* init, int base);
|
||||||
|
gInt(double init);
|
||||||
|
|
||||||
|
// DESTRUCTOR
|
||||||
~gInt();
|
~gInt();
|
||||||
|
|
||||||
// ASSIGNMENT OPERATORS
|
// ASSIGNMENT/COPY OPERATORS
|
||||||
gInt& operator=(const gInt& other);
|
gInt& operator=(const gInt& other);
|
||||||
|
gInt(const gInt& other);
|
||||||
|
gInt(gInt&& other);
|
||||||
|
|
||||||
// ARITHMETIC OPERATORS
|
// ARITHMETIC OPERATORS
|
||||||
friend gInt operator+(const gInt& left, const gInt& right);
|
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);
|
||||||
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);
|
friend std::ostream& operator<<(std::ostream& os, const gInt& value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -8,29 +8,46 @@ namespace gm3p
|
||||||
}
|
}
|
||||||
|
|
||||||
gInt::gInt(mpz_t init)
|
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(unsigned long int init) :
|
||||||
gInt::gInt()
|
gInt::gInt()
|
||||||
{
|
{
|
||||||
mpz_set_ui(value, init);
|
mpz_init_set_ui(value, init);
|
||||||
}
|
}
|
||||||
|
|
||||||
gInt::gInt(long int init) :
|
gInt::gInt(long int init) :
|
||||||
gInt::gInt()
|
gInt::gInt()
|
||||||
{
|
{
|
||||||
mpz_set_si(value, init);
|
mpz_init_set_si(value, init);
|
||||||
}
|
}
|
||||||
|
|
||||||
gInt::gInt(const char* init, int base) :
|
gInt::gInt(const char* init, int base) :
|
||||||
gInt::gInt()
|
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()
|
gInt::~gInt()
|
||||||
{
|
{
|
||||||
mpz_clear(value);
|
mpz_clear(value);
|
||||||
|
@ -40,6 +57,17 @@ namespace gm3p
|
||||||
|
|
||||||
|
|
||||||
gInt& gInt::operator=(const gInt& other)
|
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);
|
mpz_set(this->value, other.value);
|
||||||
}
|
}
|
||||||
|
@ -100,11 +128,39 @@ namespace gm3p
|
||||||
return *this;
|
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)
|
std::ostream& operator<<(std::ostream& os, const gInt& value)
|
||||||
{
|
{
|
||||||
static char* buf;
|
os << value.ToString(NULL,
|
||||||
gmp_asprintf(&buf, "%Zd", value);
|
(os.flags() & std::ios_base::dec) != 0 ? 10 :
|
||||||
os << buf;
|
(os.flags() & std::ios_base::hex) != 0 ? 16 :
|
||||||
|
8
|
||||||
|
);
|
||||||
|
return os;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
#include "gm3p.hpp"
|
#include "gm3p.hpp"
|
||||||
|
|
||||||
//#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
using namespace gm3p;
|
using namespace gm3p;
|
||||||
|
|
||||||
|
@ -9,9 +10,16 @@ int main(int argc, char** argv)
|
||||||
gInt a("12234234234095780349859034869034635235", 10);
|
gInt a("12234234234095780349859034869034635235", 10);
|
||||||
gInt b("123534622305820985092875207398734234753", 10);
|
gInt b("123534622305820985092875207398734234753", 10);
|
||||||
|
|
||||||
|
a *= b;
|
||||||
gInt c = 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;
|
return 0;
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue