c语言做一个简单的 C 语言电子词典
要用c语言的结构体和链表 (散列表)实现一个能够快速查阅 C 语言术语含义及用法的电
子词典。
程序中用到的结构体
该电子词典中的一个单词及其含义用结构体 node 来表示:
typedef struct node
{
// 存储在结点中的值
char word[WORD_LENGTH + 1];
char detail[DETAIL_LENGTH + 1];
// 链表中链接到下一个结点的指针
struct node* next;
}
node;
整个电子词典用结构体 hash_table(即:散列表)来表示:
typedef struct
{
// 单向链表数组,HASH_TABLE_SIZE 为哈希表的桶数
node*values[HASH_TABLE_SIZE];
// 词典大小
unsigned int dict_size;
}
hash_table;
实现哈希表结构
#include
#include
#include
#include
#include "include/hasht.h"
/************************************************************************
* 函数名:hash()
* 功能:哈希函数,计算一个词语的哈希值
* 目的:掌握字符型指针作为参数的传递
************************************************************************/
unsigned long hash(char *word)
{
int hash_val=0,c;
while((c=*word++)!=0){
if(c<0) c=-c; //汉字值为负数,需要转换为正数
hash_val += c;
}
return hash_val; //HASH_TABLE_SIZE为散列表的桶数
}
/************************************************************************
* 函数名:hasht_init()
* 功能:初始化哈希表,初始状态词典中没有词语,字典大小为0,并返回true
************************************************************************/
bool hasht_init(hash_table *tbl)
{
tbl->dict_size = 0;
return true;
}
/************************************************************************
* 函数名:hasht_free()
* 功能:释放哈希表,即:释放哈希表所链接的所有链表,并返回true
* 通过调用sll.c文件中实现的node_free()函数释放实参所指向的链表
************************************************************************/
bool hasht_free(hash_table *tbl)
{
// 请将代码填写在此处
return true;
}
* 函数名:hasht_append()
* 功能:增加一个结点(字符串、含义)到哈希表中
* 即:根据字符串的散列值,将该字符串和它的含义构造成一个结点,
* 加入到散列表中的相应链表
* 通过调用sll.c文件中实现的node_add()函数,将结点加入链表中
bool hasht_append(hash_table *tbl, char *word, char *detail)
{
// 请将代码填写在此处
return true;
}
/************************************************************************
* 函数名:hasht_lookup()
* 功能:在哈希表中查找字符串
* 先计算单词的哈希值,然后调用sll.c中实现的node_lookup()函数
* 如果在其中返回true,否则返回false
************************************************************************/
bool hasht_lookup(hash_table *tbl, char *word, char *detail)
{
// 请将代码填写在此处
return true;
}
* File:dictionary.c
* ------------------------------
* 实现电子词典的相关函数
#include
#include //C99新增bool类型
#include
#include "include/dictionary.h"
// 定义用于存储词典的哈希表
hash_table dict;
/************************************************************************
* 函数名:check()
* 功能:检查词语是否在词典中,如果在词典中返回true,否则返回false
* 目的:掌握字符型指针作为参数的传递
************************************************************************/
bool check(char* word, char *detail)
{
return hasht_lookup(&dict, word, detail);
}
/************************************************************************
* 函数名:load()
* 功能:加载字典到内存中,如果成功返回true,否则返回false
* 目的:了解磁盘文件加载到内存的方法和过程
************************************************************************/
bool load(char* dictionary)
{
// 打开字典文件
FILE *f = fopen(dictionary, "r");
if (f == NULL)
return false;
// 初始化hash表
hasht_init(&dict);
//从字典中加载
char buffer1[WORD_LENGTH + 2];
char buffer2[DETAIL_LENGTH + 2];
while (fgets(buffer1, WORD_LENGTH + 2, f) && fgets(buffer2, DETAIL_LENGTH + 2, f))
{
// Overwrite \n with \0
buffer1[strlen(buffer1) - 1] = '\0';
buffer2[strlen(buffer2) - 1] = '\0';
// Append the word to the hash table
hasht_append(&dict, buffer1, buffer2);
}
fclose(f);
return true;
}
/************************************************************************
* 函数名:size()
* 功能:返回字典中的单词数,如果字典还没有加载则返回0
************************************************************************/
unsigned int size()
{
return dict.dict_size;
}
/************************************************************************
* 函数名:unload()
* 功能:卸载内存中的字典,成功返回ture,否则返回false
************************************************************************/
bool unload()
{
return hasht_free(&dict);
}
/************************************************************************
* 函数名:search()
* 功能:从词典中查询关键词的含义
************************************************************************/
void search()
{
char word[WORD_LENGTH]="\0", detail[DETAIL_LENGTH]="\0";
// 请将代码填写在此处
}
0个回答
精华知识