大约两周前,我发布了一个类似的问题,我解决了我的问题。但是,经过一些修改,我的程序无法运行。说明是获取形状的周长,用户用星号 ( ) 绘制,然后用星号 ( ) 填充形状。
有两个问题:
1) The program fails to print the full input shape (at end of getArray function)
2) The program does not fill an input shape problem for row > 9 or column > 9.
我想知道为什么会出现这些问题,以及如何解决它们。
这是程序代码(抱歉冗长):
#include <stdio.h>
#include "simpio.h"
#include "genlib.h"
void getArray(char array[][20]);
int getRow(void);
int getColumn(void);
void fill(char array[][20], int row, int column);
void dispArray(char array[][20]);
void dispMsg(void);
main()
{
char array[20][20];
int row, column;
dispMsg();
getArray(array);
printf("\nPlease enter an interior point from which the program starts filling.\n");
row=getRow();
column=getColumn();
fill(array, row, column);
dispArray(array);
getchar();
}
void dispMsg(void)
{
printf("This program will ask you to input the outline of a shape, and it will fill the shape up.\n");
}
void fill(char array[][20], int row, int column)
{
if(array[row][column] != ' '|| row>20 || row<0 || column>20 || column<0)
{
}
else
{
array[row][column] = '*';
fill(array, row, column+1);
fill(array, row+1, column);
fill(array, row, column-1);
fill(array, row-1, column);
}
}
void dispArray(char array[][20])
{
int i, j;
for(i = 0; i < 20; i++)
{
printf("\n");
for(j = 0; j < 20; j++)
{
printf("%c", array[i][j]);
}
}
}
int getRow(void)
{
int row;
printf("\nEnter the row of the point: \n");
row = GetInteger();
return(row);
}
int getColumn(void)
{
int column;
printf("Enter the column of the point: ");
column = GetInteger();
return(column);
}
void getArray(char array[][20])
{
int i, j, row = 0, column = 0, num;
char input;
printf("To input the perimeter of your shape please use asterisks(*), and the <enter> key to start a new line.\nEnd the input with the '!' signal\n\n");
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
array[i][j] = ' ';
}
}
i=0;
j=0;
while(true)
{
input=getchar();
if(input=='\n')
{
i++;
j=0;
row++;
column = num;
num = 0;
}
else if(input=='!')
{
if(array[i-1][j] == '*') row--;
break;
}
else
{
array[i][j] = input;
j++;
num++;
}
}
printf("Your input shape is: \n");
for(i=0;i <= row;i++)
{
printf("\n");
for(j=0;j <= column;j++)
{
printf("%c", array[i][j]);
}
}
}
例如:
If the user enters:
**************
* *****
* *
* *
* *
******************
The output will be:
**************
******************
******************
******************
******************
******************
但是,对于我的程序:
When the input is re-printed, this comes out:
**************** *
* ***** *
* * *
* * *
* ***
And when the fill is printed:
******************
* ****************
* ****************
* ****************
******************
******************
帮助将不胜感激,谢谢:)