快译通

QT下C语言开发快译通

在这里插入图片描述

dict.txt以及软件成品可在我的CSDN下载中获取

#-------------------------------------------------
#
# Project created by QtCreator 2019-11-18T12:56:52
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Dictionary
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

RC_ICONS += app.ico

RESOURCES +=

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <QString>
#include <QDebug>
#include <QDir>
#include <QTextCodec>

#define WORDMAX 111104

struct dicts
{
    char *word;
    char *trans;
};
typedef struct dicts dic;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    dic *p = NULL;
};

#endif // MAINWINDOW_H

#include “mainwindow.h”
#include
#include

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QDir::setCurrent(a.applicationDirPath());
w.show();

return a.exec();

}

mainwindow.cpp
```c
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTextCodec *codec = QTextCodec::codecForName("GBK");
    QString pa = QDir::currentPath();
    char *pat = codec->fromUnicode(pa).data();
    const char *path = "/dict.txt";
    strcat(pat, path);
    p = (dic*)malloc(sizeof(dic) * WORDMAX);
    FILE *fp = fopen(pat, "r");
    if (!fp)
    {
        printf("文件打开失败!\n");
        return ;
    }
    int i = 0;
    char buf[1024];
    while (!feof(fp))
    {
        memset(buf, 0, 1024);
        fgets(buf, 1024, fp);
        buf[strlen(buf) - 1] = '\0';
        p[i].word = (char*)malloc(strlen(buf) + 1);
        memset(p[i].word, 0, strlen(buf) + 1);
        strcpy(p[i].word, &buf[1]);
        memset(buf, 0, 1024);
        fgets(buf, 1024, fp);
        p[i].trans = (char*)malloc(strlen(buf) + 1);
        memset(p[i].trans, 0, strlen(buf) + 1);
        strcpy(p[i].trans, &buf[6]);
        i++;
    }
    fclose(fp);
}

MainWindow::~MainWindow()
{
    delete ui;
    for (int i = 0; i < WORDMAX; i++)
    {
        free(p[i].word);
        free(p[i].trans);
    }
    free(p);
    p = NULL;
}

void MainWindow::on_pushButton_clicked()
{
    QString content = ui->textEdit->toPlainText().trimmed();
    if(content.isEmpty())
    {
        ui->labelShow->setText("请输入要查找的单词!\n");
        return;
    }
    QTextCodec *codec = QTextCodec::codecForName("GBK");
    char * con = codec->fromUnicode(content).data();
    for(int i = 0; i < WORDMAX; i++)
    {
        if(!strcmp(con, p[i].word))
        {
            ui->labelShow->setText(codec->toUnicode(p[i].trans));
            return;
        }
    }
    ui->labelShow->setText("未找到该单词解释!\n");
}

mainwindow.ui

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTextCodec *codec = QTextCodec::codecForName("GBK");
    QString pa = QDir::currentPath();
    char *pat = codec->fromUnicode(pa).data();
    const char *path = "/dict.txt";
    strcat(pat, path);
    p = (dic*)malloc(sizeof(dic) * WORDMAX);
    FILE *fp = fopen(pat, "r");
    if (!fp)
    {
        printf("文件打开失败!\n");
        return ;
    }
    int i = 0;
    char buf[1024];
    while (!feof(fp))
    {
        memset(buf, 0, 1024);
        fgets(buf, 1024, fp);
        buf[strlen(buf) - 1] = '\0';
        p[i].word = (char*)malloc(strlen(buf) + 1);
        memset(p[i].word, 0, strlen(buf) + 1);
        strcpy(p[i].word, &buf[1]);
        memset(buf, 0, 1024);
        fgets(buf, 1024, fp);
        p[i].trans = (char*)malloc(strlen(buf) + 1);
        memset(p[i].trans, 0, strlen(buf) + 1);
        strcpy(p[i].trans, &buf[6]);
        i++;
    }
    fclose(fp);
}

MainWindow::~MainWindow()
{
    delete ui;
    for (int i = 0; i < WORDMAX; i++)
    {
        free(p[i].word);
        free(p[i].trans);
    }
    free(p);
    p = NULL;
}

void MainWindow::on_pushButton_clicked()
{
    QString content = ui->textEdit->toPlainText().trimmed();
    if(content.isEmpty())
    {
        ui->labelShow->setText("请输入要查找的单词!\n");
        return;
    }
    QTextCodec *codec = QTextCodec::codecForName("GBK");
    char * con = codec->fromUnicode(content).data();
    for(int i = 0; i < WORDMAX; i++)
    {
        if(!strcmp(con, p[i].word))
        {
            ui->labelShow->setText(codec->toUnicode(p[i].trans));
            return;
        }
    }
    ui->labelShow->setText("未找到该单词解释!\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值