码迷,mamicode.com
首页 > 其他好文 > 详细

第三周作业(三)---WordCounter

时间:2016-03-22 21:57:20      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:

需求是这样的。写出一个程序,模仿wc.exe,可以统计出文件的一些信息(比如字符数、单词数目等等)

对于这个程序,我仍然用我从大一学来的C语言写的。

 第一步:打开文件

1 printf("请输入文件名称");
2     scanf("%s", filename);
3     transName(filename, outputname);
4     if( ((fp = fopen(filename, "r")) == NULL) || ((output = fopen(outputname, "w")) == NULL) ){
5         printf("ERROR: 无法打开统计文件");
6         return 0;
7     }

 

第二步:读取文件进行处理,这里我采用的是fgets函数来进行按行的读取。

fgets(content, 256, temp); //content: char[] 用来存放读取内容

第三步:对获取到的内容进行处理。

  a)获取内容字符数。这里将出空格换行以及tab以外的其他字符都计入统计。代码如下

1 int charCounter(char *content) {
2     int lengh = strlen(content);
3     for (int i = 0; i < strlen(content); i++)
4     {
5         if (32 == content[i] || 10 == content[i] || content[i] == \t)
6             lengh--;
7     }
8     return lengh;
9 }

   b)获取单词数。这里明确,单词应以字母开头,以非字母、数字、点和下划线结尾。所以判断代码如下:

 1 bool IsChar(char c) { //判断首字母是否为英文字符
 2     if ((c >= a&&c <= z) || (c >= A&&c <= Z))
 3         return true;
 4     return false;
 5 }
 6 
 7 bool IsRight(char c) { //用以判断单词结束
 8     if (IsChar(c) || c == . || c == _ || (c <= 9&&c >= 0))
 9         return true;
10     return false;
11 }
12 
13 int wordCounter(char *content) {
14     int num = 0;
15     int len = strlen(content);
16     for (int i = 0; i < len; i++) {
17         if (IsChar(content[i])) {
18             while (i<len) {
19                 if (!IsRight(content[i++]))
20                 {
21                     num++;
22                     break;
23                 }
24             }
25         }
26     }
27     return num;
28 }

  c)行数的判断运用到了字符数的判断,判断当这行内容字符数不为0时,为非空行。代码如下:

if (charCounter(content))
    line++; //非空行计数+1
else
    spaceline++; //空行计数+1

  d)注释行的判断,针对注释的两种情况,可以在传入内容时传入一个数字flag以确定状态(0:上文中无未结束的"/*" 1:上文中有未结束的"/*")。这样在判断注释行时,首先检察状态。当flag为0时,查找内容,如有"//"则注释行返回true,如有"/*"则将flag置1并返回true。否则,返回false;当flag为1时,只需查找内容中是否有"*/",如有,则置flag为0。这里返回值一定为true。代码如下:

 1 bool IsNoteline(char* content,int* flag) {  //flag 0:当前无多行注释 1:当前有多行注释
 2     if (*flag) {
 3         for (int i = 0; i < strlen(content); i++) {
 4             if (content[i] == * && content[i + 1] == \/)
 5             {
 6                 *flag = 0;
 7             }
 8         }
 9         return true;
10     }else {
11         for (int i = 0; i < strlen(content); i++)
12         {
13             if (content[i] == \/) {
14                 if (content[i + 1] == \/)
15                     return true;
16                 else if (content[i + 1] == *)
17                 {
18                     *flag = 1;
19                     return true;
20                 }
21             }
22         }
23         return false;
24     }
25 }

第四步,格式化输出统计信息。这里采用文件及控制窗口两种方式输出。代码如下:

  a)输出文件名称的转换。(默认读取的文件为非txt格式)

  

 1 void transName(char *filename, char *outputname) {//转换文件名,存储统计文件
 2     int i;
 3     for (i = 0; i < strlen(filename); i++) {
 4         if (filename[i] == .)
 5             break;
 6         outputname[i] = filename[i];
 7     }
 8     outputname[i] = .;
 9     outputname[++i] = t;
10     outputname[++i] = x;
11     outputname[++i] = t;
12     outputname[++i] = \0;
13 
14 }

  b)输出统计信息,以及将详细信息输出到文件里。

 1 while (!feof(temp)) {
 2         fgets(content, 256, temp);
 3         //printf("%d  %s\n", charcounter(content), content);
 4         fprintf(output, "\n\ncode:%schar:%d  word: %d", content, charCounter(content), wordCounter(content));//统计信息输出到文件
 5         if (charCounter(content))
 6             line++;
 7         else
 8             spaceline++;
 9         charnum += charCounter(content);
10         wordnum += wordCounter(content);
11         noteline += (IsNoteline(content, &flag) ? 1 : 0);
12     }
13     fprintf(output, "\n\n----------统计数据汇总---------\n\n有效行:%d \n无效行: %d \n注释行: %d \n字符数: %d \n单词数: %d \n", line, spaceline, noteline, charnum, wordnum);
14     printf("\n有效行:%d \n无效行: %d \n注释行: %d \n字符数: %d \n单词数: %d \n", line, spaceline, noteline, charnum, wordnum);

 

