软件工程:Wordcount程序作业

时间:2023-03-09 15:56:06
软件工程:Wordcount程序作业

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

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

那么直接看成果吧:

这是text.txt测试文件。

 int
num_word=,
num_line=,num_char=,flag;
fp=fopen("text.txt","a");
if(fp==NULL){
printf("the file open bit");
}
open

接下来是运行结果图:

软件工程:Wordcount程序作业

接下来是源代码,如下:

 // vv.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
void getWord(FILE *fp,int *num_char,int *num_word){
char ch;
do{
*num_char=*num_char+;
ch=fgetc(fp);
}while((ch>='a'&&ch<'z')||(ch>='A'&&ch<='Z')||ch=='_');
*num_word=*num_word+;
fseek(fp,-,SEEK_CUR);
} int main(int argc, char* argv[])
{
FILE *fp;
char ch;
int num_word=,num_line=,num_char=,flag;
fp=fopen("text.txt","a+");
if(fp==NULL){
printf("the file open bit");
}
while(!feof(fp)){
ch=fgetc(fp);
if((ch>='a'&&ch<'z')||(ch>='A'&&ch<='Z')){
getWord(fp,&num_char,&num_word);
}
else if(ch=='\n'){
num_line++;
}
else if(ch=='\0'||ch=='\t'){
flag=;
}
else
num_char+=;
}
fclose(fp);
printf("字符数为:%d",num_char);
printf("\n");
printf("单词数为:%d",num_word);
printf("\n");
printf("行数为:%d",num_line);
printf("\n");
return ; }