zoukankan      html  css  js  c++  java
  • 双向循环链表(C语言描述)(五)

    代码清单

      1 // dictionary.h
      2 #ifndef __DICTIONARY_H__
      3 #define __DICTIONARY_H__
      4 
      5 #include <assert.h>
      6 #include <stdio.h>
      7 #include <stdio_ext.h>
      8 
      9 #include "mystring.h"
     10 #include "linkedlist.h"
     11 
     12 void dict_init();
     13 void dict_show();
     14 
     15 #endif // __DICTIONARY_H__
     16 
     17 // dictionary.c
     18 #include "dictionary.h"
     19 #define PATH "dictionary.dat"
     20 
     21 LinkedList list;
     22 static void dict_load();
     23 static void dict_store();
     24 void dict_search(const char * eng);
     25 void dict_add(const char * eng);
     26 void dict_delete();
     27 void dict_modify();
     28 int dict_cmp(const void * s1, const void * s2);
     29 
     30 void dict_init() {
     31     list = linkedlist_new();
     32 
     33     dict_load();
     34     printf("Welcome.");
     35 }
     36 
     37 void dict_show() {
     38     while (1) {
     39         string str;
     40         printf("
    >");
     41         mygets(str);
     42 
     43         if (!strcmp(str, "quit;")) {
     44             dict_store();
     45             linkedlist_destory(&list);
     46             printf("Bye.
    ");
     47             return;
     48         } else if (!strcmp(str, "delete;")) {
     49             dict_delete();
     50         } else if (!strcmp(str, "modify;")) {
     51             dict_modify();
     52         } else {
     53             dict_search(str);
     54         }
     55     }
     56 }
     57 
     58 static void dict_load() {
     59     FILE * fp;
     60     struct Word word;
     61 
     62     while (!(fp = fopen(PATH, "rb"))) {
     63         fp = fopen(PATH, "wb");
     64         fclose(fp);
     65     }
     66     assert(fp);
     67 
     68     fread(&word, sizeof(struct Word), 1, fp);
     69     while (!feof(fp)) {
     70         linkedlist_insert(list, TRAVELDIR_BACKWARD, 1, word);
     71         fread(&word, sizeof(struct Word), 1, fp);
     72     }
     73 
     74     fclose(fp);
     75 }
     76 
     77 static void dict_store() {
     78     FILE * fp;
     79     const int count = linkedlist_length(list);
     80 
     81     assert(fp = fopen(PATH, "wb"));
     82     for (int i = 0; i < count; i++) {
     83         fwrite(linkedlist_get(list, TRAVELDIR_FORWARD, i + 1),
     84                 sizeof(struct Word), 1, fp);
     85     }
     86 
     87     fclose(fp);
     88 }
     89 
     90 int dict_cmp(const void * s1, const void * s2) {
     91     return strcmp(((LinkedListData *) s1)->eng, ((LinkedListData *) s2)->eng);
     92 }
     93 
     94 void dict_search(const char * eng) {
     95     int location;
     96     struct Word word;
     97     strcpy(word.eng, eng);
     98 
     99     if ((location = linkedlist_locate(list, TRAVELDIR_FORWARD, word, dict_cmp))
    100             == -1) {    // not found
    101         dict_add(eng);
    102     } else {            // found
    103         printf("%s
    ", linkedlist_get(list, TRAVELDIR_FORWARD, location)->chn);
    104     }
    105 }
    106 
    107 void dict_add(const char * eng) {
    108     struct Word word;
    109     strcpy(word.eng, eng);
    110 
    111     printf("The word does not exist, add it?
    y/n>");
    112     if (__fpurge(stdin), getchar() == 'y') {
    113         printf("Ok, what does it mean?
    >");
    114         mygets(word.chn);
    115 
    116         linkedlist_insert(list, TRAVELDIR_BACKWARD, 1, word);
    117         printf("The word is existed now.
    ");
    118     }
    119 }
    120 
    121 void dict_delete() {
    122     int location;
    123     struct Word word;
    124 
    125     printf("What word do you wanna delete?
    >");
    126     mygets(word.eng);
    127 
    128     if ((location = linkedlist_locate(list, TRAVELDIR_FORWARD, word, dict_cmp))
    129             != -1) {    // found
    130         struct Word * pWord = linkedlist_get(list, TRAVELDIR_FORWARD, location);
    131 
    132         printf("Delete: %s %s
    Are you sure?
    y/n>", pWord->eng, pWord->chn);
    133         if (__fpurge(stdin), getchar() == 'y') {
    134             linkedlist_delete(list, TRAVELDIR_FORWARD, location);
    135             printf("The word is deleted now.
    ");
    136         }
    137     } else {            // not found
    138         printf("The word does not exist.
    ");
    139     }
    140 }
    141 
    142 void dict_modify() {
    143     int location;
    144     struct Word word;
    145 
    146     printf("What word do you wanna modify?
    >");
    147     mygets(word.eng);
    148 
    149     if ((location = linkedlist_locate(list, TRAVELDIR_FORWARD, word, dict_cmp))
    150             != -1) {    // found
    151         struct Word * pWord = linkedlist_get(list, TRAVELDIR_FORWARD, location);
    152 
    153         printf("Ok, what does it mean?
    >");
    154         mygets(pWord->chn);
    155         printf("The word is modified now.
    ");
    156     } else {            // not found
    157         printf("The word does not exist.
    ");
    158     }
    159 }
    160 
    161 // mystring.h
    162 #ifndef __MYSTRING_H__
    163 #define __MYSTRING_H__
    164 
    165 #include <stdio.h>
    166 #include <stdio_ext.h>
    167 
    168 #define MAX_STR_LEN 8
    169 typedef char string[MAX_STR_LEN];
    170 
    171 void mygets(char * s);
    172 
    173 #endif // __MYSTRING_H__
    174 
    175 // mystring.c
    176 #include "mystring.h"
    177 
    178 void mygets(char * s)
    179 {
    180     __fpurge(stdin);
    181     fgets(s, MAX_STR_LEN, stdin);
    182     while (*s++) {
    183         *s = *s == '
    ' ? 0 : *s;
    184     }
    185 }
    186 
    187 // main.c
    188 #include "dictionary.h"
    189 
    190 int main()
    191 {
    192     dict_init();
    193     dict_show();
    194     
    195     return 0;
    196 }
  • 相关阅读:
    python之xlwt模块列宽width、行高Heights详解
    Testlink在CentOS、windows安装
    Appium中长按按钮操作
    CentOS oracle Client客户端安装
    WebDriver中自动识别验证码--Python实现
    shell批量重命令文件脚本
    MFC 显示图片
    MFC CEdit控件 自动换行
    第一次社会
    undefined reference 问题各种情况分析
  • 原文地址:https://www.cnblogs.com/lets-blu/p/7260010.html
Copyright © 2011-2022 走看看