change operators

This commit is contained in:
Andrei Yankovich 2019-07-12 18:02:08 +03:00
parent 2f2e1ff817
commit 2c4a35fcaf
3 changed files with 102 additions and 163 deletions

@ -18,14 +18,9 @@ BigInt::BigInt(const BigInt &val):
mpz_set(data, val.data);
}
BigInt::BigInt(const char *str, int base):
BigInt::BigInt(const std::string &str, int base):
BigInt() {
mpz_set_str(data, str, base);
}
BigInt::BigInt(std::string imput, int base):
BigInt(imput.c_str(), base) {
mpz_set_str(data, str.c_str(), base);
}
BigInt::BigInt(long val):
@ -33,16 +28,6 @@ BigInt::BigInt(long val):
mpz_set_si(data, val);
}
BigInt::BigInt(unsigned long val):
BigInt() {
mpz_set_ui(data, val);
}
BigInt::BigInt(double val):
BigInt() {
mpz_set_d(data, val);
}
std::string BigInt::getString(int base) const {
char *str = mpz_get_str(nullptr, base, data);
return str;
@ -80,8 +65,17 @@ BigInt &BigInt::operator =(const BigInt &val) {
return *this;
}
// add operators
BigInt &BigInt::operator =(const std::string &imput) {
mpz_set_str(data, imput.c_str(), 10);
return *this;
}
BigInt &BigInt::operator =(long val) {
mpz_set_si(data, val);
return *this;
}
// add operators
BigInt operator +(BigInt left, long right) {
if (right >= 0) {
@ -92,6 +86,10 @@ BigInt operator +(BigInt left, long right) {
return left -= std::abs(right);
}
BigInt operator +(long left, BigInt right) {
return right += left;
}
BigInt operator +(BigInt left, const BigInt &right) {
mpz_add(left.data, left.data, right.data);
return left;
@ -176,12 +174,15 @@ BigInt operator /(BigInt left, long right) {
return -left;
}
BigInt operator /(long left, BigInt right) {
return BigInt(left) / right;
}
BigInt& operator /=(BigInt &left, const BigInt &right) {
mpz_tdiv_q(left.data, left.data, right.data);
return left;
}
BigInt& operator /=(BigInt &left, long right) {
mpz_tdiv_q_ui(left.data, left.data, static_cast<unsigned long>(std::abs(right)));
@ -199,15 +200,16 @@ BigInt operator *(BigInt left, const BigInt &right) {
}
BigInt operator *(BigInt left, long right) {
mpz_mul_ui(left.data, left.data, static_cast<unsigned long>(std::abs(right)));
if (right >= 0) {
return left * static_cast<unsigned long>(right);
return left;
}
return 0 - (left * static_cast<unsigned long>(std::abs(right)));
return -left;
}
BigInt operator *(BigInt left, unsigned long right) {
mpz_mul_ui(left.data, left.data, right);
return left;
BigInt operator *(long left, BigInt right) {
return right *= left;
}
BigInt& operator *=(BigInt &left, const BigInt &right) {
@ -216,15 +218,12 @@ BigInt& operator *=(BigInt &left, const BigInt &right) {
}
BigInt& operator *=(BigInt &left, long right) {
if (right >= 0) {
return left *= static_cast<unsigned long>(right);
}
return 0 -= (left *= static_cast<unsigned long>(std::abs(right)));
}
mpz_mul_ui(left.data, left.data, static_cast<unsigned long>(std::abs(right)));
BigInt& operator *=(BigInt &left, unsigned long right) {
mpz_mul_ui(left.data, left.data, right);
return left;
if (right >= 0) {
return left;
}
return 0 -= left;
}
//mod operations
@ -233,13 +232,13 @@ BigInt operator %(BigInt left, const BigInt &right) {
return left;
}
BigInt operator %(BigInt left, unsigned long right) {
mpz_tdiv_r_ui(left.data, left.data, right);
BigInt operator %(BigInt left, long right) {
mpz_tdiv_r_ui(left.data, left.data, static_cast<unsigned long>(std::abs(right)));
return left;
}
BigInt operator %(BigInt left, long right) {
return left % static_cast<unsigned long>(std::abs(right));
BigInt operator %(long left, BigInt right) {
return BigInt(left) % right;
}
BigInt& operator %=(BigInt& left, const BigInt &right) {
@ -247,16 +246,11 @@ BigInt& operator %=(BigInt& left, const BigInt &right) {
return left;
}
BigInt& operator %=(BigInt& left, unsigned long right) {
mpz_tdiv_r_ui(left.data, left.data, right);
BigInt& operator %=(BigInt& left, long right) {
mpz_tdiv_r_ui(left.data, left.data, static_cast<unsigned long>(std::abs(right)));
return left;
}
BigInt& operator %=(BigInt& left, long right) {
return left %= static_cast<unsigned long>(std::abs(right));
}
// incriment and dicriment
BigInt &BigInt::operator--() {
*this -= 1;
@ -282,69 +276,52 @@ BigInt BigInt::operator++(int) {
// move operators
BigInt operator >>(BigInt left, unsigned int right) {
mpn_rshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
right);
return left;
}
BigInt operator <<(BigInt left, unsigned int right) {
mpn_lshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
right);
return left;
}
BigInt& operator >>=(BigInt &left, unsigned int right) {
mpn_rshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
right);
return left;
}
BigInt& operator <<=(BigInt &left, unsigned int right) {
mpn_lshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
right);
return left;
}
BigInt operator >>(BigInt left, int right) {
if (right >= 0) {
return left >> static_cast<unsigned int> (right);
mpn_rshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
static_cast<unsigned int>(std::abs(right)));
return left;
}
return left << static_cast<unsigned int>(std::abs(right));
return left << right;
}
BigInt operator <<(BigInt left, int right) {
if (right >= 0) {
return left << static_cast<unsigned int> (right);
mpn_lshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
static_cast<unsigned int>(std::abs(right)));
return left;
}
return left >> static_cast<unsigned int>(std::abs(right));
return left >> right;
}
BigInt& operator >>=(BigInt &left, int right) {
if (right >= 0) {
return left >>= static_cast<unsigned int>(right);
mpn_rshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
static_cast<unsigned int>(std::abs(right)));
return left;
}
return left <<= static_cast<unsigned int>(std::abs(right));
return left <<= right;
}
BigInt& operator <<=(BigInt &left, int right) {
if (right >= 0) {
return left <<= static_cast<unsigned int>(right);
mpn_lshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
static_cast<unsigned int>(std::abs(right)));
return left;
}
return left >>= static_cast<unsigned int>(std::abs(right));
return left >>= right;
}
// other bin operators
@ -362,10 +339,6 @@ BigInt operator |(const BigInt &left, long right) {
return left | BigInt(right);
}
BigInt operator |(const BigInt &left, unsigned long right) {
return left | BigInt(right);
}
BigInt& operator |=(BigInt &left, const BigInt &right) {
mpz_ior(left.data, left.data, right.data);
return left;
@ -375,10 +348,6 @@ BigInt& operator |=(BigInt &left, long right) {
return left |= BigInt(right);
}
BigInt& operator |=(BigInt &left, unsigned long right) {
return left |= BigInt(right);
}
BigInt operator &(BigInt left, const BigInt &right) {
mpz_and(left.data, left.data, right.data);
return left;
@ -388,9 +357,6 @@ BigInt operator &(const BigInt &left, long right) {
return left & BigInt(right);
}
BigInt operator &(const BigInt &left, unsigned long right) {
return left & BigInt(right);
}
BigInt& operator &=(BigInt &left, const BigInt &right) {
mpz_and(left.data, left.data, right.data);
@ -401,10 +367,6 @@ BigInt& operator &=(BigInt &left, long right) {
return left &= BigInt(right);
}
BigInt& operator &=(BigInt &left, unsigned long right) {
return left &= BigInt(right);
}
BigInt operator ^(BigInt left, const BigInt &right) {
mpz_xor(left.data, left.data, right.data);
return left;
@ -414,10 +376,6 @@ BigInt operator ^(const BigInt &left, long right) {
return left ^ BigInt(right);
}
BigInt operator ^(const BigInt &left, unsigned long right) {
return left ^ BigInt(right);
}
BigInt& operator ^=(BigInt &left, const BigInt &right) {
mpz_xor(left.data, left.data, right.data);
return left;
@ -427,10 +385,6 @@ BigInt& operator ^=(BigInt &left, long right) {
return left ^= BigInt(right);
}
BigInt& operator ^=(BigInt &left, unsigned long right) {
return left ^= BigInt(right);
}
// logic operators
@ -442,19 +396,15 @@ bool operator == (const BigInt& left, const BigInt& right) {
return mpz_cmp(left.data, right.data) == 0;
}
bool operator == (const BigInt& left, unsigned long right) {
return mpz_cmp_ui(left.data, right) == 0;
}
bool operator == (const BigInt& left, long right) {
return mpz_cmp_si(left.data, right) == 0;
}
bool operator != (const BigInt &left, const BigInt& right) {
return !(left == right);
bool operator == (const BigInt &left, const std::string &str) {
return left == BigInt(str);
}
bool operator != (const BigInt &left, unsigned long right) {
bool operator != (const BigInt &left, const BigInt& right) {
return !(left == right);
}
@ -466,10 +416,6 @@ bool operator < ( const BigInt &left, const BigInt& right) {
return mpz_cmp(left.data, right.data) < 0;
}
bool operator < ( const BigInt &left, unsigned long right) {
return mpz_cmp_ui(left.data, right) < 0;
}
bool operator < ( const BigInt &left, long right) {
return mpz_cmp_si(left.data, right) < 0;
}
@ -478,23 +424,14 @@ bool operator > ( const BigInt &left, const BigInt& right) {
return mpz_cmp(left.data, right.data) > 0;
}
bool operator > ( const BigInt &left, unsigned long right) {
return mpz_cmp_ui(left.data, right) > 0;
}
bool operator > ( const BigInt &left, long right) {
return mpz_cmp_si(left.data, right) > 0;
}
bool operator <= ( const BigInt &left, const BigInt& right) {
return mpz_cmp(left.data, right.data) <= 0;
}
bool operator <= ( const BigInt &left, unsigned long right) {
return mpz_cmp_ui(left.data, right) <= 0;
}
bool operator <= ( const BigInt &left, long right) {
return mpz_cmp_si(left.data, right) <= 0;
}
@ -503,10 +440,6 @@ bool operator >= ( const BigInt &left, const BigInt& right) {
return mpz_cmp(left.data, right.data) >= 0;
}
bool operator >= ( const BigInt &left, unsigned long right) {
return mpz_cmp_ui(left.data, right) >= 0;
}
bool operator >= ( const BigInt &left, long right) {
return mpz_cmp_si(left.data, right) >= 0;
}

@ -23,11 +23,9 @@ class MINIGMPSHARED_EXPORT BigInt
public:
BigInt();
BigInt(const BigInt& val );
BigInt(const char *str, int base = 10);
BigInt(std::string imput, int base = 10);
BigInt(const std::string &imput, int base = 10);
// BigInt(const char * str, int base = 10);
BigInt(long val);
BigInt(unsigned long val);
BigInt(double val);
std::string getString(int base = 10) const;
~BigInt();
@ -45,15 +43,24 @@ public:
static BigInt bigPow10(unsigned short pow);
BigInt& operator = (const BigInt& val);
BigInt& operator = (const std::string &imput);
BigInt& operator = (long val);
// BigInt& operator = (const BigInt& val);
friend BigInt operator + ( BigInt left, const BigInt& right);
friend BigInt operator + ( BigInt left, const std::string &right);
friend BigInt operator + ( BigInt left, long right);
friend BigInt operator + ( long left, BigInt right);
friend BigInt& operator += ( BigInt &left, long right);
friend BigInt& operator += ( BigInt &left, const BigInt& right);
friend BigInt& operator += ( BigInt &left, const std::string &right);
friend BigInt operator - ( BigInt left, const BigInt& right);
friend BigInt operator - ( BigInt left, long right);
friend BigInt& operator - ( BigInt left, const std::string &right);
friend BigInt operator - ( long right, BigInt left);
friend BigInt operator-(BigInt val);
@ -61,30 +68,37 @@ public:
friend BigInt& operator -= ( BigInt &left, long right);
friend BigInt& operator -= ( long left, BigInt & right);
friend BigInt& operator -= ( BigInt &left, const BigInt& right);
friend BigInt& operator -= ( BigInt &left, const std::string &right);
friend BigInt operator / ( BigInt left, const BigInt& right);
friend BigInt operator / ( BigInt left, const std::string &right);
friend BigInt operator / ( BigInt left, long right);
friend BigInt operator / ( long left, BigInt right);
friend BigInt& operator /= ( BigInt &left, long right);
friend BigInt& operator /= ( BigInt &left, const std::string &right);
friend BigInt& operator /= ( BigInt &left, const BigInt& right);
friend BigInt operator * ( BigInt left, const BigInt& right);
friend BigInt operator * ( BigInt left, unsigned long right);
friend BigInt operator * ( BigInt left, const std::string &right);
friend BigInt operator * ( BigInt left, long right);
friend BigInt& operator *= ( BigInt &left, unsigned long right);
friend BigInt operator * ( long left, BigInt right);
friend BigInt& operator *= ( BigInt &left, const BigInt& right);
friend BigInt& operator *= ( BigInt &left, long right);
friend BigInt& operator *= ( BigInt &left, const std::string &right);
friend BigInt operator % ( BigInt left, const BigInt& right);
friend BigInt operator % ( BigInt left, unsigned long right);
friend BigInt operator % ( BigInt left, const std::string &right);
friend BigInt operator % ( BigInt left, long right);
friend BigInt operator % ( long left, BigInt right);
friend BigInt& operator %= ( BigInt &left, unsigned long right);
friend BigInt& operator %= ( BigInt &left, long right);
friend BigInt& operator %= ( BigInt &left, const BigInt& right);
friend BigInt& operator %= ( BigInt &left, const std::string &right);
friend BigInt& operator %= ( BigInt &left, const BigInt& right);
friend BigInt operator << ( BigInt left, int right);
friend BigInt operator >> ( BigInt left, int right);
@ -92,37 +106,36 @@ public:
friend BigInt& operator <<= ( BigInt &left, int right);
friend BigInt& operator >>= ( BigInt &left, int right);
friend BigInt operator << ( BigInt left, unsigned int right);
friend BigInt operator >> ( BigInt left, unsigned int right);
friend BigInt& operator <<= ( BigInt &left, unsigned int right);
friend BigInt& operator >>= ( BigInt &left, unsigned int right);
friend bool operator == ( const BigInt& left, const BigInt& right);
friend bool operator == ( const BigInt& left, unsigned long right);
friend bool operator == ( const BigInt& left, long right);
friend bool operator == ( const BigInt& left, const std::string& right);
friend bool operator == ( const std::string& left, const BigInt& right);
friend bool operator != ( const BigInt& left, const BigInt& right);
friend bool operator != ( const BigInt& left, unsigned long right);
friend bool operator != ( const BigInt& left, long right);
friend bool operator != ( const BigInt& left, const std::string& str);
friend bool operator != ( const std::string& left, const BigInt& right);
friend bool operator < ( const BigInt& left, const BigInt& right);
friend bool operator < ( const BigInt& left, unsigned long right);
friend bool operator < ( const BigInt& left, long right);
friend bool operator < ( const BigInt& left, const std::string& str);
friend bool operator < ( const std::string& left, const BigInt& right);
friend bool operator > ( const BigInt& left, const BigInt& right);
friend bool operator > ( const BigInt& left, unsigned long right);
friend bool operator > ( const BigInt& left, long right);
friend bool operator > ( const BigInt& left, const std::string& str);
friend bool operator > ( const std::string& left, const BigInt& right);
friend bool operator <= ( const BigInt& left, const BigInt& right);
friend bool operator <= ( const BigInt& left, unsigned long right);
friend bool operator <= ( const BigInt& left, long right);
friend bool operator <= ( const BigInt& left, const std::string& str);
friend bool operator <= ( const std::string& left, const BigInt& right);
friend bool operator >= ( const BigInt& left, const BigInt& right);
friend bool operator >= ( const BigInt& left, unsigned long right);
friend bool operator >= ( const BigInt& left, long right);
friend bool operator >= ( const BigInt& left, const std::string& str);
friend bool operator >= ( const std::string& left, const BigInt& right);
friend bool operator!(const BigInt& val);
@ -137,27 +150,21 @@ public:
friend BigInt operator| (BigInt left, const BigInt& right);
friend BigInt operator| (const BigInt &left, long right);
friend BigInt operator| (const BigInt &left, unsigned long right);
friend BigInt& operator|= (BigInt &left, const BigInt& right);
friend BigInt& operator|= (BigInt &left, long right);
friend BigInt& operator|= (BigInt &left, unsigned long right);
friend BigInt operator& (BigInt left, const BigInt& right);
friend BigInt operator& (const BigInt &left, long right);
friend BigInt operator& (const BigInt &left, unsigned long right);
friend BigInt& operator&= (BigInt &left, const BigInt& right);
friend BigInt& operator&= (BigInt &left, long right);
friend BigInt& operator&= (BigInt &left, unsigned long right);
friend BigInt operator^ (BigInt left, const BigInt& right);
friend BigInt operator^ (const BigInt &left, long right);
friend BigInt operator^ (const BigInt &left, unsigned long right);
friend BigInt& operator^= (BigInt &left, const BigInt& right);
friend BigInt& operator^= (BigInt &left, long right);
friend BigInt& operator^= (BigInt &left, unsigned long right);
};

@ -85,7 +85,7 @@ void arithmetictests::constructorsTest() {
BigInt num1; // should be 0 by default
QVERIFY(num1 == 0);
BigInt num2(0); // 0 passed as an integer
BigInt num2 (0); // 0 passed as an integer
QVERIFY(num2 == 0);
BigInt num3("0"); // 0 passed as a string
@ -164,7 +164,6 @@ void arithmetictests::arithmeticAssignmentMultiplicationTest() {
QVERIFY(num3 == "20762936122929888116068405550279299956640101675030592995600");
num3 *= 0;
QVERIFY(num3 == 0);
num3 = 1;
num3 *= -1234567890;
QVERIFY(num3 == -1234567890);