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

按首字母将带汉语意思的英语单词分为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 InsertList(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    b.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)
        {
        case ADD:
            Add(dictionary);
            break;
        case SEARCH:
            Search(dictionary);
            break;
        case EDIT:
            Edit(dictionary);
            break;
        case DEL:
            Delete(dictionary);
            break;
        case DISP:
            Display(dictionary);
            break;
        case EXIT:
            exit(0);
            break;
        default:
            printf("Input Error! Please 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);
        strcpy(t.mean,mean);
        /* 判断记录是否已存在,若存在则显示记录,若不存在则添加记录 */
        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);
                //* 插入链表
                InsertList(list, n);
            }
        }
    }
    //printf("%d\n",h);
    fclose(fp);
}

/* 添加 */
void Add(struct lnode *list[])
{
    int i;
    struct record t;
    struct lnode *n, *r;
    struct lnode *onelist;
    /* 录入记录 */
    printf("Please input the word: ");
    getchar();
    gets(t.word);
    onelist=*(list+t.word[0]-97);//通过单词首字母找到单词所属于的链表, a是97
    fflush(stdin);
    printf("Please input the meaning:");
    gets( t.mean);

    /* 判断记录是否已存在,若存在则显示记录,若不存在则添加记录 */
    if((r = SearchPrimarykey(onelist, 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);
            /* 插入链表 */
            InsertList(onelist, n);
        }
    }
    else
    {
        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("Please 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("Please edit the word: ");
        gets(t.word);
        printf("Please edit the meaning:");
        gets(t.mean);

        /* 复制记录 */
        strcpy((r->data).word,t.word);
        strcpy((r->data).mean,t.mean);


    }
    else
    {
        printf("Record cann't find!\n");
    }
}
/* 查找 */
void Search(struct lnode *list[])
{
    char e[MAXWORD];
    struct lnode *r;
    struct lnode *onelist;
    printf("Please input the word you want to search: ");
    getchar();
    gets(e);
    onelist = *(list+(e[0]-97));
    if((r = SearchPrimarykey(onelist, e)) != NULL) //SearchPrimarykey 会返回一个p指针或者null   ,返回p 说明查找成功,返回null 说明查找失败,单词不存在
    {
        DisplayTableHead();
        DisplayRecord(r);
    }
    else
    {
        printf("Cann't find the word.");
    }
}

/* 删除 */
void Delete(struct lnode *list[])
{
    char e[MAXWORD];
    struct lnode *q, *p;
    struct lnode *onelist;

    printf("Please input the word you want to delete: ");
    getchar();
    gets(e);
    onelist = *(list+e[0]-97);
    q = onelist;
    p = onelist->next;
    while(p != NULL)
    {
        if(strcmp((p->data).word, e) == 0)
        {
            q->next = p->next;
            free(p);    /* 释放空间 */
            return ;    /* 函数返回 */
        }
        q = p;
        p = p->next;
    }
}

/* 显示所有记录 */
void Display(struct lnode *list[])
{
    int c = 0;
    struct lnode *p;
    char charr[2];//存放a-z字符

    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是当前单词首字母那个链表,不是全部的
{
    struct lnode *p = list->next;   //p指向一个节点,通过比较看一下这个节点是不是要查的单词,如果是,把p指向的节点返回。

    while (p != NULL)
    {
        if(strcmp((p->data).word, key) == 0)
        {
            return p;
        }
        p = p->next;
    }
    return NULL;
}

/* 将记录按姓名字母升序插入链表 */
void InsertList(struct lnode *list, struct lnode *n)
{
    struct lnode *p = list;

    while (p->next != NULL && strcmp((p->next->data).word, (n->data).word) < 0)
    {
        p = p->next;
    }
    n->next = p->next;
    p->next = n;
}

/* 释放整个链表空间 */
void FreeList(struct lnode *list[])
{
    struct lnode *p,*onelist;
    for(int i=0; i<26; i++)
    {
        onelist= *(list+i);
        p=onelist;
        while(p->next != NULL)
        {
            p = p->next;
            free(onelist);
            onelist = p;
        }
        free(p);
    }

}

/* 显示表头 */
void DisplayTableHead(void)
{
    printf("%-14s %s\n","WORD","MEANING");
}

/* 显示一条记录 */
void DisplayRecord(struct lnode *r)
{
    printf("%-12s %s\n", (r->data).word, (r->data).mean);
}

/* 显示菜单 */
void DisplayMenu(void)
{
    printf("\n--------- ReaderMessage Menu ---------\n");
    printf("\n\t1.Add\n\t2.Search\n\t3.Edit\n\t4.Del\n\t5.Display\n\t0.Exit\n");
    printf("\nPlease select the function number(0-5):");
}
  • 11
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
为了实现一个简单的电子英汉词典,我们可以采用C++语言来设计和实现。以下是一个简单的数据结构课程设计c++英汉词典的示例: 首先,我们需要定义一个结构体来存储每个单词的英文拼写和中文释义: ```c++ struct Word { string english; // 英文单词 string chinese; // 中文释义 }; ``` 然后,我们可以使用一个vector来存储所有的单词: ```c++ vector<Word> dictionary; ``` 接下来,我们可以实现以下功能: 1. 单词的添加 ```c++ void addWord(string english, string chinese) { Word word; word.english = english; word.chinese = chinese; dictionary.push_back(word); } ``` 2. 单词的显示 ```c++ void displayWords() { for (int i = 0; i < dictionary.size(); i++) { cout << dictionary[i].english << " " << dictionary[i].chinese << endl; } } ``` 3. 单词的查找 ```c++ void searchWord(string english) { for (int i = 0; i < dictionary.size(); i++) { if (dictionary[i].english == english) { cout << dictionary[i].english << " " << dictionary[i].chinese << endl; return; } } cout << "Word not found." << endl; } ``` 4. 单词的删除 ```c++ void deleteWord(string english) { for (int i = 0; i < dictionary.size(); i++) { if (dictionary[i].english == english) { dictionary.erase(dictionary.begin() + i); cout << "Word deleted." << endl; return; } } cout << "Word not found." << endl; } ``` 5. 单词的修改 ```c++ void modifyWord(string english, string chinese) { for (int i = 0; i < dictionary.size(); i++) { if (dictionary[i].english == english) { dictionary[i].chinese = chinese; cout << "Word modified." << endl; return; } } cout << "Word not found." << endl; } ``` 6. 单词的保存 ```c++ void saveWords() { ofstream outfile("dictionary.txt"); for (int i = 0; i < dictionary.size(); i++) { outfile << dictionary[i].english << " " << dictionary[i].chinese << endl; } outfile.close(); cout << "Words saved." << endl; } ``` 以上是一个简单的数据结构课程设计c++英汉词典的示例。当然,这只是一个基础的实现,你可以根据自己的需求进行扩展和优化。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值