diff --git a/CMakeLists.txt b/CMakeLists.txt index 65ed0a9..057169b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,9 +3,9 @@ project(BetterSTL VERSION 0.1.0 LANGUAGES C CXX) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # 定义源文件 -set(BETTERSTL_SOURCES - src/BigInteger.cpp - src/BigDecimal.cpp) +set(BETTERSTL_SOURCES + src/big_integer.cpp + src/big_decimal.cpp) # 生成静态库 (.a 文件) add_library(BetterSTL_static STATIC ${BETTERSTL_SOURCES}) @@ -26,12 +26,12 @@ add_library(BetterSTL ALIAS BetterSTL_static) enable_testing() -add_executable(BigIntegerTests tests/BigIntegerTest.cpp) +add_executable(BigIntegerTests tests/big_integer_test.cpp) target_include_directories(BigIntegerTests PRIVATE include) target_link_libraries(BigIntegerTests PRIVATE BetterSTL_static) 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_link_libraries(BigDecimalTests PRIVATE BetterSTL_static) add_test(NAME BigDecimalTests COMMAND BigDecimalTests) diff --git a/include/bstl.hpp b/include/bstl.hpp deleted file mode 100644 index 4a94a8b..0000000 --- a/include/bstl.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once -#include -#include -#include -#ifndef BETTERSTL_BSTL_HPP -#define BETTERSTL_BSTL_HPP -#endif //BETTERSTL_BSTL_HPP diff --git a/include/BigDecimal.hpp b/include/bstl/big_decimal.hpp similarity index 93% rename from include/BigDecimal.hpp rename to include/bstl/big_decimal.hpp index 322cc73..59b8639 100644 --- a/include/BigDecimal.hpp +++ b/include/bstl/big_decimal.hpp @@ -1,11 +1,11 @@ -#pragma once +#ifndef BETTERSTL_BIG_DECIMAL_H +#define BETTERSTL_BIG_DECIMAL_H #include -#include "BigInteger.hpp" +#include namespace bstl { - -class BigDecimal { + class BigDecimal { public: BigDecimal(); BigDecimal(int value); @@ -70,3 +70,5 @@ private: }; } // namespace bstl + +#endif //BETTERSTL_BIG_DECIMAL_H diff --git a/include/BigInteger.hpp b/include/bstl/big_integer.hpp similarity index 95% rename from include/BigInteger.hpp rename to include/bstl/big_integer.hpp index ed6a4a9..c4f12d9 100644 --- a/include/BigInteger.hpp +++ b/include/bstl/big_integer.hpp @@ -1,4 +1,5 @@ -#pragma once +#ifndef BETTERSTL_BIG_INTEGER_HPP +#define BETTERSTL_BIG_INTEGER_HPP #include @@ -64,6 +65,6 @@ private: static std::string divAbs(const std::string& a, const std::string& b); }; -using BitInteger = BigInteger; - } // namespace bstl + +#endif //BETTERSTL_BIG_INTEGER_HPP diff --git a/include/bstl/big_number.hpp b/include/bstl/big_number.hpp new file mode 100644 index 0000000..ddd2e3a --- /dev/null +++ b/include/bstl/big_number.hpp @@ -0,0 +1,7 @@ +#ifndef BETTERSTL_BIG_NUMBERS_HPP +#define BETTERSTL_BIG_NUMBERS_HPP + +#include +#include + +#endif // BETTERSTL_BIG_NUMBERS_HPP diff --git a/include/bstl/bstl.hpp b/include/bstl/bstl.hpp new file mode 100644 index 0000000..8ca613f --- /dev/null +++ b/include/bstl/bstl.hpp @@ -0,0 +1,8 @@ +#ifndef BETTERSTL_BSTL_HPP + +#include +#include +#include + +#define BETTERSTL_BSTL_HPP +#endif //BETTERSTL_BSTL_HPP diff --git a/include/bstlint.hpp b/include/bstl/bstlint.hpp similarity index 100% rename from include/bstlint.hpp rename to include/bstl/bstlint.hpp diff --git a/src/BigDecimal.cpp b/src/big_decimal.cpp similarity index 99% rename from src/BigDecimal.cpp rename to src/big_decimal.cpp index c4d429c..52291ee 100644 --- a/src/BigDecimal.cpp +++ b/src/big_decimal.cpp @@ -1,4 +1,4 @@ -#include "BigDecimal.hpp" +#include #include #include @@ -32,7 +32,7 @@ BigDecimal::BigDecimal(const std::string& value) : unscaledValue_(0), scale_(0) } std::string text = value; - + // 去掉前后空格 size_t start = 0, end = text.size(); while (start < end && text[start] == ' ') start++; @@ -87,7 +87,7 @@ std::string BigDecimal::toString() const { } std::string digitStr = unscaledValue_.toString(); - + // 处理符号 bool negative = false; if (digitStr[0] == '-') { @@ -156,7 +156,7 @@ bool BigDecimal::operator<(const BigDecimal& other) const { int lhsScale = scale_; int rhsScale = other.scale_; alignScale(lhs, lhsScale, rhs, rhsScale); - + if (unscaledValue_.sign() >= 0) { return lhs < rhs; } else { @@ -297,7 +297,7 @@ BigDecimal BigDecimal::operator/(const BigDecimal& other) const { BigInteger rhs = other.unscaledValue_; int lhsScale = scale_; int rhsScale = other.scale_; - + // 使用更高的精度进行除法 // 乘以 10^precision 来获得更多的精度 int precision = 10; diff --git a/src/BigInteger.cpp b/src/big_integer.cpp similarity index 99% rename from src/BigInteger.cpp rename to src/big_integer.cpp index 8353f30..3ed86c3 100644 --- a/src/BigInteger.cpp +++ b/src/big_integer.cpp @@ -1,4 +1,4 @@ -#include "BigInteger.hpp" +#include #include #include @@ -69,7 +69,7 @@ std::string BigInteger::mulAbs(const std::string& a, const std::string& b) { std::string ta = trimLeadingZeros(a); std::string tb = trimLeadingZeros(b); if (ta == "0" || tb == "0") return "0"; - + std::vector product(ta.size() + tb.size(), 0); for (size_t i = 0; i < ta.size(); i++) { 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; } } - + int carry = 0; for (size_t i = 0; i < product.size(); i++) { int sum = product[i] + carry; product[i] = sum % 10; carry = sum / 10; } - + std::string result; for (int i = static_cast(product.size()) - 1; i >= 0; 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) { return "0"; } - + std::string quotient; std::string current; for (char digit : ta) { @@ -172,29 +172,29 @@ BigInteger::BigInteger(const std::string& value) : digits_("0"), sign_(1) { sign_ = 0; return; } - + std::string text = value; sign_ = 1; - + if (text[0] == '-') { sign_ = -1; text = text.substr(1); } else if (text[0] == '+') { text = text.substr(1); } - + if (text.empty()) { digits_ = "0"; sign_ = 0; return; } - + for (char ch : text) { if (ch < '0' || ch > '9') { throw std::invalid_argument("BigInteger: invalid numeric string"); } } - + digits_ = trimLeadingZeros(text); if (digits_ == "0") { sign_ = 0; @@ -327,7 +327,7 @@ BigInteger& BigInteger::operator/=(const BigInteger& other) { BigInteger BigInteger::operator+(const BigInteger& other) const { if (isZero()) return other; if (other.isZero()) return *this; - + BigInteger result; if (sign_ == other.sign_) { result.digits_ = addAbs(digits_, other.digits_); diff --git a/tests/BigDecimalTest.cpp b/tests/big_decimal_test.cpp similarity index 95% rename from tests/BigDecimalTest.cpp rename to tests/big_decimal_test.cpp index 8a14df4..d168e21 100644 --- a/tests/BigDecimalTest.cpp +++ b/tests/big_decimal_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/tests/BigIntegerTest.cpp b/tests/big_integer_test.cpp similarity index 96% rename from tests/BigIntegerTest.cpp rename to tests/big_integer_test.cpp index 475478b..701a353 100644 --- a/tests/BigIntegerTest.cpp +++ b/tests/big_integer_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include