软件工程大作业——数独游戏2
一、PSP表格
二、问题分析
三、系统设计
四、具体实现
五、单元测试
六、程序性能及质量分析
七、GUI
八、总结
代码地址:https://github.com/friyal0730/sudoku/
这篇文章主要是第四部分具体实现、第五部分单元测试和第六部分程序性能及质量分析
四、具体实现
Check.h:负责对用户输入的命令进行处理,如果命令输入错误,进行错误提示,下面是判断输入并执行的代码部分:
int solveinput() //判断并执行命令
{
if (argc != 3) //输入格式不正确
{
cout << "Illegal paramater number" << endl;
cout << "Input like this: [sudoku.exe -c n] or [sudoku.exe -s path]" << endl;
return 1;
}
if (strcmp(argv[1], "-c") && strcmp(argv[1], "-s")) //字母错误
{
cout << "The first parameter should only be -c or -s" << endl;
cout << "-c means to generate the sudoku to file." << endl;
cout << "-s means to solve the sudoku from the file." << endl;
return 2;
}
if (!strcmp(argv[1], "-c")) //创造数独终盘
{
int sum = 0; //sudoku的个数
int len = strlen(argv[2]);
for (int i = 0; i < len; i++)
{
if (!(argv[2][i] >= '0' && argv[2][i] <= '9')) //输入的字符不合法(不是数字)
{
cout << "The third paramater after -c should be number that indicate the sudoku you want." << endl;
if (argv[2][i] == '+' || argv[2][i] == '-' || argv[2][i] == '/' || argv[2][i] == '*')
{
cout << "Please input the number!" << endl;
return 8;
}
return 3;
}
sum = 10 * sum + argv[2][i] - '0';
}
if (sum > MAX || sum < 1) //数字过大
{
cout << "The number is too large,the number should be 1-1000000" << endl;
return 4;
}
/*----------------------------------*/
/*创建数独终盘对象*/
/*...........*/
FILE* file;
file = freopen("sudoku.txt", "w", stdout); //没有文件时可以创造
Base base(sum, file); //调用Generator
base.generate();
/*----------------------------------*/
return 5;
}
if (!strcmp(argv[1], "-s")) //解题
{
FILE* ans;
FILE* question; //数独题目
question = freopen(argv[2], "r", stdin);
if (!question)
{
cout << "The file path is not right,please check." << endl;
return