大約兩個星期前,我發佈了一個類似的問題,我解決了我的問題。但是,經過一些修改後,我的程序無法正常工作。這些說明將採用用戶用星號()繪製的形狀的周長,然後用星號填充形狀()。遞歸填充形狀的程序
有兩個問題:
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:
******************
* ****************
* ****************
* ****************
******************
******************
幫助將非常讚賞,謝謝:)
你有什麼嘗試調試它?你能舉個例子說明它應該做什麼,它做了什麼?另請參閱http://whathaveyoutried.com/ –
@MihaiMaruseac請參閱編輯 –
該程序顯示了什麼? –