admin管理员组文章数量:1616675
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node {
char title[20];
char author[50];
char content[200];
char appreciation[200];
int info;
} poem;
typedef struct hashtable {
poem** table;
int tablesize;
} hashtable;
int largest(int n) {
while (true) {
bool is_prime = true;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
is_prime = false;
break;
}
}
if (is_prime) {
return n;
}
++n;
}
}
int hashfunction(char* title, hashtable* htable) {
int hashvalue = 0;
int l=strlen(title);
for (int i = 0; i < l; i++) {
hashvalue = hashvalue + title[i];
}
hashvalue = hashvalue % largest(htable->tablesize);
hashvalue = -1*hashvalue;
return hashvalue;
}//哈希函数
hashtable* maketable(int tablesize) {
hashtable* htable = (hashtable*)malloc(sizeof(hashtable));
htable->table = (poem**)malloc(sizeof(poem*) * tablesize);
htable->tablesize = tablesize;
for (int i = 0; i < tablesize; i++) {
htable->table[i] = NULL;
}
return htable;
}//创建一个哈希表
void addpoem(char*title,char* author,char* content,char* appreciation,hashtable* htable) {
int index = hashfunction(title, htable);
if(htable->table[index]==NULL){
htable->table[index] = (poem*)malloc(sizeof(poem));
strcpy(htable->table[index]->title, title);
strcpy(htable->table[index]->author, author);
strcpy(htable->table[index]->content, content);
strcpy(htable->table[index]->appreciation, appreciation);
htable->table[index]->info = 1;
} else{
while(htable->table[index]!=NULL){
index++;
if(index>=htable->tablesize) index=index%htable->tablesize;
}
htable->table[index] = (poem*)malloc(sizeof(poem));
strcpy(htable->table[index]->title, title);
strcpy(htable->table[index]->author, author);
strcpy(htable->table[index]->content, content);
strcpy(htable->table[index]->appreciation, appreciation);
htable->table[index]->info = 1;
}
}
int findpoem(char* title,hashtable* htable) {
if (htable == NULL) return 0;
int index = hashfunction(title,htable);
if (htable->table[index] == NULL) return 0;
else{
if(strcmp(title,htable->table[index]->title)==0){
return 1;
}else{
while(htable->table[index]!=NULL){
index++;
if(index>=htable->tablesize) index=index%htable->tablesize;
if(strcmp(title,htable->table[index]->title)==0) return 1;
}
return 0;
}
}
}//查找唐诗
int deletepoem(char* title,hashtable* htable) {
if (htable == NULL) return 0;
int index = hashfunction(title, htable);
if (htable->table[index] == NULL) return 0;
else{
htable->table[index] = NULL;
return 1;
}
}//删除唐诗
int modify(char* title, hashtable* htable) {
// 检查要修改的唐诗是否存在
if (findpoem(title, htable) == 0) {
printf("要修改的唐诗不存在\n");
return 0;
}
int index=hashfunction(title,htable);
char new_author[50];
char new_content[200];
char new_appreciation[200];
printf("请输入作者:\n");
scanf("%s",new_author);
printf("请输入内容:\n");
scanf("%s",new_content);
printf("请输入鉴赏:\n");
scanf("%s",new_appreciation);
strcpy(htable->table[index]->author,new_author);
strcpy(htable->table[index]->content,new_content);
strcpy(htable->table[index]->appreciation,new_appreciation);
return 1;
}
void printfpoem(char* title,hashtable* htable) {
int index = hashfunction(title, htable);
int count=1;
if (findpoem(title, htable)==1) {
while(htable->table[index]!=NULL){
if(strcmp(title,htable->table[index]->title)==0){
printf("第%d首:\n",count);
printf("题目:《%s》\n", htable->table[index]->title);
printf("作者:唐:%s\n", htable->table[index]->author);
printf("原文:%s\n", htable->table[index]->content);
printf("鉴赏:%s\n", htable->table[index]->appreciation);
count++;
}
index++;
}
}
else {
printf("该唐诗不存在\n");
}
}//打印唐诗
double loadfactor(hashtable*htable){
if(htable==NULL) return 0;
double count=0;
for(int i=0;i<htable->tablesize;i++){
if(htable->table[i]!=NULL) {
count++;
}
}
double loadfactor=count/htable->tablesize;
return loadfactor;
} //求装填因子
int main() {
int tablesize = 29;
int choice;
char title[20];
hashtable* htable = maketable(tablesize);
do {
printf("【1】 录入唐诗\n");
printf("【2】 查找唐诗\n");
printf("【3】 删除唐诗\n");
printf("【4】 修改唐诗\n");
printf("【5】 输出唐诗\n");
printf("【6】 输出装填因子\n");
printf("【0】 退出程序\n");
printf("请输入您要操作的选项编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
char author[20];
char content[200];
char appreciation[200];
printf("请输入录入唐诗标题:\n");
scanf("%s", title);
printf("请输入唐诗作者:\n");
scanf("%s", author);
printf("请输入唐诗内容:\n");
scanf("%s", content);
printf("请输入唐诗鉴赏:\n");
scanf("%s", appreciation);
addpoem(title,author,content,appreciation, htable);
break;
case 2: {
printf("请输入要查找唐诗标题:\n");
scanf("%s", title);
if (findpoem(title, htable)==1) {
printf("查找成功!\n");
}
if (findpoem(title, htable)==0) {
printf("查找失败!\n");
}
break;
}
case 3: {
printf("请输入要删除唐诗标题:\n");
scanf("%s", title);
deletepoem(title, htable);
break;
}
case 4: {
printf("请输入要修改的唐诗标题:\n");
scanf("%s", title);
modify(title, htable);
break;
}
case 5: {
printf("请输入要输出的唐诗标题:\n");
scanf("%s", title);
printfpoem(title, htable);
break;
}
case 6:{
printf("装填因子为:\n");
printf("%lf\n",loadfactor(htable));
break;
}
case 0:
printf("退出程序 谢谢使用\n");
break;
default:
printf("无效的选项,请重新输入。\n");
}
} while (choice != 0);
return 0;
}
版权声明:本文标题:用哈希表实现唐诗小程序 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728741479a1171117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论