數據結構課程設計——英漢詞典

按首字母將帶漢語意思的英語單詞分爲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):");
}
發佈了95 篇原創文章 · 獲贊 36 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章