| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 11934 人关注过本帖
标题:统计一个文本文件中的单词个数和汉字个数
取消只看楼主 加入收藏
Dream_weaver
Rank: 1
等 级:新手上路
帖 子:37
专家分:2
注 册:2012-6-8
结帖率:100%
收藏
已结贴  问题点数:40 回复次数:9 
统计一个文本文件中的单词个数和汉字个数
统计一个文本文件中的单词个数和汉字个数
我在word里面输入了一段中文,一段英文 看它统计的数据(比如单词99 ,汉字99)
然后用一下程序实现统计,发现代码实现的统计数目均会多一个(如 单词100 ,汉字100)
这个问题怎么解决呢? 代码有点长。。。

环境:xp系统
软件:Microsoft Visual C++ 2008
语言:C

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int readData(char filename[]);
void writeData(int n , int x);
int hanzi(char filename[]);

int main(void)
{
    char name[40] = "e:\\abc2.txt";
    int word_num;
    int hanzi_num;
         
    word_num = readData(name);  
    hanzi_num = hanzi(name) ;         
    writeData(word_num, hanzi_num);
    return 0;
}

int readData(char filename[]) //此函数计算英文单词个数
{
    FILE* fp;
    char ch;
    char flag = 0;
    int num=0;
   
    if((fp = fopen(filename,"r")) == NULL)
    {
        printf("%s open failure",filename);
        exit(EXIT_FAILURE);
    }
    printf("%s open sucessfully!~\n",filename);
   
    while(!feof(fp))
    {
        ch = fgetc(fp);
        //(ch < 0x80)  排除汉字的干扰
        if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'))
            flag = 0;
        else if(flag == 0 && (ch != '-' && ch != '/'&& ch != '\''))
        {
            num++;
            flag = 1;
        }
    }
    fclose(fp);
    return num;
}

int hanzi(char filename[])         //此文件计算汉字个数
{
    FILE* fp;
    int ch;  //注意这里是int型的
    int count = 0;
    if((fp = fopen(filename,"r")) == NULL)
    {
        printf("%s open failure.\n",filename);
        exit(EXIT_FAILURE);
    }

    while(!feof(fp))
    {
        ch = fgetc(fp);
        if(ch == -1)
            break;
        
        if(ch >= 0x80)
        {
            count++;
            ch = fgetc(fp);//为什么要再来一次? 哦 因为fgetc每次只能取到一个字节 汉字是两个字节(可能是为了废掉一个字节)
        }
        
    }
    fclose(fp);
    return count;
}

void writeData(int n,int x)  //此文件输出
{
    FILE* fp;
    if((fp = fopen("D:\\result.txt","a")) == NULL)
    {
        printf("File open failure!!!");
        exit(EXIT_FAILURE);
    }
   
    fprintf(fp,"文件中的单词个数为%d\n",n);
    fprintf(fp,"文件中的汉字个数为%d\n",x);
   
    printf("文件中的单词个数为%d\n",n);
    printf("文件中的汉字个数为%d\n",x);

    printf("结果保存在D:\\result.txt当中\n");
    fclose(fp);


}

搜索更多相关主题的帖子: Microsoft 文本文件 include xp系统 英文 
2013-08-04 18:35
Dream_weaver
Rank: 1
等 级:新手上路
帖 子:37
专家分:2
注 册:2012-6-8
收藏
得分:0 
我查到的信息是:
字母占一个字节 汉字占两个字节。
ACSII编码只用到0-127(不使用字节的高位)
所以判别方式是:ch > 0x80

我觉得应该是可以实现的,不然word里面怎么统计出来的呢?
2013-08-05 09:48
Dream_weaver
Rank: 1
等 级:新手上路
帖 子:37
专家分:2
注 册:2012-6-8
收藏
得分:0 
回复 4楼 邓士林
我指的是统计的个数,
一段含中英文的文章,统计结果是汉字99个,单词99个
这里把数字 算作单词吧 包含在单词数目里面
2013-08-05 09:51
Dream_weaver
Rank: 1
等 级:新手上路
帖 子:37
专家分:2
注 册:2012-6-8
收藏
得分:0 
回复 8楼 wp231957
很明显 我的代码中计算汉字的部分 跟你写的代码几乎完全一样
单独对只含汉字的文件进行操作,我的程序结果也会是正确的
所以我需要的是能同时计算汉字个数和单词个数的程序,不是单一的一种
2013-08-05 20:48
Dream_weaver
Rank: 1
等 级:新手上路
帖 子:37
专家分:2
注 册:2012-6-8
收藏
得分:0 
回复 4楼 邓士林
环境确定的话,我想结果应该会唯一的
2013-08-05 20:53
Dream_weaver
Rank: 1
等 级:新手上路
帖 子:37
专家分:2
注 册:2012-6-8
收藏
得分:0 
回复 12楼 wp231957
while(!feof(fp))
    {
        ch = fgetc(fp);
        if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'))
            flag = 0;
        else if(flag == 0 && (ch != '-' && ch != '/'&& ch != '\''))
        {
            num++;
            flag = 1;
        }
    }
