2012-05-02 72 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"); 
    } 
} 

私のプログラムの動作ほとんどの場合、塗りつぶされた図形が印刷されると、各行に1つの追加文字が追加されます。それは

*** 
*** 
*** 

誰もが、私はこの問題を解決することができます方法を知っているべきであるのに対し、例えば、ユーザからの入力であれば、それ

*** 
    * * 
    *** 

その後、出力は

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

でしょうか?

+0

つまり、コードを表示します。 –

+0

コードを追加しました。申し訳ありません。 – Joshpho

+4

いいえ、pastebinにはいません。投稿に追加し、 '{}'を使用してフォーマットしてください。 – Joe

答えて

0

コードにはいくつかの潜在的な問題がありますが、私は4列目の問題の特定に行きます。以下のコードでは、のch!=10をチェックしていますが、ループを終了する前に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; 
関連する問題