Biginteger/decimal

This commit is contained in:
ArchZer0
2026-08-16 14:55:25 +08:00
parent abd82e6961
commit 5b3320e65c
8 changed files with 975 additions and 6 deletions
+69
View File
@@ -0,0 +1,69 @@
#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