首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

不懂快要问。

2013-02-15 
不懂就要问。。。。本帖最后由 immortal920812 于 2013-01-28 15:52:10 编辑这是本人写的数独,但仍然有部分问

不懂就要问。。。。
本帖最后由 immortal920812 于 2013-01-28 15:52:10 编辑 这是本人写的数独,但仍然有部分问题,在点A 弹出数独界面,点B 移动光标填入数字时,已经明确用switch语句禁止填入字母,但当我按d的时候他会把我整个数独界面全部改成9,请问这是为什么。。。。。

#include<iostream>
#include<stdio.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int array[9][9]= {0};

int Sudoku[9][9]={{0,1,4,0,5,0,0,0,3},
                  {6,0,0,0,0,9,4,2,0},
                  {8,0,0,1,0,0,0,9,0},
                  {0,0,5,0,9,0,0,4,0},
                  {4,0,0,7,0,8,0,5,2},
                  {0,7,0,0,2,0,6,0,0},
                  {0,9,0,0,0,1,0,0,5},
                  {0,2,8,3,0,0,0,0,4},
                  {5,0,0,0,6,0,7,1,0}};

int Su[9][9]={{0,1,4,0,5,0,0,0,3},
              {6,0,0,0,0,9,4,2,0},
              {8,0,0,1,0,0,0,9,0},
              {0,0,5,0,9,0,0,4,0},
              {4,0,0,7,0,8,0,5,2},
              {0,7,0,0,2,0,6,0,0},
              {0,9,0,0,0,1,0,0,5},
              {0,2,8,3,0,0,0,0,4},
              {5,0,0,0,6,0,7,1,0}};
int S[9][9];


int sudo[9][9];
enum
{
 ARROW_UP =256+72,
 ARROW_LEFT  =256+75,
 ARROW_DOWN  =256+80,
 ARROW_RIGHT =256+77,
 KEY_ESC =27
};
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

static int get_code(void)
{
int ch=getch();
if(ch == 0|| ch == 224)
ch= 256+getch();
return ch;
}
int isvalid(const int i, const int j)
{
 const int n = sudo[i][j];

 const static int query[] = {0, 0, 0, 3, 3, 3, 6, 6, 6};
 int t, u;
 
 for (t = 0; t < 9; t++)
  if (t != i && sudo[t][j] == n || t != j && sudo[i][t] == n)
   return 0;
  
  for (t = query[i]; t < query[i] + 3; t++)
   for (u = query[j]; u < query[j] + 3; u++)
    if ((t != i || u != j) && sudo[t][u] == n)


     return 0; 
    
    return 1;
}
void output(void)
{
 static int n;
 int Sudo[9][9]={{0,1,4,0,5,0,0,0,3},{6,0,0,0,0,9,4,2,0},{8,0,0,1,0,0,0,9,0},{0,0,5,0,9,0,0,4,0},{4,0,0,7,0,8,0,5,2},{0,7,0,0,2,0,6,0,0},{0,9,0,0,0,1,0,0,5},{0,2,8,3,0,0,0,0,4},
{5,0,0,0,6,0,7,1,0}};

 
 
 
 for (int i = 0; i < 9; i++) 
 {
 int y=i+1;
 y=1+2*(y-1);
 
 
  for (int j = 0; j < 9; j++)
  {
  int x=j+1;
  x=1+3*(x-1);
  gotoxy(x,y);
  if(sudo[i][j] != Sudo[i][j])
  {
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
            FOREGROUND_RED | FOREGROUND_GREEN);

  printf("%d", sudo[i][j]);
  }
  else
  { 
  gotoxy(x+1,y);
  }
  S[i][j]=sudo[i][j];
  
  
  
 
 
  
  
   
  }
  

  
  
 }
 
 cout << endl;
}
void Try(const int n)
{

 if (n == 81)
 {
 
  output();
  return;
 }
 
 const int i = n / 9, j = n % 9;
 
 if (sudo[i][j] != 0) 
 {
 
  Try(n + 1);
  return;
  
 }
 
 for (int k = 0; k < 9; k++) 
 {
 
  sudo[i][j]++;
  if (isvalid(i,j))
  { Try(n + 1);
  }
  
 }
 sudo[i][j] = 0; 
 
}
 
 int solver(void)
{

 int m_pu[9][9] =
    {
        {0,1,4,0,5,0,0,0,3},
        {6,0,0,0,0,9,4,2,0},
        {8,0,0,1,0,0,0,9,0},
        {0,0,5,0,9,0,0,4,0},
        {4,0,0,7,0,8,0,5,2},
        {0,7,0,0,2,0,6,0,0},
        {0,9,0,0,0,1,0,0,5},
        {0,2,8,3,0,0,0,0,4},
        {5,0,0,0,6,0,7,1,0}
    };
    for (int i = 0; i < 9; ++i)
    {
        for (int j = 0; j < 9; ++j)
        {
            sudo[i][j] = m_pu[i][j];
        }
    }
    Try(0);
    return 0;
}
int sudocheck()
 {
 static int c;


 static int d;
 

 for(c=0; c<9; c++)
 {
 for(d=0; d<9; d++)
 {
 if (Sudoku[c][d] != 0 && Sudoku[c][d] != S[c][d])
 {
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED );
 int y= 2*c+1;
 int x= 3*d+1;
 gotoxy(x,y);
 printf("%d", Sudoku[c][d]);
 }
 }
 }
 
 
 
 return 1;
}


