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[][]; //传进来的字母矩阵
int visit[][];
int number; //需要放进去的单词数目
char verbal[][]; //需要放进去的单词
int lenth[]; //需要放进去的单词的长度
int d1,d2,d3,d4,d5,d6,d7,d8,d9;
int n; //矩阵大小
int check_verbal[]; //单词在s中出现的次数 int search_1(int i,int j,int k)
{
int temp=;
int starti=i,startj=j;
while(i>&&j>&&i<=n&&j<=n&&temp<lenth[k]){
if(s[i][j]==verbal[k][temp]){
j++;
temp++;
}
else
return ;
}
if(temp==lenth[k]){
for(i=;i<lenth[k];i++)
visit[starti][startj+i]=;
check_verbal[k]++;
return ;
}
else
return ;
} int search_2(int i,int j,int k)
{
int temp=;
int starti=i,startj=j;
while(i>&&j>&&i<=n&&j<=n&&temp<lenth[k]){
if(s[i][j]==verbal[k][temp]){
i++;
j++;
temp++;
}
else
return ;
}
if(temp==lenth[k]){
for(i=;i<lenth[k];i++)
visit[starti+i][startj+i]=;
check_verbal[k]++;
return ;
}
else
return ;
} int search_3(int i,int j,int k)
{
int temp=;
int starti=i,startj=j;
while(i>&&j>&&i<=n&&j<=n&&temp<lenth[k]){
if(s[i][j]==verbal[k][temp]){
i++;
temp++;
}
else
return ;
}
if(temp==lenth[k]){
for(i=;i<lenth[k];i++)
visit[starti+i][startj]=;
check_verbal[k]++;
return ;
}
else
return ;
} int search_4(int i,int j,int k)
{
int temp=;
int starti=i,startj=j;
while(i>&&j>&&i<=n&&j<=n&&temp<lenth[k]){
if(s[i][j]==verbal[k][temp]){
i++;
j--;
temp++;
}
else
return ;
}
if(temp==lenth[k]){
for(i=;i<lenth[k];i++)
visit[starti+i][startj-i]=;
check_verbal[k]++;
return ;
}
else
return ;
} int search_5(int i,int j,int k)
{
int temp=;
int starti=i,startj=j;
while(i>&&j>&&i<=n&&j<=n&&temp<lenth[k]){
if(s[i][j]==verbal[k][temp]){
j--;
temp++;
}
else
return ;
}
if(temp==lenth[k]){
for(i=;i<lenth[k];i++)
visit[starti][startj-i]=;
check_verbal[k]++;
return ;
}
else
return ;
} int search_6(int i,int j,int k)
{
int temp=;
int starti=i,startj=j;
while(i>&&j>&&i<=n&&j<=n&&temp<lenth[k]){
if(s[i][j]==verbal[k][temp]){
i--;
j--;
temp++;
}
else
return ;
}
if(temp==lenth[k]){
for(i=;i<lenth[k];i++)
visit[starti-i][startj-i]=;
check_verbal[k]++;
return ;
}
else
return ;
} int search_7(int i,int j,int k)
{
int temp=;
int starti=i,startj=j;
while(i>&&j>&&i<=n&&j<=n&&temp<lenth[k]){
if(s[i][j]==verbal[k][temp]){
i--;
temp++;
}
else
return ;
}
if(temp==lenth[k]){
for(i=;i<lenth[k];i++)
visit[starti-i][startj]=;
check_verbal[k]++;
return ;
}
else
return ;
} int search_8(int i,int j,int k)
{
int temp=;
int starti=i,startj=j;
while(i>&&j>&&i<=n&&j<=n&&temp<lenth[k]){
if(s[i][j]==verbal[k][temp]){
i--;
j++;
temp++;
}
else
return ;
}
if(temp==lenth[k]){
for(i=;i<lenth[k];i++)
visit[starti-i][startj+i]=;
check_verbal[k]++;
return ;
}
else
return ;
} void check()
{
int i,j;
int temp;
for(i=;i<number;i++){
if(check_verbal[i]!=){
printf("no");
return;
}
}
if(d1<=&&d2<=&&d3<=&&d4<=&&d5<=&&d6<=&&d7<=&&d8<=){
printf("no");
return;
}
for(i=;i<=n;i++){
temp=;
for(j=;j<=n;j++)
temp+=visit[i][j];
if(temp==){
printf("no");
return;
}
}
for(j=;j<=n;j++){
temp=;
for(i=;i<=n;i++)
temp+=visit[i][j];
if(temp==){
printf("no");
return;
}
}
temp=visit[][]+visit[][n]+visit[n][]+visit[n][n];
if(temp==){
printf("no");
return;
}
printf("yes");
} int main()
{ int i,j,k;
freopen("read.in","r",stdin);
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%s",&s[i]);
for(i=;i<=n;i++){
for(j=;i<=n;j++){
for(k=;k<number;k++){
if(s[i][j]=verbal[k][]){
if(search_1(i,j,k)==){
d1++;
if(search_2(i,j,k)==)
d2++;
if(search_3(i,j,k)==)
d3++;
if(search_4(i,j,k)==)
d4++;
if(search_5(i,j,k)==)
d5++;
if(search_6(i,j,k)==)
d6++;
if(search_7(i,j,k)==)
d7++;
if(search_8(i,j,k)==)
d8++;
}
}
}
}
check();
fclose(stdin);
return ;
}
看起来是不是很长,但是实际上功能很简单,只不过是需要进行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 |