SHA256
Compare commits
3
Commits
5828ec6f4b
...
feac230fef
| Author | SHA256 | Date | |
|---|---|---|---|
|
|
feac230fef | ||
|
|
83a75d4e76 | ||
|
|
fb2b24bc72 |
+5
-5
@@ -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)
|
||||
|
||||
@@ -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 "BigInteger.hpp"
|
||||
#include <bstl/big_integer.hpp>
|
||||
|
||||
namespace bstl {
|
||||
|
||||
class BigDecimal {
|
||||
class BigDecimal {
|
||||
public:
|
||||
BigDecimal();
|
||||
BigDecimal(int value);
|
||||
@@ -70,3 +70,5 @@ private:
|
||||
};
|
||||
|
||||
} // 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>
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -2,7 +2,6 @@
|
||||
#define BETTERSTL_BSTLINT_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <>
|
||||
|
||||
typedef std::int8_t i8;
|
||||
typedef std::int16_t i16;
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "BigDecimal.hpp"
|
||||
#include <bstl/big_decimal.hpp>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
@@ -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;
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "BigInteger.hpp"
|
||||
#include <bstl/big_integer.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#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 tb = trimLeadingZeros(b);
|
||||
if (ta == "0" || tb == "0") return "0";
|
||||
|
||||
|
||||
std::vector<int> 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<int>(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_);
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <BigDecimal.hpp>
|
||||
#include <bstl/big_decimal.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <BigInteger.hpp>
|
||||
#include <bstl/big_integer.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
Reference in New Issue
Block a user