int curser(void)
{
int ch;

int x=1;
int y=1;

gotoxy(x,y);
  while((ch = get_code()) != KEY_ESC)
{
switch(ch)
{
case ARROW_UP:
y=y-2;
if(y<0)
{
y=y+2;
gotoxy(x,y);
}
else
{

gotoxy(x,y);
}

break;
case ARROW_DOWN:
y=y+2;
if(y > 17)
{
y=y-2;
gotoxy(x,y);
}
else
{
gotoxy(x,y);
}
break;
case ARROW_LEFT:
x=x-3;
if (x < 0)
{
x=x+3;
gotoxy(x,y);
}
else
{

gotoxy(x,y);
}
break;
case ARROW_RIGHT:
x=x+3;
if(x>25)
{
x=x-3;
gotoxy(x,y);
}
else
{
gotoxy(x,y);
}
break;
case 1 :

sudocheck();
gotoxy(x,y);
break;
case 'd':
printf("%d", S[0][0]);


default :
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
            FOREGROUND_GREEN);
if(Su[(y-1)/2][(x-1)/3] != 0)
{
gotoxy(x,y);
}
else
{
switch(ch)
{
case 49 :
putchar(ch);

Sudoku[(y-1)/2][(x-1)/3]= 1;
gotoxy(x,y);

break;
case 50 :
putchar(ch);

Sudoku[(y-1)/2][(x-1)/3]= 2;
gotoxy(x,y);

break;
case 51 :
putchar(ch);

Sudoku[(y-1)/2][(x-1)/3]= 3;
gotoxy(x,y);
break;
case 52 :
putchar(ch);

Sudoku[(y-1)/2][(x-1)/3]= 4;
gotoxy(x,y);

break;
case 53 :
putchar(ch);

Sudoku[(y-1)/2][(x-1)/3]= 5;
gotoxy(x,y);

break;
case 54 :
putchar(ch);

Sudoku[(y-1)/2][(x-1)/3]= 6;
gotoxy(x,y);

break;
case 55 :
putchar(ch);

Sudoku[(y-1)/2][(x-1)/3]= 7;
gotoxy(x,y);

break;
case 56 :
putchar(ch);

Sudoku[(y-1)/2][(x-1)/3]= 8;
gotoxy(x,y);

break;
case 57 :
putchar(ch);

Sudoku[(y-1)/2][(x-1)/3]= 9;
gotoxy(x,y);

break;

default:
gotoxy(x,y);
}
}






}


}
x=30;
y=0;
gotoxy(x,y);
return 0;

}


int picture()
{
int Sudo[9][9]={{0,1,4,0,5,0,0,0,3},{6,0,0,0,0,9,4,2,0},{8,0,0,1,0,0,0,9,0},{0,0,5,0,9,0,0,4,0},{4,0,0,7,0,8,0,5,2},{0,7,0,0,2,0,6,0,0},{0,9,0,0,0,1,0,0,5},{0,2,8,3,0,0,0,0,4},


{5,0,0,0,6,0,7,1,0}};
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY);


int colum;
printf("+=========================+\n");
for(colum=0;colum<9;colum++)
{
printf("|%d|", Sudo[0][colum]);
}
printf("\n---------------------------\n");
flushall();
for(colum=0;colum<9;colum++)
{
printf("|%d|", Sudo[1][colum]);
}
printf("\n---------------------------\n");
for(colum=0;colum<9;colum++)
{
printf("|%d|", Sudo[2][colum]);
}
printf("\n===========================\n");
for(colum=0;colum<9;colum++)
{
printf("|%d|", Sudo[3][colum]);
}
printf("\n---------------------------\n");
flushall();
for(colum=0;colum<9;colum++)
{
printf("|%d|", Sudo[4][colum]);
}
printf("\n---------------------------\n");
for(colum=0;colum<9;colum++)
{
printf("|%d|", Sudo[5][colum]);
}
printf("\n===========================\n");
for(colum=0;colum<9;colum++)
{
printf("|%d|", Sudo[6][colum]);
}
printf("\n---------------------------\n");
flushall();
for(colum=0;colum<9;colum++)
{
printf("|%d|", Sudo[7][colum]);
}
printf("\n---------------------------\n");
for(colum=0;colum<9;colum++)
{
printf("|%d|", Sudo[8][colum]);
}
printf("\n+=========================+\n");
return 0;

}



void main()
{
int x,y,a,b;
char op;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n********** Sudoku Server in C **********");
printf("\nA. Load Sudoku Puzzle Data\nB. Manual Sudoku Solver:\n   >>Arrow keys for positioning\n   >>Digid keys for entering numbers\n   >> Ctrl-A key for evaluation\n   >> ESC key back to menu");
flushall();
printf("\nC. *Auto sudoku Solver\nD. Exit");
printf("Enter Your Options : >>  ");
op=getch();



while(op != 'd' && op != 'D')
{
switch(op)
{
case 'A':
case 'a':
x=0;
y=0;
gotoxy(x,y);
solver();
gotoxy(0,0);
picture();
break;
case 'B':
case 'b':
curser();
break;
case 'C' :
case 'c' :
solver();
 break;
 





}
op=getch();
}
}
 


[解决办法]
case 'd'后面没有加break,结果连default语句也执行了。
[解决办法]
我现在  越来越佩服,会自己调试程序的人了。。。。
一个 F9,F10,F11。。

热点排行