WordCount扩展与优化

合作者:201631062327,201631062128
码云地址:https://gitee.com/LIUJIA6/WordCount3

一:项目说明

本次项目是在上次作业WorldCount的基础上,利用结对编程的思想,完成对WorldCount项目的功能扩展

-s 递归处理目录下符合条件的文件。(实现)
-a 返回更复杂的数据(代码行 / 空行 / 注释行)。(实现)
空行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”。
其中,代码行:本行包括多于一个字符的代码。
空 行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”。
注释行:本行不是代码行,并且本行包括注释。一个有趣的例子是有些程序员会在单字符后面加注释。[file_name]: 文件或目录名,可以处理一般通配符。

二:部分代码

基础功能:测试返回单词数,字符数,行数

int countw(char *file)//返回文件词的数目 
{ FILE *f;
f=fopen(file,"r");
char ch;
if(NULL==(f=fopen(file,"r")))
{
printf("文件");
}
else
while(!feof(f))
{
ch=fgetc(f);
if((ch >= 'a'&&ch <= 'z')||(ch>='A'&&ch<='Z')||ch=='_')
aword=1;
else if (aword)
{
cword++;
aword=0;
}
}
fclose(f); printf("单词数:%d ",cword);
}
int countc(char *file) //返回文件的字符数 
{ FILE *f;
f = fopen(file, "r");
char a;
if(NULL==(f=fopen(file,"r")))
{
printf("file is NULL");
}
else
while (!feof(f))
{
a=fgetc(f);
if (a != ' '&&a != '\n'&&a != '\t')
cchar++;
}
fclose(f);printf("字符数:%d ",cchar);
}
int countl(char *file) //返回文件的行数
{ FILE *f;
f = fopen(file, "r");
int cline = 0;
char a;
if(NULL==(f=fopen(file,"r")))
{printf("file is NULL");}
else while(!feof(f))
{
a=fgetc(f);
if(a=='\n'||a=='\t')
cline++;
}

fclose(f);printf("行数:%d ",cline);
return 1;
}

 

拓展功能:测试返回代码行,注释行,空行和txt文件数

int count_blankline(char *file) //返回文件的空行数 
{ FILE *f;
int b_num = 0;
int ch_num = 0;
char ch;
f = fopen(file, "r");
if(NULL==(f=fopen(file,"r")))
{
printf("file is NULL");
}
else
while (!feof(f))
{
ch= fgetc(f);
if (ch=='\n'){
if (ch_num<= 1)
b_num++;
ch_num = 0;
}
else if (ch!=' '&&ch!='\t'&&ch!='}')
ch_num++;
else if(ch=='}')b_num++;
}
fclose(f);printf("空行数:%d ",b_num);
}
int count_noteline(char *file) //返回文件的注释行数 
{ FILE *f;
int ch_num = 0;int note_num=0;
char ch;
f=fopen(file, "r");
if(NULL==(f=fopen(file,"r")))
{
printf("文件不存在");
}
else
while (!feof(f))
{
ch= fgetc(f);
if(ch=='\n'){if(ch_num==2) note_num++; ch_num=0;}
else if(ch=='/') ch_num++;
else if(ch_num==1){if(ch=='/') ch_num++;} 
} 
fclose(f);
printf("注释行:%d ",note_num);
} 
int count_codeline(char *file)//返回文件的代码行数 
{ int ch_num = 0;
int code_num=0;
FILE *f;
int tag=0; int flag=0;
char a; 
f = fopen(file, "r");
if(NULL==(f=fopen(file,"r")))
{printf("文件不存在");}
else
while (!feof(f))
{
a=fgetc(f);

if(flag==2) {
flag=0;tag++;}
else{

if(a=='\n'&&ch_num>1)
{code_num++; 
ch_num=0;    }
else if(a != ' '&&a != '\n'&&a != '\t'&&a!='/') {
ch_num++;}

else if(a=='/'){ flag++;}
}

}

fclose(f); printf("代码行数:%d ",code_num-tag); 
}
int searchfile(void) //寻找文件夹中的txt文件
{
struct _finddata_t filefind;
long handle;
int t=0;

if( (handle=_findfirst( "d:\\wordcount\\*txt", &filefind)) == -1L ) 
{
printf( "没找到txt文件\n");
}
else
do{
t++;
printf("找到文件:%s\n", filefind.name); 
}while (_findnext(handle,&filefind)==0);

_findclose(handle);

printf("txt文件数量:%d\n",t);
return 0;
}

 

三:单元测试

由于本次项目采用传统的C语言,就没有很好的工具可供使用。于是我们就手写了部分功能的单元测试(单元测试大同小异,选了几个单元进行测试)。

首先是字符数统计的测试

int countc(char *file); 
int main ()
{
    FILE *fpt;
    char filename[30];
    printf("输入测试文件\n");
    scanf("%s",&filename) ;
    fpt=fopen(filename, "rb");
    countc(filename);
    return 0;
 } 

输入正确的txt文件名,输出结果如下

然后是注释行统计的测试

int count_noteline(char *file);
int main ()
{
    FILE *fpt;
    char filename[30];
    printf("输入测试文件\n");
    scanf("%s",&filename) ;
    fpt=fopen(filename, "rb");
    count_noteline(filename);
    return 0;
 } 

同样,测试结果如下

 

四:总结

通过这次的结队编程合作,了解到了在团队合作时应该具备的基本素质,需要和队友及时沟通,完成代码的结合配对。同时本次过程中对WordCount的功能有了更加全面的了解与掌握,也对单元测试有了更深的了解。

 

posted on 2018-10-21 19:38  瓦力伊娃  阅读(215)  评论(0编辑  收藏  举报