From a483471152a0ce4e4830741e014d52636bc135fe Mon Sep 17 00:00:00 2001 From: Jafarichen <3149829972@qq.com> Date: Thu, 16 Dec 2021 21:39:17 +0800 Subject: [PATCH] v1.0 --- UniversalTuringMachine.pro.user | 2 +- mainwindow.cpp | 163 ++++++++++++++++++++++++++++---- mainwindow.h | 7 +- mylabel.cpp | 15 +++ mylabel.h | 7 +- 5 files changed, 171 insertions(+), 23 deletions(-) diff --git a/UniversalTuringMachine.pro.user b/UniversalTuringMachine.pro.user index 1e7df8f..1f62928 100644 --- a/UniversalTuringMachine.pro.user +++ b/UniversalTuringMachine.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/mainwindow.cpp b/mainwindow.cpp index 85c2302..e5d0a9f 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -7,15 +7,23 @@ #define LABEL_Y 30 #define LINE_X 200 #define LINE_Y 30 +#define X 30 +#define Y 500 +#define SIZE 100 QString filepath=QDir::currentPath(); QStringList K; QStringList Sigma; -QStringList Gamma; QStringList Q0; QStringList B; QStringList F; +QString function[SIZE][5]; + +QString q; +int now; + + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) @@ -114,13 +122,24 @@ MainWindow::MainWindow(QWidget *parent) btn_step->setText("下一步"); //纸带 - for(int i=0;i<24;i++) + for(int i=0;i<25;i++) { label[i]=new MyLabel(this); - label[i]->setPos(30+40*i,500); + label[i]->setPos(X+40*i,Y); label[i]->hide(); } + label_now=new MyLabel(this); + label_now->setStyleSheet("border-width: 2px;border-style: solid;border-color: rgb(236, 255, 251);background-color: rgb(20, 250, 20);"); + label_now->setFixedSize(40,35); + label_now->hide(); + + label_fun = new QLabel(this); + label_fun->setGeometry(X+50,Y-40, 500,30); + label_fun->setAlignment(Qt::AlignCenter); + label_fun->setText("状态转移函数:"); + + //connect QObject::connect(btn_import, SIGNAL(clicked()), this, SLOT(importFile())); QObject::connect(btn_confirm, SIGNAL(clicked()), this, SLOT(startSimulate())); QObject::connect(btn_step, SIGNAL(clicked()), this, SLOT(nextStep())); @@ -133,33 +152,59 @@ MainWindow::~MainWindow() void MainWindow::importFile() { + qDebug()<<"import1"; K.clear(); Sigma.clear(); - Gamma.clear(); Q0.clear(); B.clear(); F.clear(); int row=list_file->currentRow(); list_func->clear(); QFile file(filepath + "/" + QString::number(row) + ".txt"); + qDebug()<<"import3"; if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&file); QString temp; + int i=0; while(1) { temp=in.readLine(); if(temp.size()==0) - break; + break; + qDebug()<<"import1"; QStringList list = temp.split(" "); - list_func->addItem(" δ(q"+list[0]+","+list[1]+")=(q"+list[2]+","+list[3]+","+list[4]+") "); - append(&K,"q"+list[0]); - append(&Sigma,list[1]); + qDebug()<<"import2"; + list[0]="q"+list[0]; + if(list.size()>2) + list[2]="q"+list[2]; + qDebug()<<"import3"; if(list.contains("#")) - append(&F,"q"+list[0]); + { + function[i][0]=list[0]; + function[i][1]=list[1]; + } + else + for(int j=0;j<5;j++) + function[i][j]=list[j]; +qDebug()<<"import4"; + if(list.contains("#")) + append(&F,list[0]); + else + { + qDebug()<<"import6"; + list_func->addItem(" δ("+list[0]+","+list[1]+")=("+list[2]+","+list[3]+","+list[4]+") "); + append(&K,list[0]); + append(&Sigma,list[1]); + qDebug()<<"import7"; + } + qDebug()<<"import5"<setText(k); line_sigma->setText(sigma); @@ -181,24 +227,84 @@ void MainWindow::importFile() line_q0->setEnabled(false); line_B->setEnabled(false); line_F->setEnabled(false); + + qDebug()<<"import12"; } + void MainWindow::startSimulate() { - QString str=line_str->text(); + for(int i=0;i<25;i++) + label[i]->hide(); + //qDebug()<<"1"; + if(line_str->text().length()==0) + return; + str="BBB"+line_str->text()+"BB"; label[2]->setText("B"); label[2]->show(); - for(int i=0;isetText(str[i]); - label[i+3]->show(); - label[i+4]->setText("B"); - label[i+4]->show(); - } +// for(int i=3;isetText(str[i]); +// label[i]->show(); +// } + + now = 3; + label_now->setPos(X+40*3,Y+55); + label_now->show(); + + q=Q0[0]; + label_now->setText(q); + + update(0); } void MainWindow::nextStep() { + bool flag=false; + QChar c = str[now]; + QString comp; + qDebug()<<"next0"; + comp+=c; + qDebug()<<"next1"; + for(int i=0;isetText("δ("+function[i][0]+","+function[i][1]+")=("+function[i][2]+","+function[i][3]+","+function[i][4]+")"); + qDebug()<<("δ("+function[i][0]+","+function[i][1]+")=("+function[i][2]+","+function[i][3]+","+function[i][4]+")"); + if(comp=="B" && now==str.length()-1) + { + str+="B"; + } + if(function[i][4]=="R"){ + now++; + update(1); + }else if(function[i][4]=="L"){ + now--; + update(-1); + }else{ + update(0); + } + break; + } + + if(function[i][0].length()==0) + break; + } + if(flag==false) + QMessageBox::warning(0 , " 失败! ", " 失败! ",QMessageBox::Ok | QMessageBox::Default , QMessageBox::Cancel | QMessageBox::Escape ); + + if(F.contains(q)) + { + QMessageBox::information(0 , " 成功! "," 成功! ", QMessageBox::Ok | QMessageBox::Default , QMessageBox::Cancel | QMessageBox::Escape ); + //终止状态 + } } void MainWindow::loadFileList() @@ -230,3 +336,26 @@ void MainWindow::append(QStringList* list,QString in) if(flag==0) list->append(in); } + +void MainWindow::update(int move_q) +{ + + label_now->setPos(label_now->pos().width()+move_q*40,label_now->pos().height()); + label_now->setText(q); + qDebug()<<"update"<setText(str[i]); + label[i]->show(); + if(i<=2&&str[i]=='B'&&str[i+1]=='B') + label[i]->hide(); + if(i==str.length()-2&&str[i]=='B') + { + label[i]->setText(str[i+1]); + label[i]->show(); + break; + } + } + + qDebug()<<"ipdate2"; +} diff --git a/mainwindow.h b/mainwindow.h index 3125283..fe8eb0c 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -33,11 +33,12 @@ public: ~MainWindow(); void loadFileList(); void append(QStringList* list,QString in); - + void update(int move_q); public slots: void nextStep(); void importFile(); void startSimulate(); + private: Ui::MainWindow *ui; QLabel *label_k,*label_sigma,*label_gamma,*label_q0,*label_B,*label_F; @@ -49,7 +50,9 @@ private: QGroupBox *group_1; QListWidget *list_file,*list_func; QPushButton *btn_import,*btn_confirm,*btn_step; - + QString str; MyLabel* label[25]; + MyLabel* label_now; + QLabel* label_fun; }; #endif // MAINWINDOW_H diff --git a/mylabel.cpp b/mylabel.cpp index fc84086..20384f3 100644 --- a/mylabel.cpp +++ b/mylabel.cpp @@ -46,3 +46,18 @@ void MyLabel::setPos(int x,int y) { label->setGeometry(x,y,40,55); } + +void MyLabel::setStyleSheet(QString sheet) +{ + label->setStyleSheet(sheet); +} + +void MyLabel::setFixedSize(int x, int y) +{ + label->setFixedSize(x,y); +} + +QSize MyLabel::pos() +{ + return QSize(label->geometry().x(),label->geometry().y()); +} diff --git a/mylabel.h b/mylabel.h index 59e5e36..ccf34f0 100644 --- a/mylabel.h +++ b/mylabel.h @@ -6,7 +6,7 @@ #include #include #include - +#include class MyLabel { public: @@ -18,10 +18,11 @@ public: void hide(); void show(); void setPos(int x,int y); + void setStyleSheet(QString sheet); + void setFixedSize(int x,int y); + QSize pos(); private: QLabel *label; - - }; #endif // MYLABEL_H