Merge branch 'main' of github.com:iridiumR/justhomework

This commit is contained in:
iridiumR 2022-05-07 19:40:20 +08:00
commit ef40a7c042
20 changed files with 1411 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,164 @@
#include "cal.h"
QString inToPost(QString infix) {
std::stack<char> stack;
char current = 0; //读入的字符
QString postfix; //写入后缀表达式的字符串
std::map<char, int> priority; //运算符号优先级表
priority['+'] = 0;
priority['-'] = 0;
priority['*'] = 1;
priority['/'] = 1;
priority['^'] = 2;
for (int i = 0; i < infix.length(); ++i) //逐个读取中缀表达式字符串中的字符
{
current = infix[i].toLatin1();
if (isdigit(current)) //如果是数字直接输出
{
postfix.push_back(current);
continue;
}
switch (current) {
case '+':
case '-':
case '*':
case '/':
case '^':
// postfix.push_back('#');
if (i >
0) //如果运算符的前一项不是右括号则说明前一个数字输入完毕,用#标识前面几个字符组成一个数字
{
if (infix[i - 1] != ')')
postfix.push_back('#');
/*if(infix[i-1].isDigit())
postfix.push_back('#');
else
throw "expression is illegality";*/
} else
postfix.push_back('#');
if (!stack
.empty()) //比较目前符号与栈顶符号优先级,低于则出栈,并输出字符串
{
char tempTop = stack.top();
while (tempTop != '(' && priority[current] <= priority[tempTop]) {
stack.pop();
postfix.push_back(tempTop);
if (stack.empty())
break;
tempTop = stack.top();
}
}
stack.push(
current); //符号全部出栈或者遇到了'('或者大于栈顶符号的优先级,将新符号压入栈中
break;
case '.':
postfix.push_back(current);
break;
case '%':
postfix.push_back(current);
break;
case '(':
stack.push(current); //左括号直接入栈
break;
case ')':
postfix.push_back('#'); //右括号说明前方数字输入完成,标识一下
char tempTop;
tempTop = stack.top();
while (tempTop != '(') //直到栈顶元素是左括号才停止循环
{
stack.pop();
postfix.push_back(tempTop);
tempTop = stack.top();
}
stack.pop();
break;
default:
throw "expression has illegality character";
break;
}
}
if (infix[infix.size() - 1] != ')') {
if (infix[infix.size() - 1].isDigit())
postfix.push_back('#');
else
throw "expression is illegality";
}
while (!stack.empty()) {
char tempOut = stack.top();
stack.pop();
postfix.push_back(tempOut);
}
return postfix;
}
double compute(QString s) {
std::stack<double> stack;
QString str;
double curr;
double temNum1;
double temNum2;
for (auto i = s.begin(); i != s.end(); i++) {
if ((*i).isDigit()) {
str.push_back(*i);
continue;
}
switch ((*i).toLatin1()) {
case '+':
temNum1 = stack.top();
stack.pop();
temNum2 = stack.top();
stack.pop();
stack.push(temNum2 + temNum1);
break;
case '-':
temNum1 = stack.top();
stack.pop();
temNum2 = stack.top();
stack.pop();
stack.push(temNum2 - temNum1);
break;
case '*':
temNum1 = stack.top();
stack.pop();
temNum2 = stack.top();
stack.pop();
stack.push(temNum2 * temNum1);
break;
case '/':
temNum1 = stack.top();
stack.pop();
temNum2 = stack.top();
stack.pop();
stack.push(temNum2 / temNum1);
break;
case '^':
temNum1 = stack.top();
stack.pop();
temNum2 = stack.top();
stack.pop();
stack.push(pow(temNum2, temNum1));
break;
case '.':
str.push_back(*i);
break;
case '#':
curr = str.toDouble(); //字符串转换为浮点型
str.clear();
stack.push(curr);
break;
case '%':
curr = stack.top();
stack.pop();
curr *= 0.01;
stack.push(curr);
break;
default:
throw "expression has illegality character";
break;
}
}
curr = stack.top();
return curr;
}

View file

