北京建设教育协会网站首页/长沙网站推广工具
编译环境
- Qt 6.0.0
- Qt 5 Compatibility Module
- MinGW 8.1.0 64-bit
- Qt Creator 4.14.0 (Community)
- Windows 10
题目要求
先随机生成一个各位均不相同的四位数。用户输入一个四位数,如果那一位完全对应上了,A的值就加一。如果那一位没有对上,但是那个数字在原来的四位数中存在,则B的值加以。显示-A-B来提示用户。
生成的数假设为1234:
- 输入1234,提示4A0B
- 输入1753,提示1A1B
- 输入4321,提示0A4B
程序
Guess_number.pro
QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \mainwindow.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <vector>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();std::vector<QString> original;private slots:void on_OK_clicked();void on_pushButton_clicked();private:Ui::MainWindow *ui;};
#endif // MAINWINDOW_H
mainwindow.cpp
#include <vector>
#include <ctime>
#include "mainwindow.h"
#include "ui_mainwindow.h"
using namespace std;MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);setWindowTitle("Guess Number Game");vector<int> alr{};srand(static_cast<unsigned>(time(NULL)));while(alr.size() < 4){int temp_number = rand() % 10;bool state = true;for(int i = 0; i != alr.size(); i++){if(temp_number == alr[i]){state = false;break;}}if(state == true){alr.push_back(temp_number);}}original = {QString::number(alr[0], 10),QString::number(alr[1], 10),QString::number(alr[2], 10),QString::number(alr[3], 10)};}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_OK_clicked()
{int A = 0, B = 0;QString guess = ui->lineEdit->text();if(guess.length() != 4){ui->Hint->setText("Error number!");}else{for(int i = 0; i != 4; i++){if(guess[i] == original[i]){A++;}else{for(int j = 0; j != 4; j++){if(guess[i] == original[j]){B++;break;}}}}}ui->Hint->setText(QString::number(A, 10) + "A" + QString::number(B, 10) + "B");
}void MainWindow::on_pushButton_clicked()
{ui->Original_number->setText(original[0] + original[1] + original[2] + original[3]);
}
main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>316</width><height>254</height></rect></property><property name="windowTitle"><string>MainWindow</string></property><widget class="QWidget" name="centralwidget"><layout class="QGridLayout" name="gridLayout"><item row="0" column="0"><spacer name="verticalSpacer"><property name="orientation"><enum>Qt::Vertical</enum></property><property name="sizeHint" stdset="0"><size><width>20</width><height>40</height></size></property></spacer></item><item row="1" column="0"><layout class="QHBoxLayout" name="horizontalLayout"><item><widget class="QLabel" name="label"><property name="text"><string>Guess the number:</string></property></widget></item><item><widget class="QLineEdit" name="lineEdit"><property name="placeholderText"><string>XXXX</string></property></widget></item><item><widget class="QPushButton" name="OK"><property name="text"><string>OK</string></property></widget></item></layout></item><item row="2" column="0"><spacer name="verticalSpacer_2"><property name="orientation"><enum>Qt::Vertical</enum></property><property name="sizeHint" stdset="0"><size><width>20</width><height>40</height></size></property></spacer></item><item row="3" column="0"><layout class="QHBoxLayout" name="horizontalLayout_2"><item><widget class="QLabel" name="label_2"><property name="text"><string>Hint:</string></property><property name="alignment"><set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set></property></widget></item><item><widget class="QLabel" name="Hint"><property name="text"><string>0A0B</string></property></widget></item></layout></item><item row="5" column="0"><layout class="QHBoxLayout" name="horizontalLayout_3"><item><spacer name="horizontalSpacer"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item><item><widget class="QPushButton" name="pushButton"><property name="text"><string>View original number</string></property></widget></item><item><widget class="QLabel" name="Original_number"><property name="text"><string/></property></widget></item><item><spacer name="horizontalSpacer_2"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item></layout></item><item row="4" column="0"><spacer name="verticalSpacer_3"><property name="orientation"><enum>Qt::Vertical</enum></property><property name="sizeHint" stdset="0"><size><width>20</width><height>40</height></size></property></spacer></item><item row="6" column="0"><spacer name="verticalSpacer_4"><property name="orientation"><enum>Qt::Vertical</enum></property><property name="sizeHint" stdset="0"><size><width>20</width><height>40</height></size></property></spacer></item></layout></widget><widget class="QMenuBar" name="menubar"><property name="geometry"><rect><x>0</x><y>0</y><width>316</width><height>26</height></rect></property></widget><widget class="QStatusBar" name="statusbar"/></widget><resources/><connections/>
</ui>
UI界面
输出示例
分析
- 直接将Build文件夹里的 exe 文件拿出来试运行不了的,会说缺少一些 dll 文件。如果手动找到这些 dll 很麻烦而且还会出错。所以方法详见我的博客 【Qt】 生成 exe 文件的方法。
ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。
See also
Teddy van Jerry 的导航页