c: 绘制 table 的布局到控制台
c: draw layout for the table to console
代码有效,但我不知道如何正确设置输出格式以匹配任何尺寸的布局(在本例中为虚线)的一致性?
#include <stdio.h>
int main(int argc, const char * argv[]) {
int i=0, k = 0, total_x = 3, total_y = 4;
char symbol = '+';
for (k = 1; k <= total_y; k++) {
// print symbol and row numbers
if (k == 1) {
printf("%3c | ",symbol);
int temp;
for (temp = 1; temp <= total_x; temp++) {
printf("%4d", temp);
}
printf("\n");
for (temp = 1; temp < total_x*5; temp++) {
if (temp == 5) {
printf("+");
}
printf("-");
}
printf("\n");
}
printf("%3d | ",k);
for (i = 1; i <= total_x; i++) {
printf("%4d", k + i);
}
printf("\n");
}
return 0;
}
当total_x = 3, total_y = 4时输出;:
+ | 1 2 3
----+----------
1 | 2 3 4
2 | 3 4 5
3 | 4 5 6
4 | 5 6 7
想要的结果:
+ | 1 2 3
----+--------------
1 | 2 3 4
2 | 3 4 5
3 | 4 5 6
4 | 5 6 7
当total_x = 10, total_y = 4时输出:
+ | 1 2 3 4 5 6 7 8 9 10
----+---------------------------------------------
1 | 2 3 4 5 6 7 8 9 10 11
2 | 3 4 5 6 7 8 9 10 11 12
3 | 4 5 6 7 8 9 10 11 12 13
4 | 5 6 7 8 9 10 11 12 13 14
想要的结果:
+ | 1 2 3 4 5 6 7 8 9 10
----+------------------------------------------
1 | 2 3 4 5 6 7 8 9 10 11
2 | 3 4 5 6 7 8 9 10 11 12
3 | 4 5 6 7 8 9 10 11 12 13
4 | 5 6 7 8 9 10 11 12 13 14
有没有可以帮助我正确打印的 printf 函数?非常感谢!
您可以像这样使破折号打印得更漂亮:
printf("----+--");
for (temp = 1; temp <= total_x; temp++) {
printf("----");
}
printf("\n");
或更正您所做的算术:
for (temp = 1; temp <= total_x*4+6; temp++) {
if (temp==5)
printf("+");
printf("-");
}
printf("\n");
代码有效,但我不知道如何正确设置输出格式以匹配任何尺寸的布局(在本例中为虚线)的一致性?
#include <stdio.h>
int main(int argc, const char * argv[]) {
int i=0, k = 0, total_x = 3, total_y = 4;
char symbol = '+';
for (k = 1; k <= total_y; k++) {
// print symbol and row numbers
if (k == 1) {
printf("%3c | ",symbol);
int temp;
for (temp = 1; temp <= total_x; temp++) {
printf("%4d", temp);
}
printf("\n");
for (temp = 1; temp < total_x*5; temp++) {
if (temp == 5) {
printf("+");
}
printf("-");
}
printf("\n");
}
printf("%3d | ",k);
for (i = 1; i <= total_x; i++) {
printf("%4d", k + i);
}
printf("\n");
}
return 0;
}
当total_x = 3, total_y = 4时输出;:
+ | 1 2 3
----+----------
1 | 2 3 4
2 | 3 4 5
3 | 4 5 6
4 | 5 6 7
想要的结果:
+ | 1 2 3
----+--------------
1 | 2 3 4
2 | 3 4 5
3 | 4 5 6
4 | 5 6 7
当total_x = 10, total_y = 4时输出:
+ | 1 2 3 4 5 6 7 8 9 10
----+---------------------------------------------
1 | 2 3 4 5 6 7 8 9 10 11
2 | 3 4 5 6 7 8 9 10 11 12
3 | 4 5 6 7 8 9 10 11 12 13
4 | 5 6 7 8 9 10 11 12 13 14
想要的结果:
+ | 1 2 3 4 5 6 7 8 9 10
----+------------------------------------------
1 | 2 3 4 5 6 7 8 9 10 11
2 | 3 4 5 6 7 8 9 10 11 12
3 | 4 5 6 7 8 9 10 11 12 13
4 | 5 6 7 8 9 10 11 12 13 14
有没有可以帮助我正确打印的 printf 函数?非常感谢!
您可以像这样使破折号打印得更漂亮:
printf("----+--");
for (temp = 1; temp <= total_x; temp++) {
printf("----");
}
printf("\n");
或更正您所做的算术:
for (temp = 1; temp <= total_x*4+6; temp++) {
if (temp==5)
printf("+");
printf("-");
}
printf("\n");