@ -0,0 +1,15 @@
#ifndef CAL_H
#define CAL_H
#include "mainwindow.h"
#include <QDebug>
#include <QKeyEvent>
#include <QMessageBox>
#include <QMouseEvent>
#include <cmath>
#include <iostream>
#include <map>
#include <stack>
QString inToPost(QString infix);
double compute(QString s);
#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,127 @@
#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 = compute(inToPost(ui->lineEdit->text()));
}
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[8] = {
ui->addBtn, ui->subtractionBtn, ui->mulBtn, ui->divisionBtn,
ui->squareBtn, ui->pointBtn, ui->LBtn, ui->RBtn};
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:
default:
break;
}
}

View file

@ -0,0 +1,48 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QKeyEvent>
#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 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

View file

@ -0,0 +1,194 @@
<?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>406</width>
<height>362</height>
</rect>
</property>
<property name="windowTitle">
<string>Calculator_v3_4490</string>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextOnly</enum>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<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="4" column="0">
<widget class="QPushButton" name="digitBtn7">
<property name="text">
<string>7</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="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="2" column="3">
<widget class="QPushButton" name="addBtn">
<property name="text">
<string>+</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="2" column="1">
<widget class="QPushButton" name="digitBtn2">
<property name="text">
<string>2</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="4" column="3">
<widget class="QPushButton" name="mulBtn">
<property name="text">
<string>*</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="5" column="2">
<widget class="QPushButton" name="squareBtn">
<property name="text">
<string>^</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QPushButton" name="digitBtn5">
<property name="text">
<string>5</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="5" column="0">
<widget class="QPushButton" name="pointBtn">
<property name="text">
<string>.</string>
</property>
</widget>
</item>
<item row="3" column="3">
<widget class="QPushButton" name="subtractionBtn">
<property name="text">
<string>-</string>
</property>
</widget>
</item>
<item row="6" column="2" colspan="2">
<widget class="QPushButton" name="equalBtn">
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QPushButton" name="LBtn">
<property name="text">
<string>(</string>
</property>
</widget>
</item>
<item row="1" column="2" colspan="2">
<widget class="QPushButton" name="clearAllBtn">
<property name="text">
<string>ClearAll</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QPushButton" name="clearBtn">
<property name="text">
<string>Back</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QPushButton" name="RBtn">
<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>406</width>
<height>30</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

View file

View file

@ -0,0 +1,29 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Lab3-2-2
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui

View file

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

View file

@ -0,0 +1,71 @@
#include "widget.h"
#include "ui_widget.h"
#include <QColorDialog>
#include <QDebug>
#include <QDialog>
#include <QFileDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QProgressDialog>
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
ui->setupUi(this);
errorDlg = new QErrorMessage(this);
}
Widget::~Widget() { delete ui; }
void Widget::on_pushButton_clicked() {
QColorDialog dialog(Qt::red, this);
dialog.setOption(QColorDialog::ShowAlphaChannel);
dialog.exec();
QColor color = dialog.currentColor();
qDebug() << "color:" << color;
}
void Widget::on_pushButton_5_clicked() {
QString fileName = QFileDialog::getOpenFileName(
this, "文件对话框", "D:", "图片文件(* png * jpg);;文本文件(*txt)");
qDebug() << "fileName:" << fileName;
}
void Widget::on_pushButton_2_clicked() {
bool ok = false;
QFont font = QFontDialog::getFont(&ok, this);
if (ok) {
ui->pushButton_2->setFont(font);
} else {
qDebug() << "没有选择字体";
}
}
void Widget::on_pushButton_6_clicked() {
bool ok;
QString name = QInputDialog::getText(this, "输入对话框", "请输入用户名:",
QLineEdit::Normal, "admin", &ok);
if (ok) {
qDebug() << "name:" << name;
}
int intNum = QInputDialog::getInt(this, "整数输入对话框",
"请输入-1000到1000之间的数值", 100, -1000,
1000, 10, &ok);
if (ok) {
qDebug() << "intNum:" << intNum;
}
double doubleNum = QInputDialog::getDouble(this, "浮点数输入对话框",
"请输入-1000到1000的数值", 0.00,
-1000, 1000, 2, &ok);
if (ok) {
qDebug() << "doubleNum:" << doubleNum;
}
QStringList items;
items << "条目1"
<< "条目2";
QString item = QInputDialog::getItem(
this, "条目输入对话框", "请选择或输入一个条目", items, 0, true, &ok);
if (ok) {
qDebug() << "item:" << item;
}
}

