You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不同坐标打印不同字符串?能否用循环替代硬编码的控制台定位逻辑?

优化控制台表格打印的硬编码问题

我需要制作一个表格,不想采用硬编码的方式。目前我通过以下代码实现控制台指定坐标打印字符串:

COORD point = { 30, 10 };
SetConsoleCursorPosition(hConsole, point);
printf("%s", a);
COORD point1 = { 30,12 };
SetConsoleCursorPosition(hConsole, point1);
printf("%s", b);
COORD point2 = { 30,14 };
SetConsoleCursorPosition(hConsole, point2);
printf("%s", c);
COORD point3 = { 30,16 };
SetConsoleCursorPosition(hConsole, point3);
printf("%s", d);
COORD point4 = { 30,18 };
SetConsoleCursorPosition(hConsole, point4);
printf("%s", e);
COORD point5 = { 50,10 };
SetConsoleCursorPosition(hConsole, point5);
printf("%s", f);
COORD point6 = { 50,12 };
SetConsoleCursorPosition(hConsole, point6);
printf("%s", g);
COORD point7 = { 50,14 };
SetConsoleCursorPosition(hConsole, point7);
printf("%s", h);
COORD point8 = { 50,16 };
SetConsoleCursorPosition(hConsole, point8);
printf("%s", k);
COORD point9 = { 50,18 };
SetConsoleCursorPosition(hConsole, point9);
printf("%d", l);

想了解是否可以通过for循环等方式优化这段代码,避免硬编码。

当然可以通过结构化的方式优化,以下是几种实用方案:

方案1:用结构体数组统一管理打印项

把每个打印操作的坐标、格式字符串和对应数据打包成结构体,存入数组后用循环批量处理,后续修改或扩展只需调整数组内容:

#include <stdio.h>
#include <windows.h>

// 定义结构体存储单条打印信息
typedef struct {
    COORD coord;
    const char* format;
    union {
        const char* str_data;
        int int_data;
    } content;
} ConsolePrintTask;

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
    // 假设变量已定义
    const char* a = "a_val";
    const char* b = "b_val";
    const char* c = "c_val";
    const char* d = "d_val";
    const char* e = "e_val";
    const char* f = "f_val";
    const char* g = "g_val";
    const char* h = "h_val";
    const char* k = "k_val";
    int l = 123;

    // 初始化所有打印任务
    ConsolePrintTask tasks[] = {
        {{30, 10}, "%s", {.str_data = a}},
        {{30, 12}, "%s", {.str_data = b}},
        {{30, 14}, "%s", {.str_data = c}},
        {{30, 16}, "%s", {.str_data = d}},
        {{30, 18}, "%s", {.str_data = e}},
        {{50, 10}, "%s", {.str_data = f}},
        {{50, 12}, "%s", {.str_data = g}},
        {{50, 14}, "%s", {.str_data = h}},
        {{50, 16}, "%s", {.str_data = k}},
        {{50, 18}, "%d", {.int_data = l}}
    };

    int task_count = sizeof(tasks) / sizeof(tasks[0]);
    // 循环执行所有打印任务
    for (int i = 0; i < task_count; i++) {
        SetConsoleCursorPosition(hConsole, tasks[i].coord);
        if (tasks[i].format[1] == 's') {
            printf(tasks[i].format, tasks[i].content.str_data);
        } else if (tasks[i].format[1] == 'd') {
            printf(tasks[i].format, tasks[i].content.int_data);
        }
    }

    return 0;
}

方案2:按列批量处理

观察你的代码,两列的X坐标固定(30、50),Y坐标按步长2递增,可以把每列的内容存入数组,用嵌套循环处理行列:

#include <stdio.h>
#include <windows.h>

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
    // 按列组织数据
    const char* col1[] = {"a_val", "b_val", "c_val", "d_val", "e_val"};
    const char* col2_str[] = {"f_val", "g_val", "h_val", "k_val"};
    int col2_int = 123;

    int col_x[] = {30, 50};
    int start_y = 10;
    int y_step = 2;
    int row_count = sizeof(col1) / sizeof(col1[0]);

    // 打印第一列
    for (int i = 0; i < row_count; i++) {
        COORD coord = {col_x[0], start_y + i * y_step};
        SetConsoleCursorPosition(hConsole, coord);
        printf("%s", col1[i]);
    }

    // 打印第二列前4行字符串
    for (int i = 0; i < sizeof(col2_str)/sizeof(col2_str[0]); i++) {
        COORD coord = {col_x[1], start_y + i * y_step};
        SetConsoleCursorPosition(hConsole, coord);
        printf("%s", col2_str[i]);
    }
    // 打印第二列最后一行整数
    COORD last_coord = {col_x[1], start_y + 4 * y_step};
    SetConsoleCursorPosition(hConsole, last_coord);
    printf("%d", col2_int);

    return 0;
}

方案3:通用表格打印函数

如果需要频繁扩展表格行列,可以封装一个通用函数,传入起始坐标、行列间距和数据,自动计算位置打印:

#include <stdio.h>
#include <windows.h>

// 通用表格打印:支持任意行列,自动计算坐标
void printConsoleTable(HANDLE hConsole, int start_x, int start_y, 
                       int col_gap, int row_gap, int rows, int cols, const char** data) {
    for (int i = 0; i < rows; i++) {
        int current_y = start_y + i * row_gap;
        for (int j = 0; j < cols; j++) {
            int current_x = start_x + j * col_gap;
            COORD coord = {current_x, current_y};
            SetConsoleCursorPosition(hConsole, coord);
            printf("%s", data[i * cols + j]);
        }
    }
}

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
    // 将整数转为字符串统一处理
    char l_str[20];
    sprintf(l_str, "%d", 123);
    const char* table_data[] = {
        "a_val", "f_val",
        "b_val", "g_val",
        "c_val", "h_val",
        "d_val", "k_val",
        "e_val", l_str
    };

    // 调用函数:起始(30,10),列间距20,行间距2,5行2列
    printConsoleTable(hConsole, 30, 10, 20, 2, 5, 2, table_data);

    return 0;
}

内容的提问来源于stack exchange,提问作者wertik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 20:24:52