表格中的动态空间

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

Dynamic spaces in a table

问题

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

这是我已经做的:

  1. #include <stdio.h>
  2. #define TEN 10
  3. int len_num(int *num);
  4. int main(int argc, const char * argv[])
  5. {
  6. int i, j;
  7. int space = 0;
  8. int multi;
  9. for(i = 1; i <= TEN; i++)
  10. {
  11. for(j = 1; j <= TEN; j++)
  12. {
  13. multi = i * j;
  14. space = len_num(&multi);
  15. printf("%*d", space, i*j);
  16. }
  17. printf("\n");
  18. }
  19. return 0;
  20. }
  21. int len_num(int *num)
  22. {
  23. int length = 0;
  24. while (*num != 0)
  25. {
  26. *num /= 10;
  27. length++;
  28. }
  29. return length;
  30. }

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

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

有什么建议吗?谢谢。

英文:

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:

  1. #include &lt;stdio.h&gt;
  2. #define TEN 10
  3. int len_num(int *num);
  4. int main(int argc, const char * argv[])
  5. {
  6. int i,j;
  7. int space = 0;
  8. int multi;
  9. for(i = 1; i &lt;= TEN; i++)
  10. {
  11. for(j=1; j &lt;= TEN; j++)
  12. {
  13. multi = i * j;
  14. space = len_num(&amp;multi);
  15. printf(&quot;%*d&quot;, space, i*j);
  16. }
  17. printf(&quot;\n&quot;);
  18. }
  19. return 0;
  20. }
  21. int len_num(int *num)
  22. {
  23. int length = 0;
  24. while (*num != 0)
  25. {
  26. *num /= 10;
  27. length++;
  28. }
  29. return length;
  30. }

For some reason the spaces do not appear.

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

Any suggestions ?

Thanks.

答案1

得分: 1

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

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

为:

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

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

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

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

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

这将产生以下输出:

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

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

To:

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

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

  1. 1 2 3 4 5 6 7 8 9 10
  2. 2 4 6 8 10 12 14 16 18 20
  3. 3 6 9 12 15 18 21 24 27 30
  4. 4 8 12 16 20 24 28 32 36 40
  5. 5 10 15 20 25 30 35 40 45 50
  6. 6 12 18 24 30 36 42 48 54 60
  7. 7 14 21 28 35 42 49 56 63 70
  8. 8 16 24 32 40 48 56 64 72 80
  9. 9 18 27 36 45 54 63 72 81 90
  10. 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):

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

This will yield the following output:

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

  1. 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:

确定