软件工程:Wordcount程序作业

      由于时间的关系,急着交作业,加上这一次也不是那么很认真的去做,草草写了“Wordcount程序”几个功能,即是 .txt文件的读取,能计算出文件内容的单词数,文件内容的字符数,及行数。

这次选用C来做,调试加写代码做了不到半个点,也就这么回事了吧。

那么直接看成果吧:

这是text.txt测试文件。

1 int 
2 num_word=0,
3 num_line=0,num_char=0,flag;
4 fp=fopen("text.txt","a");
5     if(fp==NULL){
6         printf("the file open bit");
7     }
8  open 

接下来是运行结果图:

接下来是源代码,如下:

 1 // vv.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "stdio.h"
 6 #include "stdlib.h"
 7 void getWord(FILE *fp,int *num_char,int *num_word){
 8     char ch;
 9     do{
10         *num_char=*num_char+1;
11         ch=fgetc(fp);
12     }while((ch>='a'&&ch<'z')||(ch>='A'&&ch<='Z')||ch=='_');
13     *num_word=*num_word+1;
14     fseek(fp,-1,SEEK_CUR);
15 }
16 
17 int main(int argc, char* argv[])
18 {   
19     FILE *fp;
20     char ch;
21     int num_word=0,num_line=0,num_char=0,flag;
22     fp=fopen("text.txt","a+");
23     if(fp==NULL){
24         printf("the file open bit");
25     }
26     while(!feof(fp)){
27         ch=fgetc(fp);
28         if((ch>='a'&&ch<'z')||(ch>='A'&&ch<='Z')){
29             getWord(fp,&num_char,&num_word);
30         }
31         else if(ch=='
'){
32             num_line++;
33         }
34         else if(ch==''||ch=='	'){
35             flag=1;
36         }
37         else
38             num_char+=1;
39     }
40     fclose(fp);
41     printf("字符数为:%d",num_char);
42     printf("
");
43     printf("单词数为:%d",num_word);
44     printf("
");
45     printf("行数为:%d",num_line);
46     printf("
");
47     return 0;
48 
49 }
原文地址:https://www.cnblogs.com/zxr63/p/5304505.html