无套路,均已上机通过,求个关注求个赞,提供答疑解惑服务。
一份C源代码存储在一个文本文件中,请统计该文件中关键字出现的频度,并按此频度对关键字进行排序。要求:
#include
#include
#include
#include
#define MAX_KEYWORDS 100
#define MAX_KEYWORD_LENGTH 50
#define HASH_TABLE_SIZE 101
typedef struct {
char keyword[MAX_KEYWORD_LENGTH];
int frequency;
} KeywordInfo;
typedef struct HashNode {
KeywordInfo data;
struct HashNode *next;
} HashNode;
typedef struct {
HashNode *table[HASH_TABLE_SIZE];
} HashTable;
int compareKeywords(const void *a, const void *b) {
const KeywordInfo *keywordA = (const KeywordInfo *)a;
const KeywordInfo *keywordB = (const KeywordInfo *)b;
if (keywordB->frequency != keywordA->frequency) {
return keywordB->frequency - keywordA->frequency;
}
return strcmp(keywordA->keyword, keywordB->keyword);
}
void initializeHashTable(HashTable *hashTable) {
int i;
for (i = 0; i < HASH_TABLE_SIZE; i++) {
hashTable->table[i] = NULL;
}
}
unsigned int hashFunction(const char *str) {
unsigned int hash = 0;
while (*str) {
hash = (hash << 5) + *str++;
}
return hash % HASH_TABLE_SIZE;
}
HashNode *searchHashTable(HashTable *hashTable, const char *keyword) {
unsigned int hashValue = hashFunction(keyword);
HashNode *current = hashTable->table[hashValue];
while (current != NULL) {
if (strcmp(current->data.keyword, keyword) == 0) {
return current;
}
current = current->next;
}
return NULL;
}
void insertHashTable(HashTable *hashTable, const KeywordInfo *keywordInfo) {
unsigned int hashValue = hashFunction(keywordInfo->keyword);
HashNode *newNode = (HashNode *)malloc(sizeof(HashNode));
if (newNode == NULL) {
printf("内存分配失败\n");
exit(1);
}
newNode->data = *keywordInfo;
newNode->next = hashTable->table[hashValue];
hashTable->table[hashValue] = newNode;
}
void freeHashTable(HashTable *hashTable) {
int i;
for (i = 0; i < HASH_TABLE_SIZE; i++) {
HashNode *current = hashTable->table[i];
while (current != NULL) {
HashNode *next = current->next;
free(current);
current = next;
}
}
}
void countKeywordsSequential(FILE *file, char *keywords[], int numKeywords, KeywordInfo keywordInfo[]) {
char line[1024];
rewind(file);
while (fgets(line, sizeof(line), file) != NULL) {
for (int i = 0; i < numKeywords; i++) {
char *pos = strstr(line, keywords[i]);
while (pos != NULL) {
if ((pos == line || !isalpha(pos[-1])) && !isalpha(pos[strlen(keywords[i])])) {
keywordInfo[i].frequency++;
}
pos = strstr(pos + 1, keywords[i]);
}
}
}
}
void countKeywordsHash(FILE *file, HashTable *hashTable, char *keywords[], int numKeywords, KeywordInfo keywordInfo[]) {
char line[1024];
rewind(file);
while (fgets(line, sizeof(line), file) != NULL) {
for (int i = 0; i < numKeywords; i++) {
char *pos = strstr(line, keywords[i]);
while (pos != NULL) {
if ((pos == line || !isalpha(pos[-1])) && !isalpha(pos[strlen(keywords[i])])) {
// 检查哈希表中是否已存在该关键字的节点
HashNode *existingNode = searchHashTable(hashTable, keywords[i]);
if (existingNode != NULL) {
// 如果已存在,则更新哈希表中节点的频度
existingNode->data.frequency++;
} else {
// 如果不存在,则插入新节点到哈希表
KeywordInfo newKeywordInfo = { .frequency = 1 };
strcpy(newKeywordInfo.keyword, keywords[i]);
insertHashTable(hashTable, &newKeywordInfo);
}
// 更新数组中的频度
keywordInfo[i].frequency++;
}
pos = strstr(pos + 1, keywords[i]);
}
}
}
}
void writeKeywordStats(FILE *outputFile, KeywordInfo keywordInfo[], int numKeywords) {
for (int i = 0; i < numKeywords; i++) {
fprintf(outputFile, "%s: %d\n", keywordInfo[i].keyword, keywordInfo[i].frequency);
}
}
void writeSortedKeywordStats(FILE *outputFile, KeywordInfo keywordInfo[], int numKeywords) {
for (int i = 0; i < numKeywords; i++) {
if (keywordInfo[i].frequency > 0) {
fprintf(outputFile, "%s: %d\n", keywordInfo[i].keyword, keywordInfo[i].frequency / 2);
}
}
}
void writeHashTableStats(FILE *outputFile, HashTable *hashTable, char *keywords[], int numKeywords) {
for (int i = 0; i < numKeywords; i++) {
HashNode *node = searchHashTable(hashTable, keywords[i]);
fprintf(outputFile, "%s: %d\n", keywords[i], (node != NULL) ? node->data.frequency : 0);
}
}
void sortKeywords(KeywordInfo keywordInfo[], int numKeywords) {
qsort(keywordInfo, numKeywords, sizeof(KeywordInfo), compareKeywords);
}
int main(void) {
FILE *keywordFile, *codeFile;
FILE *outputFile1, *outputFile2, *outputFile3;
char *keywords[MAX_KEYWORDS];
int numKeywords = 0;
char tempKeyword[MAX_KEYWORD_LENGTH];
KeywordInfo keywordInfo[MAX_KEYWORDS];
HashTable hashTable;
// 打开文件
keywordFile = fopen("Key.txt", "r");
codeFile = fopen("InFile.txt", "r");
outputFile1 = fopen("OutFile1.txt", "w");
outputFile2 = fopen("OutFile2.txt", "w");
outputFile3 = fopen("OutFile3.txt", "w");
if (keywordFile == NULL || codeFile == NULL || outputFile1 == NULL || outputFile2 == NULL || outputFile3 == NULL) {
printf("无法打开文件\n");
return 1;
}
// 读取关键字列表
while (fscanf(keywordFile, "%s", tempKeyword) != EOF && numKeywords < MAX_KEYWORDS) {
keywords[numKeywords] = (char *)malloc(strlen(tempKeyword) + 1);
strcpy(keywords[numKeywords], tempKeyword);
numKeywords++;
}
// 初始化关键字信息数组
for (int i = 0; i < numKeywords; i++) {
strcpy(keywordInfo[i].keyword, keywords[i]);
keywordInfo[i].frequency = 0;
}
// 初始化哈希表
initializeHashTable(&hashTable);
int choice;
do {
// 显示菜单
printf("\n菜单:\n");
printf("1. 顺序查找关键字并统计频度(输出到OutFile1.txt)\n");
printf("2. 哈希查找关键字并统计频度(输出到OutFile2.txt)\n");
printf("3. 排序关键字并输出到OutFile3.txt\n");
printf("4. 退出\n");
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// 顺序查找关键字并统计频度
countKeywordsSequential(codeFile, keywords, numKeywords, keywordInfo);
writeKeywordStats(outputFile1, keywordInfo, numKeywords);
break;
case 2:
// 哈希查找关键字并统计频度
countKeywordsHash(codeFile, &hashTable, keywords, numKeywords, keywordInfo);
writeHashTableStats(outputFile2, &hashTable, keywords, numKeywords);
break;
case 3:
// 排序关键字并输出
sortKeywords(keywordInfo, numKeywords);
writeSortedKeywordStats(outputFile3, keywordInfo, numKeywords);
break;
case 4:
// 退出
break;
default:
printf("无效的选项,请重新选择\n");
break;
}
} while (choice != 4);
// 关闭文件和释放内存
fclose(keywordFile);
fclose(codeFile);
fclose(outputFile1);
fclose(outputFile2);
fclose(outputFile3);
for (int i = 0; i < numKeywords; i++) {
free(keywords[i]);
}
freeHashTable(&hashTable);
return 0;
}