String和stdio支持 #2

Merged
ArchZer0 merged 5 commits from archzero_dev into main 2026-08-16 16:57:13 +08:00
11 changed files with 249 additions and 0 deletions
Showing only changes of commit ceb7c3fa44 - Show all commits
+5
View File
@@ -42,6 +42,11 @@ target_include_directories(StringTests PRIVATE include)
target_link_libraries(StringTests PRIVATE BetterSTL_static) target_link_libraries(StringTests PRIVATE BetterSTL_static)
add_test(NAME StringTests COMMAND StringTests) add_test(NAME StringTests COMMAND StringTests)
add_executable(IOTests tests/IOTest.cpp)
target_include_directories(IOTests PRIVATE include)
target_link_libraries(IOTests PRIVATE BetterSTL_static)
add_test(NAME IOTests COMMAND IOTests)
# 生成导出信息用于外部使用 # 生成导出信息用于外部使用
install(TARGETS BetterSTL_static BetterSTL_shared install(TARGETS BetterSTL_static BetterSTL_shared
LIBRARY DESTINATION lib LIBRARY DESTINATION lib
+91
View File
@@ -0,0 +1,91 @@
# BetterSTL cin/cout 支持
现在三个核心类都支持标准输入输出:
## BigInteger - 高精度整数
```cpp
#include <bstl/big_integer.hpp>
#include <iostream>
bstl::BigInteger bi;
std::cout << "Enter a large integer: ";
std::cin >> bi;
std::cout << "You entered: " << bi << std::endl;
```
支持任意精度的整数输入输出,不受 `long long` 限制。
## BigDecimal - 高精度浮点数
```cpp
#include <bstl/big_decimal.hpp>
#include <iostream>
bstl::BigDecimal bd;
std::cout << "Enter a decimal number: ";
std::cin >> bd;
std::cout << "You entered: " << bd << std::endl;
```
支持任意精度的浮点数输入输出,精度不丢失。
## String - 增强字符串
```cpp
#include <bstl/string.hpp>
#include <iostream>
bstl::String str;
std::cout << "Enter a word: ";
std::cin >> str;
std::cout << "You entered: " << str << std::endl;
// 可以使用 Python 风格的方法
std::cout << "Uppercase: " << str.upper() << std::endl;
std::cout << "Split: ";
auto parts = str.split("_");
```
## 使用示例
编译和运行演示程序:
```bash
cd /home/archzero/C++/BetterSTL
cmake -S . -B out/build -G Ninja
cmake --build out/build
# 运行库测试
ctest --test-dir out/build
# 运行演示(需要在终端交互)
./out/build/IOTests
```
## 实现细节
### operator<<(输出)
- BigInteger: 通过 `toString()` 输出
- BigDecimal: 通过 `toString()` 输出
- String: 通过 `str()` 输出
### operator>>(输入)
- BigInteger: 读取字符串,创建新对象
- BigDecimal: 读取字符串,创建新对象
- String: 读取字符串,创建新对象
所有运算符都定义在 `bstl` 命名空间中。
## 编译链接
在编译你的程序时:
```bash
g++ your_program.cpp -I/path/to/include -L/path/to/lib -lBetterSTL -o your_program
```
或者使用静态库:
```bash
g++ your_program.cpp -I/path/to/include /path/to/libBetterSTL.a -o your_program
```
+41
View File
@@ -0,0 +1,41 @@
#include <bstl/big_integer.hpp>
#include <bstl/big_decimal.hpp>
#include <bstl/string.hpp>
#include <iostream>
int main() {
using bstl::BigInteger;
using bstl::BigDecimal;
using bstl::String;
std::cout << "=== BetterSTL cin/cout Demo ===\n\n";
// BigInteger 示例
std::cout << "BigInteger Example:\n";
std::cout << "Enter a large integer: ";
BigInteger bi;
std::cin >> bi;
std::cout << "You entered: " << bi << "\n";
std::cout << "Doubled: " << (bi * BigInteger(2)) << "\n\n";
// BigDecimal 示例
std::cout << "BigDecimal Example:\n";
std::cout << "Enter a decimal number: ";
BigDecimal bd;
std::cin >> bd;
std::cout << "You entered: " << bd << "\n";
std::cout << "Doubled: " << (bd * BigDecimal(2)) << "\n\n";
// String 示例
std::cout << "String Example:\n";
std::cout << "Enter a word: ";
String str;
std::cin >> str;
std::cout << "You entered: " << str << "\n";
std::cout << "Uppercase: " << str.upper() << "\n";
std::cout << "Lowercase: " << str.lower() << "\n\n";
std::cout << "Demo completed!\n";
return 0;
}
Executable
BIN
View File
Binary file not shown.
+5
View File
@@ -2,6 +2,7 @@
#define BETTERSTL_BIG_DECIMAL_H #define BETTERSTL_BIG_DECIMAL_H
#include <string> #include <string>
#include <iostream>
#include <bstl/big_integer.hpp> #include <bstl/big_integer.hpp>
namespace bstl { namespace bstl {
@@ -69,6 +70,10 @@ private:
BigInteger& rhs, int& rhsScale); BigInteger& rhs, int& rhsScale);
}; };
// 流运算符重载
std::ostream& operator<<(std::ostream& os, const BigDecimal& bd);
std::istream& operator>>(std::istream& is, BigDecimal& bd);
} // namespace bstl } // namespace bstl
#endif //BETTERSTL_BIG_DECIMAL_H #endif //BETTERSTL_BIG_DECIMAL_H
+5
View File
@@ -2,6 +2,7 @@
#define BETTERSTL_BIG_INTEGER_HPP #define BETTERSTL_BIG_INTEGER_HPP
#include <string> #include <string>
#include <iostream>
namespace bstl { namespace bstl {
@@ -65,6 +66,10 @@ 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);
}; };
// 流运算符重载
std::ostream& operator<<(std::ostream& os, const BigInteger& bi);
std::istream& operator>>(std::istream& is, BigInteger& bi);
} // namespace bstl } // namespace bstl
#endif //BETTERSTL_BIG_INTEGER_HPP #endif //BETTERSTL_BIG_INTEGER_HPP
+5
View File
@@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <iostream>
namespace bstl { namespace bstl {
@@ -89,4 +90,8 @@ private:
std::string data_; std::string data_;
}; };
// 流运算符重载
std::ostream& operator<<(std::ostream& os, const String& str);
std::istream& operator>>(std::istream& is, String& str);
} // namespace bstl } // namespace bstl
+11
View File
@@ -338,4 +338,15 @@ BigDecimal BigDecimal::round(int scale) const {
return BigDecimal(rounded, scale); return BigDecimal(rounded, scale);
} }
std::ostream& operator<<(std::ostream& os, const BigDecimal& bd) {
return os << bd.toString();
}
std::istream& operator>>(std::istream& is, BigDecimal& bd) {
std::string str;
is >> str;
bd = BigDecimal(str);
return is;
}
} // namespace bstl } // namespace bstl
+11
View File
@@ -374,5 +374,16 @@ BigInteger BigInteger::operator/(const BigInteger& other) const {
return result; return result;
} }
std::ostream& operator<<(std::ostream& os, const BigInteger& bi) {
return os << bi.toString();
}
std::istream& operator>>(std::istream& is, BigInteger& bi) {
std::string str;
is >> str;
bi = BigInteger(str);
return is;
}
} // namespace bstl } // namespace bstl
+11
View File
@@ -359,4 +359,15 @@ bool String::operator>=(const String& other) const {
return data_ >= other.data_; return data_ >= other.data_;
} }
std::ostream& operator<<(std::ostream& os, const String& str) {
return os << str.str();
}
std::istream& operator>>(std::istream& is, String& str) {
std::string temp;
is >> temp;
str = String(temp);
return is;
}
} // namespace bstl } // namespace bstl
+64
View File
@@ -0,0 +1,64 @@
#include <bstl/big_integer.hpp>
#include <bstl/big_decimal.hpp>
#include <bstl/string.hpp>
#include <sstream>
#include <cassert>
#include <iostream>
int main() {
using bstl::BigInteger;
using bstl::BigDecimal;
using bstl::String;
// 测试 BigInteger 的 cout
BigInteger bi1("12345678901234567890");
std::ostringstream oss1;
oss1 << bi1;
assert(oss1.str() == "12345678901234567890");
// 测试 BigInteger 的 cin
std::istringstream iss1("987654321");
BigInteger bi2;
iss1 >> bi2;
assert(bi2.toString() == "987654321");
// 测试负数
std::istringstream iss2("-555");
BigInteger bi3;
iss2 >> bi3;
assert(bi3.toString() == "-555");
// 测试 BigDecimal 的 cout
BigDecimal bd1("123.45");
std::ostringstream oss2;
oss2 << bd1;
assert(oss2.str() == "123.45");
// 测试 BigDecimal 的 cin
std::istringstream iss3("67.89");
BigDecimal bd2;
iss3 >> bd2;
assert(bd2.toString() == "67.89");
// 测试负小数
std::istringstream iss4("-12.34");
BigDecimal bd3;
iss4 >> bd3;
assert(bd3.toString() == "-12.34");
// 测试 String 的 cout
String s1("Hello World");
std::ostringstream oss3;
oss3 << s1;
assert(oss3.str() == "Hello World");
// 测试 String 的 cin
std::istringstream iss5("HelloWorld");
String s2;
iss5 >> s2;
assert(s2.str() == "HelloWorld");
std::cout << "All cin/cout tests passed!\n";
return 0;
}