如此美妙的修正

This commit is contained in:
iridiumR 2022-03-29 16:42:01 +08:00
parent e8fa6e2d7b
commit 6acdb21e2d
7 changed files with 439 additions and 0 deletions

View file

@ -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

View file

@ -0,0 +1,75 @@
#include <algorithm> // remove()
#include <cmath> // pow()
#include <iostream>
#include <stack> // stack<T>
#include <stdexcept> // runtime_error
#include <string> // 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<char> &ops, std::stack<double> &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<double> operands; //存储表达式中的运算符
static std::stack<char> operators; //存储表达式中的数值
//移除用户输入表达式中包含的无用的空格
size_t index{};
//每个表达式必须以数字开头,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();
}

View file

@ -0,0 +1,5 @@
#ifndef CAL_H
#define CAL_H
#include <iostream>
double cal(std::string exp);
#endif // CAL_H

View file

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

View file

@ -0,0 +1,94 @@
#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");
waitForOperand = true;
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() {
result = cal(displayText.toStdString());
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::on_squareBtn_clicked() {}
void MainWindow::on_pointBtn_clicked() {}
void MainWindow::on_signBtn_clicked() {}

View file

@ -0,0 +1,51 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
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 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 on_signBtn_clicked();
void operatorClicked();
void on_pointBtn_clicked();
void on_squareBtn_clicked();
};
#endif // MAINWINDOW_H

View file

@ -0,0 +1,177 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>405</width>
<height>320</height>
</rect>
</property>
<property name="windowTitle">
<string>Calculator_v2_4490</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="1">
<widget class="QPushButton" name="digitBtn5">
<property name="text">
<string>5</string>
</property>
</widget>
</item>
<item row="5" column="3">
<widget class="QPushButton" name="divisionBtn">
<property name="text">
<string>/</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="digitBtn3">
<property name="text">
<string>3</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="digitBtn4">
<property name="text">
<string>4</string>
</property>
</widget>
</item>
<item row="4" column="3">
<widget class="QPushButton" name="mulBtn">
<property name="text">
<string>*</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="digitBtn2">
<property name="text">
<string>2</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="clearBtn">
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="clearAllBtn">
<property name="text">
<string>ClearAll</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QPushButton" name="pointBtn">
<property name="text">
<string>.</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QPushButton" name="digitBtn0">
<property name="text">
<string>0</string>
</property>
</widget>
</item>
<item row="1" column="2" colspan="2">
<widget class="QPushButton" name="equalBtn">
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="digitBtn1">
<property name="text">
<string>1</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QPushButton" name="digitBtn6">
<property name="text">
<string>6</string>
</property>
</widget>
</item>
<item row="3" column="3">
<widget class="QPushButton" name="subtractionBtn">
<property name="text">
<string>-</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QPushButton" name="digitBtn7">
<property name="text">
<string>7</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="4">
<widget class="QLineEdit" name="lineEdit">
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QPushButton" name="digitBtn8">
<property name="text">
<string>8</string>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QPushButton" name="digitBtn9">
<property name="text">
<string>9</string>
</property>
</widget>
</item>
<item row="2" column="3">
<widget class="QPushButton" name="addBtn">
<property name="text">
<string>+</string>
</property>
</widget>
</item>
<item row="5" column="2">
<widget class="QPushButton" name="squareBtn">
<property name="text">
<string>^</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>405</width>
<height>30</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>