码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-06-15 13:54:00      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:ems   lib   电子词典   二级指针   fgets   secure   null   查找   sea   

#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\n", 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\n", size);
 end_ms = clock();
 printf("open_dict used: %ld ms\n", 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 \n", content);
  }
  else
  {
   printf("not find\n");
  }
  end_ms = clock();
  printf("search dict used %ld ms\n", end_ms - start_ms);
 }
 start_ms = clock();
 free_dict(p, size);
 end_ms = clock();
 printf("free dict used %ld ms\n", end_ms - start_ms);
 return 0;
}

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

标签:ems   lib   电子词典   二级指针   fgets   secure   null   查找   sea   

原文地址:https://www.cnblogs.com/countryboy666/p/11027117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!