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

误差分析:本次项目的主要误差存在于具体的编码和开发阶段,因为前期自己对文件的使用不熟悉以及上传到码云的初次接触,导致在这些地方耗费了大量的时间(总计多出200分钟左右)

 

 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.单词数的统计,当遇到空格时,如果上一个字符不是空格,则可判断这是一个新单词

 1 while(fgets(buffer, 1003, fp) != NULL){
 2 bufferLen = strlen(buffer);
 3 
 4 for(i=0; i<bufferLen; i++){
 5 c = buffer[i];
 6 if( c==' ' || c=='\t'){ 
 7 !isLastBlank && wordNum++; //当上一个不是空格,而这一个是空格时单词数+1 
 8 isLastBlank = 1;//表明一个空格
 9 }else if(c!='\n'&&c!='\r'){ 
10 
11 isLastBlank = 0;
12 }
13 }
14 !isLastBlank && wordNum++;

 

 

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

 1 int main(int argc,char* argv[]){
 2 char filname[30];
 3 char operation;
 4 int totalline;//总行数
 5 int toalchar;//总字符数
 6 int totalword;//总单词数
 7 if(!strcmp(argv[1],"-w")) 
 8 countw(argv[2]); 
 9 else if(!strcmp(argv[1],"-c")) 
10 
11 countc(argv[2]); 
12 else if(!strcmp(argv[1],"-l")) 
13 countl(argv[2]); 
14 return 0;
15 }

 

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

 

  1 1 #include <stdio.h>
  2   2 #include <string.h>
  3   3 #include<malloc.h>
  4   4 int countw(char *filename);
  5   5 int countc(char *filename);
  6   6 int countl(char *filename);
  7   7 
  8   8 int main(int argc,char* argv[]){
  9   9 char filname[30];
 10  10 char operation;
 11  11 int totalline;//总行数
 12  12 int toalchar;//总字符数
 13  13 int totalword;//总单词数
 14  14 if(!strcmp(argv[1],"-w")) 
 15  15 countw(argv[2]); 
 16  16 else if(!strcmp(argv[1],"-c")) 
 17  17 
 18  18 countc(argv[2]); 
 19  19 else if(!strcmp(argv[1],"-l")) 
 20  20 countl(argv[2]); 
 21  21 return 0;
 22  22 }
 23  23 
 24  24 int countw(char *filename){
 25  25 FILE *fp=NULL; 
 26  26 FILE *fp2=NULL;
 27  27 char buffer[1003]; 
 28  28 int bufferLen; 
 29  29 int i; 
 30  30 char c; 
 31  31 int isLastBlank = 0; 
 32  32 int totalword=0;
 33  33 int wordNum = 0; 
 34  34 if( (fp=fopen(filename, "rb")) == NULL ){
 35  35 perror(filename);
 36  36 return NULL;
 37  37 }
 38  38 
 39  39 while(fgets(buffer, 1003, fp) != NULL){
 40  40 bufferLen = strlen(buffer);
 41  41 
 42  42 for(i=0; i<bufferLen; i++){
 43  43 c = buffer[i];
 44  44 if( c==' ' || c=='\t'){ 
 45  45 !isLastBlank && wordNum++; //当上一个不是空格,而这一个是空格时单词数+1 
 46  46 isLastBlank = 1;//表明一个空格
 47  47 }else if(c!='\n'&&c!='\r'){ 
 48  48 
 49  49 isLastBlank = 0;
 50  50 }
 51  51 }
 52  52 !isLastBlank && wordNum++; 
 53  53 isLastBlank = 1; 
 54  54 totalword += wordNum; 
 55  55 wordNum = 0;
 56  56 }
 57  57 printf("totalword:%d ",totalword);
 58  58 fp2=fopen("result.txt","a");
 59  59 if(fp2){
 60  60 fprintf(fp2,"单词总数:%d\n",totalword);
 61  61 fclose(fp2);
 62  62 }
 63  63 return 0;
 64  64 }
 65  65 int countc(char *filename){
 66  66 
 67  67 FILE *fp=NULL;
 68  68 FILE *fp2=NULL; 
 69  69 char buffer[1003]; 
 70  70 int bufferLen; 
 71  71 int i; 
 72  72 char c; 
 73  73 int isLastBlank = 0; 
 74  74 int totalchar=0;
 75  75 int charNum = 0; 
 76  76 if( (fp=fopen(filename, "rb")) == NULL ){
 77  77 perror(filename);
 78  78 return NULL;
 79  79 }
 80  80 while(fgets(buffer, 1003, fp) != NULL){
 81  81 bufferLen = strlen(buffer);
 82  82 for(i=0; i<bufferLen; i++){
 83  83 c = buffer[i];
 84  84 if( c==' ' || c=='\t'){ 
 85  85 isLastBlank = 1;//字符不统计空格
 86  86 }else if(c!='\n'&&c!='\r'){ 
 87  87 charNum++; 
 88  88 isLastBlank = 0;
 89  89 }
 90  90 }
 91  91 
 92  92 isLastBlank = 1; 
 93  93 
 94  94 totalchar += charNum; 
 95  95 
 96  96 charNum = 0;
 97  97 
 98  98 }
 99  99 printf("totalchar:%d",totalchar);
100 100 fp2=fopen("result.txt","a");
101 101 if(fp2){
102 102 fprintf(fp2,"字符总数:%d\n",totalchar);
103 103 fclose(fp2);
104 104 }
105 105 return 0;
106 106 }
107 107 
108 108 int countl(char *filename){
109 109 FILE *fp=NULL; 
110 110 FILE *fp2=NULL;
111 111 char buffer[1003]; 
112 112 int bufferLen; 
113 113 int i; 
114 114 char c; 
115 115 int totalline=-1;
116 116 if( (fp=fopen(filename, "rb")) == NULL ){
117 117 perror(filename);
118 118 return NULL;
119 119 }
120 120 
121 121 while(fgets(buffer, 1003, fp) != NULL){
122 122 bufferLen = strlen(buffer);
123 123 for(i=0; i<bufferLen; i++){
124 124 c=buffer[i];
125 125 if(c=='\n'||c=='\r'){ 
126 126 totalline++; 
127 127 
128 128 }
129 129 }
130 130 }
131 131 printf("totalline:%d",totalline);
132 132 fp2=fopen("result.txt","a");
133 133 if(fp2){
134 134 fprintf(fp2,"总行数:%d\n",totalline);
135 135 fclose(fp2);
136 136 }
137 137 return 0;
138 138 
139 139 }
All Code

 

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/

posted @ 2018-09-24 18:23  记得喝牛奶  阅读(1005)  评论(0编辑  收藏  举报