Software Engineering major operations - Sudoku 2

Software Engineering major operations - Sudoku 2


A, PSP form
two, problem analysis
Third, the system design of
four concrete realization of
five unit testing
six program performance and quality analysis of
seven, GUI
eight summarizes
code address: https://github.com/friyal0730/sudoku/
this The main article is the fourth part concrete realization, part V and part VI unit testing application performance and quality analysis

Fourth, the specific implementation

Check.h: command input by the user is responsible for processing, if the command input error, error prompt, enter the following code portion is determined and executed:

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 6;
			}
			/*----------------------------------*/
			/*创建数独求解对象*/
			/*...........*/
			ans = freopen("sudoku.txt", "w", stdout);
			Solver solver(question, ans);   //调用Solver
			flag = solver.in();
			/*----------------------------------*/
			return 7;
		}
		
		return 8;   //正常执行,消除警告
	}

Base.h: generating a number of independent Zhongpan Bureau, a matrix conversion method, the first wrap, back to the column, the last change number, line feed codes are as follows:

void generate()   //生成函数
	{
		int number = 0;
		while (number < count)
		{
			Out();
			number++;

			Line_exchange_floor(&number);//换上面部分的行
			Line_exchange_middle(&number);//换中间部分的行
			Line_exchange_ground(&number);//换下面部分的行

			if (number < count)
			{
				TransForm();
				Change();
			}
		}
	}

Answer.h: single number to solve the problem using backtracking algorithm, dfs specific code as follows:

bool dfs(int tot)   //dfs搜索方法
	{             
		if (tot > 80)
		{
			return true;
		}
		
		int line = tot / 9;
		int col = tot % 9;

		if (sudoku[line][col] > 0)
		{
		    return dfs(tot + 1);
		}

		for(int i = 1;i <= 9;i++)
		{
			sudoku[line][col] = i;
			if (check(line, col, i))
			{
				if (dfs(tot + 1)) 
				{
					return true;
				}
			}
			sudoku[line][col] = 0;
		}
		
		return false;
	}

question.cpp: to generate a number of separate subject, used the rand function, specifically implemented as follows:
 

void change()
{
	int a = 0, b = 2;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			for (int k = 0; k < 2; k++)
			{
				int ran1 = (rand() % (b - a + 1)) + a;
				int ran2 = (rand() % (b - a + 1)) + a;
				if ((ran1 + i * 3)*(ran2 + j * 3) != 0)
				{
					incom_sudoku[ran1 + i * 3][ran2 + j * 3] = 0;
				}
			}
		}
	}
	a = 0, b = 8;
	for (int i = 0; i < 42; i++)
	{
		int ran1 = (rand() % (b - a + 1)) + a;
		int ran2 = (rand() % (b - a + 1)) + a;
		if (ran1 != 0 && ran2 != 0)
		{
			incom_sudoku[ran1][ran2] = 0;
		}
	}
}

Fourth, the test unit and Code Description


