From e8fa6e2d7bdcbb4e6f5a71391ba3eca2f7e2455e Mon Sep 17 00:00:00 2001 From: iridiumR Date: Tue, 29 Mar 2022 15:02:47 +0800 Subject: [PATCH] =?UTF-8?q?2-2-5=E6=8A=8A=E4=B9=A6=E4=B8=8A=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E9=80=9A=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SoftwareDesign/Code/2-2-5/2-2-5.pro | 24 +++ SoftwareDesign/Code/2-2-5/main.cpp | 11 ++ SoftwareDesign/Code/2-2-5/mainwindow.cpp | 170 ++++++++++++++++++++++ SoftwareDesign/Code/2-2-5/mainwindow.h | 49 +++++++ SoftwareDesign/Code/2-2-5/mainwindow.ui | 177 +++++++++++++++++++++++ 5 files changed, 431 insertions(+) create mode 100644 SoftwareDesign/Code/2-2-5/2-2-5.pro create mode 100644 SoftwareDesign/Code/2-2-5/main.cpp create mode 100644 SoftwareDesign/Code/2-2-5/mainwindow.cpp create mode 100644 SoftwareDesign/Code/2-2-5/mainwindow.h create mode 100644 SoftwareDesign/Code/2-2-5/mainwindow.ui diff --git a/SoftwareDesign/Code/2-2-5/2-2-5.pro b/SoftwareDesign/Code/2-2-5/2-2-5.pro new file mode 100644 index 0000000..b915c09 --- /dev/null +++ b/SoftwareDesign/Code/2-2-5/2-2-5.pro @@ -0,0 +1,24 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++17 + +# 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.0 + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + +FORMS += \ + 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 diff --git a/SoftwareDesign/Code/2-2-5/main.cpp b/SoftwareDesign/Code/2-2-5/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/SoftwareDesign/Code/2-2-5/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/SoftwareDesign/Code/2-2-5/mainwindow.cpp b/SoftwareDesign/Code/2-2-5/mainwindow.cpp new file mode 100644 index 0000000..962ee42 --- /dev/null +++ b/SoftwareDesign/Code/2-2-5/mainwindow.cpp @@ -0,0 +1,170 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include + +static std::string displayTest; + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); + ui->lineEdit->setText("0"); + result=0.0; + waitForOperand=true; + + connectSlots(); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +//计算 +bool MainWindow::calculate(double operand, QString pendingOperator) +{ + if(pendingOperator == "+") + { + result += operand; + } + else if(pendingOperator == "-") + { + result -= operand; + } + else if(pendingOperator == "*") + { + result *= operand; + } + else if(pendingOperator == "/") + { + if(operand == 0.0) + return false; + result /= operand; + } + return true; +} + +//错误处理 +void MainWindow::abortOperation() +{ + result = 0.0; + pendingOperator.clear(); + ui->lineEdit->setText("0"); + waitForOperand = true; + QMessageBox::warning(this, "运算错误", "除数不能为零"); +} + +//似乎是数字被点击 +void MainWindow::digitClicked() +{ + QPushButton *digitBtn = static_cast(sender()); + QString value = digitBtn->text(); +// if(ui->lineEdit->text() == "0" && value == "0") +// return; + if(waitForOperand) + { + ui->lineEdit->setText(value); + waitForOperand = false; + } + else + { + ui->lineEdit->setText(ui->lineEdit->text() + value); + } +} + +void MainWindow::on_clearBtn_clicked() +{ + //将当前显示的数归零 + ui->lineEdit->setText("0"); + waitForOperand = true; +} + +void MainWindow::on_clearAllBtn_clicked() +{ + //将当前显示的数据归零,并将之前保存的数据运算清除 + ui->lineEdit->setText("0"); + waitForOperand = true; + result = 0.0; + pendingOperator.clear(); +} + +void MainWindow::on_equalBtn_clicked() +{ + double operand = ui->lineEdit->text().toDouble(); + if(pendingOperator.isEmpty()) + return; + if(!calculate(operand, pendingOperator)) + { + abortOperation(); + return; + } + ui->lineEdit->setText(QString::number(result)); + pendingOperator.clear(); + result = 0.0; + waitForOperand = true; +} + +void MainWindow::on_signBtn_clicked() +{ + QString text = ui->lineEdit->text(); + double value = text.toDouble(); + if(value > 0) + { + text.prepend('-'); + } + else if(value < 0) + { + text.remove(0, 1); + } + ui->lineEdit->setText(text); +} + +void MainWindow::operatorClicked() +{ + QPushButton *clickedBtn = qobject_cast(sender()); + QString clickedOperator = clickedBtn->text(); + double operand = ui->lineEdit->text().toDouble(); + if(!pendingOperator.isEmpty()) + { + if(!calculate(operand, pendingOperator)) + { + abortOperation(); + return; + } + ui->lineEdit->setText(QString::number(result)); + } + else + { + result = operand; + } + pendingOperator = clickedOperator; + waitForOperand = true; +} + + +void MainWindow::on_pointBtn_clicked() +{ + if (waitForOperand) + ui->lineEdit->setText("0"); + if (!ui->lineEdit->text().contains('.')) + ui->lineEdit->setText(ui->lineEdit->text() + "."); + waitForOperand = false; +} + +void MainWindow::connectSlots() +{ + + QPushButton *digitBtns[10] = { + ui->digitBtn0, ui->digitBtn1, ui->digitBtn2, ui->digitBtn3, + ui->digitBtn4, ui->digitBtn5, ui->digitBtn6, ui->digitBtn7, + ui->digitBtn8, ui->digitBtn9 + }; + for (auto btn : digitBtns) + connect(btn, &QPushButton::clicked, this, &MainWindow::digitClicked); + QPushButton *operatorBtns[4] = { + ui->addBtn, ui->subtractionBtn, ui->mulBtn, ui->divisionBtn, + }; + for (auto btn : operatorBtns) + connect(btn, &QPushButton::clicked, this, &MainWindow::operatorClicked); +} diff --git a/SoftwareDesign/Code/2-2-5/mainwindow.h b/SoftwareDesign/Code/2-2-5/mainwindow.h new file mode 100644 index 0000000..1bd9cc3 --- /dev/null +++ b/SoftwareDesign/Code/2-2-5/mainwindow.h @@ -0,0 +1,49 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; + + bool calculate(double operand, QString pendingOperator); + //终止运算,清除数据,报错 + void abortOperation(); + //连接信号和槽 + void connectSlots(); + //储存运算符 + QString pendingOperator; + //储存运算结果 + double result; + //标记是否等待一个操作数 + bool waitForOperand; + +private slots: + void on_clearBtn_clicked(); + + void on_clearAllBtn_clicked(); + + void on_equalBtn_clicked(); + + void digitClicked(); + + void on_signBtn_clicked(); + + void operatorClicked(); + + void on_pointBtn_clicked(); + +}; +#endif // MAINWINDOW_H diff --git a/SoftwareDesign/Code/2-2-5/mainwindow.ui b/SoftwareDesign/Code/2-2-5/mainwindow.ui new file mode 100644 index 0000000..af1cde9 --- /dev/null +++ b/SoftwareDesign/Code/2-2-5/mainwindow.ui @@ -0,0 +1,177 @@ + + + MainWindow + + + + 0 + 0 + 405 + 320 + + + + Calculator_v2_4490 + + + + + + + 5 + + + + + + + / + + + + + + + 3 + + + + + + + 4 + + + + + + + * + + + + + + + 2 + + + + + + + Clear + + + + + + + ClearAll + + + + + + + . + + + + + + + 0 + + + + + + + = + + + + + + + 1 + + + + + + + 6 + + + + + + + - + + + + + + + 7 + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + true + + + + + + + 8 + + + + + + + 9 + + + + + + + + + + + + + + + +/- + + + + + + + + + 0 + 0 + 405 + 30 + + + + + + + +