#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <random>
#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"; }
std::string jint(long long v) { return std::to_string(v); }
std::string juint(unsigned long long v) { return std::to_string(v); }
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: Numeric definitions & default iostream formatting
short s = 10;
int i = 1000;
long l = 1000000L;
float f = 230.47f;
double d = 30949.374;
std::ostringstream oss;
oss << s << "," << i << "," << l << "," << f << "," << d;
cases.push_back({
"numeric_types_and_formatting",
lab::esc("10,1000,1000000,230.47,30949.4"),
lab::esc(oss.str()),
oss.str() == "10,1000,1000000,230.47,30949.4"
});
// Case 2: <cmath> core functions (cos, sin, tan, log, pow, hypot, sqrt, abs, fabs, floor)
bool math_ok = (std::fabs(std::cos(0.0) - 1.0) < 1e-12) &&
(std::fabs(std::sin(200.374) - (-0.634939)) < 1e-5) &&
(std::fabs(std::tan(0.0)) < 1e-12) &&
(std::fabs(std::log(std::exp(2.0)) - 2.0) < 1e-12) &&
(std::fabs(std::pow(2.0, 10.0) - 1024.0) < 1e-12) &&
(std::fabs(std::hypot(3.0, 4.0) - 5.0) < 1e-12) &&
(std::fabs(std::sqrt(144.0) - 12.0) < 1e-12) &&
(std::abs(-1000) == 1000) &&
(std::fabs(-12.5) == 12.5) &&
(std::floor(200.374) == 200.0);
cases.push_back({
"cmath_trigonometric_and_power",
lab::jbool(true),
lab::jbool(math_ok),
math_ok
});
// Case 3: Deterministic seeded srand/rand vs modern C++11 <random> mt19937
std::srand(42u);
int r1 = std::rand();
std::srand(42u);
int r2 = std::rand();
std::mt19937 gen(2026u);
std::uniform_int_distribution<int> dist(1, 100);
bool in_range = true;
for (int k = 0; k < 20; ++k) {
int v = dist(gen);
if (v < 1 || v > 100) in_range = false;
}
bool rng_ok = (r1 == r2) && in_range;
cases.push_back({
"prng_srand_and_mt19937",
lab::jbool(true),
lab::jbool(rng_ok),
rng_ok
});
// Case 4: Computing pi, e, phi accurately in C++17 (prior to C++20 <numbers>)
const double pi_v = std::acos(-1.0);
const double e_v = std::exp(1.0);
const double phi_v = (1.0 + std::sqrt(5.0)) / 2.0;
bool const_ok = (std::fabs(pi_v - 3.141592653589793) < 1e-12) &&
(std::fabs(e_v - 2.718281828459045) < 1e-12) &&
(std::fabs(phi_v - 1.618033988749895) < 1e-12);
cases.push_back({
"math_constants_pi_e_phi_cpp17",
lab::jbool(true),
lab::jbool(const_ok),
const_ok
});
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-numbers-lab/explore-cpp-numbers.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-numbers.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2384.zip