• 控制台电子词典---链表


    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <assert.h>

    typedef struct dict
    {
     char* key;
     char* content;
     struct dict* next;
    }dict;

    //打开字典文件  读入内存  注意这里是二级指针
    int open_dict(dict** p, const char* dict_filename)
    {
     FILE* pfile = fopen(dict_filename, "r");
     if (pfile == NULL)
      return 0;

     char buf[2048] = {0};
     size_t len = 0;
     int i = 0;

     *p = (dict*)malloc(sizeof(dict));
     assert(*p);
     memset(*p, 0, sizeof(dict));

     dict* pD = *p;
     while (!feof(pfile))
     {
      memset(buf, 0, sizeof(buf));
      fgets(buf, sizeof(buf), pfile);
      len = strlen(buf)+1;
      if (len > 0)
      {
       pD->key = (char*)malloc(len);
       assert(pD->key);
       memset(pD->key, 0, len);
       strcpy(pD->key, &buf[1]);
      }
      memset(buf, 0, sizeof(buf));
      fgets(buf, sizeof(buf), pfile);
      len = strlen(buf)+1;
      if (len > 0)
      {
       pD->content = (char*)malloc(len);
       assert(pD->content);
       memset(pD->content, 0, len);
       strcpy(pD->content, &buf[6]);
      }
      pD->next = (dict*)malloc(sizeof(dict));
      assert(pD->next);
      memset(pD->next, 0, sizeof(dict));
      pD = pD->next;
      i++;

     }
     fclose(pfile);
     return i;
    }
    //查找单词
    int search_dict(const dict* p, const char* key, char* content)
    {
     const dict* pD = p;
     while (pD)
     {
      if ((pD->key) && (pD->content))
      {
       if (strncmp(pD->key, key,strlen(key)) == 0)
       {
        //printf("%s: %s   %s---%d", pD->key,key, __FILE__, __LINE__);
        strcpy(content, pD->content);  //资源  目标不能搞反
        return 1;
       }
        

      }
      pD = pD->next;
      
     }
     return 0;
    }
    //释放内存
    void free_dict(dict* p, int size)
    {
     dict* pD = p;
     dict* qD=NULL;
     while(pD)
     {
      if (pD->key)
       free(pD->key);
      if (pD->content)
       free(pD->content);
      qD = pD->next;
      free(pD);
      pD = qD;
     }
     

    }
    int main(int argc, char** argv)
    {
     if (argc < 2)
     {
      printf("uage %s dict-filename ", argv[0]);
      return 0;
     }
     long start_ms = 0;
     long end_ms = 0;
     dict* p = NULL;
     start_ms = clock();
     int size = open_dict(&p, argv[1]);
     if (size == 0)
      return 0;
     printf("get_world:%d ", size);
     end_ms = clock();
     printf("open_dict used: %ld ms ", end_ms - start_ms);
     char key[2048];
     char content[2048];
     while (1)
     {
      memset(key, 0, sizeof(key));
      memset(content, 0, sizeof(content));
      scanf("%s", key);
      if (strncmp(key, "command=exit", 12) == 0)
       break;
      start_ms = clock();
      if (search_dict(p, key, content))
      {
       printf("%s ", content);
      }
      else
      {
       printf("not find ");
      }
      end_ms = clock();
      printf("search dict used %ld ms ", end_ms - start_ms);
     }
     start_ms = clock();
     free_dict(p, size);
     end_ms = clock();
     printf("free dict used %ld ms ", end_ms - start_ms);
     return 0;
    }

  • 相关阅读:
    .NET基础 一步步 一幕幕[循环、逻辑语句块]
    .NET 基础 一步步 一幕幕 [注释、命名规则、访问修饰符、数据类型、常量、变量]
    .NET 基础 一步步 一幕幕 [.NET 系列预热]
    .NET 基础 一步步 一幕幕 [前言]
    前端面试题五
    前端面试题四
    ActiveMQ 的客户端选项
    ActiveMq 高级特性的使用
    企业环境中部署 ActiveMQ
    在其他平台上使用 ActiveMQ
  • 原文地址:https://www.cnblogs.com/countryboy666/p/11027117.html
Copyright © 2020-2023  润新知