|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 |
#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);} |
原文:http://www.cnblogs.com/fk15690320339/p/3578141.html