0

我正在尝试打印一个 2d 数组,该数组最多有 3 个在打印时对齐的数字。例如,使用简单的printf,它看起来像这样:

[0, 232, 20, 96, 176, 0, 0]
[0, 0, 24, 0, 0, 176, 0]
[0, 0, 0, 0, 0, 0, 0]

我希望它打印时所有逗号都沿列对齐,并带有额外的空格,如下所示:

[   0, 232,  20,  96, 176,   0,   0]
[   0,   0,  24,   0,   0, 176,   0]
[   0,   0,   0,   0,   0,   0,   0]

我该怎么做printf

4

3 回答 3

2

这个给你。

#include <stdio.h>

int main(void) 
{
    enum { M = 3, N = 7 };
    int a[M][N] =   
    {   
        { 0, 232, 20, 96, 176,   0, 0 },
        { 0,   0, 24,  0,   0, 176, 0 },
        { 0,   0,  0,  0,   0,   0, 0 }
    };

    int width = 4;

    for ( size_t i = 0; i < M; i++ )
    {
        putchar( '[' );
        for ( size_t j = 0; j < N; j++ )
        {
            if ( j != 0 ) putchar( ',' );
            printf( "%*d", width, a[i][j] );
        }
        printf( "]\n" );
    }

    putchar( '\n' );

    return 0;
}

程序输出为

[   0, 232,  20,  96, 176,   0,   0]
[   0,   0,  24,   0,   0, 176,   0]
[   0,   0,   0,   0,   0,   0,   0]

width例如,如果您想输出比由 3 位数字组成的值更大的值,您可以更改变量的值。那就是输出足够灵活。

如果要将数组输出放在单独的函数中,则如果编译器支持可变长度数组,则相应的函数可以如下所示。

#include <stdio.h>

void format_output( size_t m, size_t n, int a[m][n], int width )
{
    for ( size_t i = 0; i < m; i++ )
    {
        putchar( '[' );
        for ( size_t j = 0; j < n; j++ )
        {
            if ( j != 0 ) putchar( ',' );
            printf( "%*d", width, a[i][j] );
        }
        printf( "]\n" );
    }
}

int main(void) 
{
    enum { M = 3, N = 7 };
    int a[M][N] =   
    {   
        { 0, 232, 20, 96, 176,   0, 0 },
        { 0,   0, 24,  0,   0, 176, 0 },
        { 0,   0,  0,  0,   0,   0, 0 }
    };

    int width = 4;

    format_output( M, N, a, width );

    putchar( '\n' );

    return 0;
}

程序输出与上图相同,即

[   0, 232,  20,  96, 176,   0,   0]
[   0,   0,  24,   0,   0, 176,   0]
[   0,   0,   0,   0,   0,   0,   0]
于 2020-03-20T18:11:10.217 回答
2

您可以使用宽度前缀指定printf转换的最小宽度:printf("%4d", x);将打印int变量x填充到左侧,并留有足够的空格以产生至少 4 个字符。

如果您知道数组中任何数字的最大宽度,则可以在格式字符串中对该数字进行硬编码。否则,您可以计算所需的宽度并使用%*d并传递一个额外的参数来指定计算的宽度。

这是修改后的版本:

#include <stdio.h>

int main(void) {
#define M  3
#define N  7
    int a[M][N] = {
        { 0, 232, 20, 96, 176,   0, 0 },
        { 0,   0, 24,  0,   0, 176, 0 },
        { 0,   0,  0,  0,   0,   0, 0 },
    };

    int width = 0;

    /* compute the required width */
    for (size_t i = 0; i < M; i++) {
        for (size_t j = 0; j < N; j++) {
            int w = snprintf(NULL, 0, "%d", a[i][j]);
            if (width < w) {
                width = w;
            }
        }
    }

    /* print the arrays */
    for (size_t i = 0; i < M; i++) {
        printf("[");
        for (size_t j = 0; j < N; j++) {
            if (j != 0) printf(", ");
            printf("%*d", width, a[i][j]);
        }
        printf("]\n");
    }

    return 0;
}

输出:

[  0, 232,  20,  96, 176,   0,   0]
[  0,   0,  24,   0,   0, 176,   0]
[  0,   0,   0,   0,   0,   0,   0]
于 2020-03-20T19:10:34.913 回答
1

使用这种格式printf printf("%4d", arr[i][j]);

int main()
{
    int arr[3][7] = { {0, 232, 20, 96, 176, 0, 0}
    ,{0, 0, 24, 0, 0, 176, 0}
    ,{0, 0, 0, 0, 0, 0, 0} };
    for (int i = 0; i < 3; i++)
    {
        printf("[");
        for (int j = 0; j < 7; j++)
        {
            if (j < 6)
                printf("%4d,", arr[i][j]);
            if (j == 6)
                printf("%4d", arr[i][j]);

        }
        printf("]");
        printf("\n");
    }
}

PS:空间大小可以根据需要改变,随变"%4d"

于 2020-03-20T18:01:44.277 回答