WordCount C语言实现求文本的字符数,单词数,行数

1.码云地址:

https://gitee.com/miaomiaobobo/WordCount

2.psp表格

PSP2.1表格

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 25

20

· Estimate

· 估计这个任务需要多少时间

 10

5

Development

开发

 200 

350

· Analysis

· 需求分析 (包括学习新技术)

 25

 20

· Design Spec

· 生成设计文档

 30  

 20

· Design Review

· 设计复审 (和同事审核设计文档)

20

 40

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 20

 15

· Design

· 具体设计

 20

 40

· Coding

· 具体编码

200    

 300

· Code Review

· 代码复审

 20

 40

· Test

· 测试(自我测试,修改代码,提交修改)

 30

 60

Reporting

报告

 20

 20

· Test Report

· 测试报告

 15  

 10

· Size Measurement

· 计算工作量

10

 10

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 20

 30

 

合计

 665

950

 3.需求功能分析

WordCount的需求:能够通过cmd执行.exe程序,并且传入文本文件,然后对其中的字符数,单词书,行数进行计算,并将结果保存在.exe程序的同级别目录下。

解题思路:1.通过cmd的命令行向main函数中传入操作方式与相应的文件。

                   2.通过设置flag记录上一个字符是否为空格结合当前是否为空格来判断单词数;

                   3.判断是否有“\n”来 确定行数。

其中,C语言实现WordCount参考链接:https://www.cnblogs.com/xiaobao123/articles/9649687.html

           字符指针数组的空间分配参考链接:https://www.cnblogs.com/mensanu/p/7979462.html

4.程序设计

程序总共包含4个函数(main,countw,countc,countl)其中,main为主函数,通过传入的操作数不同分别调用不同的函数,

 主要的模块有:

1.单词数的统计,当遇到空格时,如果上一个字符不是空格,则可判断这是一个新单词

复制代码

while(fgets(buffer, 1003, fp) != NULL){
bufferLen = strlen(buffer);

for(i=0; i<bufferLen; i++){
c = buffer[i];
if( c==' ' || c=='\t'){
!isLastBlank && wordNum++; //当上一个不是空格,而这一个是空格时单词数+1
isLastBlank = 1;//表明一个空格
}else if(c!='\n'&&c!='\r'){

isLastBlank = 0;
}
}
!isLastBlank && wordNum++;

复制代码

2.主函数,将cmd的操作指令传入,并通过函数strcmp进行比较,调用不同的函数:

复制代码

int main(int argc,char* argv[]){
char filname[30];
char operation;
int totalline;//总行数
int toalchar;//总字符数
int totalword;//总单词数
if(!strcmp(argv[1],"-w"))
countw(argv[2]);
else if(!strcmp(argv[1],"-c"))

countc(argv[2]);
else if(!strcmp(argv[1],"-l"))
countl(argv[2]);
return 0;
}

复制代码

5.具体代码(具体代码也可参考码云链接中的.cpp文件)

复制代码

#include <stdio.h>
#include <string.h>
#include<malloc.h>
int countw(char *filename);
int countc(char *filename);
int countl(char *filename);

int main(int argc,char* argv[]){
char filname[30];
char operation;
int totalline;//总行数
int toalchar;//总字符数
int totalword;//总单词数
if(!strcmp(argv[1],"-w"))
countw(argv[2]);
else if(!strcmp(argv[1],"-c"))

countc(argv[2]);
else if(!strcmp(argv[1],"-l"))
countl(argv[2]);
return 0;
}

int countw(char *filename){
FILE *fp=NULL;
FILE *fp2=NULL;
char buffer[1003];
int bufferLen;
int i;
char c;
int isLastBlank = 0;
int totalword=0;
int wordNum = 0;
if( (fp=fopen(filename, "rb")) == NULL ){
perror(filename);
return NULL;
}

while(fgets(buffer, 1003, fp) != NULL){
bufferLen = strlen(buffer);

for(i=0; i<bufferLen; i++){
c = buffer[i];
if( c==' ' || c=='\t'){
!isLastBlank && wordNum++; //当上一个不是空格,而这一个是空格时单词数+1
isLastBlank = 1;//表明一个空格
}else if(c!='\n'&&c!='\r'){

isLastBlank = 0;
}
}
!isLastBlank && wordNum++;
isLastBlank = 1;
totalword += wordNum;
wordNum = 0;
}
printf("totalword:%d ",totalword);
fp2=fopen("result.txt","a");
if(fp2){
fprintf(fp2,"单词总数:%d\n",totalword);
fclose(fp2);
}
return 0;
}

int countc(char *filename){

FILE *fp=NULL;
FILE *fp2=NULL;
char buffer[1003];
int bufferLen;
int i;
char c;
int isLastBlank = 0;
int totalchar=0;
int charNum = 0;
if( (fp=fopen(filename, "rb")) == NULL ){
perror(filename);
return NULL;
}
while(fgets(buffer, 1003, fp) != NULL){
bufferLen = strlen(buffer);
for(i=0; i<bufferLen; i++){
c = buffer[i];
if( c==' ' || c=='\t'){
isLastBlank = 1;//字符不统计空格
}else if(c!='\n'&&c!='\r'){
charNum++;
isLastBlank = 0;
}
}

isLastBlank = 1;

totalchar += charNum;

charNum = 0;

}
printf("totalchar:%d",totalchar);
fp2=fopen("result.txt","a");
if(fp2){
fprintf(fp2,"字符总数:%d\n",totalchar);
fclose(fp2);
}
return 0;
}

int countl(char *filename){
FILE *fp=NULL;
FILE *fp2=NULL;
char buffer[1003];
int bufferLen;
int i;
char c;
int totalline=-1;
if( (fp=fopen(filename, "rb")) == NULL ){
perror(filename);
return NULL;
}

while(fgets(buffer, 1003, fp) != NULL){
bufferLen = strlen(buffer);
for(i=0; i<bufferLen; i++){
c=buffer[i];
if(c=='\n'||c=='\r'){
totalline++;

}
}
}
printf("totalline:%d",totalline);
fp2=fopen("result.txt","a");
if(fp2){
fprintf(fp2,"总行数:%d\n",totalline);
fclose(fp2);
}
return 0;

}

6.本程序的测试

cmd测试命令行:

待测试的文件:

处理的结果:

7.个人项目总结

本次项目,学到的很多新知识。但是在项目开始的时候,我本来对文本的处理不是很熟悉,所以在这方面花费了很多时间,之后通过cmd运行可执行文件也耗费了我大量的时间。所以,这个项目虽然实现的功能比较简单,但总的花费我差不多一天的时间,但收获也是巨大的,也明白了自己的不足。项目本身存在着一些缺陷,包括对错误操作的提示不足,只能进行单一文件处理等,都需要改进。

8.参考资料

1.C语言实现WordCount参考链接:https://www.cnblogs.com/xiaobao123/articles/9649687.html

2. 字符指针数组的空间分配参考链接:https://www.cnblogs.com/mensanu/p/7979462.html

3.%s在c语言中对于空格的处理 : http://bbs.bccn.net/thread-352772-1-1.html

4.git的使用:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/

猜你喜欢

转载自www.cnblogs.com/miaobo/p/9696026.html
今日推荐