C/C++で辞書プログラムを実現する
9501 ワード
次の3段のコードがあって、実现の方式は基本的に同じで、第1の方式、辞书の行数は固定の第2の方式で、2回辞书を読んで、辞书の関数によってメモリの第3の方式を割り当てることができて、ダイナミックにメモリを分配して、毎回1行を読んで、メモリは私达がテストしたことをプラスして、第2の方式のスピードが最も速いことを発见します.
dict0.c
dict1.c
dict2.c
上のコンソールプロジェクトをQTプロジェクトに移植することもでき、粗末なグラフィックスインタフェースを実現することができます.
dict0.c
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#define MAX 111111 //
struct dict
{
char *key;
char *content;
};
// ,
int open_dict(struct dict **p, const char *dict_filename)
{
FILE *pfile = fopen(dict_filename, "r");
if (pfile == NULL)
return 0;// ,
*p = (struct dict *)malloc(sizeof(struct dict) * MAX);// MAX
memset(*p, 0, sizeof(struct dict) * MAX);// 0
char buf[1024] = { 0 };
size_t len = 0;
int i = 0;//
while (!feof(pfile))// ,
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);//
len = strlen(buf);//
if (len > 0)
{
(*p)[i].key = (char *)malloc(len);//
memset((*p)[i].key, 0, len);
strcpy((*p)[i].key, &buf[1]);// key
}
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
(*p)[i].content = (char *)malloc(len);
memset((*p)[i].content, 0, len);
strcpy((*p)[i].content, &buf[6]);
}
i++;// 1
}
fclose(pfile);//
return i;//
}
// key,
int search_dict(const struct dict *p, int size, const char *key, char *content)
{
int i = 0;
for (i = 0; i < size; i++)//
{
if ((p[i].key == NULL) || (p[i].content == NULL))
continue;
if (strncmp(p[i].key, key, strlen(key)) == 0)
{
strcpy(content, p[i].content);
return 1;// , 1
}
}
return 0;// , 0
}
//
void free_dict(struct dict *p, int size)
{
int i = 0;
for (i = 0; i < size; i++)// key content
{
if (p[i].key)
free(p[i].key);
if (p[i].content)
free(p[i].content);
}
free(p);// p
}
int main(int argc, char *args[])
{
if (argc < 2)
{
printf("usage: %s dict-filename
", args[0]);
return 0;// ,
}
long start_ms = 0;//
long end_ms = 0;//
struct dict *p = NULL;
start_ms = clock();
int dict_size = open_dict(&p, args[1]);// ,
if (dict_size == 0)
return 0;// ,
end_ms = clock();
printf("open_dict used %ld ms
", end_ms - start_ms);// , :
char key[1024];
char content[1024];
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, dict_size, key, content))// ,
{
printf("%s", content);
} else
{
printf("not found
");
}
end_ms = clock();
printf("search_dict used %ld ms
", end_ms - start_ms);// , :
}
start_ms = clock();
free_dict(p, dict_size);
end_ms = clock();
printf("free_dict used %ld ms
", end_ms - start_ms);// , :
return 0;
}
dict1.c
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
struct dict
{
char *key;
char *content;
};
int get_dict_size(FILE *pfile)//
{
if (pfile == NULL)
return 0;
int i = 0;
char buf[2048];
while (!feof(pfile))
{
fgets(buf, sizeof(buf), pfile);
fgets(buf, sizeof(buf), pfile);
i++;// , 1
}
return i;
}
// ,
int open_dict(struct dict **p, const char *dict_filename)
{
FILE *pfile = fopen(dict_filename, "r");
if (pfile == NULL)
return 0;// ,
int dict_size = get_dict_size(pfile);//
if (dict_size == 0)
return 0;
*p = (struct dict *)malloc(sizeof(struct dict) * dict_size);//
memset(*p, 0, sizeof(struct dict) * dict_size);// 0
char buf[2048] = { 0 };
size_t len = 0;
int i = 0;
fseek(pfile, 0L, SEEK_SET);//
while (!feof(pfile))// ,
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);//
len = strlen(buf);//
if (len > 0)
{
(*p)[i].key = (char *)malloc(len);//
memset((*p)[i].key, 0, len);
strcpy((*p)[i].key, &buf[1]);// key
}
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
(*p)[i].content = (char *)malloc(len);
memset((*p)[i].content, 0, len);
strcpy((*p)[i].content, &buf[6]);
}
i++;
}
fclose(pfile);//
return i;//
}
// key,
int search_dict(const struct dict *p, int size, const char *key, char *content)
{
int i = 0;
for (i = 0; i < size; i++)//
{
if ((p[i].key == NULL) || (p[i].content == NULL))
continue;
if (strncmp(p[i].key, key, strlen(key)) == 0)
{
strcpy(content, p[i].content);
return 1;// , 1
}
}
return 0;// , 0
}
//
void free_dict(struct dict *p, int size)
{
int i = 0;
for (i = 0; i < size; i++)// key content
{
if (p[i].key)
free(p[i].key);
if (p[i].content)
free(p[i].content);
}
free(p);// p
}
int main(int argc, char *args[])
{
if (argc < 2)
{
printf("usage: %s dict-filename
", args[0]);
return 0;// ,
}
long start_ms = 0;//
long end_ms = 0;//
struct dict *p = NULL;
start_ms = clock();
int dict_size = open_dict(&p, args[1]);// ,
if (dict_size == 0)
return 0;// ,
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, dict_size, key, content))// ,
{
printf("%s", content);
} else
{
printf("not found
");
}
end_ms = clock();
printf("search_dict used %ld ms
", end_ms - start_ms);// , :
}
start_ms = clock();
free_dict(p, dict_size);//
end_ms = clock();
printf("free_dict used %ld ms
", end_ms - start_ms);// , :
return 0;
}
dict2.c
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
struct dict
{
char *key;
char *content;
};
// ,
int open_dict(struct dict **p, const char *dict_filename)
{
FILE *pfile = fopen(dict_filename, "r");
if (pfile == NULL)
return 0;// ,
*p = (struct dict *)malloc(sizeof(struct dict));
memset(*p, 0, sizeof(struct dict));
char buf[2048] = { 0 };
size_t len = 0;
int i = 0;
while (!feof(pfile))// ,
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);//
len = strlen(buf);//
if (len > 0)
{
(*p)[i].key = (char *)malloc(len);//
memset((*p)[i].key, 0, len);
strcpy((*p)[i].key, &buf[1]);// key
}
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
(*p)[i].content = (char *)malloc(len);
memset((*p)[i].content, 0, len);
strcpy((*p)[i].content, &buf[6]);
}
i++;
*p = (struct dict *)realloc(*p, (1 + i) * sizeof(struct dict));
memset(*p + i, 0, sizeof(struct dict));
}
fclose(pfile);//
return i;//
}
// key,
int search_dict(const struct dict *p, int size, const char *key, char *content)
{
int i = 0;
for (i = 0; i < size; i++)//
{
if ((p[i].key == NULL) || (p[i].content == NULL))
continue;
if (strncmp(p[i].key, key, strlen(key)) == 0)
{
strcpy(content, p[i].content);
return 1;// , 1
}
}
return 0;// , 0
}
//
void free_dict(struct dict *p, int size)
{
int i = 0;
for (i = 0; i < size; i++)// key content
{
if (p[i].key)
free(p[i].key);
if (p[i].content)
free(p[i].content);
}
free(p);// p
}
int main(int argc, char *args[])
{
if (argc < 2)
{
printf("usage: %s dict-filename
", args[0]);
return 0;// ,
}
long start_ms = 0;//
long end_ms = 0;//
struct dict *p = NULL;
start_ms = clock();
int dict_size = open_dict(&p, args[1]);// ,
if (dict_size == 0)
return 0;// ,
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, dict_size, key, content))// ,
{
printf("%s", content);
} else
{
printf("not found
");
}
end_ms = clock();
printf("search_dict used %ld ms
", end_ms - start_ms);// , :
}
start_ms = clock();
free_dict(p, dict_size);//
end_ms = clock();
printf("free_dict used %ld ms
", end_ms - start_ms);// , :
return 0;
}
上のコンソールプロジェクトをQTプロジェクトに移植することもでき、粗末なグラフィックスインタフェースを実現することができます.