数据结构课程设计——英汉词典

更新时间:2023-06-15 05:03:02 阅读: 评论:0

数据结构课程设计——英汉词典按⾸字母将带汉语意思的英语单词分为26个⽂本⽂件,每个⾸字母对应⼀个排序好的链表。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWORD 25
#define MAXMEAN 50
#define FILEN 100  //读取⽂件时⼀⾏数据的长度
struct record  //记录结构_读者
{
char word[MAXWORD+1];  //key
char mean[MAXMEAN+1];
};
struct lnode    //链表结点结构
{
struct record data;
struct lnode *next;
};
/* 函数声明 */
void Add(struct lnode *list[]);
void FileAdd(struct lnode *list,char *filename);
void Search(struct lnode *list[]);
void Edit(struct lnode *list[]);
void Delete(struct lnode *list[]);
void Display(struct lnode *list[]);
struct lnode *SearchPrimarykey(struct lnode *list, char *key);//*****************************
void InrtList(struct lnode *list, struct lnode *n);
void FreeList(struct lnode *list[]);
void DisplayTableHead(void);
void DisplayRecord(struct lnode *r);
void DisplayMenu(void);
/* 主程序 */
int main(int argc, char *argv[])
{
struct lnode *dictionary[26];
struct lnode *p;
char filename[20];//⽂件名字(⽤来读取a.txt    ⾥⾯的单词⽂件, *.txt  名字是需要改变的)
char charr[2];//存放a-z字符
/* 功能选择,依次为:退出、添加、查找、删除、显⽰所有记录 */
enum {EXIT, ADD, SEARCH, EDIT, DEL, DISP} function = DISP;
/* 头结点 */
for(int i=0; i<26; i++)
{
dictionary[i] = (struct lnode *)malloc(sizeof(struct lnode));
if(dictionary[i] != NULL)
{
dictionary[i]->next = NULL; //初始化
}
}
for(int i=0; i<26; i++)
{
char charr[2];
charr[0]=(char)(i+97);
charr[1]='\0';
strcpy(filename,"./dir/");
strcat(filename,charr);
strcat(filename,".txt\0");
//printf("%s\n",filename);
p=*(dictionary+i);
FileAdd(p,filename);
}
while(function != EXIT)
{
p=*dictionary;
DisplayMenu();
scanf("%d",&function);
while(function < EXIT || function > DISP)
{
scanf("%d",&function);
}
switch(function)
{
ca ADD:
Add(dictionary);
break;
ca SEARCH:
Search(dictionary);
break;
ca EDIT:
Edit(dictionary);
break;
ca DEL:
Delete(dictionary);
break;
ca DISP:
Display(dictionary);
break;
ca EXIT:
exit(0);
break;
default:
printf("Input Error! Plea input the right word.");
break;
}
}
FreeList(dictionary);
}
//从⽂件中添加单词
void FileAdd(struct lnode *list,char *filename)
{
struct record t;
struct lnode *n, *r;
//char filename[20];
FILE *fp;
char str[FILEN+1];
char word[30];
char mean[50];
int h = 0;
if( (fp=fopen(filename,"rt")) == NULL )
{
printf("Cannot open file, press any key to exit!\n");        getchar();
exit(1);
}
while(fgets(str, FILEN, fp) != NULL)
{
//printf("%s", str);
h++;
int i = 0;
int j = 0;
for(i; str[i]!=' '; i++)
{
word[i] = str[i];
}
word[i]='\0';
for(++i,j; str[i]!='\n'; i++,j++)
{
mean[j] = str[i];
}
mean[j] = '\0';
//cout<<"单词"<<word<<"有以下⼏种意思"<<mean<<endl;
strcpy(t.word,word);
an,mean);
/* 判断记录是否已存在,若存在则显⽰记录,若不存在则添加记录 */
calm什么意思
if((r = SearchPrimarykey(list, t.word)) == NULL)
{
// 申请lnode空间并初始化
n = (struct lnode *)malloc(sizeof(struct lnode));
if(n != NULL)
{
//* 复制记录
strcpy((n->data).word,t.word);
strcpy((n->data).mean, t.mean);
//* 插⼊链表
InrtList(list, n);
}
}
}
//printf("%d\n",h);
fclo(fp);
}
/* 添加 */
mp3电子书void Add(struct lnode *list[])
{
int i;
struct record t;
struct lnode *n, *r;
struct lnode *onelist;
/* 录⼊记录 */
printf("Plea input the word: ");
getchar();
gets(t.word);
onelist=*(list+t.word[0]-97);//通过单词⾸字母找到单词所属于的链表, a是97    fflush(stdin);
printf("Plea input the meaning:");
gets( t.mean);
/* 判断记录是否已存在,若存在则显⽰记录,若不存在则添加记录 */
if((r = SearchPrimarykey(onelist, t.word)) == NULL)
{
/* 申请lnode空间并初始化 */
n = (struct lnode *)malloc(sizeof(struct lnode));
if(n != NULL)
{
/* 复制记录 */
henman
strcpy((n->data).word,t.word);
strcpy((n->data).mean, t.mean);
/* 插⼊链表 */
InrtList(onelist, n);
}
}
el
{
printf("Record Existed!\n");
DisplayTableHead();
DisplayRecord(r);
}
}
/* 修改 */
void Edit(struct lnode *list[])
{
struct record t;
struct lnode *r, *p;
struct lnode *onelist;
char e[MAXWORD];
printf("Plea input the word you want to edit: ");
fflush(stdin);
//getchar();
gets(e);
onelist = *(list+(e[0]-97));
p = onelist;
if((r = SearchPrimarykey(onelist, e)) != NULL)
{
fflush(stdin);
printf("Plea edit the word: ");gerbe
gets(t.word);
printf("Plea edit the meaning:");
an);
/* 复制记录 */
strcpy((r->data).word,t.word);
premiumqualitystrcpy((r->data).an);
}
el
{
printf("Record cann't find!\n");
}
}
/* 查找 */
void Search(struct lnode *list[])
{
char e[MAXWORD];
struct lnode *r;
struct lnode *onelist;
printf("Plea input the word you want to arch: ");
getchar();
gets(e);
onelist = *(list+(e[0]-97));
if((r = SearchPrimarykey(onelist, e)) != NULL) //SearchPrimarykey 会返回⼀个p指针或者null  ,返回p 说明查找成功,返回null 说明查找失败,单词不存在    {
DisplayTableHead();
DisplayRecord(r);
}
el
{
printf("Cann't find the word.");
}
}
/* 删除 */
void Delete(struct lnode *list[])
{
char e[MAXWORD];
struct lnode *q, *p;
struct lnode *onelist;
printf("Plea input the word you want to delete: ");
getchar();
gets(e);
onelist = *(list+e[0]-97);
牛津小学英语3aq = onelist;
p = onelist->next;
while(p != NULL)
{
if(strcmp((p->data).word, e) == 0)
{
q->next = p->next;
free(p);    /* 释放空间 */
return ;    /* 函数返回 */
}
q = p;
enthusiasm形容词
p = p->next;
}
}
/* 显⽰所有记录 */
void Display(struct lnode *list[])
brazzaville
{
int c = 0;
struct lnode *p;
char charr[2];//存放a-z字符
otherwi什么意思
printf("\n--------请输⼊a--z中任意⼀个字符---------\n");
getchar();
gets(charr);
p = *(list+charr[0]-97);
p=p->next;
printf("\n--------- ReaderMessage Display ---------\n");
DisplayTableHead();
while(p != NULL)
{
DisplayRecord(p);
c++;    /* 记录条数 */
p = p->next;
}
printf("\n--------- Total:  %d  Record(s) ---------\n",c);
}
/* 按主键查找 */
struct lnode *SearchPrimarykey(struct lnode *list, char *key)//传进来的list是当前单词⾸字母那个链表,不是全部的references
{
struct lnode *p = list->next;  //p指向⼀个节点,通过⽐较看⼀下这个节点是不是要查的单词,如果是,把p指向的节点返回。
while (p != NULL)
{
if(strcmp((p->data).word, key) == 0)
{
return p;
}
p = p->next;
}
return NULL;

本文发布于:2023-06-15 05:03:02,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/957797.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:单词   记录   存在   返回   查找   字母   链表
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图