Compare commits

..
4 Commits
Author SHA256 Message Date
ArchZer0 c145fa8d05 Merge branch 'main' into wcjbr_dev 2026-08-16 15:21:50 +08:00
CupWater feac230fef merge 2026-08-16 15:20:11 +08:00
CupWater 83a75d4e76 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	include/bstl/bstl.hpp
#	src/BigDecimal.cpp
#	src/BigInteger.cpp
2026-08-16 15:02:29 +08:00
CupWater fb2b24bc72 修改bstlint 2026-08-16 15:00:09 +08:00
11 changed files with 48 additions and 37 deletions
+5 -5
View File
@@ -3,9 +3,9 @@ project(BetterSTL VERSION 0.1.0 LANGUAGES C CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# 定义源文件 # 定义源文件
set(BETTERSTL_SOURCES set(BETTERSTL_SOURCES
src/BigInteger.cpp src/big_integer.cpp
src/BigDecimal.cpp) src/big_decimal.cpp)
# 生成静态库 (.a 文件) # 生成静态库 (.a 文件)
add_library(BetterSTL_static STATIC ${BETTERSTL_SOURCES}) add_library(BetterSTL_static STATIC ${BETTERSTL_SOURCES})
@@ -26,12 +26,12 @@ add_library(BetterSTL ALIAS BetterSTL_static)
enable_testing() enable_testing()
add_executable(BigIntegerTests tests/BigIntegerTest.cpp) add_executable(BigIntegerTests tests/big_integer_test.cpp)
target_include_directories(BigIntegerTests PRIVATE include) target_include_directories(BigIntegerTests PRIVATE include)
target_link_libraries(BigIntegerTests PRIVATE BetterSTL_static) target_link_libraries(BigIntegerTests PRIVATE BetterSTL_static)
add_test(NAME BigIntegerTests COMMAND BigIntegerTests) add_test(NAME BigIntegerTests COMMAND BigIntegerTests)
add_executable(BigDecimalTests tests/BigDecimalTest.cpp) add_executable(BigDecimalTests tests/big_decimal_test.cpp)
target_include_directories(BigDecimalTests PRIVATE include) target_include_directories(BigDecimalTests PRIVATE include)
target_link_libraries(BigDecimalTests PRIVATE BetterSTL_static) target_link_libraries(BigDecimalTests PRIVATE BetterSTL_static)
add_test(NAME BigDecimalTests COMMAND BigDecimalTests) add_test(NAME BigDecimalTests COMMAND BigDecimalTests)
-7
View File
@@ -1,7 +0,0 @@
#pragma once
#include<BigDecimal.hpp>
#include<BigInteger.hpp>
#include <bstlint.hpp>
#ifndef BETTERSTL_BSTL_HPP
#define BETTERSTL_BSTL_HPP
#endif //BETTERSTL_BSTL_HPP
@@ -1,11 +1,11 @@
#pragma once #ifndef BETTERSTL_BIG_DECIMAL_H
#define BETTERSTL_BIG_DECIMAL_H
#include <string> #include <string>
#include "BigInteger.hpp" #include <bstl/big_integer.hpp>
namespace bstl { namespace bstl {
class BigDecimal {
class BigDecimal {
public: public:
BigDecimal(); BigDecimal();
BigDecimal(int value); BigDecimal(int value);
@@ -70,3 +70,5 @@ private:
}; };
} // namespace bstl } // namespace bstl
#endif //BETTERSTL_BIG_DECIMAL_H
@@ -1,4 +1,5 @@
#pragma once #ifndef BETTERSTL_BIG_INTEGER_HPP
#define BETTERSTL_BIG_INTEGER_HPP
#include <string> #include <string>
@@ -64,6 +65,6 @@ private:
static std::string divAbs(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 } // namespace bstl
#endif //BETTERSTL_BIG_INTEGER_HPP
+7
View File
@@ -0,0 +1,7 @@
#ifndef BETTERSTL_BIG_NUMBERS_HPP
#define BETTERSTL_BIG_NUMBERS_HPP
#include <bstl/biginteger.hpp>
#include <bstl/bigdecimal.hpp>
#endif // BETTERSTL_BIG_NUMBERS_HPP
+8
View File
@@ -0,0 +1,8 @@
#ifndef BETTERSTL_BSTL_HPP
#include<bstl/big_decimal.hpp>
#include<bstl/big_integer.hpp>
#include <bstl/bstlint.hpp>
#define BETTERSTL_BSTL_HPP
#endif //BETTERSTL_BSTL_HPP
+5 -5
View File
@@ -1,4 +1,4 @@
#include "BigDecimal.hpp" #include <bstl/big_decimal.hpp>
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
@@ -32,7 +32,7 @@ BigDecimal::BigDecimal(const std::string& value) : unscaledValue_(0), scale_(0)
} }
std::string text = value; std::string text = value;
// 去掉前后空格 // 去掉前后空格
size_t start = 0, end = text.size(); size_t start = 0, end = text.size();
while (start < end && text[start] == ' ') start++; while (start < end && text[start] == ' ') start++;
@@ -87,7 +87,7 @@ std::string BigDecimal::toString() const {
} }
std::string digitStr = unscaledValue_.toString(); std::string digitStr = unscaledValue_.toString();
// 处理符号 // 处理符号
bool negative = false; bool negative = false;
if (digitStr[0] == '-') { if (digitStr[0] == '-') {
@@ -156,7 +156,7 @@ bool BigDecimal::operator<(const BigDecimal& other) const {
int lhsScale = scale_; int lhsScale = scale_;
int rhsScale = other.scale_; int rhsScale = other.scale_;
alignScale(lhs, lhsScale, rhs, rhsScale); alignScale(lhs, lhsScale, rhs, rhsScale);
if (unscaledValue_.sign() >= 0) { if (unscaledValue_.sign() >= 0) {
return lhs < rhs; return lhs < rhs;
} else { } else {
@@ -297,7 +297,7 @@ BigDecimal BigDecimal::operator/(const BigDecimal& other) const {
BigInteger rhs = other.unscaledValue_; BigInteger rhs = other.unscaledValue_;
int lhsScale = scale_; int lhsScale = scale_;
int rhsScale = other.scale_; int rhsScale = other.scale_;
// 使用更高的精度进行除法 // 使用更高的精度进行除法
// 乘以 10^precision 来获得更多的精度 // 乘以 10^precision 来获得更多的精度
int precision = 10; int precision = 10;
+11 -11
View File
@@ -1,4 +1,4 @@
#include "BigInteger.hpp" #include <bstl/big_integer.hpp>
#include <algorithm> #include <algorithm>
#include <stdexcept> #include <stdexcept>
@@ -69,7 +69,7 @@ std::string BigInteger::mulAbs(const std::string& a, const std::string& b) {
std::string ta = trimLeadingZeros(a); std::string ta = trimLeadingZeros(a);
std::string tb = trimLeadingZeros(b); std::string tb = trimLeadingZeros(b);
if (ta == "0" || tb == "0") return "0"; if (ta == "0" || tb == "0") return "0";
std::vector<int> product(ta.size() + tb.size(), 0); std::vector<int> product(ta.size() + tb.size(), 0);
for (size_t i = 0; i < ta.size(); i++) { for (size_t i = 0; i < ta.size(); i++) {
for (size_t j = 0; j < tb.size(); j++) { for (size_t j = 0; j < tb.size(); j++) {
@@ -77,14 +77,14 @@ std::string BigInteger::mulAbs(const std::string& a, const std::string& b) {
product[i + j] += mul; product[i + j] += mul;
} }
} }
int carry = 0; int carry = 0;
for (size_t i = 0; i < product.size(); i++) { for (size_t i = 0; i < product.size(); i++) {
int sum = product[i] + carry; int sum = product[i] + carry;
product[i] = sum % 10; product[i] = sum % 10;
carry = sum / 10; carry = sum / 10;
} }
std::string result; std::string result;
for (int i = static_cast<int>(product.size()) - 1; i >= 0; i--) { for (int i = static_cast<int>(product.size()) - 1; i >= 0; i--) {
result += char('0' + product[i]); result += char('0' + product[i]);
@@ -101,7 +101,7 @@ std::string BigInteger::divAbs(const std::string& a, const std::string& b) {
if (compareAbs(ta, tb) < 0) { if (compareAbs(ta, tb) < 0) {
return "0"; return "0";
} }
std::string quotient; std::string quotient;
std::string current; std::string current;
for (char digit : ta) { for (char digit : ta) {
@@ -172,29 +172,29 @@ BigInteger::BigInteger(const std::string& value) : digits_("0"), sign_(1) {
sign_ = 0; sign_ = 0;
return; return;
} }
std::string text = value; std::string text = value;
sign_ = 1; sign_ = 1;
if (text[0] == '-') { if (text[0] == '-') {
sign_ = -1; sign_ = -1;
text = text.substr(1); text = text.substr(1);
} else if (text[0] == '+') { } else if (text[0] == '+') {
text = text.substr(1); text = text.substr(1);
} }
if (text.empty()) { if (text.empty()) {
digits_ = "0"; digits_ = "0";
sign_ = 0; sign_ = 0;
return; return;
} }
for (char ch : text) { for (char ch : text) {
if (ch < '0' || ch > '9') { if (ch < '0' || ch > '9') {
throw std::invalid_argument("BigInteger: invalid numeric string"); throw std::invalid_argument("BigInteger: invalid numeric string");
} }
} }
digits_ = trimLeadingZeros(text); digits_ = trimLeadingZeros(text);
if (digits_ == "0") { if (digits_ == "0") {
sign_ = 0; sign_ = 0;
@@ -327,7 +327,7 @@ BigInteger& BigInteger::operator/=(const BigInteger& other) {
BigInteger BigInteger::operator+(const BigInteger& other) const { BigInteger BigInteger::operator+(const BigInteger& other) const {
if (isZero()) return other; if (isZero()) return other;
if (other.isZero()) return *this; if (other.isZero()) return *this;
BigInteger result; BigInteger result;
if (sign_ == other.sign_) { if (sign_ == other.sign_) {
result.digits_ = addAbs(digits_, other.digits_); result.digits_ = addAbs(digits_, other.digits_);
@@ -1,4 +1,4 @@
#include <BigDecimal.hpp> #include <bstl/big_decimal.hpp>
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
@@ -1,4 +1,4 @@
#include <BigInteger.hpp> #include <bstl/big_integer.hpp>
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>