用哈希表实现唐诗小程序

#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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值