SHA256
76 lines
2.6 KiB
C++
76 lines
2.6 KiB
C++
#ifndef BETTERSTL_BIG_INTEGER_HPP
|
|
#define BETTERSTL_BIG_INTEGER_HPP
|
|
|
|
#include <string>
|
|
#include <iostream>
|
|
|
|
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;
|
|
|
|
[[nodiscard]] bool isZero() const;
|
|
[[nodiscard]] int sign() const;
|
|
[[nodiscard]] 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 m_digits; // 无符号数字,从高位到低位
|
|
int m_sign; // -1, 0, 1
|
|
|
|
static std::string m_trimLeadingZeros(const std::string& value);
|
|
static int m_compareAbs(const std::string& a, const std::string& b);
|
|
static std::string m_addAbs(const std::string& a, const std::string& b);
|
|
static std::string m_subAbs(const std::string& a, const std::string& b);
|
|
static std::string m_mulAbs(const std::string& a, const std::string& b);
|
|
static std::string divAbs(const std::string& a, const std::string& b);
|
|
};
|
|
|
|
// 流运算符重载
|
|
std::ostream& operator<<(std::ostream& os, const BigInteger& bi);
|
|
std::istream& operator>>(std::istream& is, BigInteger& bi);
|
|
|
|
} // namespace bstl
|
|
|
|
#endif //BETTERSTL_BIG_INTEGER_HPP
|