软件工程第三次作业
github 地址:https://github.com/zhx485/031702325
文件:
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 60 | 20 |
Estimate | 估计这个任务需要多少时间 | 10 | 20 |
Development | 开发 | 200 | 100 |
Analysis | 需求分析 (包括学习新技术) | 100 | 600 |
Design Spec | 生成设计文档 | 100 | 100 |
Design Review | 设计复审 | 100 | 100 |
Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 30 | 30 |
Design | 具体设计 | 200 | 100 |
Coding | 具体编码 | 300 | 100 |
Code Review | 代码复审 | 100 | 50 |
Test | 测试(自我测试,修改代码,提交修改) | 150 | 600 |
Reporting | 报告 | 50 | 50 |
Test Repor | 测试报告 | 50 | 20 |
Size Measurement | 计算工作量 | 50 | 20 |
Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 50 | 20 |
合计 | 1520 | 1980 |
代码历程:
刚看到这个题目,感觉挺简单的,构思了一下就准备动手,然后就发现我错了。我以前学代码的时候不扎实现在有苦说不出,文件输入输出也不会都要从网上重新学,回溯法也是现学的,比预计时间多花了好多时间。所幸最后还是完成了。
单个数字与其他不冲突:
int fuhe(int j,int g,int sk,int jk){
int gong(int j, int g);
int flag = 0;
bp[j][g] = sk;
for (int u = 0; u < m ; u ++)
{
if (bp[j][g] == bp[j][u] && g != u)//行相等
{
flag = 1;
return (-1);
}
}
for (int u = 0; u < m; u++)
{
if (bp[j][g] == bp[u+jk*m][g] && j != (u+jk*m))//列相等
{
flag = 1;
return (-1);
}
}
if (gong(j, g) == -1) return (-1);//宫符合
return 1;}
深度搜索:
int dfs(int io,int jk) {//dfs深度搜索
int x = (io-1)/m+jk*m;
int y = ((io-1) % m);
if (io > m * m ) // 结束了
return 1;
if (bp[x][y] != 0) {
return dfs(io + 1,jk);
}
else {
for (int uo = 1; uo <= m; uo++)
{
if (fuhe(x, y, uo,jk) == 1) {
bp[x][y] = uo ;
if (dfs(io + 1,jk))//往下走
return 1;
}
bp[x][y] = 0;//回溯置0
}
}
return 0;
}
质量检测
调用了一些未使用的参数,还有fopen函数的使用警告。
性能探查
示例:
3阶
4阶
5阶
6阶
7阶
8阶
9阶
完整代码:
#include <iostream>
#include<fstream>
#include<string>
#include<cstdio>
int m=0, n=0;
int weizhi[2] = { 0 };
using namespace std;
string k1,k2;
int bp[90][11];
void intxt(string h) {
int k;
char filename[10] ; //文件名
for (k = 0; k < h.length(); k++) //string => char[]
{
filename[k] = h[k];
}
filename[k] = ' ';
FILE* fp;
char StrLine[1024];
if ((fp = fopen(filename, "a+")) == NULL) //判断文件是否存在及可读
{
printf("error!");
}
int i ,j;
for (int time = 0; time < n; time++)
{
for (i = m*time; i < m*(time+1); i++)
{
for (j = 0; j < m; j++)
{
fscanf(fp, "%d", &bp[i][j]);
}
}
}
i = 0;
fclose(fp);
}
int fuhe(int j,int g,int sk,int jk){
int gong(int j, int g);
int flag = 0;
bp[j][g] = sk;
for (int u = 0; u < m ; u ++)
{
if (bp[j][g] == bp[j][u] && g != u)//行相等
{
flag = 1;
return (-1);
}
}
for (int u = 0; u < m; u++)
{
if (bp[j][g] == bp[u+jk*m][g] && j != (u+jk*m))//列相等
{
flag = 1;
return (-1);
}
}
if (gong(j, g) == -1) return (-1);//宫符合
return 1;}
int main()
{
int gong(int j, int g);
int dfs(int io,int jk);
char qi[4][8] = {0};
string h, h1;
for (int i = 0; i < 4; i++) //4种命令
{
cin >> qi[i][0] >> qi[i][1];
switch (qi[i][1])
{
case 'i': cin >> k1; continue;
case 'm': cin >> m; continue;
case 'n': cin >> n; continue;
case 'o': cin >> k2; continue;
default: cout << "error"<<endl;
break;
}
}
intxt(k1);//读表盘
char outname[20];//输出
for (int y = 0; y < k2.length(); y++)
{
outname[y] = k2[y];
}
outname[k2.length()] = ' ';
for (int i = 0; i < n; i++)
{
int j, f, g;
if (dfs(1,i))
cout << " success"<<endl;
FILE* fp;
if ((fp = fopen(outname, "a+")) == NULL) //创建输出文件
{
printf("error!");
}
for (int il = i*m; il < m*(i+1); il++)
{
for (int j = 0; j < m; j++)
if (j != m - 1)fprintf(fp, "%d ", bp[il][j]);
else fprintf(fp, "%d
", bp[il][j]);
}
}
return 0;
}
int gong(int j,int g) {
int x, y;
if (m == 4)
{
x = (j / 2) * 2;
y = (g / 2) * 2;
for (int i = x; i < x + 2; i++)
{
for (int o = y; o < y + 2; o++)
{
if (bp[j][g] == bp[i][o] && (j != i || g != o)) return (-1);//4阶
}
}
}
if (m == 6)
{
x = (j / 2) * 2;
y = (g / 3) * 3;
for (int i = x; i < x + 2; i++)
{
for (int o = y; o < y + 3; o ++)
{
if (bp[j][g] == bp[i][o] && (j != i || g != o)) return (-1);//6阶
}
}
}
if (m == 8)
{
x = (j / 4) * 4;
y = (g / 2) * 2;
for (int i = x; i < x + 4; i++)
{
for (int o = y; o < y + 2; o ++)
{
if (bp[j][g] == bp[i][o] && (j != i || g != o)) return (-1);//8阶
}
}
}
if (m == 9)
{
x = (j / 3) * 3;
y = (g / 3) * 3;
for (int i = x; i < x + 3; i++)
{
for (int o = y; o < y + 3; o ++)
{
if (bp[j][g] == bp[i][o] && (j != i || g != o)) return (-1);//9阶
}
}
}
return (1);
}
int dfs(int io,int jk) {//dfs深度搜索
int x = (io-1)/m+jk*m;
int y = ((io-1) % m);
if (io > m * m ) // 结束了
return 1;
if (bp[x][y] != 0) {
return dfs(io + 1,jk);
}
else {
for (int uo = 1; uo <= m; uo++)
{
if (fuhe(x, y, uo,jk) == 1) {// 判断当前情况下uo是否可用
bp[x][y] = uo ;
if (dfs(io + 1,jk)) //往下试探
return 1;
}
bp[x][y] = 0;
}
}
return 0;
}