【问题标题】:print equally spaced elements of a 2D array with printf使用 printf 打印二维数组的等距元素
【发布时间】:2020-07-01 21:23:54
【问题描述】:

我正在尝试打印一个二维数组,该数组最多包含 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 做到这一点?

【问题讨论】:

  • 任何像样的书或教程都应该有关于printf的字段宽度修饰符的信息。
  • 尝试使用 printf("%4d",array[index][index]);以便它保留 4 个空格,无论是 2 位还是 3 位数字

标签: c multidimensional-array printf conversion-specifier


【解决方案1】:

使用printfprintf("%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"

【讨论】:

  • 但是方括号和逗号在哪里?
  • @ArdentCoder 抱歉我忘记添加了,谢谢提醒。我编辑了我的帖子。
  • 但是,由于%6d,这次你的空格太多了。根据 OP 的演示进行调整。
  • @ArdentCoder 感谢所有 cmets,看来我今天打字不好。
  • @ArdentCoder 我希望在本网站用户的帮助下了解更多关于编程的知识,我将始终乐于通过有用的评论改进我的代码。我非常感谢他们。
【解决方案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]

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

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

#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]

【讨论】:

    【解决方案3】:

    您可以使用 width 前缀来指定 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]
    

    【讨论】:

    • 优雅,不需要块引号(这次);)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    相关资源
    最近更新 更多