| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3978 人关注过本帖
标题:C编程 实现 统计文件中各个单词出现的次数
只看楼主 加入收藏
zwd94
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-3-12
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
C编程 实现 统计文件中各个单词出现的次数
已有文本文件test.txt 内容为 hello,how are you!Welcome you to China!编写一个程序读取test.txt。统计各单词出现次数,并将个单词和气出现的次数输出到屏幕和文件中.
要求用C语言编程。 谢谢
搜索更多相关主题的帖子: 文本文件 Welcome hello C语言 China 文本文件 Welcome hello C语言 China 
2017-03-12 20:27
zwd94
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-3-12
收藏
得分:0 
#include<stdio.h>
#include<string.h>
struct word
{
    char c[30];
    int n;
}w[10000];

void main()
{
    FILE *fp;
    char b[30],ch;
    int i=0,m=1,j=0,k=0,t=0,flag=0;
    fp=fopen("test.txt","r");
    while((ch=fgetc(fp))!=EOF)
        {
           if('A'<=ch&&ch<='Z') ch=ch+32;
           if('a'<=ch && ch<='z')
            {b[i]=ch;i++;flag=1;}
            else
            {
                if(ch=='-'&&(ch=fgetc(fp))=='\n')flag=0;
                else
                {
                     if(flag==1)
                        { b[i]='\0';i=0;flag=0;m=0;
                    for(j=0;j<k;j++)
                        if(strcmp(b,w[j].c)==0){m=1;break;}
                    if(m) w[j].n++;
                        else
                            {w[k].n=1;strcpy(w[k].c,b);k++;}
                }
            }
            if('A'<=ch && ch<='Z') ch+=32;
            if('a'<=ch && ch<='z'){b[i]=ch;i++;flag=1;}
        }
    }
   
    for(i=0;i<k&&i<5;i++)
    {
    t=0;
    while(w[t].n==0) t++;
    for(j=1;j<k;j++)
    {
    if(w[j].n>w[t].n) t=j;
    else
    if(w[j].n==w[t].n)
    {
    if(strcmp(w[j].c,w[t].c)<0)
    t=j;
    }
    }
    printf("%s %d\n",w[t].c,w[t].n);
    w[t].n=0;
    }
    return 0;
    }
貌似哪有错误
2017-03-12 20:35
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:20 
很早以前写的,稍微改改似乎就满足你的要求了。
PS,不要吐槽变量名。

现在可以了,刚才有点小BUG。

程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct word {
    char * words;
    int count;
    struct word * Next;
};
struct word_tab {
    char alphaber;
    struct word_tab *Next;
    struct word *Word_Next;
};
int
index_word_tab( struct word_tab **p_total, char word[] );
void
print_word( struct word_tab **p_total );
int
getword( char word[], int n, FILE *fp );


int
main( void )
{
    struct word_tab *p;
    struct word_tab **kp;
        char word1[ 50 ];
        FILE *fp;

    p = NULL;
    kp = &p;

        if( ( fp = fopen("word.txt","r") ) == NULL)
        {
        fprintf( stderr,"无法打开读取文件\n" );
        exit( EXIT_FAILURE );
        }
    
    while( getword( word1, 50, fp ) > 0)
        index_word_tab( kp, word1 );

        print_word( kp );
    
        fclose( fp );
        getchar();
    
    return 0;
}

int
getword( char word[], int n, FILE *fp )
{
        int i, c;
    
    while( ispunct( c = fgetc( fp ) ) )
        ;

        for( i = 0; ( i < n - 1 ) && ( c != EOF ) && ( !isspace( c ) ); i++ )
        {
        word[ i ] = c;
        c = fgetc( fp );
        }

        if( ispunct( word[ i - 1 ] ) && ( i - 1 ) > 0 )
        {
        word[ i - 1 ] = '\0';
        return c;
        }
    
    word[ i ] = '\0';
    
    return c;
}


int
index_word_tab( struct word_tab **p_total, char word[] )
{
        int determine( char word[] );
        struct word_tab * next;
        struct word_tab * new_total;
        struct word * next_word;
        struct word * new_word;
        struct word ** kNext;
        char w;
    
    if( word[ 0 ] == '\0')
            return 0;
    
    if( determine( word ) < 0 )
        {
        printf( "ERORR: %s\n", word );
        return 0;
        }
    
    w = tolower( word[ 0 ] );
    
    while( ( next = *p_total ) != NULL && next->alphaber < w )
        p_total = &next->Next;
    
    if( next == NULL || next->alphaber != w )
        {
        new_total = ( struct word_tab * )malloc( sizeof( struct word_tab ) );
            if ( new_total == NULL )
                return 0;
            *p_total = new_total;
            new_total->Next = next;
            new_total->alphaber = w;
            new_total->Word_Next = NULL;
        }
    
    kNext = &(*p_total)->Word_Next;
    
    while ( ( next_word = *kNext ) != NULL && strcmp( next_word->words, word ) <= 0)
        {
        if ( strcmp( next_word->words, word ) == 0 )
        {
                next_word->count++;
                return 0;
        }
        kNext = &next_word->Next;
        }
    
    new_word = ( struct word * )malloc( sizeof( struct word ) );
        if ( new_word == NULL )
        return 0;
        *kNext = new_word;
        new_word->Next = next_word;
        new_word->count = 1;
        new_word->words = strdup( word );
    
    return 1;
}


void
print_word( struct word_tab **p_total )
{
        struct word_tab *next;
        struct word *word_next;
        FILE *fp;
    
    if( ( fp = fopen( "words.txt","w" ) ) == NULL )
        {
        fprintf( stderr,"无法打开写入文件\n" );
        exit( EXIT_FAILURE );
        }
    
    while( ( next = *p_total ) != NULL )
        {
        for( word_next = next->Word_Next; word_next != NULL; word_next = word_next->Next )
                fprintf( stdout, "%d %s %c", word_next->count, word_next->words,'\n' );
        fprintf( stdout, "\n\n" );
        p_total = &next->Next;
        }
    
    fclose( fp );
}


int
determine( char word[] )
{
        int i,j;

        j = strlen( word );
    
    for( i = 0; isalpha( word[ i ] ) || ( i > 0 && ( word[ i ] == '\'' || word[ i ] == '-' ) ); i++ )
          ;

    if(i == j)
        return i;
        else
        return -1;
}



[此贴子已经被作者于2017-3-12 21:29编辑过]


09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-03-12 20:37
快速回复:C编程 实现 统计文件中各个单词出现的次数
数据加载中...
 
   



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

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