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

如何用C语言实现MySQL风格的查询结果打印输出?

在C语言中实现MySQL样式的查询结果打印

要解决内容长度不固定导致的排版错位问题,核心是先计算每列的最大宽度,再统一按照这个宽度排版所有行。以下是具体实现方案:

实现思路

  • 计算每列最大宽度:遍历表头和所有数据行,记录每列最长字符串的长度,再加上预留的左右空格宽度(保证内容居中)。
  • 打印分隔线:根据每列的计算宽度,生成由+和-组成的分隔线。
  • 打印行内容:对表头和每个数据行,将内容居中填充到对应列的宽度中,用|分隔每列。

完整代码示例

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// 计算每列的最大显示宽度(含预留空格)
void calculate_max_widths(char **headers, char ***data, int cols, int rows, int *max_widths) {
    // 初始化宽度为表头长度
    for (int i = 0; i < cols; i++) {
        max_widths[i] = strlen(headers[i]);
    }
    // 遍历数据行更新最大宽度
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            int len = strlen(data[i][j]);
            if (len > max_widths[j]) {
                max_widths[j] = len;
            }
        }
    }
    // 每列左右各留3个空格(可按需调整),总宽度=最大内容长度+6
    for (int i = 0; i < cols; i++) {
        max_widths[i] += 6;
    }
}

// 打印表格分隔线
void print_separator(int *max_widths, int cols) {
    for (int i = 0; i < cols; i++) {
        printf("+");
        for (int j = 0; j < max_widths[i]; j++) {
            printf("-");
        }
    }
    printf("+\n");
}

// 打印单行内容(表头或数据),居中对齐
void print_row(char **row, int *max_widths, int cols) {
    for (int i = 0; i < cols; i++) {
        printf("|");
        int content_len = strlen(row[i]);
        int total_pad = max_widths[i] - content_len;
        int left_pad = total_pad / 2;
        int right_pad = total_pad - left_pad;
        
        // 填充左侧空格
        for (int j = 0; j < left_pad; j++) {
            printf(" ");
        }
        printf("%s", row[i]);
        // 填充右侧空格
        for (int j = 0; j < right_pad; j++) {
            printf(" ");
        }
    }
    printf("|\n");
}

int main() {
    // 示例表头与查询数据
    char *headers[] = {"Name", "Age", "City"};
    int cols = sizeof(headers) / sizeof(headers[0]);
    
    // 模拟查询返回的数据集
    char ***data = malloc(3 * sizeof(char **));
    data[0] = malloc(cols * sizeof(char *));
    data[0][0] = "XYZ";
    data[0][1] = "25";
    data[0][2] = "New York";
    
    data[1] = malloc(cols * sizeof(char *));
    data[1][0] = "Alice";
    data[1][1] = "30";
    data[1][2] = "London";
    
    data[2] = malloc(cols * sizeof(char *));
    data[2][0] = "Bob Smith";
    data[2][1] = "45";
    data[2][2] = "Paris";
    
    int rows = 3;

    int *max_widths = malloc(cols * sizeof(int));
    calculate_max_widths(headers, data, cols, rows, max_widths);

    // 按MySQL样式打印表格
    print_separator(max_widths, cols);
    print_row(headers, max_widths, cols);
    print_separator(max_widths, cols);
    for (int i = 0; i < rows; i++) {
        print_row(data[i], max_widths, cols);
    }
    print_separator(max_widths, cols);

    // 释放动态分配的内存
    for (int i = 0; i < rows; i++) {
        free(data[i]);
    }
    free(data);
    free(max_widths);

    return 0;
}

代码说明

  • 宽度计算:calculate_max_widths函数先获取每列最长内容长度,再额外添加6个字符的空格(左右各3个),确保内容居中,和示例样式匹配。
  • 分隔线生成:print_separator根据每列宽度生成对应长度的-段,用+连接,形成表格边框。
  • 居中对齐:print_row自动计算内容所需的左右空格数,填充后保证不同长度的内容都能在列中居中,彻底避免错位。

运行代码后,输出效果如下:

+-----------+---------+-------------+
|    Name   |   Age   |    City     |
+-----------+---------+-------------+
|    XYZ    |   25    |  New York   |
|   Alice   |   30    |   London    |
|  Bob Smith|   45    |    Paris    |
+-----------+---------+-------------+

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 04:05:53