#include <stdio.h>
#include <string.h>
static char classify_score(int score) {
if (score >= 90) {
return 'A';
} else if (score >= 60) {
if (score >= 75) {
return 'B';
}
return 'C';
} else {
return 'D';
}
}
static int nested_switch_code(int category, int sub) {
switch (category) {
case 1:
switch (sub) {
case 10: return 110;
case 20: return 120;
default: return 199;
}
case 2:
return 200;
default:
return -1;
}
}
int main(void) {
int *p_null = NULL;
int v_neg = -5;
int t_neg = v_neg ? 1 : 0;
int t_zero = 0 ? 1 : 0;
int t_null = p_null ? 1 : 0;
int c1_pass = (t_neg == 1 && t_zero == 0 && t_null == 0);
char g1 = classify_score(95);
char g2 = classify_score(78);
char g3 = classify_score(55);
int c2_pass = (g1 == 'A' && g2 == 'B' && g3 == 'D');
int sw1 = nested_switch_code(1, 20);
int sw2 = nested_switch_code(2, 0);
int sw3 = nested_switch_code(9, 0);
int c3_pass = (sw1 == 120 && sw2 == 200 && sw3 == -1);
const char *p0 = (0 % 2 == 0) ? "even" : "odd";
const char *p42 = (42 % 2 == 0) ? "even" : "odd";
const char *p17 = (17 % 2 == 0) ? "even" : "odd";
const char *pm3 = ((-3) % 2 == 0) ? "even" : "odd";
int c4_pass = (strcmp(p0, "even") == 0 && strcmp(p42, "even") == 0 &&
strcmp(p17, "odd") == 0 && strcmp(pm3, "odd") == 0);
printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
printf(" {\"name\": \"truthiness_nonzero_and_null\", \"expected\": {\"neg\": 1, \"zero\": 0, \"null_ptr\": 0}, \"actual\": {\"neg\": %d, \"zero\": %d, \"null_ptr\": %d}, \"passed\": %s},\n",
t_neg, t_zero, t_null, c1_pass ? "true" : "false");
printf(" {\"name\": \"if_else_and_nested_if\", \"expected\": [\"A\", \"B\", \"D\"], \"actual\": [\"%c\", \"%c\", \"%c\"], \"passed\": %s},\n",
g1, g2, g3, c2_pass ? "true" : "false");
printf(" {\"name\": \"switch_and_nested_switch\", \"expected\": [120, 200, -1], \"actual\": [%d, %d, %d], \"passed\": %s},\n",
sw1, sw2, sw3, c3_pass ? "true" : "false");
printf(" {\"name\": \"ternary_even_odd\", \"expected\": [\"even\", \"even\", \"odd\", \"odd\"], \"actual\": [\"%s\", \"%s\", \"%s\", \"%s\"], \"passed\": %s}\n",
p0, p42, p17, pm3, c4_pass ? "true" : "false");
printf("]}\n");
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。
入口文件:projects/c-decision-lab/explore-decision.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-decision.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3216.zip