This commit is contained in:
CupWater
2026-08-16 15:20:11 +08:00
parent 83a75d4e76
commit feac230fef
12 changed files with 30 additions and 34 deletions
+70
View File
@@ -0,0 +1,70 @@
#ifndef BETTERSTL_BIG_INTEGER_HPP
#define BETTERSTL_BIG_INTEGER_HPP
#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);
};
} // namespace bstl
#endif //BETTERSTL_BIG_INTEGER_HPP