Finished operator overloading
This commit is contained in:
parent
97125eaf07
commit
971f13a59b
|
@ -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:
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
#include "gm3p.hpp"
|
||||
|
||||
//#include <iostream>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in a new issue