C言語辞書で辞書機能を実現するC言語辞書

5834 ワード

#include 
#include 
#include 

#define MAXWORD 25
#define MAXMEAN 50

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 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;
    /*     ,   :  、  、  、  、       */
    enum {EXIT, ADD, SEARCH, EDIT, DEL, DISP} function = DISP;

    /*     */
    dictionary = (struct lnode *)malloc(sizeof(struct lnode));
    if(dictionary != NULL) {
        dictionary->next = NULL; //   
    }

    while(function != EXIT) {
        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 Add(struct lnode *list)
{
    int i;
    struct record t;
    struct lnode *n, *r;

    /*      */
    printf("Please input the word: ");
    getchar();
    gets(t.word);
    fflush(stdin);
    printf("Please input the meaning:");
    gets( t.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);
        }
    } else {
        printf("Record Existed!
"); DisplayTableHead(); DisplayRecord(r); } } /* */ void Edit(struct lnode *list) { struct record t; struct lnode *r, *p; char e[MAXWORD]; p = list; printf("Please input the word you want to edit: "); getchar(); gets(e); if((r = SearchPrimarykey(list, 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!
"); } } /* */ void Search(struct lnode *list) { char e[MAXWORD]; struct lnode *r; printf("Please input the word you want to search: "); getchar(); gets(e); if((r = SearchPrimarykey(list, e)) != NULL) { DisplayTableHead(); DisplayRecord(r); } else { printf("Cann't find the word."); } } /* */ void Delete(struct lnode *list) { char e[MAXWORD]; struct lnode *q, *p; q = list; p = list->next; printf("Please input the word you want to delete: "); getchar(); gets(e); 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 = list->next; printf("
--------- ReaderMessage Display ---------
"); DisplayTableHead(); while(p != NULL) { DisplayRecord(p); c++; /* */ p = p->next; } printf("
--------- Total: %d Record(s) ---------
",c); } /* */ struct lnode *SearchPrimarykey(struct lnode *list, char *key) { struct lnode *p = list->next; 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 = list; while(p->next != NULL) { p = p->next; free(list); list = p; } free(p); } /* */ void DisplayTableHead(void) { printf("%-10s %s
","WORD","MEANING"); } /* */ void DisplayRecord(struct lnode *r) { printf("%-10s %s
", (r->data).word, (r->data).mean); } /* */ void DisplayMenu(void) { printf("
--------- ReaderMessage Menu ---------
"); printf("
\t1.Add
\t2.Search
\t3.Edit
\t4.Del
\t5.Display
\t0.Exit
"); printf("
Please select the function number(0-5):"); }
//    segmentfault  neebla   ,        ,        !    :https://segmentfault.com/q/1010000007742436