Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每次在';中读取函数时,循环函数时遇到问题\n';从C中的输入文件_C_Parsing_Input_Newline - Fatal编程技术网

每次在';中读取函数时,循环函数时遇到问题\n';从C中的输入文件

每次在';中读取函数时,循环函数时遇到问题\n';从C中的输入文件,c,parsing,input,newline,C,Parsing,Input,Newline,在我的一门编程课上,我们应该编写一个程序,用回溯法解决数独难题。虽然这应该是最难的部分,但令人尴尬的是,我一直在解析输入。 程序应以以下形式读入输入: 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

在我的一门编程课上,我们应该编写一个程序,用回溯法解决数独难题。虽然这应该是最难的部分,但令人尴尬的是,我一直在解析输入。 程序应以以下形式读入输入:

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()读取拼图,我从instrctor中得到一个想法,设置一个全局变量,当这个函数达到EOF时,该变量将被打开。然后主要有一个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] = '\0';
    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);
    }
}
#包括
#定义真1
#定义FALSE 0
int done=FALSE;
void readInPuzzle(整数拼图[9][9])
{
//拼图[8][9]='\0';
int i=0;
int j=0;
INTC;
对于(i=0;i<9;i++)
{
对于(j=0;j<9;j++)
{
c=getchar();
如果(c!='\n')
{
/*这会将getchar()中的字符转换为更易于管理的整数*/
如果(c='.')c=48;
拼图[i][j]=(c-‘0’);
}
如果(c==EOF)done=TRUE;
}
}
}
void main()
{
智力拼图[9][9];
while(done==FALSE);
{
阅读拼图;
if(solvePuzzle(puzzle,0,0)=TRUE)
印刷拼图(拼图);
}
}
当我运行程序并给它上面的输入时,它将永远运行,我假设这意味着“done”变量永远不会被设置为1。关于如何使用全局变量,我是否偏离了基础?是否有更好的方法让此程序在每次从输入读取'\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;
}
#包括
#定义真1
#定义FALSE 0
int readInPuzzle(int拼图[9][9])
{
int i,j;
字符c;
对于(i=0;i<9;i++)
{
对于(j=0;j<9;j++)
{
如果(EOF==scanf(“%c”、&c))
返回FALSE;
如果(c='.')c='0';
拼图[i][j]=c-'0';
}
}
返回TRUE;
}
内部主(空)
{
智力拼图[9][9];
while(FALSE!=readInPuzzle(拼图))
{
if(solvePuzzle(puzzle,0,0)==TRUE)printPuzzle(puzzle);
}
返回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] = '\0';
    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
#包括
#定义真1
#定义FALSE 0
int done=FALSE;
整数解谜(整数谜题[9][9],整数行,整数列)
{
拼图=拼图;
行=行;
col=col;
返回TRUE;
}
无效打印拼图(整数拼图[9][9])
{
拼图=拼图;
}
void readInPuzzle(整数拼图[9][9])
{
//拼图[8][9]='\0';
int i=0;
int j=0;
INTC;
对于(i=0;i<9;i++)
{
对于(j=0;j<9;j++)
{
c=getchar();
如果(EOF==c)
{
if(i | | j)
{//然后输入文件格式不正确
printf(“输入文件格式不正确,行=%d,列=%d\n”,i,j处出现意外EOF);
}
完成=正确;
返回;
}
else如果('\n'==c)
{//然后输入文件格式不正确
printf(“输入文件格式不正确,行=%d列=%d\n”,i,j处出现意外换行符”;
完成=正确;
返回;
}
/*这会将getchar()中的字符转换为更易于管理的整数*/
如果(c='.')c='0';
拼图[i][j]=(c-‘0’);
}//结束
}//结束
getchar();//读取换行符(如果有)
}//结束函数:readInPuzzle
内部主(空)
{
智力拼图[9][9];
而(!完成)
{
阅读拼图;
如果(完成)中断;
if(解算拼图(拼图,0,0))
{
印刷拼图(拼图);
}
}
返回0;
}//结束函数;主要的

使用:
\include
函数
main()
的返回类型始终为“int”,而不是“void”,而不是让void函数设置全局标志,您可以使用函数的返回值来指示成功或失败。不要编辑问题/标题,请选择下面的一个答案。输入行是否可以少于81个字符(9x9)?如果每个输入行都是81个字符+尾随的'\n',那么readInPuzzle()的逻辑需要一些修改哈哈,这是令人尴尬的。谢谢你!它的编程…和这样简单的问题有时会破坏你整个晚上的工作。。。!