Files
2026-08-16 19:23:09 +08:00

77 lines
2.7 KiB
C++

#ifndef BETTERSTL_BIG_INTEGER_HPP
#define BETTERSTL_BIG_INTEGER_HPP
#include <string>
#include <vector>
#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::vector<int> m_digits; // 无符号数字,低位在前
int m_sign; // -1, 0, 1
static std::vector<int> m_trimLeadingZeros(const std::vector<int>& value);
static int m_compareAbs(const std::vector<int>& a, const std::vector<int>& b);
static std::vector<int> m_addAbs(const std::vector<int>& a, const std::vector<int>& b);
static std::vector<int> m_subAbs(const std::vector<int>& a, const std::vector<int>& b);
static std::vector<int> m_mulAbs(const std::vector<int>& a, const std::vector<int>& b);
static std::vector<int> m_divAbs(const std::vector<int>& a, const std::vector<int>& 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