注:效果截图如下,末尾处为源代码和测试文件 test.c:

 技术分享

技术分享

技术分享

技术分享
  1 /*
  2     @version 1.0
  3     @author Coder Li
  4     @description    
  5         This is a tool for count your code.
  6 */
  7 #include<stdio.h>
  8 #include<string.h>
  9 #include<stdlib.h>
 10 
 11 void transName(char *filename, char *outputname) {//转换文件名,存储统计文件
 12     int i;
 13     for (i = 0; i < strlen(filename); i++) {
 14         if (filename[i] == .)
 15             break;
 16         outputname[i] = filename[i];
 17     }
 18     outputname[i] = .;
 19     outputname[++i] = t;
 20     outputname[++i] = x;
 21     outputname[++i] = t;
 22     outputname[++i] = \0;
 23 
 24 }
 25 
 26 int charCounter(char *content) {
 27     int lengh = strlen(content);
 28     for (int i = 0; i < strlen(content); i++)
 29     {
 30         if (32 == content[i] || 10 == content[i] || content[i] == \t)
 31             lengh--;
 32     }
 33     return lengh;
 34 }
 35 
 36 bool IsChar(char c) { //判断首字母是否为英文字符
 37     if ((c >= a&&c <= z) || (c >= A&&c <= Z))
 38         return true;
 39     return false;
 40 }
 41 
 42 bool IsRight(char c) { //判断单词结束
 43     if (IsChar(c) || c == . || c == _ || (c <= 9&&c >= 0))
 44         return true;
 45     return false;
 46 }
 47 
 48 int wordCounter(char *content) {
 49     int num = 0;
 50     int len = strlen(content);
 51     for (int i = 0; i < len; i++) {
 52         if (IsChar(content[i])) {
 53             while (i<len) {
 54                 if (!IsRight(content[i++]))
 55                 {
 56                     num++;
 57                     break;
 58                 }
 59             }
 60         }
 61     }
 62     return num;
 63 }
 64 
 65 
 66 bool IsNoteline(char* content,int* flag) {  //flag 0:当前无多行注释 1:当前有多行注释
 67     if (*flag) {
 68         for (int i = 0; i < strlen(content); i++) {
 69             if (content[i] == * && content[i + 1] == \/)
 70             {
 71                 *flag = 0;
 72             }
 73         }
 74         return true;
 75     }else {
 76         for (int i = 0; i < strlen(content); i++)
 77         {
 78             if (content[i] == \/) {
 79                 if (content[i + 1] == \/)
 80                     return true;
 81                 else if (content[i + 1] == *)
 82                 {
 83                     *flag = 1;
 84                     return true;
 85                 }
 86             }
 87         }
 88         return false;
 89     }
 90 }
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 int main() {
 99     FILE *fp, *temp, *output;
100     char filename[80],outputname[80];
101     char content[256];    
102     int line = 0, spaceline = 0, noteline = 0, flag = 0;
103         //行数统计 line:有效行 spaceline:无效行 noteline:含注释的行 flag:注释行标记
104     int charnum = 0;//字符数统计
105     int wordnum = 0; //单词数统计
106     
107     printf("请输入文件名称");
108     scanf("%s", filename);
109     transName(filename, outputname);
110     if( ((fp = fopen(filename, "r")) == NULL) || ((output = fopen(outputname, "w")) == NULL) ){
111         printf("ERROR: 无法打开统计文件");
112         return 0;
113     }
114 
115     temp = fp;
116     fprintf(output, "文件名:%s\n", filename);
117     while (!feof(temp)) {
118         fgets(content, 256, temp);
119         //printf("%d  %s\n", charcounter(content), content);
120         fprintf(output, "\n\ncode:%schar:%d  word: %d", content, charCounter(content), wordCounter(content));//统计信息输出到文件
121         if (charCounter(content))
122             line++;
123         else
124             spaceline++;
125         charnum += charCounter(content);
126         wordnum += wordCounter(content);
127         noteline += (IsNoteline(content, &flag) ? 1 : 0);
128     }
129     fprintf(output, "\n\n----------统计数据汇总---------\n\n有效行:%d \n无效行: %d \n注释行: %d \n字符数: %d \n单词数: %d \n", line, spaceline, noteline, charnum, wordnum);
130     printf("\n有效行:%d \n无效行: %d \n注释行: %d \n字符数: %d \n单词数: %d \n", line, spaceline, noteline, charnum, wordnum);
131     printf("\n\n详细信息请看当前目录下同名txt文件\n输入任意数字结束程序\n");
132     scanf("%d", &charnum);
133     fclose(fp);
134     fclose(output);
135     return 0;
136 }
源码
技术分享
  1 /*
  2     @version 1.1
  3     @author Coder Li
  4     @description    This is a tool for  your code.*/
  5 
  6 #include<stdio.h>
  7 #include<string.h>
  8 #include<stdlib.h>
  9 
 10 bool IsNoteline(char* content,int* flag) {  //flag 0:当前无多行注释 1:当前有多行注释,找不到多行注释
 11     for (int i = 0; i < strlen(content); i++)
 12     {
 13         if (!(*flag)) {
 14             if (content[i] == \/) {
 15                 if (content[i + 1] == \/)
 16                     return true;
 17                 else if (content[i + 1] == *)
 18                 {
 19                     *flag = 1;
 20                     return true;
 21                 }
 22             }
 23         }
 24         else {
 25             if (content[i] == * && content[i + 1] == \/)
 26             {
 27                 *flag = 0;
 28                 return true;
 29             }    
 30         }
 31     }
 32     return false;
 33 }
 34 
 35 bool IsChar(char c) {
 36     if ((c >= a&&c <= z)||(c>=A&&c<=Z))
 37         return true;
 38     return false;
 39 }
 40 
 41 bool IsRight(char c) {
 42     if (IsChar(c) || c == . || c == _ || (c <= 9&&c >= 0))
 43         return true;
 44     return false;
 45 }
 46 
 47 int charcounter(char *content) {
 48     int lengh = strlen(content);
 49     for (int i = 0; i < strlen(content); i++)
 50     {
 51         if (32 == content[i] || 10 == content[i] || content[i] == \t)
 52             lengh--;
 53     }
 54     return lengh;
 55 }
 56 
 57 int wordcounter(char *content) {
 58     int num = 0;
 59     int len = strlen(content);
 60     for (int i = 0; i < len; i++) {
 61         if (IsChar(content[i])) {
 62             while(i<len){
 63                 if (!IsRight(content[i++]))
 64                 {
 65                     num++;
 66                     break;
 67                 }    
 68             }
 69         }
 70     }
 71     return num;
 72 }
 73 
 74 int main() {
 75     FILE *fp, *temp, *output;
 76     char filename[256];
 77     char content[1024];
 78     int line = 0, spaceline = 0 , noteline = 0 , flag = 0; //行数统计 line:有效行 spaceline:无效行 noteline:含注释的行 flag:注释行标记
 79     int charnum = 0;//字符数统计
 80     int wordnum = 0; //单词数统计
 81     output = fopen("output.txt", "w");
 82     printf("请输入文件名称");
 83     scanf("%s", filename);
 84     if ((fp = fopen(filename, "r")) == NULL) {
 85         printf("ERROR: 无法打开文件");
 86         return 0;
 87     }
 88     while (!feof(fp)) {
 89         fgets(content, 100, fp);
 90         fprintf(fp,"%d%s", charcounter(content), content);
 91         printf("%d\t%s\n", charcounter(content), content);
 92         if (charcounter(content))
 93             line++;
 94         else
 95             spaceline++;
 96         charnum += charcounter(content);
 97         wordnum += wordcounter(content);
 98 
 99         noteline += IsNoteline(content, &flag) ? 1 : 0;
100     }
101     printf("\n有效行:%d \n无效行:%d \n注释行:%d \n字符数: %d \n单词数: %d \n", line, spaceline, noteline, charnum, wordnum);
102     scanf("%d", &charnum);
103     return 0;
104 }
test.c

 

第三周作业(三)---WordCounter

标签:

原文地址:http://www.cnblogs.com/lishida/p/5308378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!