英文:
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 <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;
}
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
附注:
len_num
没有修改num
参数的必要,因此它应该按值获取它(int len_num(int num)
)。TEN
不是一个很好的名称,考虑更改为MAX
(以防您可能会更改值10)。
英文:
If you want a value with n digits to be prefixed by n spaces, you need to change:
printf("%*d", space, i*j);
To:
printf("%*d", 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("%*d", 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:
- There's no reason for
len_num
to modify thenum
parameter, and therefore it should get it by value (int len_num(int num)
). TEN
is not such a good name, consider to change toMAX
(in case you might change the value 10).
答案2
得分: 1
"它看起来太简单:\n\nprintf( "%4d", i*j );"
英文:
It looks too simple:
printf( "%4d", i*j );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论