Chinaunix首页 | 论坛 | 博客
  • 博客访问: 476605
  • 博文数量: 112
  • 博客积分: 2436
  • 博客等级: 大尉
  • 技术积分: 2769
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 19:48
文章分类

全部博文(112)

文章存档

2013年(7)

2012年(105)

分类: C/C++

2012-03-22 16:38:11


点击(此处)折叠或打开

  1. //dictionary.c

  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>

  6. #define WORD_LEN    128
  7. #define EX_LEN        1024

  8. typedef struct word
  9. {
  10.     char word[50];
  11.     long loc;
  12.     struct word *next;
  13. }node_t;

  14. int menu(void)
  15. {
  16.     int choice;

  17.     printf("1.search for a word\n2.add a word\n9.quit\nPlease make a choice:");
  18.     scanf("%d", &choice);
  19.     getchar();

  20.     return choice;
  21. }

  22. node_t *make_node(char *w, long loc)
  23. {
  24.     node_t *cur;
  25.     cur = malloc(sizeof(node_t));
  26.     assert(cur);

  27.     strcpy(cur->word, w);
  28.     cur->loc = loc;
  29.     cur->next = NULL;
  30.     
  31.     return cur;
  32. }

  33. node_t *insert_to_link(node_t *head, char *w, long loc)
  34. {
  35.     node_t *cur = make_node(w, loc);

  36.     if(head == NULL)
  37.         head = cur;
  38.     else
  39.         cur->next = head;

  40.     return cur;
  41. }

  42. node_t *create_link(void)
  43. {
  44.     FILE *fp;
  45.     char ch, w[WORD_LEN];
  46.     node_t *head = NULL;
  47.     long pos = 0;

  48.     fp = fopen("dict.txt", "r+");
  49.     assert(fp);

  50.     while((ch = getc(fp)) != EOF)
  51.     {
  52.         if(ch == '#')
  53.         {
  54.             pos = ftell(fp);

  55.             fgets(w, WORD_LEN-1, fp);
  56.             head = insert_to_link(head, w, pos);
  57.         }
  58.     }
  59.     fclose(fp);
  60.     return head;
  61. }

  62. void print_exp(char *exp)
  63. {
  64.     char *token, *savep;
  65.     int i;

  66.     exp += 6;
  67.     for(i = 1; ; exp = NULL, i ++)
  68.     {
  69.         token = strtok_r(exp, "@", &savep);

  70.         if(! token)
  71.             break;
  72.         printf("%d : %s\n", i, token);
  73.     }
  74. }

  75. void get_explanation(long loc)
  76. {
  77.     FILE *fp;
  78.     char buffer[1024];

  79.     fp = fopen("dict.txt", "r");
  80.     assert(fp);

  81.     fseek(fp, loc, SEEK_SET);
  82.     fgets(buffer, WORD_LEN, fp);
  83.     fgets(buffer, EX_LEN, fp);

  84.     print_exp(buffer);

  85.     fclose(fp);
  86. }

  87. int search3(node_t *head, char *w)
  88. {
  89.     node_t *cur;
  90.     
  91.     for(cur = head; cur != NULL; cur = cur->next)
  92.     {
  93.         if(strcmp(cur->word, w) == 0)
  94.         {
  95.             get_explanation(cur->loc);    
  96.             return 0;
  97.         }
  98.     }

  99.     return -1;
  100. }

  101. void print_similar(node_t *head, char *w)
  102. {
  103.     int i = 1;
  104.     char buffer[20];
  105.     node_t *cur;

  106.     for(cur = head; cur != NULL; cur = cur->next)
  107.     {
  108.         if(strncmp(cur->word, w, strlen(w) - 1) == 0)
  109.         {
  110.             printf("%s", cur->word);
  111.             if(((i++)%20) == 0)
  112.             {
  113.                 printf("press enter to continue...(press 'q' to quit):");
  114.                 fgets(buffer, 19, stdin);

  115.                 if(buffer[0] == 'q')
  116.                     break;
  117.             }
  118.         }
  119.     }
  120. }

  121. void search_for_word(node_t *head)
  122. {
  123.     char word[WORD_LEN];

  124.     while(1)
  125.     {
  126.         printf("Please input the word:(input 1 to quit): ");
  127.         fgets(word, WORD_LEN-1, stdin);        
  128.         if(word[0] == '1')
  129.             break;

  130.         if(word[strlen(word) - 2] == '*')
  131.         {
  132.             word[strlen(word) - 2] = '\n';
  133.             word[strlen(word) - 1] = '\0';
  134.             print_similar(head, word);
  135.         }
  136.         else
  137.         {
  138.             if(search3(head, word) < 0)
  139.                 print_similar(head, word);
  140.         }            
  141.     }        
  142. }

  143. int get_info(char *w, char *exp)
  144. {
  145.     char exp_buf[1024];
  146.     w[0] = '#';
  147.     strcpy(exp, "Trans:");

  148.     printf("please input word:");
  149.     fgets(w+1, WORD_LEN, stdin);
  150.     int i = 1;
  151.     while(1)
  152.     {
  153.         printf("input explanation %d(input 1 to quit): ", i++);
  154.         fgets(exp_buf,EX_LEN, stdin);

  155.         if(*exp_buf == '1')
  156.             break;

  157.         if(i > 2)
  158.         {
  159.             exp[strlen(exp)-1] = '@';
  160.         }            
  161.         strcat(exp, exp_buf);
  162.     }
  163.     return 1;
  164. }

  165. void move_to_file(long location, char *w, char *exp)
  166. {
  167.     FILE *fp, *fp_tmp;
  168.     char ch;
  169.     long loc;
  170.     
  171.     //对fp文件操作
  172.     fp= fopen("dict.txt", "r+");
  173.     assert(fp);
  174.     
  175.     fseek(fp, location, SEEK_SET);//定位到应该插入的前面一个单词处
  176.     while((ch = getc(fp)) != EOF)
  177.         if(ch == '#')
  178.             break;
  179.     loc = ftell(fp) - 1; //找到应该插入的位置

  180.     //对fp_tmp文件操作
  181.     fp_tmp= fopen("tmp.txt", "w+");
  182.     assert(fp_tmp);

  183.     fwrite(w, 1, strlen(w), fp_tmp); //将新添加单词和译文 写入临时文件fp_tmp中
  184.     fwrite(exp, 1, strlen(exp), fp_tmp);
  185.     fseek(fp, loc, SEEK_SET);
  186.     while((ch = getc(fp)) != EOF) //将文件fp中的内容,读入到临时文件fp_tmp中
  187.         putc(ch, fp_tmp);

  188.     //把临时文件中的内容,写回到fp文件中
  189.     rewind(fp_tmp);
  190.     fseek(fp, loc, SEEK_SET);
  191.     while((ch = getc(fp_tmp)) != EOF)
  192.         putc(ch, fp);

  193.     fclose(fp);
  194.     fclose(fp_tmp);
  195. }

  196. node_t *update_link(node_t *head)
  197. {
  198.     node_t *cur, *nextp;

  199.     for(cur = head; cur != NULL; cur = nextp)
  200.     {
  201.         nextp = cur->next;
  202.         free(cur);
  203.     }
  204.     cur = create_link();
  205.     return cur;
  206. }

  207. node_t *insert_to_file(node_t *head, char *w, char *exp)
  208. {
  209.     node_t *cur;

  210.     for(cur = head; cur != NULL; cur = cur->next)
  211.     {
  212.        if(strcmp(cur->word, w + 1) < 0)
  213.        {
  214.             move_to_file(cur->loc, w, exp);
  215.             head = update_link(head);
  216.             break;
  217.        }
  218.     }
  219.     return head;
  220. }


  221. node_t *add_word(node_t *head)
  222. {
  223.     char w[WORD_LEN];
  224.     char exp[EX_LEN];

  225.     if(get_info(w, exp) > 0)
  226.         head = insert_to_file(head, w, exp);

  227.     return head;
  228. }


  229. int main(void)
  230. {
  231.     int choice;
  232.     node_t *head = NULL;

  233.     head = create_link();
  234.     while(1)
  235.     {
  236.         choice = menu();
  237.         if(choice == 1)
  238.         {
  239.             search_for_word(head);
  240.         }
  241.         else if(choice == 2)
  242.         {
  243.             head = add_word(head);
  244.         }
  245.         else if(choice == 9)
  246.         {
  247.             break;
  248.         }
  249.     }
  250.     return 0;
  251. }

阅读(1309) | 评论(0) | 转发(2) |
0

上一篇:二叉树 _0313

下一篇:const关键字

给主人留下些什么吧!~~