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/Lab4/Lab4-1_Calculator4490_v6/mainwindow.cpp

193 lines
4.8 KiB
C++
Raw Normal View History

#include "mainwindow.h"
#include "cal.h"
#include "ui_mainwindow.h"
#include <QClipboard>
#include <QMessageBox>
#include <QScrollBar>
static QString displayText;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
result = 0.0;
displayText += '\n';
isFinished = false;
connectSlots();
}
MainWindow::~MainWindow() { delete ui; }
//错误处理
void MainWindow::abortOperation() {
result = 0.0;
QMessageBox::warning(this, "运算错误", "算式不合法");
}
//数字被点击
void MainWindow::normalClicked() {
QPushButton *digitBtn = static_cast<QPushButton *>(sender());
if (isFinished) {
displayText += '\n';
isFinished = 0;
}
displayText += digitBtn->text();
ui->lineEdit->setPlainText(displayText);
ui->lineEdit->verticalScrollBar()->setValue(
ui->lineEdit->verticalScrollBar()->maximum());
return;
}
void MainWindow::on_clearBtn_clicked() {
//将当前显示的数归零
if (displayText[displayText.length() - 1] == '\n')
return;
displayText.chop(1);
ui->lineEdit->setPlainText(displayText);
ui->lineEdit->verticalScrollBar()->setValue(
ui->lineEdit->verticalScrollBar()->maximum());
}
void MainWindow::on_equalBtn_clicked() {
if (isFinished)
return;
try {
result = compute(inToPost(ui->lineEdit->toPlainText().right(
ui->lineEdit->toPlainText().size() -
(ui->lineEdit->toPlainText().lastIndexOf("\n") + 1))));
} catch (...) {
abortOperation();
}
displayText += '=' + QString::number(result);
ui->lineEdit->setPlainText(displayText);
ui->lineEdit->verticalScrollBar()->setValue(
ui->lineEdit->verticalScrollBar()->maximum());
isFinished = true;
}
//槽链接
void MainWindow::connectSlots() {
QPushButton *digitBtns[21] = {
ui->digitBtn0, ui->digitBtn1, ui->digitBtn2, ui->digitBtn3,
ui->digitBtn4, ui->digitBtn5, ui->digitBtn6, ui->digitBtn7,
ui->digitBtn8, ui->digitBtn9, ui->addBtn, ui->subtractionBtn,
ui->mulBtn, ui->divisionBtn, ui->squareBtn, ui->pointBtn,
ui->LBtn, ui->RBtn, ui->cBtn, ui->sBtn,
ui->perBtn};
for (auto btn : digitBtns)
connect(btn, &QPushButton::clicked, this, &MainWindow::normalClicked);
}
void MainWindow::on_actionPaste_triggered() {
QClipboard *board = QApplication::clipboard();
QString text = board->text();
ui->lineEdit->setPlainText(text);
}
void MainWindow::on_actionClearHistroy_triggered() {
displayText.clear();
displayText += '\n';
ui->lineEdit->setPlainText(displayText);
ui->lineEdit->verticalScrollBar()->setValue(
ui->lineEdit->verticalScrollBar()->maximum());
}
void MainWindow::on_clearLine_clicked() {
isFinished = false;
displayText.chop(displayText.size() - displayText.lastIndexOf('\n') - 1);
ui->lineEdit->setPlainText(displayText);
ui->lineEdit->verticalScrollBar()->setValue(
ui->lineEdit->verticalScrollBar()->maximum());
result = 0.0;
}
void MainWindow::on_ansBtn_clicked() {
if (isFinished) {
isFinished = false;
displayText += '\n';
}
displayText += QString::number(result);
ui->lineEdit->setPlainText(displayText);
ui->lineEdit->verticalScrollBar()->setValue(
ui->lineEdit->verticalScrollBar()->maximum());
}
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_AsciiCircum:
emit ui->squareBtn->clicked();
break;
case Qt::Key_BracketLeft:
emit ui->LBtn->clicked();
break;
case Qt::Key_BracketRight:
emit ui->RBtn->clicked();
break;
case Qt::Key_A:
emit ui->ansBtn->clicked();
break;
case Qt::Key_Equal:
case Qt::Key_Enter:
emit ui->equalBtn->clicked();
break;
case Qt::Key_Backspace:
emit ui->clearBtn->clicked();
break;
case Qt::Key_Delete:
case Qt::Key_Escape:
emit ui->clearLine->clicked();
break;
default:
break;
}
}