C ++数独解算器

时间:2014-02-17 22:03:13

标签: c++ arrays multidimensional-array

需要帮助找出readPuzzleprintPuzzle函数的问题。 SetOfSmallInts是一个包含{1,2,3,4,5,6,7,8,9}或这些数字的任意组合的集合。 singletonSet(s)在一个集合中存储单个数字。无论我在程序上运行什么输入,它都只输出1。即使所有输入都是' - ',输出也是81 1。有什么建议? main中的Puzzle类型构成SetofSmallInts [9][9]的数组。

//==============================================================
//                      readPuzzle
//==============================================================
// Reads in puzzle p from the standard input.
//==============================================================

void readPuzzle(Puzzle p)
{
   int i, j;
   SetOfSmallInts s;
   s = rangeSet(1, 9);
   char n;

   for(i = 0; i < 9; i++)
   {
      for(j = 0; j < 9; j++) 
      {
       n = scanf("%c", &n);  
       if (n == '-')
       {
         p[i][j]= s;
       }
       else if(n==1 || n==2 || n==3 || n==4 || n==5 || n==6 ||
               n==7 || n==8 || n==9)
      {
         p[i][j]= singletonSet(n);
      }
      }
   }      
}


//==============================================================
//                      printPuzzle
//==============================================================
// Prints in puzzle p.
//==============================================================
void printPuzzle(Puzzle p)
{
   int i, j, s;
   SetOfSmallInts x;

   for(i = 0; i < 9; i++)
   {
      for(j = 0; j < 9; j++)
      {
         x = p[i][j];
         if(isEmpty(x)) 
         {
            printf("%i ", 0);
         }
         else if(isSingleton(x)) //returns true if x hold Singleton set
         {
            s = smallest(x); // returns the smallest member of s
            printf("%i ", s);
         }
         else
         {
            printf("%c ", '-');
         }
      }
   }
}  

//==============================================================
//                      showPuzzle
//==============================================================
// Shows in puzzle p in a format that can be used for debugging
//==============================================================


//==============================================================
//                   main
//==============================================================

int main(int argc, char** argv)
{
   Puzzle p;
   readPuzzle(p);
   printPuzzle(p);
   return 0;
}

2 个答案:

答案 0 :(得分:5)

n = scanf("%c", &n);

scanf返回扫描的项目数。你只想要

scanf("%c", &n);

答案 1 :(得分:2)

您只需要scanf("%c", &n)

也是一个字符

所以

else if(n==1 || n==2 || n==3 || n==4 || n==5 || n==6 ||
               n==7 || n==8 || n==9)

需要做n == '1'等等

else if( n >= '1' && n <= '9')

然后你可能需要

p[i][j]= singletonSet(n - '0');