c语言 电子词典程序设计,c语言做一个简单的 C 语言电子词典_数据库_编程_电脑软件_天涯问答_天涯社区...

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个回答

67b0a70febe552c922c54bb8560d6ef6.png

精华知识

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值