This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/SoftwareDesign/Code/Lab2-3_Calculator4490_v3/mainwindow.cpp
iridiumR 19ba10a89f 2-3
2022-04-19 21:45:51 +08:00

160 lines
3.8 KiB
C++

#include "mainwindow.h"
#include "cal.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
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<QPushButton *>(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<QPushButton *>(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;
}
}