第三次作业(WC扩展)

(1)码云地址:
(2)参与者:201631062316,201631062216
(3)本次作业地址:https://edu.cnblogs.com/campus/xnsy/2018softwaretest2398/homework/2187

WordCount作业思路

使用c语言编写wc扩展功能,并进行相关测试

程序设计实现过程

代码说明

1.统计空行

int count_blankL(char*filename_counted)
{
	FILE*fp=fopen(filename_counted,"r");
	char buffer[100];
	int i=0;
	char c=0;
	char cl=0;
	int bufferLen;
	int charNum=0;
	int blankL=0;
	while(fgets(buffer,100,fp)!=NULL)
	{
		bufferLen=strlen(buffer);
		for(i=0;i<bufferLen;i++)
		{
			c=buffer[i];
			if(c!=' '&&c!='\t'&&c!='\n'&&c!='\r')
			{
				charNum++;
				cl=c;
			}
		}
		if(charNum==0)
        {
            blankL++;
        }
        if(charNum==1&&(cl=='{'||cl=='}'))
        {
            blankL++;
        }
	}
	fclose(fp);
	printf("%s, 空行数: %d\n",filename_counted,blankL);
	return blankL;
}

2.统计注释行

int count_noteL(char*filename_counted)
{
	FILE*fp=fopen(filename_counted,"r");
	char buffer[100];
	int i=0;
	char c=0;
	char cl=0;
	int bufferLen;
	int noteL=0;
	int isLastNote=0;
	while(fgets(buffer,100,fp)!=NULL)
	{
		bufferLen=strlen(buffer);
		for(i=0;i<bufferLen;i++)
		{
			c=buffer[i];
			if(c=='/')
			{
                if(isLastNote==1)
                {
                    noteL++;
                }
				isLastNote=1;
			}
		}
		isLastNote=0;
	}
	fclose(fp);
	printf("%s, 注释行: %d\n",filename_counted,noteL);
	return noteL;
}

3.递归遍历文件

int FileSearch(const char *dir)
{
    long handle;
    struct _finddata_t fileinfo;
    char dirNew[500];
    strcpy(dirNew, dir);
    strcat(dirNew,"\\*.*");
if ((handle = _findfirst(dirNew, &fileinfo)) == -1L)
{
	printf("Failed to findfrist file");
	return -1;
}
    while (_findnext(handle,&fileinfo)==0)
    {
        if (fileinfo.attrib & _A_SUBDIR)
        {
            if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)
                continue;
            strcpy(dirNew, dir);
            strcat(dirNew, "\\");
            strcat(dirNew, fileinfo.name);
            FileSearch(dirNew);
        }
        else
        {

            if (++i < Max&&strstr(fileinfo.name,".c"))//将路径入栈
            {
                strcpy(data[i].path , dir);
                strcat(data[i].path, "\\");
                strcat(data[i].path, fileinfo.name);
                strcpy(data[i].filename_matched,fileinfo.name);
                data[i].size = fileinfo.size;
            }
        }
    }
    _findclose(handle);
}

4.对遍历符合条件的文件进行相关操作

void op_s(Data data[],char*filename_result)
{
    int totalC=0;
    int totalW=0;
    int totalL=0;
    printf("ok");
    while (i > -1)
    {
            printf("okin");
        totalC=countC(data[i].path);
        output(filename_result,data[i].filename_matched,"字符数",totalC);
        totalW=countW(data[i].path);
        output(filename_result,data[i].filename_matched,"单词数",totalC);
        totalL=countL(data[i].path);
        output(filename_result,data[i].filename_matched,"总行数",totalC);
    i--;
    }
}

测试用例

1.

2.

3.

测试效果

总结

通过本次结对编程,暴露出自己很多问题,比如代码格式、命名不规范给结对编程的过程带来很多麻烦,同时此次也是自己首次体验结对编程,对这种模式有了更多的了解。

参考文献

https://blog.csdn.net/gang9881/article/details/78504407
https://www.cnblogs.com/collectionne/p/6815924.html

posted @ 2018-10-21 22:54  落枫过后  阅读(69)  评论(0编辑  收藏  举报