统计单词个数,我的算法里判定条件是 :
若第一个字符为字母或者数字,第二个不为字符为字母或者数字(但可以为- / \这几个符号),单词个数就加一
也就是说:win-xp   cet4/6  a  都分别可以认为是一个单词,当然这种算法的健壮性明显不够,就比如你的含+的字符串
会被认为是2个单词。

我看过网上其它的程序,均以空格作为判别条件,太简陋了。
2013-08-06 14:21
Dream_weaver
Rank: 1
等 级:新手上路
帖 子:37
专家分:2
注 册:2012-6-8
收藏
得分:0 
我的帖子还没解决,为什么让我结贴?
2013-08-07 13:39
Dream_weaver
Rank: 1
等 级:新手上路
帖 子:37
专家分:2
注 册:2012-6-8
收藏
得分:0 
回复 15楼 TonyDeng
就是对一段含中英文的文本的统计,能够将我的程序更改正确,或者给出其它的程序也可以。
以下是测试文本。
我不知道错在哪里。
麻烦了。

Good morning, ladies and gentlemen!
It is really my honor to have this opportunity for an interview. I hope I can make a good performance today.  
Now I will introduce myself briefly. I am 26 years old, born in HuBei province. I got my bachelor degree from The three gorges University.   when I was a undergratuate student , I was the minister of my department in student union. During that time ,I have Applied for a  for utility model patent successfully. It now appears that the patent without any technology. But it can show my ability to find problem in our life, and then  solve it or improve it in some way.  Besides,   I spent most of my time on study, and I’ve passed CET-4/6 .I also passed examination in spoken English ,grade B. So i was given/awarded the national scholarship.

这算是有感而发。是和上一位网友沟通的结果。这位网友说他大三了,准备暑期要找份实习的工作,把简历发过来让我瞅瞅,看有问题没有。结果一看,还真有问题,我给他讲了一些写简历的基本思想,他觉得还能接受,沟通挺愉快的。
后来,我想想,干脆,在征得他同意后,把这个沟通结果发出来,让大家也参考一下,也许能帮得到大家也说不定哈。
嗯,还是那句话,一家之言,欢迎拍砖哈。
2013-08-09 12:34
Dream_weaver
Rank: 1
等 级:新手上路
帖 子:37
专家分:2
注 册:2012-6-8
收藏
得分:0 
回复 17楼 TonyDeng
Good morning, ladies and gentlemen!
It is really my honor to have this opportunity for an interview. I hope I can make a good performance today.  
Now I will introduce myself briefly. I am 26 years old, born in HuBei province. I got my bachelor degree from The three gorges University.   when I was a undergratuate student , I was the minister of my department in student union. During that time ,I have Applied for a  for utility model patent successfully. It now appears that the patent without any technology. But it can show my ability to find problem in our life, and then  solve it or improve it in some way.  Besides,   I spent most of my time on study, and I’ve passed CET-4/6 .I also passed examination in spoken English ,grade B. So i was given/awarded the national scholarship.

这算是有感而发。是和上一位网友沟通的结果。这位网友说他大三了,准备暑期要找份实习的工作,把简历发过来让我瞅瞅,看有问题没有。结果一看,还真有问题,我给他讲了一些写简历的基本思想,他觉得还能接受,沟通挺愉快的。
后来,我想想,干脆,在征得他同意后,把这个沟通结果发出来,让大家也参考一下,也许能帮得到大家也说不定哈。
嗯,还是那句话,一家之言,欢迎拍砖哈
2013-08-09 14:13
Dream_weaver
Rank: 1
等 级:新手上路
帖 子:37
专家分:2
注 册:2012-6-8
收藏
得分:0 
回复 20楼 TonyDeng
谢谢你!
受教了。
继续努力。
2013-08-10 12:56
快速回复:统计一个文本文件中的单词个数和汉字个数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012481 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved