#include<iostream.h> #include<string.h> #include<fstream.h> struct word{ char name[30]; int num; struct word *next; }; void readfile(struct word*&head) { struct word*fp; if((fp=fopen("in.txt","r"))==NULL) { cout<<"无法打开此文件!<<endl; break; } char ch; char temp[30]; struct word *p; while(!feof(fp)) { int i=0; ch=fgetc(fp); temp[0]=' '; while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||temp[0]==' ') { if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') { temp[i]=ch; i++; } ch=fgetc(fp); if(feof(fp)) break; } temp[i]='\0'; p=head->next; while(p) { if(!_stricmp(temp,p->name)) { p->num++; break; } p=p->next; } if(!p&&temp[0]!='\0') { p=new word; strcpy(p->name,temp); p->num=1; p->next=head->next; head->next=p; } } } void puts(struct word*&head) { struct word *q; q=q->name; } void sort(struct word*&head) { struct word *ptr; int a[10],i; for(i=0;i<10;i++) a[i]=0; cout<<"文章中出现频率最高的十个单词如下:<<endl; for(i=0;i<10;i++) { ptr=head; while(ptr!=NULL) { if(ptr->num>a[i]) a[i]=ptr->num; else ptr=ptr->next; } ptr=head; while(ptr!=NULL) { if(a[i]==ptr->num) { ptr->num=0; cout<<"出现频率:cout<<a[i]<<endl; puts(ptr->name); break; } else ptr=ptr->next; } } } void main() { struct word *head; head=new word; head->next=NULL; readfile(head); sort(head); }