作业帮 > 综合 > 作业

使用Hash表实现英文单词表并实现单词查询操作:

来源:学生作业帮 编辑:搜搜做题作业网作业帮 分类:综合作业 时间:2024/07/06 13:21:33
使用Hash表实现英文单词表并实现单词查询操作:
给定单词查询其英文翻译:
(1) 构造用于存放英文单词表的散列表结构:定义Hash函数;使
用开散列法处理冲突;
(2) 英文单词表在附件文本中,从文件读入,一行一条.每一行
前半部分为英文单词本身,后半部分为中文翻译,两部分之间用tab字
符隔开(’\t’).注意英文单词中可能有空格;
提供用户单词查询操作:根据输入的英文单词,给出对应的中文翻译.
操作平台:VC6.0
英文单词表的txt文件格式如下:
Swimming 游泳
swimming pool 游泳池
changing room 更衣室
shower 淋浴
spaning platform 跳台
ten-meter platform 10米跳台
five-meter platform 5米跳台
three-meter springboard 3米跳板
one-meter springboard 1米跳板
spaning pool 跳水池
non-swimmer's pool 浅水池
swimmer's pool 深水池
starting block 出发台
starting spane 出发起跳
rope with cork floats 水线
swimming lane 泳道
touching the finishing line 终点触线
timekeeper 计时员
lifesaver 救生员
land drill 陆上练习
breaststroke 蛙泳
crawl stroke 爬泳
back stroke 仰泳
side stroke 侧泳
butterfly stroke 蝶泳
dolphin butterfly stroke 海豚式蝶泳
treading water 踩水
underwater swimming 潜泳
swimming pool with artificial waves 人工海浪泳池
artificial waves 人工海浪
swimming trunks 泳裤
swimsuit 泳衣
swimming cap 泳帽
bikini 比基尼泳衣
bikini bottom 比基尼式泳裤
surfing 冲浪
surfboard 冲浪板
water ski 滑水橇
请帮忙写出完整的代码,这个对我来说实在太复杂了,所以只能找高人求教了.
使用Hash表实现英文单词表并实现单词查询操作:
#include
#include
#include
#define N 100//散列表长度
struct Node
{
char* key; char* val;
Node* next;
}*heads[N];//散列表,用链处理冲突
int hash(char* key)//散列函数
{
unsigned long h=0;
while(*key)
{
h=(h24;
h&=~g;
}
return h&N;
}
Node* find(char* key)//查找
{
Node* cur=heads[hash(key)];
while(cur)
{
if(!strcmp(cur->key,key))
return cur;
cur=cur->next;
}
return NULL;
}
void insert(char* key,char* val)//插入
{
int i=hash(key);
Node* head=heads[i];
if(find(key)==NULL)
{
Node* tmp=(Node*)malloc(sizeof(Node));
tmp->key=(char*)malloc(strlen(key)+1);
tmp->val=(char*)malloc(strlen(val)+1);
strcpy(tmp->key,key);
strcpy(tmp->val,val);
tmp->next=head;
heads[i]=tmp;
}
}
main()
{
char tmp[100],*key,*val;
Node* cur;
FILE *fp=fopen("abc.txt","r");
if(fp==NULL)
{
printf("打开文件有问题\n");
exit(1);
}
while(fgets(tmp,100,fp)!=NULL)
{
if(tmp[strlen(tmp)-1]=='\n')
tmp[strlen(tmp)-1]='\0';
key=strtok(tmp,"\t");
val=strtok(NULL,"\t");
insert(key,val);
}
printf("输入你要查找的单词:\n");
while(gets(tmp))
{
cur=find(tmp);
if(cur==NULL)
printf("没找到\n");
else
printf("%s\n",cur->val);
}
}