diff --git a/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/Lab4-1_Calculator4490_v6.pro b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/Lab4-1_Calculator4490_v6.pro new file mode 100644 index 0000000..bc0f036 --- /dev/null +++ b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/Lab4-1_Calculator4490_v6.pro @@ -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 diff --git a/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/cal.cpp b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/cal.cpp new file mode 100644 index 0000000..715846b --- /dev/null +++ b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/cal.cpp @@ -0,0 +1,180 @@ +#include "cal.h" +QString inToPost(QString infix) { + std::stack stack; + char current = 0; //读入的字符 + QString postfix; //写入后缀表达式的字符串 + + std::map priority; //运算符号优先级表 + priority['+'] = 0; + priority['-'] = 0; + priority['*'] = 1; + priority['/'] = 1; + priority['^'] = 2; + priority['s'] = 2; + priority['c'] = 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 '^': + case 's': + case 'c': + // 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 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; + case 's': + curr = stack.top(); + stack.pop(); + curr = sin(curr); + stack.push(curr); + break; + case 'c': + curr = stack.top(); + stack.pop(); + curr = cos(curr); + stack.push(curr); + break; + default: + throw "error"; + break; + } + } + curr = stack.top(); + return curr; +} diff --git a/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/cal.h b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/cal.h new file mode 100644 index 0000000..198fcdc --- /dev/null +++ b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/cal.h @@ -0,0 +1,15 @@ +#ifndef CAL_H +#define CAL_H +#include "mainwindow.h" +#include +#include +#include +#include +#include +#include +#include +#include + +QString inToPost(QString infix); +double compute(QString s); +#endif // CAL_H diff --git a/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/main.cpp b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/mainwindow.cpp b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/mainwindow.cpp new file mode 100644 index 0000000..ec7ffe9 --- /dev/null +++ b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/mainwindow.cpp @@ -0,0 +1,192 @@ +#include "mainwindow.h" +#include "cal.h" +#include "ui_mainwindow.h" +#include +#include +#include +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(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; + } +} diff --git a/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/mainwindow.h b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/mainwindow.h new file mode 100644 index 0000000..b019e0f --- /dev/null +++ b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/mainwindow.h @@ -0,0 +1,43 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +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(); + //储存运算结果 + double result; + bool isFinished; +private slots: + void on_clearBtn_clicked(); + + void on_equalBtn_clicked(); + + void normalClicked(); + + void on_actionPaste_triggered(); + void on_actionClearHistroy_triggered(); + void on_clearLine_clicked(); + + void keyPressEvent(QKeyEvent *event); + void on_ansBtn_clicked(); +}; +#endif // MAINWINDOW_H diff --git a/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/mainwindow.ui b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/mainwindow.ui new file mode 100644 index 0000000..3721112 --- /dev/null +++ b/SoftwareDesign/Lab4/Lab4-1_Calculator4490_v6/mainwindow.ui @@ -0,0 +1,246 @@ + + + MainWindow + + + + 0 + 0 + 406 + 439 + + + + Calculator_v3_4490 + + + Qt::ToolButtonTextOnly + + + + + + + ^ + + + + + + + 7 + + + + + + + 9 + + + + + + + ( + + + + + + + 1 + + + + + + + ClearLine + + + + + + + QFrame::StyledPanel + + + true + + + + + + + 2 + + + + + + + 8 + + + + + + + - + + + + + + + 4 + + + + + + + 6 + + + + + + + / + + + + + + + + + + + + + + + ) + + + + + + + * + + + + + + + 5 + + + + + + + . + + + + + + + 3 + + + + + + + Back + + + + + + + 0 + + + + + + + % + + + + + + + c + + + + + + + s + + + + + + + Ans + + + + + + + = + + + + + + + + + 0 + 0 + 406 + 30 + + + + + Menu + + + + + + + + + + Paste (V) + + + Ctrl+V + + + + + Clear Histroy(U) + + + Ctrl+U + + + + + +