SHA256
70 lines
2.4 KiB
C++
70 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
|
|
namespace bstl {
|
|
|
|
class BigInteger {
|
|
public:
|
|
BigInteger();
|
|
BigInteger(int value);
|
|
BigInteger(long long value);
|
|
BigInteger(unsigned long long value);
|
|
explicit BigInteger(const std::string& value);
|
|
explicit BigInteger(const char* value);
|
|
BigInteger(const BigInteger& other) = default;
|
|
BigInteger& operator=(const BigInteger& other) = default;
|
|
|
|
bool isZero() const;
|
|
int sign() const;
|
|
std::string toString() const;
|
|
|
|
bool operator==(const BigInteger& other) const;
|
|
bool operator!=(const BigInteger& other) const;
|
|
bool operator<(const BigInteger& other) const;
|
|
bool operator>(const BigInteger& other) const;
|
|
bool operator<=(const BigInteger& other) const;
|
|
bool operator>=(const BigInteger& other) const;
|
|
|
|
bool operator==(int value) const;
|
|
bool operator!=(int value) const;
|
|
bool operator<(int value) const;
|
|
bool operator>(int value) const;
|
|
bool operator<=(int value) const;
|
|
bool operator>=(int value) const;
|
|
|
|
bool operator==(long long value) const;
|
|
bool operator!=(long long value) const;
|
|
bool operator<(long long value) const;
|
|
bool operator>(long long value) const;
|
|
bool operator<=(long long value) const;
|
|
bool operator>=(long long value) const;
|
|
|
|
BigInteger operator+() const;
|
|
BigInteger operator-() const;
|
|
BigInteger& operator+=(const BigInteger& other);
|
|
BigInteger& operator-=(const BigInteger& other);
|
|
BigInteger& operator*=(const BigInteger& other);
|
|
BigInteger& operator/=(const BigInteger& other);
|
|
|
|
BigInteger operator+(const BigInteger& other) const;
|
|
BigInteger operator-(const BigInteger& other) const;
|
|
BigInteger operator*(const BigInteger& other) const;
|
|
BigInteger operator/(const BigInteger& other) const;
|
|
|
|
private:
|
|
std::string digits_; // 无符号数字,从高位到低位
|
|
int sign_; // -1, 0, 1
|
|
|
|
static std::string trimLeadingZeros(const std::string& value);
|
|
static int compareAbs(const std::string& a, const std::string& b);
|
|
static std::string addAbs(const std::string& a, const std::string& b);
|
|
static std::string subAbs(const std::string& a, const std::string& b);
|
|
static std::string mulAbs(const std::string& a, const std::string& b);
|
|
static std::string divAbs(const std::string& a, const std::string& b);
|
|
};
|
|
|
|
using BitInteger = BigInteger;
|
|
|
|
} // namespace bstl
|