View file

@ -0,0 +1,38 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QErrorMessage>
#include <QWidget>
#include <QWizard>
namespace Ui {
class Widget;
}
class Widget : public QWidget {
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
QWizardPage *createPage1();
QWizardPage *createPage2();
QWizardPage *createPage3();
private slots:
void on_pushButton_clicked();
void on_pushButton_5_clicked();
void on_pushButton_2_clicked();
void on_pushButton_6_clicked();
private:
Ui::Widget *ui;
QErrorMessage *errorDlg;
};
#endif // WIDGET_H

View file

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>50</x>
<y>40</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>颜色对话框</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>50</x>
<y>90</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>字体对话框</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_5">
<property name="geometry">
<rect>
<x>220</x>
<y>50</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>文件对话框</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_6">
<property name="geometry">
<rect>
<x>220</x>
<y>90</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>输入对话框</string>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

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,164 @@
#include "cal.h"
QString inToPost(QString infix) {
std::stack<char> stack;
char current = 0; //读入的字符
QString postfix; //写入后缀表达式的字符串
std::map<char, int> priority; //运算符号优先级表
priority['+'] = 0;
priority['-'] = 0;
priority['*'] = 1;
priority['/'] = 1;
priority['^'] = 2;
for (int i = 0; i < infix.length(); ++i) //逐个读取中缀表达式字符串中的字符
{
current = infix[i].toLatin1();
if (isdigit(current)) //如果是数字直接输出
{
postfix.push_back(current);
continue;
}
switch (current) {
case '+':
case '-':
case '*':
case '/':
case '^':
// postfix.push_back('#');
if (i >
0) //如果运算符的前一项不是右括号则说明前一个数字输入完毕,用#标识前面几个字符组成一个数字
{
if (infix[i - 1] != ')')
postfix.push_back('#');
/*if(infix[i-1].isDigit())
postfix.push_back('#');
else
throw "expression is illegality";*/
} else
postfix.push_back('#');
if (!stack
.empty()) //比较目前符号与栈顶符号优先级,低于则出栈,并输出字符串
{
char tempTop = stack.top();
while (tempTop != '(' && priority[current] <= priority[tempTop]) {
stack.pop();
postfix.push_back(tempTop);
if (stack.empty())
break;
tempTop = stack.top();
}
}
stack.push(
current); //符号全部出栈或者遇到了'('或者大于栈顶符号的优先级,将新符号压入栈中
break;
case '.':
postfix.push_back(current);
break;
case '%':
postfix.push_back(current);
break;
case '(':
stack.push(current); //左括号直接入栈
break;
case ')':
postfix.push_back('#'); //右括号说明前方数字输入完成,标识一下
char tempTop;
tempTop = stack.top();
while (tempTop != '(') //直到栈顶元素是左括号才停止循环
{
stack.pop();
postfix.push_back(tempTop);
tempTop = stack.top();
}
stack.pop();
break;
default:
throw "expression has illegality character";
break;
}
}
if (infix[infix.size() - 1] != ')') {
if (infix[infix.size() - 1].isDigit())
postfix.push_back('#');
else
throw "expression is illegality";
}
while (!stack.empty()) {
char tempOut = stack.top();
stack.pop();
postfix.push_back(tempOut);
}
return postfix;
}
double compute(QString s) {
std::stack<double> stack;
QString str;
double curr;
double temNum1;
double temNum2;
for (auto i = s.begin(); i != s.end(); i++) {
if ((*i).isDigit()) {
str.push_back(*i);
continue;
}
switch ((*i).toLatin1()) {
case '+':
temNum1 = stack.top();
stack.pop();
temNum2 = stack.top();
stack.pop();
stack.push(temNum2 + temNum1);
break;
case '-':
temNum1 = stack.top();
stack.pop();
temNum2 = stack.top();
stack.pop();
stack.push(temNum2 - temNum1);
break;
case '*':
temNum1 = stack.top();
stack.pop();
temNum2 = stack.top();
stack.pop();
stack.push(temNum2 * temNum1);
break;
case '/':
temNum1 = stack.top();
stack.pop();
temNum2 = stack.top();
stack.pop();
stack.push(temNum2 / temNum1);
break;
case '^':
temNum1 = stack.top();
stack.pop();
temNum2 = stack.top();
stack.pop();
stack.push(pow(temNum2, temNum1));
break;
case '.':
str.push_back(*i);
break;
case '#':
curr = str.toDouble();
str.clear();
stack.push(curr);
break;
case '%':
curr = stack.top();
stack.pop();
curr *= 0.01;
stack.push(curr);
break;
default:
throw "error";
break;
}
}
curr = stack.top();
return curr;
}

