c语言 词频统计

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》


c语言 词频统计_第1张图片

你可能感兴趣的:(c语言试题)