代码有效,但是我不知道如何正确地格式化输出以匹配任何大小的布局(在这种情况下绘制破折号)的一致性?
#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功能?非常感谢你!
答案 0 :(得分:0)
您可以使破折号打印得更漂亮:
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");