欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-04 12:07:42
...

按首字母将带汉语意思的英语单词分为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):");
}
相关标签: 数据结构