作业要求:
程序实现思路:
源代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct wordsdata {
char words[30];
int m;
struct wordsdata *next;
};
int main()
{
struct wordsdata *head=NULL;
struct wordsdata *q;
FILE *fp;
int i;
int a[10];
char b;
for (i=0;i<10;i++)
{
a[i]=0;
}
fp=fopen("E://zuoye.txt","r");//读取本地文本文件
while (!feof(fp))
{
char *p=(char*)malloc(30*sizeof(char));
fscanf(fp,"%s",p);
if (head==NULL)//分析单词频率
{
struct wordsdata *temp =(struct wordsdata*)malloc(sizeof(struct wordsdata));
strcpy(temp->words,p);
temp->m=1;
temp->next=NULL;
head=temp;
}
else
{
struct wordsdata *l=head;
while(l!=NULL)
{
if (strcmp(l->words,p)==0)
{
int count =l->m;
count++;
l->m=count;
break;
}
l=l->next;
}
if (l==NULL)
{
struct wordsdata *temp=(struct wordsdata*)malloc(sizeof(struct wordsdata));
strcpy(temp->words,p);
temp->m=1;
temp->next=head;
head=temp;
}
}
}
printf("单词出现频率由高到低:\n");//排序出现频率最高的10个单词并输出
for (i=0;i<10;i++)
{
q=head;
while (q!=NULL)
{
if(q->m>a[i])
a[i]=q->m;
else
q=q->next;
}
q=head;
while (q!=NULL)
{
if (a[i]==q->m)
{
q->m=0;
printf("%s\t\n",q->words);
printf("单词出现频率:%d\t\