diff --git a/CMakeLists.txt b/CMakeLists.txt index fa21fe4..8656933 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,11 @@ target_include_directories(StringTests PRIVATE include) target_link_libraries(StringTests PRIVATE BetterSTL_static) 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 LIBRARY DESTINATION lib diff --git a/IO_SUPPORT.md b/IO_SUPPORT.md new file mode 100644 index 0000000..eddd0c3 --- /dev/null +++ b/IO_SUPPORT.md @@ -0,0 +1,91 @@ +# BetterSTL cin/cout 支持 + +现在三个核心类都支持标准输入输出: + +## BigInteger - 高精度整数 + +```cpp +#include +#include + +bstl::BigInteger bi; +std::cout << "Enter a large integer: "; +std::cin >> bi; +std::cout << "You entered: " << bi << std::endl; +``` + +支持任意精度的整数输入输出,不受 `long long` 限制。 + +## BigDecimal - 高精度浮点数 + +```cpp +#include +#include + +bstl::BigDecimal bd; +std::cout << "Enter a decimal number: "; +std::cin >> bd; +std::cout << "You entered: " << bd << std::endl; +``` + +支持任意精度的浮点数输入输出,精度不丢失。 + +## String - 增强字符串 + +```cpp +#include +#include + +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 +``` diff --git a/examples/IODemo.cpp b/examples/IODemo.cpp new file mode 100644 index 0000000..8800568 --- /dev/null +++ b/examples/IODemo.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +#include + +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; +} diff --git a/examples/a.out b/examples/a.out new file mode 100755 index 0000000..978346a Binary files /dev/null and b/examples/a.out differ diff --git a/include/bstl/big_decimal.hpp b/include/bstl/big_decimal.hpp index 59b8639..0ce87b6 100644 --- a/include/bstl/big_decimal.hpp +++ b/include/bstl/big_decimal.hpp @@ -2,6 +2,7 @@ #define BETTERSTL_BIG_DECIMAL_H #include +#include #include namespace bstl { @@ -69,6 +70,10 @@ private: BigInteger& rhs, int& rhsScale); }; +// 流运算符重载 +std::ostream& operator<<(std::ostream& os, const BigDecimal& bd); +std::istream& operator>>(std::istream& is, BigDecimal& bd); + } // namespace bstl #endif //BETTERSTL_BIG_DECIMAL_H diff --git a/include/bstl/big_integer.hpp b/include/bstl/big_integer.hpp index c4f12d9..2f6e088 100644 --- a/include/bstl/big_integer.hpp +++ b/include/bstl/big_integer.hpp @@ -2,6 +2,7 @@ #define BETTERSTL_BIG_INTEGER_HPP #include +#include namespace bstl { @@ -65,6 +66,10 @@ private: 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 #endif //BETTERSTL_BIG_INTEGER_HPP diff --git a/include/bstl/string.hpp b/include/bstl/string.hpp index 1253201..37588fe 100644 --- a/include/bstl/string.hpp +++ b/include/bstl/string.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace bstl { @@ -89,4 +90,8 @@ private: std::string data_; }; +// 流运算符重载 +std::ostream& operator<<(std::ostream& os, const String& str); +std::istream& operator>>(std::istream& is, String& str); + } // namespace bstl diff --git a/src/big_decimal.cpp b/src/big_decimal.cpp index 52291ee..2570da5 100644 --- a/src/big_decimal.cpp +++ b/src/big_decimal.cpp @@ -338,4 +338,15 @@ BigDecimal BigDecimal::round(int scale) const { 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 diff --git a/src/big_integer.cpp b/src/big_integer.cpp index 3ed86c3..4436fd9 100644 --- a/src/big_integer.cpp +++ b/src/big_integer.cpp @@ -374,5 +374,16 @@ BigInteger BigInteger::operator/(const BigInteger& other) const { 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 diff --git a/src/string.cpp b/src/string.cpp index 0c8b784..e1e2a1a 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -359,4 +359,15 @@ bool String::operator>=(const String& other) const { 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 diff --git a/tests/IOTest.cpp b/tests/IOTest.cpp new file mode 100644 index 0000000..eae9339 --- /dev/null +++ b/tests/IOTest.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +#include +#include +#include + +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; +}