For each module, I have designed a unit test, wrote a total of ten use cases.
First, for the test inputs, a total of eight possible for the input formula, also tested, as follows:

                UnitTest1()
		{
			/*测试命令 "sudoku.exe -c 100" */
			argv = new char*[3];

			argv[0] = new char[100];
			strcpy_s(argv[0], 100, "sudoku.exe");

			argv[1] = new char[100];
			strcpy_s(argv[1], 100, "-c");

			argv[2] = new char[100];
			strcpy_s(argv[2], 100, "100");
		}

		TEST_METHOD(TestMethod1)   //输入格式不正确的时候
		{
			/*测试命令 “sudoku.exe”*/
			argc = 1;
			Solve Solve1(argc, argv);
			int result = Solve1.Solveinput();
			assert(result == 1);
		}

		TEST_METHOD(TestMethod2)   //输入的不是-c或者-s的时候
		{
			
			/*测试命令 "sudoku.exe -k 100" */
			argc = 3;

			argv = new char*[3];
			argv[0] = new char[100];
			strcpy_s(argv[0], 100, "sudoku.exe");
			argv[1] = new char[100];
			strcpy_s(argv[1], 100, "-k");
			argv[2] = new char[100];
			strcpy_s(argv[2], 100, "100");

			Solve Solve2(argc, argv);
			int result = Solve2.Solveinput();
			assert(result == 2);
		}

		TEST_METHOD(TestMethod3)   //输入不是数字的时候
		{
			/*测试命令 "sudoku.exe -c abc" */
			argc = 3;

			argv = new char*[3];
			argv[0] = new char[100];
			strcpy_s(argv[0], 100, "sudoku.exe");
			argv[1] = new char[100];
			strcpy_s(argv[1], 100, "-c");
			argv[2] = new char[100];
			strcpy_s(argv[2], 100, "abc");

			Solve Solve3(argc, argv);
			int result = Solve3.Solveinput();
			assert(result == 3);
		}

		TEST_METHOD(TestMethod4)   //输入的数字过大
		{
			/*测试命令 "sudoku.exe -c 1000009" */
			argc = 3;

			argv = new char*[3];
			argv[0] = new char[100];
			strcpy_s(argv[0], 100, "sudoku.exe");
			argv[1] = new char[100];
			strcpy_s(argv[1], 100, "-c");
			argv[2] = new char[100];
			strcpy_s(argv[2], 100, "1000009");

			Solve Solve4(argc, argv);
			int result = Solve4.Solveinput();
			assert(result == 4);
		}

		TEST_METHOD(TestMethod5)   //输入生成数独终盘命令正确
		{
			/*测试命令 "sudoku.exe -c 10000" */
			argc = 3;

			argv = new char*[3];
			argv[0] = new char[100];
			strcpy_s(argv[0], 100, "sudoku.exe");
			argv[1] = new char[100];
			strcpy_s(argv[1], 100, "-c");
			argv[2] = new char[100];
			strcpy_s(argv[2], 100, "10000");

			Solve Solve5(argc, argv);
			int result = Solve5.Solveinput();
			assert(result == 5);
		}

		TEST_METHOD(TestMethod6)   //解数独题的路径错误
		{
			/*测试命令 "sudoku.exe -s 100" */
			argc = 3;

			argv = new char*[3];
			argv[0] = new char[100];
			strcpy_s(argv[0], 100, "sudoku.exe");
			argv[1] = new char[100];
			strcpy_s(argv[1], 100, "-s");
			argv[2] = new char[100];
			strcpy_s(argv[2], 100, "100");

			Solve Solve6(argc, argv);
			int result = Solve6.Solveinput();
			assert(result == 6);
		}

		TEST_METHOD(TestMethod7)   //解数独题的路径正确
		{
			/*测试命令 "sudoku.exe -s solver.txt" */
			argc = 3;

			argv = new char*[3];
			argv[0] = new char[100];
			strcpy_s(argv[0], 100, "sudoku.exe");
			argv[1] = new char[100];
			strcpy_s(argv[1], 100, "-s");
			argv[2] = new char[100];
			strcpy_s(argv[2], 100, "solver.txt");

			Solve Solve7(argc, argv);
			int result = Solve7.Solveinput();
			assert(result == 7);
		}

		TEST_METHOD(TestMethod8)   //输入算式
		{
			/*测试命令 "sudoku.exe -c 10/5" */
			argc = 3;

			argv = new char*[3];
			argv[0] = new char[100];
			strcpy_s(argv[0], 100, "sudoku.exe");
			argv[1] = new char[100];
			strcpy_s(argv[1], 100, "-c");
			argv[2] = new char[100];
			strcpy_s(argv[2], 100, "10/5");

			Solve Solve8(argc, argv);
			int result = Solve8.Solveinput();
			assert(result == 8);
		}

 

For Generator.h test:
The main purpose of: generating a check matrix for duplicate case

TEST_METHOD(TestMethod10)   //证明没有生成重复的矩阵
		{
			int sudoku_number = 1000000;
			FILE* file;
			freopen_s(&file, "sudoku_temp.txt", "w", stdout);//写文件
			assert(file != NULL);
			//生成数独
			Base sudoku_generator(sudoku_number, file);
			sudoku_generator.generate();
			fclose(stdout);

			freopen_s(&file, "sudoku_temp.txt", "r", stdin);//读文件
			assert(file != NULL);
			string s1;
			bool end = false;
			//将数独加入集合,若集合里数独的个数与sudoku_number相等,则无重复
			set<string> container;

			while (true)
			{
				int temp;
				for (int i = 0; i < 9; i++)
				{
					for (int j = 0; j < 9; j++)
					{
						if (fscanf_s(file, "%d", &temp) == EOF)
						{
							end = true;
							break;
						}
						s1.push_back(temp + '0');
					}
					if (end) break;
				}
				if (end) break;
				container.insert(s1);
				s1.clear();
			}
			fclose(stdin);
			
			assert(container.size() != sudoku_number);
		}

Answer.h test:

The main purpose: Check-generated Sudoku matrices are correct

TEST_METHOD(TestMethod9) //解题是否成功
		{
			/*测试命令 "sudoku.exe -s solver.txt" */
			argc = 3;

			argv = new char*[3];
			argv[0] = new char[100];
			strcpy_s(argv[0], 100, "sudoku.exe");
			argv[1] = new char[100];
			strcpy_s(argv[1], 100, "-s");
			argv[2] = new char[100];
			strcpy_s(argv[2], 100, "solver.txt");

			Solve Solve7(argc, argv);
			assert(Solve7.flag == 0);
		}

Coverage Test:

Fifth, program performance and quality analysis

At first, I chose randomly generated Sudoku Zhongpan generate one million the number of independent Zhongpan spend 40s of time. So I changed matrix conversion method saves a lot of time, efficiency is very high, the performance has improved a lot. I also removed some unnecessary cycle, the final number of unique algorithms to generate one million end game in less than 4s time performance improved tenfold. Here is my performance analysis:
 

发布了2 篇原创文章 · 获赞 0 · 访问量 23

Guess you like

Origin blog.csdn.net/qq_41343044/article/details/104029657
Recommended