#include <cstdio>
#include <iostream>
#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
struct Box { int v; };
int main() {
std::vector<lab::Case> cases;
// Case 1: Arithmetic, relational, logical operators
int a = 21, b = 10;
int d1 = 10, post_inc = d1++;
int d2 = 10, post_dec = d2--;
bool arl_ok = (a + b == 31) && (a - b == 11) && (a * b == 210) &&
(a / b == 2) && (a % b == 1) && (post_inc == 10) && (d1 == 11) &&
(post_dec == 10) && (d2 == 9) &&
!(a == b) && (a != b) && (a > b) && !(a < b) &&
(5 <= 20) && (20 >= 5) &&
(5 && 20) && (5 || 0) && !(0 && 10);
cases.push_back({
"arithmetic_relational_and_logical",
lab::jbool(true),
lab::jbool(arl_ok),
arl_ok
});
// Case 2: Bitwise operators & truth table (A=60, B=13)
unsigned int ua = 60, ub = 13;
int c_and = static_cast<int>(ua & ub);
int c_or = static_cast<int>(ua | ub);
int c_xor = static_cast<int>(ua ^ ub);
int c_not = static_cast<int>(~ua);
int c_shl = static_cast<int>(ua << 2);
int c_shr = static_cast<int>(ua >> 2);
bool bit_ok = (c_and == 12) && (c_or == 61) && (c_xor == 49) &&
(c_not == -61) && (c_shl == 240) && (c_shr == 15);
cases.push_back({
"bitwise_and_truth_table",
lab::esc("12,61,49,-61,240,15"),
lab::esc(std::to_string(c_and) + "," + std::to_string(c_or) + "," +
std::to_string(c_xor) + "," + std::to_string(c_not) + "," +
std::to_string(c_shl) + "," + std::to_string(c_shr)),
bit_ok
});
// Case 3: Compound assignment chain
int x = 21, c = x;
c += x; // 42
c -= x; // 21
c *= x; // 441
c /= x; // 21
c = 200;
c %= x; // 11
c <<= 2; // 44
c >>= 2; // 11
c &= 2; // 2
c ^= 2; // 0
c |= 2; // 2
cases.push_back({
"compound_assignment_chain",
lab::jint(2),
lab::jint(c),
c == 2
});
// Case 4: Misc operators & operator precedence
int p = 20, q = 10, r = 15, s = 5;
int e1 = (p + q) * r / s; // 90
int e2 = ((p + q) * r) / s; // 90
int e3 = (p + q) * (r / s); // 90
int e4 = p + (q * r) / s; // 50
int e5 = 7 + 3 * 2; // 13
Box bx{88};
Box* pbx = &bx;
int comma_v = (p = 1, q = 2, p + q);
int tern_v = (pbx->v > 50) ? static_cast<int>(2.9) : 0;
bool prec_ok = (e1 == 90) && (e2 == 90) && (e3 == 90) && (e4 == 50) &&
(e5 == 13) && (sizeof(int) == 4) && (comma_v == 3) && (tern_v == 2);
cases.push_back({
"misc_operators_and_precedence",
lab::jbool(true),
lab::jbool(prec_ok),
prec_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-operators-lab/explore-cpp-operators.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-operators.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2386.zip