基于QT-widget的电子词典实现

来源:互联网 发布:大数据和股票有哪些 编辑:程序博客网 时间:2024/04/28 20:15

基于QT-widget的电子词典实现#ifndefWIDGET_H

widget.h:

#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
struct dict
{
    char *key;
    char *content;
    struct dict *next;
};
class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private slots:
    void on_pushButton_clicked();
private:
    Ui::Widget *ui;
private:
    struct dict *p ;
    int dict_size;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <QTextCodec>
//打开字典文件,并读取文件内容
int open_dict(struct dict **p, const char *dict_filename)
{
    FILE *pfile = fopen(dict_filename, "r");
    if (pfile == NULL)
        return 0;//打开文件失败,函数返回
    int filerow = 0;
    while (1)
    {
        filerow++;
        char buf0[1024] = { 0 };
        if (fgets(buf0, sizeof(buf0), pfile) == NULL)
        {
            printf("(%s)",buf0);
            break;
        }
        //else
    }
    printf("%d",filerow);
    /*fclose(pfile);
    FILE *pfile1 = fopen(dict_filename, "r");
    if (pfile1 == NULL)
        return 0;//打开文件失败,函数返回*/
    fseek(pfile,0,SEEK_SET);
    *p = (struct dict *)malloc(sizeof(struct dict) * (filerow/2));//固定分配MAX大小内存
    memset(*p, 0, sizeof(struct dict) * (filerow/2));//将分配内存初始化为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->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 *)calloc(sizeof(struct dict), 1);
        pD = pD->next;
        i++;//计数器加1
    }
    fclose(pfile);//关闭字典文件
    return i;//返回读取到的字典词条数
}
//根据关键字key,在字典中查找内容
int search_dict(const struct dict *p, const char *key, char *content)
{
   const struct dict *pD=p;
    while (pD)//遍历字典
    {
        if ((pD->key == NULL) || (pD->content == NULL))
        {
            continue;
        }
        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)
{
    struct dict *pD = p;
    while (pD)
    {
        if (pD->key)
            free(pD->key);
        if (pD->content)
            free(pD->content);
        struct dict *tmp = pD->next;
        free(pD);
        pD = tmp;
    }
}
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //open_dict();
    dict_size=open_dict(&p,"D:\\dict.txt");
    if(dict_size==0)
        exit(0);
}
Widget::~Widget()
{
    free(p);
    delete ui;
}
void Widget::on_pushButton_clicked()
{
    QTextCodec *codec=QTextCodec::codecForName("GBK");
   // ui->lineEdit->text();
   char content[1024]={0};
 if(search_dict(p,codec->fromUnicode(ui->lineEdit->text().toStdString().data()),content))
   {
      // printf();
       ui->label->setText(codec->toUnicode(content));
   }
   else
   {
      ui->label ->setText("not found");
   }
}
                                             
0 0