您的位置:首页 > 其它

用VS测试程序

2016-03-21 19:45 274 查看
怀着一种忐忑的心情,我开始了我的软件测试。

#include "stdio.h"
#include "stdlib.h"

int main(int argc, char* argv[])
{
FILE *fp;
bool flag = false;
char ch;
int count = 0,word = 0,line = 0;
fp=fopen ("wang.txt","r");
if (fp==NULL)
{
printf ("the file:wang.txt not found\n");
exit (-1);
}

if((ch=fgetc(fp))!=EOF)
{
line++;
count++;
}

while ( (ch=fgetc(fp))!=EOF)
{
count++;
if ((ch<'a'||ch>'z')&&(ch<'A'||ch>'Z'))
{
word++;
flag = false;
}
else {
flag = true;
}
if (ch=='\n')
line++;
}

if(flag){
word++;
}
printf("字符为:%d\n",count);
printf("单词为:%d\n",word);
printf("行数为:%d\n",line);
fclose(fp);
return (0);
}


其实只是一个比较简单的程序,当时写完我还感到一丝的不可思议,因为它实在是太短了。竟然完成了我想要的功能,看来开始是我想复杂了,哎,没头脑呀!


这是wang.txt



这是运行结果。

我想把这个程序完善一下,因为当出现连续的非英文字符时(如:,,,, 。。。。)单词的数目会出现错误,我想见一个链表进行数据的存储,然后方便前后的字符进行比较。可我觉得这个方法太麻烦了,哪位大神还有更好的方法,欢迎指教呀!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: