该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
可以读出文件中出现所有单词的出现次数并显示求只显示出现次数最多的5个单词及其出现次数
现代码如下:
#include
#include
#include
#include
struct Word {
char w[20];
int k;
struct Word *next;
};
int main(int argc, char *argv[])
{
FILE *fp = fopen("input.txt","r");
struct Word *Head = NULL;
while (!feof(fp)) {
char *p = (char *)malloc(20*sizeof(char));
fscanf(fp, "%s", p);
if(Head == NULL){
struct Word *temp = (struct Word *)malloc(sizeof(struct Word));
strcpy(temp->w, p);
temp->k = 1;
temp->next = NULL;
Head = temp;
} else {
struct Word *pp = Head;
while (pp != NULL) {
if (strcasecmp( pp->w, p) == 0){
++pp->k;
break;
} else {
pp = pp->next;
}
}
if (pp == NULL){
struct Word *temp = (struct Word *)malloc(sizeof(struct Word));
strcpy(temp->w, p);
temp->k = 1;
temp->next = Head;
Head = temp;
}
}
}
struct Word *q = Head;
while (q != NULL) {
printf("%s ", q->w);
printf("%d\n", q->k);
q = q->next;
}
return 0;
}