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/Lab2/2-2-5/mainwindow.cpp

171 lines
3.8 KiB
C++
Raw Normal View History

2022-03-29 07:02:47 +00:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
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<QPushButton*>(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<QPushButton *>(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);
}