2012-05-02 2 views
-1

나는 모양에 대한 문자 보더의 사용자의 입력 주어의 형태를 채우고, C에서 프로그램을 만들려고하고 있습니다.채우기 프로그램

http://pastebin.com/aax1dt0b

#include <stdio.h> 
#include "simpio.h" 
#include "genlib.h" 

#define size 100 

bool initArray(bool a[size][size]); 
bool getshape(bool a[size][size]); /* Gets the input of the boarder of the shape from   the user */ 
void fill(int x, int y, bool a[size][size]); /* fills the shape */ 
void printarray(bool a[size][size]); /* prints the filled shape */ 


main() 
{ 
    int x, y; 
    char i; 
    bool a[size][size]; 
    initArray(a); 
    getshape(a); 
    printf("Enter the coordinates of the point the shape should be filled.\n"); 
    printf("x=n\n"); /* gets the coordinates of the array to begin the fill algorithm from */ 
    x = GetInteger(); 
    printf("y=\n"); 
    y = GetInteger(); 
    fill(x, y, a); 
    printarray(a); 
    printf("Scroll up to view your filled shape\n"); 
    getchar(); 
} 

bool initArray(bool a[size][size]) 
{ 
    int i, j; 
    for (i = 0; i < 100; i++) 
    { 
     for (j = 0; j < 100; j++) 
     { 
      a[i][j] = FALSE; 
     } 
    } 
} 

bool getshape(bool a[size][size]) 
{ 
    int i, j, k; 
    bool flag; 
    char ch; 
    ch = 1; 
    printf("Enter your shape. When you are finished, type 'E'. \n"); 
    for (i = 0; i < 100; i++) 
    { 
     flag = TRUE; 
     for (j = 0; ch != 10; j++) 
     { 
      ch = getchar(); 
      if (ch == 69) 
      { 
       return a; 
      } 
      if (ch != 32) a[i][j] = TRUE; 
     } 

     ch = 1; 
    } 
} 


void fill(int x, int y, bool a[size][size]) 
{ 
    if (a[y][x] != TRUE) a[y][x] = TRUE; 
    if (a[y][x - 1] != TRUE) fill(x - 1, y, a); 
    if (a[y - 1][x] != TRUE) fill(x, y - 1, a); 
    if (a[y][x + 1] != TRUE) fill(x + 1, y, a); 
    if (a[y + 1][x] != TRUE) fill(x, y + 1, a); 
} 

void printarray(bool a[size][size]) 
{ 
    int i, j; 
    printf("\n\n\n"); 
    for (i = 0; i < 100; i++) 
    { 
     for (j = 0; j < 100; j++) 
     { 
      if (a[i][j] == FALSE) printf(" "); 
      if (a[i][j] == TRUE) printf("*"); 
     } 
     printf("\n"); 
    } 
} 

내 프로그램 작품 대부분은 채워진 모양을 인쇄 할 때 각 행에 하나의 문자를 추가합니다. 이

*** 
*** 
*** 

사람이 내가이 문제를 해결할 수있는 방법을 알고 있어야합니다 반면 예를 들어, 사용자의 입력이

*** 
    * * 
    *** 

그런 다음 출력은

**** 
**** 
**** (one extra row then it should be) 

될 것인지?

+0

즉, 코드를 표시하십시오. –

+0

코드를 추가했습니다. 죄송합니다. – Joshpho

+4

아니요. pastebin에 없으므로 게시물에 추가하고'{} '을 사용하여 형식을 지정하십시오. – Joe

답변

0

코드에 몇 가지 잠재적 인 문제가 있지만 4 번째 열 *의 문제를 식별 할 것입니다. 아래의 코드에서 을 for statement으로 확인했지만 루프를 종료하기 전에 a[i][j]의 값이 TRUE으로 지정됩니다. 따라서 if(ch!=32 && ch!=10) a[i][j]=TRUE;을 원할 수 있습니다.

     flag=TRUE; 
        for(j=0;ch!=10;j++) 
        { 
             ch=getchar(); 
             if(ch==69) 
             { 
               return a; 
             } 
             if(ch!=32) a[i][j]=TRUE; 
        } 

        ch=1; 
관련 문제