需要1个字典文件。
命令行版
MFC版
QT版
1,命令行版 -- 【1】
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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 (p == NULL)
return 0;//打开文件失败,函数返回
*p = (struct dict *)malloc(sizeof(struct dict) * MAX);//固定分配MAX大小内存
memset(*p, 0, sizeof(struct dict) * MAX);//将分配内存初始化为0
struct dict *pD = *p;//pD指向数组p的首地址
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)
{
pD[i].key = (char *)malloc(len);//根据字符串长度分配内存
memset(pD[i].key, 0, len);
strcpy(pD[i].key, &buf[1]);//将读取到的内容拷贝到key中
}
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
pD[i].content = (char *)malloc(len);
memset(pD[i].content, 0, len);
strcpy(pD[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\n", args[0]);
return 0;//参数不足,程序退出
}
long start_ms = 0;//记录函数执行的开始时间
long end_ms = 0;//记录函数执行的结束时间
struct dict *p = NULL;
start_ms = clock();
int size = open_dict(&p, args[1]);//根据命令行第一个参数做为字典文件名,打开字典文件
if (size == 0)
return 0;//打开字典文件失败,程序退出
end_ms = clock();
printf("open_dict used %ld ms\n", 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, size, key, content))//根据用户输入,在字典中检索
{
printf("%s", content);
} else
{
printf("not found\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;
}
1,命令行版 -- 【2】,这个可以自动读取字典文件到底有多少行
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
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 (p == NULL)
return 0;//打开文件失败,函数返回
int size = get_dict_size(pfile);//得到字典文件中词条总数
if (size == 0)
return 0;
*p = (struct dict *)malloc(sizeof(struct dict) * size);//根据字典文件词条总数分配内存
memset(*p, 0, sizeof(struct dict) * size);//将分配内存初始化为0
struct dict *pD = *p;//pD指向数组p的首地址
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)
{
pD[i].key = (char *)malloc(len);//根据字符串长度分配内存
memset(pD[i].key, 0, len);
strcpy(pD[i].key, &buf[1]);//将读取到的内容拷贝到key中
}
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
pD[i].content = (char *)malloc(len);
memset(pD[i].content, 0, len);
strcpy(pD[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\n", args[0]);
return 0;//参数不足,程序退出
}
long start_ms = 0;//记录函数执行的开始时间
long end_ms = 0;//记录函数执行的结束时间
struct dict *p = NULL;
start_ms = clock();
int size = open_dict(&p, args[1]);//根据命令行第一个参数做为字典文件名,打开字典文件
if (size == 0)
return 0;//打开字典文件失败,程序退出
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, size, key, content))//根据用户输入,在字典中检索
{
printf("%s", content);
} else
{
printf("not found\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;
}
1,命令行版 -- 【3】,利用链表,1次就可以自动读取字典文件到底有多少行
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct dict
{
char *key;
char *content;
struct dict *next;//指向链表下一个节点的指针
};
//打开字典文件,并读取文件内容
int open_dict(struct dict **p, const char *dict_filename)//open dict.txt,and read dict
{
FILE *pfile = fopen(dict_filename, "r");//只读方式打开文件
if (p == NULL)
return 0;//打开文件失败,函数返回
char buf[2048] = { 0 };
size_t len = 0;
int i = 0;//计数器,记录读到到的词条总数
*p = (struct dict *)malloc(sizeof(struct dict));//分配链表首节点内存
memset(*p, 0, sizeof(struct dict));
struct dict *pD = *p;//pD指向链表首地址
while (!feof(pfile))//循环读取文件,直到文件末尾
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);//读取文件一行
len = strlen(buf);//得到读取到字符串长度
if (len > 0)
{
pD->key = (char *)malloc(len);//根据字符串长度分配内存
memset(pD->key, 0, len);
strcpy(pD->key, &buf[1]);//将读取到的内容拷贝到key中
}
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
pD->content = (char *)malloc(len);
memset(pD->content, 0, len);
strcpy(pD->content, &buf[6]);
}
pD->next = (struct dict *)malloc(sizeof(struct dict));//为链表的下一个节点分配内存
memset(pD->next, 0, sizeof(struct dict));
pD = pD->next;//将pD指向下一个节点位置
i++;
}
fclose(pfile);//关闭字典文件
return i;//返回读取到的字典词条数
}
//根据关键字key,在字典中查找内容
int search_dict(const struct dict *p, int size, const char *key, char *content)
{
const struct dict *pD = p;
while (pD)//遍历字典
{
if ((pD->key) && (pD->content))
{
if (strncmp(pD->key, key, strlen(key)) == 0)
{
strcpy(content, pD->content);
return 1;//找到符合条件记录,返回1
}
}
pD = pD->next;//指向链表下一个节点位置
}
return 0;//没有找到符合条件记录,返回0
}
//释放链表内存
void free_dict(struct dict *p, int size)
{
struct dict *pD = p;
while (pD)
{
if (pD->key)//删除链表节点中key成员内存
free(pD->key);
if (pD->content)//删除链表节点中content成员内存
free(pD->content);
struct dict *tmp = pD->next;//保存链表下一个节点的地址
free(pD);//删除链表当前节点
pD = tmp;//p指向下一个节点的位置
}
}
int main(int argc, char *args[])
{
if (argc < 2)
{
printf("usage: %s dict-filename\n", args[0]);
return 0;//参数不足,程序退出
}
long start_ms = 0;//记录函数执行的开始时间
long end_ms = 0;//记录函数执行的结束时间
start_ms = clock();
struct dict *p = NULL;
int size = open_dict(&p, args[1]);//根据命令行第一个参数做为字典文件名,打开字典文件
if (size == 0)
return 0;//打开字典文件失败,程序退出
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, size, key, content))//根据用户输入,在字典中检索
{
printf("%s", content);
} else
{
printf("not found\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;
}
1,命令行版 -- 【4】,linux终端字符是UTF8的,在【3】的基础加入字符转换
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <iconv.h>
int gbk2utf8(char *src, size_t *srclen, char *dest, size_t *destlen)
{
iconv_t cd = iconv_open("UTF8", "GBK"); //源字符串为GBK,目标UTF8
if (cd == (iconv_t) - 1)
{
printf("open iconv error %s\n", strerror(errno));
return -1;
}
size_t rc = iconv(cd, &src, srclen, &dest, destlen); //将src字符串转化为目标dest
if (rc == (size_t) - 1)
{
printf("iconv error %s\n", strerror(errno));
return -1;
}
iconv_close(cd);
return 0;
}
struct dict
{
char *key;
char *content;
struct dict *next; //指向链表下一个节点的指针
};
//打开字典文件,并读取文件内容
int open_dict(struct dict **p, const char *dict_filename) //open dict.txt,and read dict
{
FILE *pfile = fopen(dict_filename, "r"); //只读方式打开文件
if (p == NULL)
return 0; //打开文件失败,函数返回
char buf[2048] = { 0 };
char srcbuf[2048] = { 0 };
size_t len = 0;
size_t srclen = 0;
int i = 0; //计数器,记录读到到的词条总数
*p = (struct dict *) malloc(sizeof(struct dict)); //分配链表首节点内存
memset(*p, 0, sizeof(struct dict));
struct dict *pD = *p; //pD指向链表首地址
while (!feof(pfile)) //循环读取文件,直到文件末尾
{
memset(buf, 0, sizeof(buf));
memset(srcbuf, 0, sizeof(srcbuf));
fgets(srcbuf, sizeof(srcbuf), pfile); //读取文件一行
srclen = strlen(srcbuf);
len = sizeof(buf);
gbk2utf8(srcbuf, &srclen, buf, &len);//将读取到的字符串从gbk转化为utf8格式
len = strlen(buf); //得到读取到字符串长度
if (len > 0)
{
pD->key = (char *) malloc(len); //根据字符串长度分配内存
memset(pD->key, 0, len);
strcpy(pD->key, &buf[1]); //将读取到的内容拷贝到key中
}
memset(buf, 0, sizeof(buf));
memset(srcbuf, 0, sizeof(srcbuf));
fgets(srcbuf, sizeof(srcbuf), pfile);
srclen = strlen(srcbuf);
len = sizeof(buf);
gbk2utf8(srcbuf, &srclen, buf, &len);
len = strlen(buf);
if (len > 0)
{
pD->content = (char *) malloc(len);
memset(pD->content, 0, len);
strcpy(pD->content, &buf[6]);
}
pD->next = (struct dict *) malloc(sizeof(struct dict)); //为链表的下一个节点分配内存
memset(pD->next, 0, sizeof(struct dict));
pD = pD->next; //将pD指向下一个节点位置
i++;
}
fclose(pfile); //关闭字典文件
return i; //返回读取到的字典词条数
}
//根据关键字key,在字典中查找内容
int search_dict(const struct dict *p, int size, const char *key, char *content)
{
const struct dict *pD = p;
while (pD) //遍历字典
{
if ((pD->key) && (pD->content))
{
if (strncmp(pD->key, key, strlen(key)) == 0)
{
strcpy(content, pD->content);
return 1; //找到符合条件记录,返回1
}
}
pD = pD->next; //指向链表下一个节点位置
}
return 0; //没有找到符合条件记录,返回0
}
//释放链表内存
void free_dict(struct dict *p, int size)
{
struct dict *pD = p;
while (pD)
{
if (pD->key) //删除链表节点中key成员内存
free(pD->key);
if (pD->content) //删除链表节点中content成员内存
free(pD->content);
struct dict *tmp = pD->next; //保存链表下一个节点的地址
free(pD); //删除链表当前节点
pD = tmp; //p指向下一个节点的位置
}
}
int main(int argc, char *args[])
{
if (argc < 2)
{
printf("usage: %s dict-filename\n", args[0]);
return 0; //参数不足,程序退出
}
long start_ms = 0; //记录函数执行的开始时间
long end_ms = 0; //记录函数执行的结束时间
start_ms = clock();
struct dict *p = NULL;
int size = open_dict(&p, args[1]); //根据命令行第一个参数做为字典文件名,打开字典文件
if (size == 0)
return 0; //打开字典文件失败,程序退出
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, size, key, content)) //根据用户输入,在字典中检索
{
printf("%s", content);
} else
{
printf("not found\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;
}
MFC部分代码:
完整代码在云盘,51cto目录
// MFCApplication5Dlg.cpp : 实现文件
//
#include "stdafx.h"
#include "MFCApplication5.h"
#include "MFCApplication5Dlg.h"
#include "afxdialogex.h"
#pragma warning(disable:4996)
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
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 (p == NULL)
return 0;//打开文件失败,函数返回
int size = get_dict_size(pfile);//得到字典文件中词条总数
if (size == 0)
return 0;
*p = (struct dict *)malloc(sizeof(struct dict) * size);//根据字典文件词条总数分配内存
memset(*p, 0, sizeof(struct dict) * size);//将分配内存初始化为0
struct dict *pD = *p;//pD指向数组p的首地址
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)
{
pD[i].key = (char *)malloc(len);//根据字符串长度分配内存
memset(pD[i].key, 0, len);
strcpy(pD[i].key, &buf[1]);//将读取到的内容拷贝到key中
}
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
pD[i].content = (char *)malloc(len);
memset(pD[i].content, 0, len);
strcpy(pD[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内存
}
struct dict *p = NULL;
int dict_size = 0;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CMFCApplication5Dlg 对话框
CMFCApplication5Dlg::CMFCApplication5Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CMFCApplication5Dlg::IDD, pParent)
, text1(_T(""))
, lable1(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCApplication5Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, text1);
DDX_Text(pDX, IDC_STATIC1, lable1);
}
BEGIN_MESSAGE_MAP(CMFCApplication5Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &CMFCApplication5Dlg::OnBnClickedOk)
ON_BN_CLICKED(IDCANCEL, &CMFCApplication5Dlg::OnBnClickedCancel)
ON_STN_CLICKED(IDC_STATIC1, &CMFCApplication5Dlg::OnStnClickedStatic1)
END_MESSAGE_MAP()
// CMFCApplication5Dlg 消息处理程序
BOOL CMFCApplication5Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
dict_size = open_dict(&p,"dict.txt");//根据命令行第一个参数做为字典文件名,打开字典文件
if (dict_size == 0)
exit(0);//打开字典文件失败,程序退出
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CMFCApplication5Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CMFCApplication5Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFCApplication5Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMFCApplication5Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
//CDialogEx::OnOK();
char key[2048];
char content[2048];
memset(key, 0, sizeof(key));
memset(content, 0, sizeof(content));
UpdateData(TRUE);//将用户在edit控件中输入的文字放入text1这个变量中
CStringA key_a(text1);//将宽码转GBK
strcpy(key,key_a);
if (search_dict(p, dict_size, key, content))
{
lable1 = content;
}
else
{
lable1 = L"找不到";
}
UpdateData(FALSE); //将lable1的内容同步到控件
}
void CMFCApplication5Dlg::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
CDialogEx::OnCancel();
}
void CMFCApplication5Dlg::OnStnClickedStatic1()
{
free_dict(p, dict_size);//释放内存
// TODO: 在此添加控件通知处理程序代码
}
QT部分代码:
完整代码在云盘,51cto目录
QT部分代码:
#include "widget.h"
#include "ui_widget.h"
#include <QTextCodec>
#define MAX 111111 //最大记录数
struct dict
{
char *key;
char *content;
};
struct dict *p = NULL;
int dict_size = 0;
//打开字典文件,并读取文件内容
int open_dict(struct dict **p, const char *dict_filename)
{
FILE *pfile = fopen(dict_filename, "r");
if (p == NULL)
return 0;//打开文件失败,函数返回
*p = (struct dict *)malloc(sizeof(struct dict) * MAX);//固定分配MAX大小内存
memset(*p, 0, sizeof(struct dict) * MAX);//将分配内存初始化为0
struct dict *pD = *p;//pD指向数组p的首地址
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)
{
pD[i].key = (char *)malloc(len);//根据字符串长度分配内存
memset(pD[i].key, 0, len);
strcpy(pD[i].key, &buf[1]);//将读取到的内容拷贝到key中
}
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), pfile);
len = strlen(buf);
if (len > 0)
{
pD[i].content = (char *)malloc(len);
memset(pD[i].content, 0, len);
strcpy(pD[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内存
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
dict_size = open_dict(&p, "D:\\dict.txt");
ui->setupUi(this);
}
Widget::~Widget()
{
free_dict(p, dict_size);
delete ui;
}
void Widget::on_pushButton_clicked()
{
QTextCodec *codec = QTextCodec::codecForName("GBK");
char content[1024] = {0};
//codec->fromUnicode(ui->lineEdit->text());//将edit1的内容转化为GBK
if (search_dict(p, dict_size, codec->fromUnicode(ui->lineEdit->text()).data(), content))//根据用户输入,在字典中检索
{
//codec->toUnicode(content);//把content从GBK编码转化为UTF8
ui->label->setText(codec->toUnicode(content));
} else
{
ui->label->setText("not found");
}
}
转载于:https://blog.51cto.com/990487026/1788347