表格中的动态空间

huangapple go评论105阅读模式
英文:

Dynamic spaces in a table

问题

我正在尝试打印乘法表,并且希望在每个结果之前根据结果的位数添加一定数量的空格。

这是我已经做的:

#include <stdio.h>
#define TEN 10

int len_num(int *num);

int main(int argc, const char * argv[])
{
    int i, j;
    int space = 0;
    int multi;
    for(i = 1; i <= TEN; i++)
    {
        for(j = 1; j <= TEN; j++)
        {
            multi = i * j;
            space = len_num(&multi);
            printf("%*d", space, i*j);
        }
        printf("\n");
    }
    return 0;
}

int len_num(int *num)
{
    int length = 0;
    while (*num != 0)
    {
        *num /= 10;
        length++;
    }
    return length;
}

出现某种原因,空格没有出现。

这是我想要实现的效果:
表格中的动态空间

有什么建议吗?谢谢。

英文:

I'm trying to print multiplication table and before each result I would like to add a certain amount of spaces in accordance with the number of digits of the result.

Here is what I've done:

#include &lt;stdio.h&gt;
#define TEN 10

int len_num(int *num);
int main(int argc, const char * argv[])
{
    int i,j;
    int space = 0;
    int multi;
    for(i = 1; i &lt;= TEN; i++)
    {
        for(j=1; j &lt;= TEN; j++)
        {
            multi = i * j;
            space = len_num(&amp;multi);
            printf(&quot;%*d&quot;, space, i*j);
        }
        printf(&quot;\n&quot;);
    }
    return 0;
}

int len_num(int *num)
{
    int length = 0;
    while (*num != 0)
    {
        *num /= 10;
        length++;
    }
    return length;
}

For some reason the spaces do not appear.

Here's what I'm trying to accomplish:
表格中的动态空间

Any suggestions ?

Thanks.

答案1

得分: 1

如果您想要在具有_n_位数的值前加_n_个空格,您需要更改:

printf("%*d", space, i*j);

为:

printf("%*d", space * 2, i*j);

然而,这将导致表格的列不对齐:

 1 2 3 4 5 6 7 8 9  10
 2 4 6 8  10  12  14  16  18  20
 3 6 9  12  15  18  21  24  27  30
 4 8  12  16  20  24  28  32  36  40
 5  10  15  20  25  30  35  40  45  50
 6  12  18  24  30  36  42  48  54  60
 7  14  21  28  35  42  49  56  63  70
 8  16  24  32  40  48  56  64  72  80
 9  18  27  36  45  54  63  72  81  90
  10  20  30  40  50  60  70  80  90   100

如果您想要一个格式正确的表格,您可能需要为值指定固定的宽度,例如(宽度为5):

printf("%*d", 5, i*j);

这将产生以下输出:

    1    2    3    4    5    6    7    8    9   10
    2    4    6    8   10   12   14   16   18   20
    3    6    9   12   15   18   21   24   27   30
    4    8   12   16   20   24   28   32   36   40
    5   10   15   20   25   30   35   40   45   50
    6   12   18   24   30   36   42   48   54   60
    7   14   21   28   35   42   49   56   63   70
    8   16   24   32   40   48   56   64   72   80
    9   18   27   36   45   54   63   72   81   90
   10   20   30   40   50   60   70   80   90  100

附注:

  1. len_num 没有修改 num 参数的必要,因此它应该按值获取它(int len_num(int num))。
  2. TEN 不是一个很好的名称,考虑更改为 MAX(以防您可能会更改值10)。
英文:

If you want a value with n digits to be prefixed by n spaces, you need to change:

printf(&quot;%*d&quot;, space, i*j);

To:

printf(&quot;%*d&quot;, space * 2, i*j);

However this will format the table in a way where columns are not aligned:

 1 2 3 4 5 6 7 8 9  10
 2 4 6 8  10  12  14  16  18  20
 3 6 9  12  15  18  21  24  27  30
 4 8  12  16  20  24  28  32  36  40
 5  10  15  20  25  30  35  40  45  50
 6  12  18  24  30  36  42  48  54  60
 7  14  21  28  35  42  49  56  63  70
 8  16  24  32  40  48  56  64  72  80
 9  18  27  36  45  54  63  72  81  90
  10  20  30  40  50  60  70  80  90   100

If you'd like a properly formatted table, you probably need a fixed width for the values, e.g. (for a width of 5):

printf(&quot;%*d&quot;, 5, i*j);

This will yield the following output:

    1    2    3    4    5    6    7    8    9   10
    2    4    6    8   10   12   14   16   18   20
    3    6    9   12   15   18   21   24   27   30
    4    8   12   16   20   24   28   32   36   40
    5   10   15   20   25   30   35   40   45   50
    6   12   18   24   30   36   42   48   54   60
    7   14   21   28   35   42   49   56   63   70
    8   16   24   32   40   48   56   64   72   80
    9   18   27   36   45   54   63   72   81   90
   10   20   30   40   50   60   70   80   90  100

Side notes:

  1. There's no reason for len_num to modify the num parameter, and therefore it should get it by value (int len_num(int num)).
  2. TEN is not such a good name, consider to change to MAX (in case you might change the value 10).

答案2

得分: 1

"它看起来太简单:\n\nprintf( "%4d", i*j );"

英文:

It looks too simple:

printf( &quot;%4d&quot;, i*j );

huangapple
  • 本文由 发表于 2023年3月7日 17:54:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定