Merge remote-tracking branch 'origin/main'

# Conflicts:
#	include/bstl/bstl.hpp
#	src/BigDecimal.cpp
#	src/BigInteger.cpp
This commit is contained in:
CupWater
2026-08-16 15:02:29 +08:00
8 changed files with 976 additions and 7 deletions
+72
View File
@@ -0,0 +1,72 @@
#pragma once
#include <string>
#include "BigInteger.hpp"
namespace bstl {
class BigDecimal {
public:
BigDecimal();
BigDecimal(int value);
BigDecimal(long long value);
BigDecimal(double value);
explicit BigDecimal(const std::string& value);
explicit BigDecimal(const char* value);
BigDecimal(const BigInteger& unscaledValue, int scale);
BigDecimal(const BigDecimal& other) = default;
BigDecimal& operator=(const BigDecimal& other) = default;
bool isZero() const;
int sign() const;
int getScale() const;
BigInteger getUnscaledValue() const;
std::string toString() const;
// Comparison operators
bool operator==(const BigDecimal& other) const;
bool operator!=(const BigDecimal& other) const;
bool operator<(const BigDecimal& other) const;
bool operator>(const BigDecimal& other) const;
bool operator<=(const BigDecimal& other) const;
bool operator>=(const BigDecimal& 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==(double value) const;
bool operator!=(double value) const;
bool operator<(double value) const;
bool operator>(double value) const;
bool operator<=(double value) const;
bool operator>=(double value) const;
// Arithmetic operators
BigDecimal operator+() const;
BigDecimal operator-() const;
BigDecimal& operator+=(const BigDecimal& other);
BigDecimal& operator-=(const BigDecimal& other);
BigDecimal& operator*=(const BigDecimal& other);
BigDecimal& operator/=(const BigDecimal& other);
BigDecimal operator+(const BigDecimal& other) const;
BigDecimal operator-(const BigDecimal& other) const;
BigDecimal operator*(const BigDecimal& other) const;
BigDecimal operator/(const BigDecimal& other) const;
BigDecimal abs() const;
BigDecimal round(int scale) const;
private:
BigInteger unscaledValue_; // 去掉小数点后的值
int scale_; // 小数点后的位数
static void alignScale(BigInteger& lhs, int& lhsScale,
BigInteger& rhs, int& rhsScale);
};
} // namespace bstl
+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
+4 -3
View File
@@ -1,6 +1,7 @@
#ifndef BETTERSTL_BSTL_HPP
#define BETTERSTL_BSTL_HPP
#include<bstl/BigDecimal.hpp>
#include<bstlBigInteger.hpp>
#include <bstl/bstlint.hpp>
#define BETTERSTL_BSTL_HPP
#endif //BETTERSTL_BSTL_HPP