c语言 词频统计

来源:互联网 发布:淘宝卖家信誉帐号 编辑:程序博客网 时间:2024/05/17 07:55
c语言没有向量可以存储单词信息,需要自己定义数据结构,此处方便用了结构体数组,因为无法确定单词数量,所以申请的空间很大,也可以用链表做,但是链表查找较为繁琐,时间上较慢。

#include#include#include#include//词频统计//存储单词用结构体typedef struct node{char word[26];//一个单词int count;//出现次数}wordnode;wordnode* readfile(char *filename);void divide_word(char temp[]);void insert_word(char temp[],wordnode* words,int* count);void show_word(wordnode* words,int sum);void sort_word(wordnode* words,int sum);void swap(wordnode* e1, wordnode* e2);void quickSort(wordnode* words, int l, int r);//排序void swap(wordnode* e1, wordnode* e2){        wordnode tmp = *e1;        *e1 = *e2;        *e2 = tmp;    }void quickSort(wordnode* words, int l, int r){            if(l >= r) return;int ref = words[l].count;        int lt = l;        int gt = r;        int i = l + 1;        while(i<=gt){            if(words[i].count < ref) swap(&words[i++], &words[lt++]);            else if(words[i].count > ref) swap(&words[i], &words[gt--]);            else ++i;        }        quickSort(words, l, lt - 1);        quickSort(words, gt + 1, r);    }//读文件wordnode* readfile(char *filename,int* sum){char temp[1024];int count=0;wordnode* words=(wordnode*)malloc(1000*sizeof(wordnode));FILE *fp;fp=fopen(filename,"r");if(words==NULL){printf("error!\n");exit(1);}while(!feof(fp)){fscanf(fp,"%s",temp);divide_word(temp);insert_word(temp,words,&count);}*sum=count;fclose(fp);return words;}//划分单词void divide_word(char temp[]){int i=0,j;while(temp[i]!='\0'){if(temp[i]>='A' && temp[i]<='Z'){   //大写转小写temp[i]=temp[i]+32;}if(!((temp[i]>='A' && temp[i]<='Z')  //将非字母的和‘-’和‘‘’|| (temp[i]>='a' && temp[i]<='z')|| temp[i]=='-' || temp[i]=='\'')){for(j=i;temp[j]!='\0';j++){temp[j]=temp[j+1];}if(i==0) i--;}i++;}return;}//存储单词void insert_word(char temp[],wordnode* words,int* count){int i;for(i=0;i<*count;i++){if(strcmp(words[i].word,temp)==0){words[i].count++;return;}}strcpy(words[*count].word,temp);words[*count].count=1;(*count)++;return;}//显示单词void show_word(wordnode* words,int sum){int i;for(i=0;i



结果见下:引用文章部分截选《I have a dream》