#include <cmath>
#include <complex>
#include <cstdio>
#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"; }
bool near(double a, double b, double eps = 1e-6) {
return std::fabs(a - b) <= eps;
}
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: construction, real()/imag(), stream format, float/double/long double specializations
std::complex<double> z1(3.0, 4.0);
std::complex<float> zf(1.0f, 2.0f);
std::complex<long double> zld(2.0L, -3.0L);
std::ostringstream oss;
oss << z1;
bool ok1 = (oss.str() == "(3,4)")
&& lab::near(z1.real(), 3.0) && lab::near(z1.imag(), 4.0)
&& lab::near(zf.real(), 1.0) && lab::near(zf.imag(), 2.0)
&& lab::near(static_cast<double>(zld.real()), 2.0) && lab::near(static_cast<double>(zld.imag()), -3.0);
cases.push_back({
"complex_construct_and_parts",
lab::jbool(true),
lab::jbool(ok1),
ok1
});
// Case 2: four arithmetic operations on (5+3i) and (2-4i)
std::complex<double> c1(5.0, 3.0);
std::complex<double> c2(2.0, -4.0);
auto sum = c1 + c2;
auto diff = c1 - c2;
auto prod = c1 * c2;
auto quot = c1 / c2;
bool ok2 = lab::near(sum.real(), 7.0) && lab::near(sum.imag(), -1.0)
&& lab::near(diff.real(), 3.0) && lab::near(diff.imag(), 7.0)
&& lab::near(prod.real(), 22.0) && lab::near(prod.imag(), -14.0)
&& lab::near(quot.real(), -0.1) && lab::near(quot.imag(), 1.3);
cases.push_back({
"complex_arithmetic_ops",
lab::jbool(true),
lab::jbool(ok2),
ok2
});
// Case 3: conj, abs, arg, exp, sin, cos (including Euler identity)
const double pi = std::acos(-1.0);
auto cj = std::conj(c1);
double mod_z1 = std::abs(z1);
double mod_c1 = std::abs(c1);
double arg_c1 = std::arg(c1);
auto euler = std::exp(std::complex<double>(0.0, pi));
auto sin_val = std::sin(std::complex<double>(pi / 2.0, 0.0));
auto cos_val = std::cos(std::complex<double>(0.0, 0.0));
bool ok3 = lab::near(cj.real(), 5.0) && lab::near(cj.imag(), -3.0)
&& lab::near(mod_z1, 5.0)
&& lab::near(mod_c1, std::sqrt(34.0))
&& lab::near(arg_c1, std::atan2(3.0, 5.0))
&& lab::near(euler.real(), -1.0) && lab::near(euler.imag(), 0.0)
&& lab::near(sin_val.real(), 1.0) && lab::near(sin_val.imag(), 0.0)
&& lab::near(cos_val.real(), 1.0) && lab::near(cos_val.imag(), 0.0);
cases.push_back({
"complex_conj_abs_arg_transcendental",
lab::jbool(true),
lab::jbool(ok3),
ok3
});
// Case 4: std::polar(r, theta) roundtrip
double r = 5.0;
double theta = pi / 4.0;
std::complex<double> zp = std::polar(r, theta);
double expected_comp = 5.0 * std::sqrt(2.0) / 2.0;
bool ok4 = lab::near(zp.real(), expected_comp)
&& lab::near(zp.imag(), expected_comp)
&& lab::near(std::abs(zp), r)
&& lab::near(std::arg(zp), theta);
cases.push_back({
"complex_polar_representation",
lab::jbool(true),
lab::jbool(ok4),
ok4
});
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-complex-lab/explore-cpp-libs-complex.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-complex.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2339.zip