当前位置: 首页 > news >正文

登封网站开发进入百度搜索网站

登封网站开发,进入百度搜索网站,交互设计英文,洪梅镇网站建设公司文章目录 前言功能要求运行截图全部代码 前言 无套路,均已上机通过,求个关注求个赞,提供答疑解惑服务。 功能要求 一份C源代码存储在一个文本文件中,请统计该文件中关键字出现的频度,并按此频度对关键字进行排序。要…

文章目录

  • 前言
  • 功能要求
  • 运行截图
  • 全部代码

前言

无套路,均已上机通过,求个关注求个赞,提供答疑解惑服务。

功能要求

一份C源代码存储在一个文本文件中,请统计该文件中关键字出现的频度,并按此频度对关键字进行排序。要求:

  • 从文本文件InFile.txt读取C源代码,从文本文件Key.txt读取关键字列表。
  • 分别采用如下不同的查找策略进行频度统计:
    • 链式存储上的顺序查找;
    • 基于链地址法的哈希查找。
  • 基于快速排序实现关键字排序。
  • 不论采取哪种查找和排序策略,完成功能均相同。
    • 关键字统计:依次从关键字列表Key.txt中读取关键字,若该关键字未在文本文件InFile.txt中出现,则将其频度计为0;每检索到一次该关键字,则将其频度增加1。统计结束后,将所有关键字及其频度按关键字列表顺序写入文本文件中。其中,无论关键字列表中的关键字是否出现都要计数。不同查找策略所获得的结果分别写入不同的文件(OutFile1.txt,OutFile2.txt)。
    • 关键字排序:根据关键字出现的频度对所有关键字进行从高到低排序,舍弃关键字列表中未出现的关键字。如果关键字出现的频度相等,则按照关键字的首字母从小到大排序,将排序后的关键字及其频度写入文本文件(OutFile3.txt)中。
  • 设计菜单,实现上述功能。

运行截图

在这里插入图片描述

全部代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>#define MAX_KEYWORDS 100
#define MAX_KEYWORD_LENGTH 50
#define HASH_TABLE_SIZE 101typedef 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;
}
http://www.mmbaike.com/news/86402.html

相关文章:

  • 网站备案 暂住证福州短视频seo方法
  • vps做网站 推广怎样在百度上发布广告
  • 临沂网站服务器价格淘宝关键词优化工具
  • 网站建设常态化工作机制四川网站seo
  • 南京建设银行官方网站app推广拉新
  • 龙华营销型网站费用广告竞价
  • 网站开发的业务需求分析sem推广是什么意思呢
  • 做仿站如何获取网站源码北京互联网公司排名
  • 旅游网站开发目的和意义如何在百度发布广告
  • 国外做无纺布的网站小米市场营销案例分析
  • .net建设网站步骤详解网络热词英语
  • 和平区网站制作台湾搜索引擎
  • 恶搞网站源码网上销售培训课程
  • 免费做网站有哪些家企业推广语
  • 上海营销型网站网络营销的核心是什么
  • 内部网站建设教程谷歌浏览器下载安装2022
  • 个人网站开论坛seo门户网站建设方案
  • 网站栏目管理seo站群优化技术
  • 深圳营销网站建设多少钱哈尔滨百度推广联系人
  • 台州网站建设公司哪家好产品宣传推广策划
  • ios软件资源网站谷歌搜索引擎入口google
  • 莱州网站建设费用线下推广方式有哪些
  • 怎样建设赌博网站巨量引擎广告投放
  • 如何写好网站开发技术文档免费外链发布平台在线
  • flashfxp与Wordpress网站关键词优化排名公司
  • 独立网站的建设武汉今日头条最新消息
  • 怎么建网站手机版销售平台软件有哪些
  • 河南专业网站建设公司哪家好seo网络营销推广排名
  • 做类似3d溜溜的网站营销软文范例大全100
  • 个人建站系统友情链接是外链吗