蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 C11 多模块接口契约(Point/distance/is_prime)、头文件保护宏、extern 状态与目录版本策略

C本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。explore-project-structure.c
explore-project-structure.c98 行
#include <math.h>
#include <stdio.h>
#include <string.h>

/* Simulated include/math_utils.h with include guard */
#ifndef MBSKY_MATH_UTILS_H
#define MBSKY_MATH_UTILS_H

#define PROJECT_NAME "BlueSkyCApp"
#define VERSION_MAJOR 1
#define VERSION_MINOR 0

typedef struct {
    int x;
    int y;
} Point;

extern int g_module_calls;
double calc_distance(Point a, Point b);
int is_prime_num(int n);

#endif /* MBSKY_MATH_UTILS_H */

/* Second inclusion guarded cleanly */
#ifndef MBSKY_MATH_UTILS_H
#error "Include guard failed"
#endif

/* Simulated src/math_utils.c implementation */
int g_module_calls = 0;

double calc_distance(Point a, Point b) {
    g_module_calls++;
    double dx = (double)(a.x - b.x);
    double dy = (double)(a.y - b.y);
    return sqrt(dx * dx + dy * dy);
}

int is_prime_num(int n) {
    g_module_calls++;
    if (n < 2) return 0;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return 0;
    }
    return 1;
}

static int should_commit_dir(const char *dir) {
    if (strcmp(dir, "build/") == 0) return 0;
    if (strcmp(dir, "src/") == 0 || strcmp(dir, "include/") == 0 ||
        strcmp(dir, "tests/") == 0 || strcmp(dir, "doc/") == 0) {
        return 1;
    }
    return -1; /* lib/ depends on dependency policy */
}

int main(void) {
    /* Case 1: math_utils distance(Point(0,0), Point(3,4)) == 5.0 */
    Point p0 = {0, 0};
    Point p1 = {3, 4};
    double dist = calc_distance(p0, p1);
    int c1_pass = (fabs(dist - 5.0) < 1e-9);

    /* Case 2: math_utils is_prime_num checks */
    int pr1 = is_prime_num(1);
    int pr2 = is_prime_num(2);
    int pr7 = is_prime_num(7);
    int pr9 = is_prime_num(9);
    int c2_pass = (pr1 == 0 && pr2 == 1 && pr7 == 1 && pr9 == 0);

    /* Case 3: header contract macros and extern state tracking */
    char ver[32];
    snprintf(ver, sizeof(ver), "%s-v%d.%d", PROJECT_NAME, VERSION_MAJOR, VERSION_MINOR);
    int c3_pass = (strcmp(ver, "BlueSkyCApp-v1.0") == 0 && g_module_calls == 5);

    /* Case 4: standard directory VCS policy & 4-stage pipeline artifacts */
    int vcs_src = should_commit_dir("src/");
    int vcs_inc = should_commit_dir("include/");
    int vcs_bld = should_commit_dir("build/");
    int vcs_lib = should_commit_dir("lib/");
    int c4_pass = (vcs_src == 1 && vcs_inc == 1 && vcs_bld == 0 && vcs_lib == -1);

    printf("{\n");
    printf("  \"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\",\n");
    printf("  \"cases\": [\n");
    printf("    {\"name\": \"math_utils_distance\", \"expected\": \"5.00\", \"actual\": \"%.2f\", \"passed\": %s},\n",
           dist, c1_pass ? "true" : "false");
    printf("    {\"name\": \"math_utils_is_prime\", \"expected\": [0, 1, 1, 0], \"actual\": [%d, %d, %d, %d], \"passed\": %s},\n",
           pr1, pr2, pr7, pr9, c2_pass ? "true" : "false");
    printf("    {\"name\": \"header_guard_and_extern\", \"expected\": \"BlueSkyCApp-v1.0|5\", \"actual\": \"%s|%d\", \"passed\": %s},\n",
           ver, g_module_calls, c3_pass ? "true" : "false");
    printf("    {\"name\": \"directory_vcs_policy\", \"expected\": [1, 1, 0, -1], \"actual\": [%d, %d, %d, %d], \"passed\": %s}\n",
           vcs_src, vcs_inc, vcs_bld, vcs_lib, c4_pass ? "true" : "false");
    printf("  ]\n");
    printf("}\n");
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。

入口文件:projects/c-project-structure-lab/explore-project-structure.c

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-project-structure.c,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3233.zip

下载课程关联工程包:C

课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习