散列表实现唐诗

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<math.h>  
typedef struct TangPoetry{  
    char name[50];    /* 诗的名称 */  
    char author[10];   /* 作者 */  
    char information[500]; /* 诗内容 */  
    char interpretation[500]; /* 释义 */  
} ElementType;  
  
typedef struct TblNode {  
    ElementType * Poretry;  
    int size;  
} TblNode, *HashTable;   
HashTable createTable(int tableSize) {  
    HashTable hashTable = (HashTable)malloc(sizeof(TblNode));  
    if (!hashTable) {  
        exit(EXIT_FAILURE);  
    }  
    hashTable->Poretry = (ElementType *)calloc(tableSize, sizeof(ElementType));  
    if (!hashTable->Poretry) {  
        free(hashTable);  
        exit(EXIT_FAILURE);  
    }  
    hashTable->size = tableSize;  
    return hashTable;  
}  
int getAddress(char name[], int tableSize) {  
    int i = 0;  
    int address = 0;  
    while (name[i] != '\0') {  
        address += name[i];  
        i++;  
    }  
    address = abs(address) % tableSize;  
    return address;  
}  
void insertTable(HashTable hashTable, ElementType item) {  
    int index = getAddress(item.name, hashTable->size);  
    strcpy(hashTable->Poretry[index].name, item.name);  
    strcpy(hashTable->Poretry[index].author, item.author);  
    strcpy(hashTable->Poretry[index].information, item.information);  
    strcpy(hashTable->Poretry[index].interpretation, item.interpretation);  
}  
void searchTable(HashTable hashTable, char name[]) {  
    int index = getAddress(name, hashTable->size);  
    ElementType *item = &hashTable->Poretry[index];  
    if (strcmp(item->name, name) == 0) {  
        printf("-------------------------------------------------------------------------------\n");  
        printf("诗名:%s\n作者:%s\n诗内容:%s\n释义:%s\n",  
               item->name, item->author, item->information, item->interpretation);  
        printf("-------------------------------------------------------------------------------\n");  
    } else {  
        printf("未找到诗名:%s\n", name);  
    }  
}  
int main() {  
    int tableSize = 10;  
    HashTable hashTable = createTable(tableSize);  
    ElementType item;  
    int num;  
    printf("请输入要插入的诗的个数:");  
    scanf("%d", &num);  
    int i;  
    for (i = 0; i < num; i++) {  
        printf("请输入诗名:");  
        scanf(" %s", item.name);  
        printf("请输入诗的作者:");  
        scanf(" %s", item.author);  
        printf("请输入诗的内容:");  
        scanf(" %[^\n]", item.information); 
        printf("请输入诗的释义:");  
        scanf(" %[^\n]", item.interpretation); 
        insertTable(hashTable, item);  
    }  
    char name[50];
    printf("请输入要查找的诗名:");  
    scanf(" %s", name);  
    searchTable(hashTable, name);  
    double a;
	a=1.0*num/tableSize;
	printf("%.2f",a);   
    free(hashTable->Poretry);  
    free(hashTable);  
      
    return 0;  
}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值