homework-04
1.准备工作
本次结对编程我们对项目本身就行了分工,既然是测试来驱动开发,那么我们就把本次工作分成了测试与生成两个部分,小明同学负责生成测试数据,而我写测试程序检测测试结果是否正确,相对来说还是小明同学的那部分较为复杂。因为若想得到正确的结果,测试数据必然不能随机生成。然后我们就开始了我们的工作。
2.思路分析
先看下测试程序需要测试哪些功能?
a) Stage 1
a. Every phrase in the input file is covered once and only once.
b. No less than 2 of the phrases must be in these directions:
i. top-down, bottom-up, left-right, right-left, and all 4 diagonal directions.
c. The width and height of the matrix can be different
- d. there doesn’t exist a row or column of letters where none of the letters are covered (不存在一行或一列字母不被任何短语覆盖)。
b) Stage 2
a. The matrix must have the same width and height
Stage 3
The four corners of the output matrix must be occupied by a phrase.
看到了需求之后我们就能够确定我们需要记录哪些数据了?首先是每个phrase在输入文件中出现的次数,第二点是每个方向上出现的单词个数以及输入文件中每个字符的访问次数。
为了完成这项工程,首先想到的是深度优先搜索,但是由于需要对8个方向上进行深度优先搜索,函数的参数处理比较繁琐,并且考虑到输入矩阵不会太大,最终我们选择了暴力搜索的方式。
3.实际编码
#include<stdio.h> #include<string.h> char s[22][22]; //传进来的字母矩阵 int visit[22][22]; int number; //需要放进去的单词数目 char verbal[10][10]; //需要放进去的单词 int lenth[10]; //需要放进去的单词的长度 int d1,d2,d3,d4,d5,d6,d7,d8,d9; int n; //矩阵大小 int check_verbal[10]; //单词在s中出现的次数 int search_1(int i,int j,int k) { int temp=0; int starti=i,startj=j; while(i>0&&j>0&&i<=n&&j<=n&&temp<lenth[k]){ if(s[i][j]==verbal[k][temp]){ j++; temp++; } else return 0; } if(temp==lenth[k]){ for(i=0;i<lenth[k];i++) visit[starti][startj+i]=1; check_verbal[k]++; return 1; } else return 0; } int search_2(int i,int j,int k) { int temp=0; int starti=i,startj=j; while(i>0&&j>0&&i<=n&&j<=n&&temp<lenth[k]){ if(s[i][j]==verbal[k][temp]){ i++; j++; temp++; } else return 0; } if(temp==lenth[k]){ for(i=0;i<lenth[k];i++) visit[starti+i][startj+i]=1; check_verbal[k]++; return 1; } else return 0; } int search_3(int i,int j,int k) { int temp=0; int starti=i,startj=j; while(i>0&&j>0&&i<=n&&j<=n&&temp<lenth[k]){ if(s[i][j]==verbal[k][temp]){ i++; temp++; } else return 0; } if(temp==lenth[k]){ for(i=0;i<lenth[k];i++) visit[starti+i][startj]=1; check_verbal[k]++; return 1; } else return 0; } int search_4(int i,int j,int k) { int temp=0; int starti=i,startj=j; while(i>0&&j>0&&i<=n&&j<=n&&temp<lenth[k]){ if(s[i][j]==verbal[k][temp]){ i++; j--; temp++; } else return 0; } if(temp==lenth[k]){ for(i=0;i<lenth[k];i++) visit[starti+i][startj-i]=1; check_verbal[k]++; return 1; } else return 0; } int search_5(int i,int j,int k) { int temp=0; int starti=i,startj=j; while(i>0&&j>0&&i<=n&&j<=n&&temp<lenth[k]){ if(s[i][j]==verbal[k][temp]){ j--; temp++; } else return 0; } if(temp==lenth[k]){ for(i=0;i<lenth[k];i++) visit[starti][startj-i]=1; check_verbal[k]++; return 1; } else return 0; } int search_6(int i,int j,int k) { int temp=0; int starti=i,startj=j; while(i>0&&j>0&&i<=n&&j<=n&&temp<lenth[k]){ if(s[i][j]==verbal[k][temp]){ i--; j--; temp++; } else return 0; } if(temp==lenth[k]){ for(i=0;i<lenth[k];i++) visit[starti-i][startj-i]=1; check_verbal[k]++; return 1; } else return 0; } int search_7(int i,int j,int k) { int temp=0; int starti=i,startj=j; while(i>0&&j>0&&i<=n&&j<=n&&temp<lenth[k]){ if(s[i][j]==verbal[k][temp]){ i--; temp++; } else return 0; } if(temp==lenth[k]){ for(i=0;i<lenth[k];i++) visit[starti-i][startj]=1; check_verbal[k]++; return 1; } else return 0; } int search_8(int i,int j,int k) { int temp=0; int starti=i,startj=j; while(i>0&&j>0&&i<=n&&j<=n&&temp<lenth[k]){ if(s[i][j]==verbal[k][temp]){ i--; j++; temp++; } else return 0; } if(temp==lenth[k]){ for(i=0;i<lenth[k];i++) visit[starti-i][startj+i]=1; check_verbal[k]++; return 1; } else return 0; } void check() { int i,j; int temp; for(i=0;i<number;i++){ if(check_verbal[i]!=1){ printf("no"); return; } } if(d1<=2&&d2<=2&&d3<=2&&d4<=2&&d5<=2&&d6<=2&&d7<=2&&d8<=2){ printf("no"); return; } for(i=1;i<=n;i++){ temp=0; for(j=1;j<=n;j++) temp+=visit[i][j]; if(temp==0){ printf("no"); return; } } for(j=1;j<=n;j++){ temp=0; for(i=1;i<=n;i++) temp+=visit[i][j]; if(temp==0){ printf("no"); return; } } temp=visit[1][1]+visit[1][n]+visit[n][1]+visit[n][n]; if(temp==0){ printf("no"); return; } printf("yes"); } int main() { int i,j,k; freopen("read.in","r",stdin); scanf("%d",&n); for(i=1;i<=n;i++) scanf("%s",&s[i]); for(i=1;i<=n;i++){ for(j=1;i<=n;j++){ for(k=0;k<number;k++){ if(s[i][j]=verbal[k][0]){ if(search_1(i,j,k)==1){ d1++; if(search_2(i,j,k)==1) d2++; if(search_3(i,j,k)==1) d3++; if(search_4(i,j,k)==1) d4++; if(search_5(i,j,k)==1) d5++; if(search_6(i,j,k)==1) d6++; if(search_7(i,j,k)==1) d7++; if(search_8(i,j,k)==1) d8++; } } } } check(); fclose(stdin); return 0; }
看起来是不是很长,但是实际上功能很简单,只不过是需要进行8个方向上的搜索,子函数较多一些。当然这个代码是最初版本,当我从梦中惊醒的时候发现数组开小了,每个方向至少有两个phrase,那么至少就要有16个phrase,而我预先假定的是不超过10个phrase。所以代码还需要进行下一步的修改。
未完待续中。。。
4.测试结果




5、时间统计
|
Personal Software Process Stages |
时间百分比(%) |
实际花费的时间 (分钟) |
原来估计的时间 (分钟) |
|
计划 |
10% | 18 | 12 |
|
· 估计这个任务需要多少时间,把工作细化并大致排序 |
10% | 18 | 12 |
|
开发 |
85% | 153 | 102 |
|
· 需求分析 (包括学习新技术) |
15% | 27 | 18 |
|
· 设计复审 (和同事审核设计文档) |
10% | 18 | 12 |
|
· 代码规范 (制定合适的规范) |
5% | 9 | 6 |
|
· 具体设计 |
10% | 18 | 12 |
|
· 具体编码 |
35% | 63 | 42 |
|
· 代码复审 |
5% | 9 | 6 |
|
· 测试(自我测试,修改代码,提交修改)
|
5% | 9 | 6 |
|
总结报告 |
5% | 9 | 6 |
|
总计
|
100% | 总用时 180 |
浙公网安备 33010602011771号