每次我从 C 中的输入文件中读取 '\n' 时都无法循环函数

Having trouble looping over a function every time I read in a '\n' from an input file in C

对于我的一个编程 类,我们应该编写一个可以使用回溯解决数独谜题的程序。虽然这应该是困难的部分,但我尴尬地坚持解析输入。 该程序应该以以下形式读取输入:

2..5.4.3.1...9...8.........9...5...6.4.6.2.7.7...4...5.........4...8...1.5.2.3.4.
7.....4...2..7..8...3..8.799..5..3...6..2..9...1.97..6...3..9...3..4..6...9..1.35
....7..2.8.......6.1.2.5...9.54....8.........3....85.1...3.2.8.4.......9.7..6....

程序读取一行,如果它是一个有效的谜题,它会解决它并打印解决方案,然后继续到下一行,然后是下一行,直到它最终到达 EOF。现在我有一个使用 getchar() 读取拼图的函数,我从我的导师那里得到了设置一个全局变量的想法,该变量在该函数到达 EOF 时打开。然后在 main 中有一个 while 循环,本质上说

While(not done)
{
read puzzle
solve puzzle
}

无论如何,这是实际的代码,我只包括了我试图用来读取输入的两个函数,我已经确认了有关实际数独求解器工作的所有内容:

#include <stdio.h>

#define TRUE 1
#define FALSE 0
int done = FALSE;

void readInPuzzle(int puzzle[9][9])
{
    //puzzle [8][9] = '[=12=]';
    int i = 0;
    int j = 0;
    int c;
    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 9; j++)
        {
            c = getchar();
            if (c != '\n')
            {
                /*This converts the chars from getchar(); into easier to manage ints*/
                if (c == '.') c = 48;
                puzzle[i][j] = (c - '0');
            }
            if (c == EOF) done = TRUE;
        }
    }
}

void main()
{
    int puzzle[9][9];
    while (done == FALSE);
    {
        readInPuzzle(puzzle);
        if (solvePuzzle(puzzle,0,0) == TRUE)
            printPuzzle(puzzle);
    }
}

当我 运行 程序并给它上面的输入时,它永远 运行s 我认为这意味着 'done' 变量永远不会设置为 1。我离得远了吗base 如何使用全局变量?有没有更好的方法让这个程序每次从输入中读入 '\n' 时重复自己?

while (done == FALSE);

这里它只停留在 while 语句中。

删除 ;

  while (done == FALSE)
  {
    readInPuzzle(puzzle);
    if (solvePuzzle(puzzle,0,0) == TRUE)printPuzzle(puzzle);
  }

样本修复

#include <stdio.h>

#define TRUE  1
#define FALSE 0

int readInPuzzle(int puzzle[9][9])
{
  int i, j;
  char c;
  for (i = 0; i < 9; i++)
  {
    for (j = 0; j < 9; j++)
    {
      if(EOF == scanf(" %c", &c))
        return FALSE;
      if (c == '.') c = '0';
      puzzle[i][j] = c - '0';
    }
  }
  return TRUE;
}

int main(void)
{
  int puzzle[9][9];
  while (FALSE != readInPuzzle(puzzle))
  {
    if (solvePuzzle(puzzle,0,0) == TRUE)printPuzzle(puzzle);
  }
  return 0;
}

以下代码更正了拼图中的读取逻辑,处理了某些输入文件格式错误并更正了 main()

中 'while' 语句的问题

注意:添加存根函数 solvePuzzle() 和 printPuzzle() 只是为了使发布的代码能够干净地编译

#include <stdio.h>

#define TRUE 1
#define FALSE 0
int done = FALSE;

int solvePuzzle(int puzzle[9][9], int row, int col)
{
    puzzle = puzzle;
    row = row;
    col = col;
    return TRUE;
}

void printPuzzle(int puzzle[9][9])
{
    puzzle = puzzle;
}

void readInPuzzle(int puzzle[9][9])
{
    //puzzle [8][9] = '[=10=]';
    int i = 0;
    int j = 0;
    int c;

    for (i = 0; i < 9; i++)
    {

        for (j = 0; j < 9; j++)
        {

            c = getchar();

            if( EOF == c )
            {
                if( i || j)
                { // then input file improperly formatted
                    printf("input file format incorrect, unexpected EOF at row=%d, col=%d\n", i, j );
                }
                done = TRUE;
                return;
            }

            else if( '\n' == c )
            { // then input file improperly formatted
                printf( "input file format incorrect, unexpected newline at row=%d col=%d\n", i, j );
                done = TRUE;
                return;
            }


            /*This converts the chars from getchar() into easier to manage ints*/
            if (c == '.') c = '0';
            puzzle[i][j] = (c - '0');
        } // end for
    } // end for
    getchar();  // read newline (if any)
} // end function: readInPuzzle


int main( void )
{
    int puzzle[9][9];

    while ( !done )
    {
        readInPuzzle(puzzle);
        if( done ) break;

        if (solvePuzzle(puzzle,0,0) )
        {
            printPuzzle(puzzle);
        }
    }
    return 0;
} // end function; main