设计一个程序,实现对一个文本文件中的单词进行统计并按照出现次数从高到低进行排序输出。要求程序能够处理大量的文本数据,并且在内存占用和运行时间上有较高的效率。同时,需要考虑处理文本中的标点符号和大小写等情况,并且能够正确识别单词。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LENGTH 100
#define MAX_WORDS 10000
typedef struct {
char word[lbk]MAX_WORD_LENGTH[rbk];
int count;
} WordCount;
int compare(const void *a, const void *b) {
WordCount *wordCountA = (WordCount *)a;
WordCount *wordCountB = (WordCount *)b;
return wordCountB->count - wordCountA->count;
}
int main() {
FILE *file = fopen("input.txt", "r");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
WordCount wordCounts[lbk]MAX_WORDS[rbk];
int numWords = 0;
char word[lbk]MAX_WORD_LENGTH[rbk];
while (fscanf(file, "%s", word) == 1) {
for (int i = 0; word[lbk]i[rbk]; i++) {
word[lbk]i[rbk] = tolower(word[lbk]i[rbk]);
}
int found = 0;
for (int i = 0; i < numWords; i++) {
if (strcmp(word, wordCounts[lbk]i[rbk].word) == 0) {
wordCounts[lbk]i[rbk].count++;
found = 1;
break;
}
}
if (!found) {
strcpy(wordCounts[lbk]numWords[rbk].word, word);
wordCounts[lbk]numWords[rbk].count = 1;
numWords++;
}
}
qsort(wordCounts, numWords, sizeof(WordCount), compare);
for (int i = 0; i < numWords; i++) {
printf("%s: %d\n", wordCounts[lbk]i[rbk].word, wordCounts[lbk]i[rbk].count);
}
fclose(file);
return 0;
}
```
这个程序首先打开一个名为 "input.txt" 的文本文件,然后逐个读取单词并进行统计。在统计过程中,将单词转换为小写并去除标点符号,然后使用结构体数组存储每个单词和其使用快速排序对单词进行按出现次数从高到低的排序,并输出结果。这个程序考验了对文件操作、字符串处理、结构体和排序算法的综合运用。
湖州师范学院881
上海电机学院805
浙江科技学院818
上海理工大学854
上海工程技术大学803
南昌工程学院853
湖南师范大学967
以上学

院都有最后三套
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LENGTH 100
#define MAX_WORDS 10000
typedef struct {
char word[lbk]MAX_WORD_LENGTH[rbk];
int count;
} WordCount;
int compare(const void *a, const void *b) {
WordCount *wordCountA = (WordCount *)a;
WordCount *wordCountB = (WordCount *)b;
return wordCountB->count - wordCountA->count;
}
int main() {
FILE *file = fopen("input.txt", "r");
if (file == NULL) {
printf("Error opening file\n");
return 1;
}
WordCount wordCounts[lbk]MAX_WORDS[rbk];
int numWords = 0;
char word[lbk]MAX_WORD_LENGTH[rbk];
while (fscanf(file, "%s", word) == 1) {
for (int i = 0; word[lbk]i[rbk]; i++) {
word[lbk]i[rbk] = tolower(word[lbk]i[rbk]);
}
int found = 0;
for (int i = 0; i < numWords; i++) {
if (strcmp(word, wordCounts[lbk]i[rbk].word) == 0) {
wordCounts[lbk]i[rbk].count++;
found = 1;
break;
}
}
if (!found) {
strcpy(wordCounts[lbk]numWords[rbk].word, word);
wordCounts[lbk]numWords[rbk].count = 1;
numWords++;
}
}
qsort(wordCounts, numWords, sizeof(WordCount), compare);
for (int i = 0; i < numWords; i++) {
printf("%s: %d\n", wordCounts[lbk]i[rbk].word, wordCounts[lbk]i[rbk].count);
}
fclose(file);
return 0;
}
```
这个程序首先打开一个名为 "input.txt" 的文本文件,然后逐个读取单词并进行统计。在统计过程中,将单词转换为小写并去除标点符号,然后使用结构体数组存储每个单词和其使用快速排序对单词进行按出现次数从高到低的排序,并输出结果。这个程序考验了对文件操作、字符串处理、结构体和排序算法的综合运用。
湖州师范学院881
上海电机学院805
浙江科技学院818
上海理工大学854
上海工程技术大学803
南昌工程学院853
湖南师范大学967
以上学


院都有最后三套