View file

@ -0,0 +1,15 @@
#ifndef CAL_H
#define CAL_H
#include "mainwindow.h"
#include <QDebug>
#include <QKeyEvent>
#include <QMessageBox>
#include <QMouseEvent>
#include <cmath>
#include <iostream>
#include <map>
#include <stack>
QString inToPost(QString infix);
double compute(QString s);
#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,133 @@
#include "mainwindow.h"
#include "cal.h"
#include "ui_mainwindow.h"
#include <QClipboard>
#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 = compute(inToPost(ui->lineEdit->text()));
}
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[8] = {
ui->addBtn, ui->subtractionBtn, ui->mulBtn, ui->divisionBtn,
ui->squareBtn, ui->pointBtn, ui->LBtn, ui->RBtn};
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:
default:
break;
}
}
void MainWindow::on_actionPaste_triggered() {
QClipboard *board = QApplication::clipboard();
QString text = board->text();
ui->lineEdit->setText(text);
}

View file

@ -0,0 +1,49 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QKeyEvent>
#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 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();
void on_actionPaste_triggered();
};
#endif // MAINWINDOW_H

View file

@ -0,0 +1,206 @@
<?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>406</width>
<height>362</height>
</rect>
</property>
<property name="windowTitle">
<string>Calculator_v3_4490</string>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextOnly</enum>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<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="4" column="0">
<widget class="QPushButton" name="digitBtn7">
<property name="text">
<string>7</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="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="2" column="3">
<widget class="QPushButton" name="addBtn">
<property name="text">
<string>+</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="2" column="1">
<widget class="QPushButton" name="digitBtn2">
<property name="text">
<string>2</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="4" column="3">
<widget class="QPushButton" name="mulBtn">
<property name="text">
<string>*</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="5" column="2">
<widget class="QPushButton" name="squareBtn">
<property name="text">
<string>^</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QPushButton" name="digitBtn5">
<property name="text">
<string>5</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="5" column="0">
<widget class="QPushButton" name="pointBtn">
<property name="text">
<string>.</string>
</property>
</widget>
</item>
<item row="3" column="3">
<widget class="QPushButton" name="subtractionBtn">
<property name="text">
<string>-</string>
</property>
</widget>
</item>
<item row="6" column="2" colspan="2">
<widget class="QPushButton" name="equalBtn">
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QPushButton" name="LBtn">
<property name="text">
<string>(</string>
</property>
</widget>
</item>
<item row="1" column="2" colspan="2">
<widget class="QPushButton" name="clearAllBtn">
<property name="text">
<string>ClearAll</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QPushButton" name="clearBtn">
<property name="text">
<string>Back</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QPushButton" name="RBtn">
<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>406</width>
<height>30</height>
</rect>
</property>
<widget class="QMenu" name="menuMenu">
<property name="title">
<string>Menu</string>
</property>
<addaction name="actionPaste"/>
</widget>
<addaction name="menuMenu"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionPaste">
<property name="text">
<string>Paste (V)</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>