我需要编写一个动态函数,用C打印2D数组的指定值。值必须能够更改,最好是在其他函数中,并且函数仍然应该打印正确的显示。所以我开始在函数本身中分配值并打印它们,但2D结构不会打印出来,一切都只是在一行上。
继承人我想要的东西:
1 2 3 4 5 6 \n
\n
x x x x x x 1\n
x x x x x x 2\n
x x x x x x 3\n
x x x x x x 4\n
x x x x x x 5\n
x x x x x x 6\n
\n
\n
下面是代码: 正如你所看到的,我尝试了各种策略...... xD
/* assigncleanboard: this function is used to initiate the display board by assigning question marks to all cells in the display board at a new game. */
#include <stdio.h>
#define MAXSIZE 15
void assigncleanboard(int size) {
char displayboard[MAXSIZE][MAXSIZE];
int row=0, col=0;
for(row=0; row<size; row++)
printf("\n");
if(size<=(MAXSIZE-3) && size>=(MAXSIZE-9)) {
//for(row=2; row<(size+2); row++) {
// displayboard[row][MAXSIZE] = 47+row;
// printf(" %c ", displayboard[row][MAXSIZE]);
//}
for(col=0; col<size+3; col++) {
for(row=0; row<MAXSIZE; row++) {
if(col == 0 && col == 1 && col == size && col == (size+2) && row == 0 && row == 1 && row == size && row == (size+2) ) {
displayboard[row][col] = 32;
}
else {
displayboard[row][col] = 88;
}
displayboard[0][col] = 49+col;
displayboard[row][size+3] = 49+row;
printf("| %c |", displayboard[row][col]);
}
}
}
/*
for(row=2; row<(size+2); row++) {
for(col=2; col<(size+2); col++)
displayboard[row][col] = 63;
}
for(row=0; row<size; row++) {
for(col=0; col<size; col++)
displayboard[row][col]=88;
printf("%c", displayboard[row][col]);
}
printf("%c", displayboard[2][MAXSIZE]);
for(row=2; row<(size+2); row++) {
for(col=2; col<(size+2); col++)
printf("%c", displayboard[row][col]);
}
for(row=0; row<size; row++)
printf("\n");
}*/
else
printf("Invalid number:\n");
}
我也试过这个:
if(size<=(MAXSIZE-3) && size>=(MAXSIZE-9)) {
//for(row=2; row<(size+2); row++) {
// displayboard[row][MAXSIZE] = 47+row;
// printf(" %c ", displayboard[row][MAXSIZE]);
//}
for(col=0; col<size+4; col++) {
for(row=0; row<size+4; row++) {
if(col == 0)
displayboard[row][col] = 32;
else if(col == 1)
displayboard[row][col] = 32;
else if(col == size)
displayboard[row][col] = 32;
else if(col == size+3)
displayboard[row][size+3] = 49+row;
else if(row == 0)
displayboard[0][col] = 49+col;
else if(row == 0 && col == size)
displayboard[row][col] = 32;
else if(row == 1 && col == size+3)
displayboard[row][col] = 32;
else
displayboard[row][col] = 88;
printf(" %c ", displayboard[row][col]);
}
}
}
答案 0 :(得分:1)
您的代码应该多次运行printf,您的程序可能会崩溃。你可以调试代码吗?我运行下面的代码,得到了多个printf&#39。
laptop:/home/me> xkill
Select the window whose client you wish to kill with button 1....
^Z
[1] + 22082 Suspended xkill
laptop:/home/me> fg
xkill
^C
laptop:/home/me>
编辑:我打印出来:
#include <stdio.h>
#define MAXSIZE 15
void assigncleanboard(void)
{
char displayboard[MAXSIZE][MAXSIZE];
int row = 0, col = 0;
for(row = 0; row < MAXSIZE; row++)
{
printf("\n");
for(col = 0; col < MAXSIZE; col++)
{
printf("| %c |", displayboard[row][col]);
}
}
}
答案 1 :(得分:0)
是的,谢谢你的回答。我修复了它,主要问题是在for循环中的每一行之后\ n。
int row=0, col=0;
// Assign initial characters to display board
for(row=0; row<=size+3; row++) {
for(col=0; col<=size+3; col++) {
if(col == 0 || col == 1 || col == size+2 || col == size+3 && row == 0 || row == 1 || row == size+2 || row == size+3)
// First two row and column cells are assigned blank spaces.
displayboard[row][col] = 32;
else if(row == 0)
// First row cells are assigned incrememting capital letters for column coordinates
displayboard[row][col] = col+63;
else if(col == size+3)
// Last column cells are assigned incrementing lowercase small letters for row coordinates
displayboard[row][size+3] = 95+row;
else
// Cells not including the first two rows and columns are assigned question marks
displayboard[row][col] = 63;
//printf(" %c ", displayboard[row][col]);
}
//printf("\n");
}
}
void printboard() {
int row=0, col=0,n=0;
printf("\n\n\n\n\n\n\n\n\n\n");
for(row = 0; row<MAXSIZE; row++) {
for(col = 0; col<MAXSIZE; col++)
printf(" %c ", displayboard[row][col]);
printf("\n");
}
printf("\n\n\n\n\n\n\n\n\n");
}