diff --git a/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/Lab2-3_Calculator4490_v3.pro b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/Lab2-3_Calculator4490_v3.pro new file mode 100644 index 0000000..bc0f036 --- /dev/null +++ b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/Lab2-3_Calculator4490_v3.pro @@ -0,0 +1,26 @@ +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 += \ + cal.cpp \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + cal.h \ + 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/Lab2-3_Calculator4490_v3/cal.cpp b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/cal.cpp new file mode 100644 index 0000000..03454f6 --- /dev/null +++ b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/cal.cpp @@ -0,0 +1,75 @@ +#include // remove() +#include // pow() +#include +#include // stack +#include // runtime_error +#include // string +using std::string; + +// 返回运算符的优先级,值越大,优先级越高 +inline size_t precedence(const char op) { + if (op == '+' || op == '-') + return 1; + if (op == '*' || op == '/') + return 2; + if (op == '^') + return 3; + throw std::runtime_error{string{"表达中包含无效的运算符"} + op}; +} +// 计算 +double execute(std::stack &ops, std::stack &operands) { + double result; + double rhs=operands.top(); // 得到右操作数 + operands.pop(); + double lhs=operands.top(); // 得到左操作数 + operands.pop(); + switch (ops.top()) // 根据两个操作数之间的运算符,执行相应计算 + { + case '+': + result = lhs + rhs; + break; + case '-': + result = lhs - rhs; + break; + case '*': + result = lhs * rhs; + break; + case '/': + result = lhs / rhs; + break; + case '^': + result = std::pow(lhs, rhs); + break; + default: + throw std::runtime_error{string{"invalid operator: "} + ops.top()}; + } + ops.pop(); //计算完成后,该运算符要弹栈 + operands.push(result); //将新计算出来的结果入栈 + return result; +} +double cal(string exp) { + static std::stack operands; //数值 + static std::stack operators; //运算符 + + size_t index{}; //位数 + + operands.push(std::stod(exp, &index)); // 将表达式中第一个数字进栈 + while (true) { + operators.push(exp[index++]); // 将运算符进栈 + size_t i{}; + operands.push( + std::stod(exp.substr(index), + &i)); //将运算符后的数字也进栈,并将数字的位数赋值给 i。 + index += i; //更新 index + if (index == exp.length()) { + while (!operators.empty()) //如果 operators不为空,表示还没有计算完 + execute(operators, operands); + break; + } + //如果表达式还未遍历完,但子表达式中的运算符优先级比其后面的运算符优先级大,就先计算当前的子表达式的值 + while (!operators.empty() && + precedence(exp[index]) <= precedence(operators.top())) + execute(operators, operands); + } + return operands.top(); +} diff --git a/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/cal.h b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/cal.h new file mode 100644 index 0000000..06dde1d --- /dev/null +++ b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/cal.h @@ -0,0 +1,5 @@ +#ifndef CAL_H +#define CAL_H +#include +double cal(std::string exp); +#endif // CAL_H diff --git a/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/main.cpp b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/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/Lab2-3_Calculator4490_v3/mainwindow.cpp b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/mainwindow.cpp new file mode 100644 index 0000000..f3fca90 --- /dev/null +++ b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/mainwindow.cpp @@ -0,0 +1,160 @@ +#include "mainwindow.h" +#include "cal.h" +#include "ui_mainwindow.h" +#include + +static QString displayText; + +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; } + +//错误处理 +void MainWindow::abortOperation() { + result = 0.0; + pendingOperator.clear(); + ui->lineEdit->setText("0"); + isFinished = false; + QMessageBox::warning(this, "运算错误", "算式不合法"); +} + +//数字被点击 +void MainWindow::digitClicked() { + QPushButton *digitBtn = static_cast(sender()); + // if(ui->lineEdit->text() == "0" && value == "0") + // return; + if (!isFinished) { + displayText.append(digitBtn->text()); + ui->lineEdit->setText(displayText); + } else { + return; + } +} + +void MainWindow::on_clearBtn_clicked() { + //将当前显示的数归零 + displayText.chop(1); + ui->lineEdit->setText(displayText); + isFinished = false; +} + +void MainWindow::on_clearAllBtn_clicked() { + //将当前显示的数据归零,并将之前保存的数据运算清除 + displayText.clear(); + ui->lineEdit->setText(displayText); + isFinished = false; + result = 0.0; +} + +void MainWindow::on_equalBtn_clicked() { + try { + result = cal(displayText.toStdString()); + } + + catch (...) { + abortOperation(); + } + isFinished = 1; + ui->lineEdit->setText(QString::number(result)); +} + +//运算符 +void MainWindow::operatorClicked() { + QPushButton *clickedBtn = qobject_cast(sender()); + if (!isFinished) { + displayText.append(clickedBtn->text()); + ui->lineEdit->setText(displayText); + } else { + return; + } +} + +//槽链接 +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[6] = {ui->addBtn, ui->subtractionBtn, + ui->mulBtn, ui->divisionBtn, + ui->squareBtn, ui->pointBtn}; + for (auto btn : operatorBtns) + connect(btn, &QPushButton::clicked, this, &MainWindow::operatorClicked); +} + +void MainWindow::keyPressEvent(QKeyEvent *event) { + switch (event->key()) { + case Qt::Key_0: + emit ui->digitBtn0->clicked(); + break; + case Qt::Key_1: + emit ui->digitBtn1->clicked(); + break; + case Qt::Key_2: + emit ui->digitBtn2->clicked(); + break; + case Qt::Key_3: + emit ui->digitBtn3->clicked(); + break; + case Qt::Key_4: + emit ui->digitBtn4->clicked(); + break; + case Qt::Key_5: + emit ui->digitBtn5->clicked(); + break; + case Qt::Key_6: + emit ui->digitBtn6->clicked(); + break; + case Qt::Key_7: + emit ui->digitBtn7->clicked(); + break; + case Qt::Key_8: + emit ui->digitBtn8->clicked(); + break; + case Qt::Key_9: + emit ui->digitBtn9->clicked(); + break; + case Qt::Key_Plus: + emit ui->addBtn->clicked(); + break; + case Qt::Key_Minus: + emit ui->subtractionBtn->clicked(); + break; + case Qt::Key_Asterisk: + emit ui->mulBtn->clicked(); + break; + case Qt::Key_Slash: + emit ui->divisionBtn->clicked(); + break; + case Qt::Key_Period: + emit ui->pointBtn->clicked(); + break; + case Qt::Key_Equal: + emit ui->equalBtn->clicked(); + case Qt::Key_Backspace: + emit ui->clearBtn->clicked(); + break; + case Qt::Key_Delete: + emit ui->clearAllBtn->clicked(); + break; + case Qt::Key_Enter: + emit ui->equalBtn->clicked(); + break; + case Qt::Key_Escape: + emit ui->clearAllBtn->clicked(); + break; + default: + break; + } +} diff --git a/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/mainwindow.h b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/mainwindow.h new file mode 100644 index 0000000..84efef0 --- /dev/null +++ b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/mainwindow.h @@ -0,0 +1,48 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#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; + + void keyPressEvent(QKeyEvent *e); + + //终止运算,清除数据,报错 + void abortOperation(); + //连接信号和槽 + void connectSlots(); + //储存运算符 + QString pendingOperator; + //储存运算结果 + double result; + //标记是否等待一个操作数 + bool waitForOperand; + + bool isFinished; + +private slots: + void on_clearBtn_clicked(); + + void on_clearAllBtn_clicked(); + + void on_equalBtn_clicked(); + + void digitClicked(); + + void operatorClicked(); +}; +#endif // MAINWINDOW_H diff --git a/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/mainwindow.ui b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/mainwindow.ui new file mode 100644 index 0000000..f0e35e6 --- /dev/null +++ b/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/mainwindow.ui @@ -0,0 +1,180 @@ + + + MainWindow + + + + 0 + 0 + 405 + 320 + + + + Calculator_v3_4490 + + + Qt::ToolButtonTextOnly + + + + + + + 5 + + + + + + + / + + + + + + + 3 + + + + + + + 4 + + + + + + + * + + + + + + + 2 + + + + + + + Back + + + + + + + ClearAll + + + + + + + . + + + + + + + 0 + + + + + + + = + + + + + + + 1 + + + + + + + 6 + + + + + + + - + + + + + + + 7 + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + true + + + + + + + 8 + + + + + + + 9 + + + + + + + + + + + + + + + ^ + + + + + + + + + 0 + 0 + 405 + 30 + + + + + + + + diff --git a/SoftwareDesign/Code/lab2-3-5_KeyEvent/mainwindow.cpp b/SoftwareDesign/Code/lab2-3-5_KeyEvent/mainwindow.cpp index fcc48fa..a4262f2 100644 --- a/SoftwareDesign/Code/lab2-3-5_KeyEvent/mainwindow.cpp +++ b/SoftwareDesign/Code/lab2-3-5_KeyEvent/mainwindow.cpp @@ -21,12 +21,12 @@ void MainWindow::keyPressEvent(QKeyEvent *event) { } void MainWindow::keyReleaseEvent(QKeyEvent *e) { if (e->key() == Qt::Key_Up) { - ui->l->move(ui->l->pos().x(), ui->l->pos().y() - 20); - } else if (e->key() == Qt::Key_Down) { ui->l->move(ui->l->pos().x(), ui->l->pos().y() + 20); + } else if (e->key() == Qt::Key_Down) { + ui->l->move(ui->l->pos().x(), ui->l->pos().y() - 20); } else if (e->key() == Qt::Key_Left) { - ui->l->move(ui->l->pos().x() - 20, ui->l->pos().y()); - } else if (e->key() == Qt::Key_Right) { ui->l->move(ui->l->pos().x() + 20, ui->l->pos().y()); + } else if (e->key() == Qt::Key_Right) { + ui->l->move(ui->l->pos().x() - 20, ui->l->pos().y()); } }