#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
int main() {
std::vector<lab::Case> cases;
// Case 1: setw, setfill, left, right, internal
std::ostringstream s1, s2, s3, s4, s5, s6;
s1 << std::setw(5) << 42;
s2 << std::setfill('*') << std::setw(5) << 42;
s3 << std::left << std::setw(5) << 42;
s4 << std::right << std::setw(5) << 42;
s5 << std::internal << std::setw(5) << -42;
s6 << std::setfill('*') << std::setw(10) << "World";
bool ok1 = (s1.str() == " 42") &&
(s2.str() == "***42") &&
(s3.str() == "42 ") &&
(s4.str() == " 42") &&
(s5.str() == "- 42") &&
(s6.str() == "*****World");
cases.push_back({
"iomanip_width_fill_alignment",
lab::jbool(true),
lab::jbool(ok1),
ok1
});
// Case 2: setprecision, fixed, scientific
std::ostringstream p1, p2, p3, p4, p5;
double pi = 3.141592653589793;
p1 << std::setprecision(3) << pi;
p2 << std::setprecision(7) << pi;
p3 << std::fixed << std::setprecision(2) << 3.14159;
p4 << std::fixed << 123456789.0;
p5 << std::scientific << 3.14159;
bool ok2 = (p1.str() == "3.14") &&
(p2.str() == "3.141593") &&
(p3.str() == "3.14") &&
(p4.str() == "123456789.000000") &&
(p5.str() == "3.141590e+00");
cases.push_back({
"iomanip_precision_fixed_scientific",
lab::jbool(true),
lab::jbool(ok2),
ok2
});
// Case 3: hex/oct/dec/setbase, showbase/noshowbase, uppercase/nouppercase, showpos/noshowpos, boolalpha/noboolalpha
std::ostringstream b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12;
b1 << std::hex << 42;
b2 << std::oct << 42;
b3 << std::dec << 42;
b4 << std::showbase << std::hex << 42;
b5 << std::noshowbase << std::hex << 42;
b6 << std::uppercase << std::hex << 42;
b7 << std::nouppercase << std::hex << 42;
b8 << std::showpos << 42;
b9 << std::noshowpos << 42;
b10 << std::boolalpha << true;
b11 << std::noboolalpha << true;
b12 << std::setbase(16) << 42;
bool ok3 = (b1.str() == "2a") && (b2.str() == "52") && (b3.str() == "42") &&
(b4.str() == "0x2a") && (b5.str() == "2a") &&
(b6.str() == "2A") && (b7.str() == "2a") &&
(b8.str() == "+42") && (b9.str() == "42") &&
(b10.str() == "true") && (b11.str() == "1") && (b12.str() == "2a");
cases.push_back({
"iomanip_base_showbase_case_sign_bool",
lab::jbool(true),
lab::jbool(ok3),
ok3
});
// Case 4: setiosflags and resetiosflags
std::ostringstream f1, f2, f3;
f1 << std::setiosflags(std::ios::uppercase) << std::hex << 255;
f2 << std::setiosflags(std::ios::uppercase) << std::resetiosflags(std::ios::uppercase) << std::hex << 255;
f3 << std::setiosflags(std::ios::showbase) << std::hex << 42 << "/"
<< std::resetiosflags(std::ios::showbase) << std::hex << 42;
std::string act4 = f1.str() + "|" + f2.str() + "|" + f3.str();
std::string exp4 = "FF|ff|0x2a/2a";
cases.push_back({
"iomanip_setiosflags_resetiosflags",
lab::esc(exp4),
lab::esc(act4),
act4 == exp4
});
lab::emit(cases);
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cpp-libs-iomanip-lab/explore-cpp-libs-iomanip.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-iomanip.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2352.zip