代码拉取完成,页面将自动刷新
#include "triepart.h"
#include "ui_triepart.h"
#include <QMessageBox>
#include <QFile>
TriePart::TriePart(QWidget *parent) :
QWidget(parent),
ui(new Ui::TriePart)
{
QFile files("D:\\Qt\\Documents\\DataStructureCourseDesign\\all.txt");
files.open(QIODevice::ReadOnly);
while(!files.atEnd()){
QByteArray line = files.readLine();
QString str(line);
string s = str.toStdString();
int len = s.size();
string s1 = "",s2 = "";
int i;
for(i = 0; i < len && s[i] != ' '; ++i){
if(str[i] != ' '){
s1 += s[i];
}
}
for(i++;i < len; ++i){
s2 += s[i];
}
trie->insert(s1,s2);
}
ui->setupUi(this);
this->setWindowTitle("英汉字典(HashTrie)");
connect(ui->pushButton,&QPushButton::clicked,[=](){
QString QKey = ui->textEdit->toPlainText();
string key = QKey.toStdString();
vector<trieTree> values = trie->search(key);
ui->textEdit_3->setText("");
if(values.size()){
if(values[0]->key == key){
ui->textEdit_2->setText(QString::fromStdString(values[0]->value));
}else{
ui->textEdit_2->setText(QString::fromStdString("NOT FOND!"));
int len = values.size();
ui->textEdit_3->insertPlainText("你可能要查找的单词:\n");
for(int i =0;i < len; ++i){
QString keyI = QString::fromStdString(values[i]->key);
QString valueI = QString::fromStdString(values[i]->value);
ui->textEdit_3->insertPlainText(keyI + "\t" + valueI + '\n');
}
}
}else{
ui->textEdit_2->setText(QString::fromStdString("NOT FOND!"));
QMessageBox::information(this,"查找单词","未找到该单词!");
}
});
//清除
connect(ui->pushButton_2,&QPushButton::clicked,this,[=](){
ui->textEdit_2->setText(tr(""));
ui->textEdit->setText(tr(""));
ui->textEdit_3->setText("");
});
//插入
connect(ui->pushButton_3,&QPushButton::clicked,[=](){
QString s1 = ui->textEdit->toPlainText();
QString s2 = ui->textEdit_2->toPlainText();
ui->textEdit_3->setText("");
if(s1.size() == 0|| s2.size() == 0){
QMessageBox::information(this,"插入单词","插入单词错误!");
}
if(trie->insert(s1.toStdString(),s2.toStdString())){
QMessageBox::information(this,"插入单词","新建单词成功!");
}else{
QMessageBox::information(this,"插入单词","插入重复单词!");
}
});
connect(ui->pushButton_4,&QPushButton::clicked,[=](){
QString s = ui->textEdit->toPlainText();
ui->textEdit_3->setText("");
if(s.size() == 0){
QMessageBox::information(this,"插入单词","删除单词错误!");
}
if(trie->deleteWord(s.toStdString())){
QMessageBox::information(this,"删除单词","成功删除!");
}else{
QMessageBox::information(this,"删除单词","删除失败,未找到该单词!");
}
});
}
TriePart::~TriePart()
{
delete ui;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。