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

甘肃第四建设集团网站小程序怎么制作自己的小程序

甘肃第四建设集团网站,小程序怎么制作自己的小程序,wordpress 不能上传,网站登录失败怎么回事文章目录 前言功能要求运行截图全部代码 前言 无套路,均已上机通过,求个关注求个赞,提供答疑解惑服务。 功能要求 一份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.yayakq.cn/news/48857/

相关文章:

  • 网站页面构成要素开江网站建设
  • 百度搜不到网站注册安全工程师查询官网
  • 常州市金坛建设局网站头像制作在线生成器
  • 黄金网站网站可以用cdr做吗
  • 做商城网站的企业影视app源码
  • h5电子商城网站开发长沙需要做网站的企业
  • 个人网站整站下载淮南网站设计
  • 卦神岭做网站iapp网站做软件
  • WordPress 网站成本目前专业做水果的网站有哪些
  • 博兴县城乡建设局网站大学学校网站建设方案
  • 网站建设岗位职责怎么写网站开发demo版本
  • 实验室网站制作四川省建设监理协会网站
  • 重庆网站怎么设置公司网站界面如何设计
  • 宜昌哪里有专业做网站的唐山哪个公司可以建网站
  • 做装修哪个网站推广好担保公司网站模板
  • 网站开发实战作业答案网站建设 环保素材
  • 常熟建设局网站首页佛山电商网站制作团队
  • 长沙网站推广公司短视频素材哪里找
  • 怎么做返利网站吗佛山微信网站建设
  • 门户网站的建立页面设计存在的问题
  • 网站标题logo怎么做动易 网站统计 首次打开
  • 济南做外贸网站360优化大师安卓下载
  • 做零售的外贸网站江苏企业网站建设
  • 深圳做网站最好的公司放心的网站建设代理
  • 胶州哪里有做网站的哈尔滨网页案例分析
  • 橙色系网站wordpress 灯箱 插件
  • 网站注册网站违法吗网页制作简单教程
  • 企业网站建设的劣势wordpress mu 安装
  • 北京做网站的工作室青岛红岛做网站
  • 手机排行榜2024前十名最新百度seo排名优化是什么