就这样吧

This commit is contained in:
iridiumR 2022-05-17 17:22:31 +08:00
parent ad8f153aaa
commit 9b1d453853
4 changed files with 92 additions and 24 deletions

View file

@ -59,9 +59,6 @@ QString inToPost(QString infix) {
case '.':
postfix.push_back(current);
break;
case '%':
postfix.push_back(current);
break;
case '(':
stack.push(current); //左括号直接入栈
break;

View file

@ -2,10 +2,11 @@
#include "cal.h"
#include "ui_mainwindow.h"
#include <QClipboard>
#include <QDebug>
#include <QMessageBox>
#include <QScrollBar>
static QString displayText;
static QString calcLog;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
@ -52,13 +53,19 @@ void MainWindow::on_equalBtn_clicked() {
if (isFinished)
return;
try {
result = compute(inToPost(ui->lineEdit->toPlainText().right(
QString s = inToPost(ui->lineEdit->toPlainText().right(
ui->lineEdit->toPlainText().size() -
(ui->lineEdit->toPlainText().lastIndexOf("\n") + 1))));
(ui->lineEdit->toPlainText().lastIndexOf("\n") + 1)));
std::cout << s.toStdString() << std::endl;
result = compute(s);
} catch (...) {
abortOperation();
}
calcLog += ui->lineEdit->toPlainText().right(
ui->lineEdit->toPlainText().size() -
(ui->lineEdit->toPlainText().lastIndexOf("\n") + 1)) +
'=' + QString::number(result) + '\n';
displayText += '=' + QString::number(result);
ui->lineEdit->setPlainText(displayText);
ui->lineEdit->verticalScrollBar()->setValue(
@ -75,7 +82,7 @@ void MainWindow::connectSlots() {
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};
ui->sciBtn};
for (auto btn : digitBtns)
connect(btn, &QPushButton::clicked, this, &MainWindow::normalClicked);
}
@ -94,6 +101,12 @@ void MainWindow::on_actionClearHistroy_triggered() {
ui->lineEdit->verticalScrollBar()->maximum());
}
void MainWindow::on_actionShow_History_H_triggered() {
ui->lineEdit->setPlainText(calcLog);
ui->lineEdit->verticalScrollBar()->setValue(
ui->lineEdit->verticalScrollBar()->maximum());
}
void MainWindow::on_clearLine_clicked() {
isFinished = false;
displayText.chop(displayText.size() - displayText.lastIndexOf('\n') - 1);
@ -165,15 +178,21 @@ void MainWindow::keyPressEvent(QKeyEvent *event) {
case Qt::Key_AsciiCircum:
emit ui->squareBtn->clicked();
break;
case Qt::Key_BracketLeft:
case Qt::Key_ParenLeft:
emit ui->LBtn->clicked();
break;
case Qt::Key_BracketRight:
case Qt::Key_ParenRight:
emit ui->RBtn->clicked();
break;
case Qt::Key_A:
emit ui->ansBtn->clicked();
break;
case Qt::Key_S:
emit ui->sBtn->clicked();
break;
case Qt::Key_C:
emit ui->cBtn->clicked();
break;
case Qt::Key_Equal:
case Qt::Key_Enter:
emit ui->equalBtn->clicked();
@ -190,3 +209,25 @@ void MainWindow::keyPressEvent(QKeyEvent *event) {
break;
}
}
void MainWindow::on_actionc_triggered() {
if (isFinished) {
isFinished = false;
displayText += '\n';
}
displayText += "299792458";
ui->lineEdit->setPlainText(displayText);
ui->lineEdit->verticalScrollBar()->setValue(
ui->lineEdit->verticalScrollBar()->maximum());
}
void MainWindow::on_actionpi_triggered() {
if (isFinished) {
isFinished = false;
displayText += '\n';
}
displayText += "3.1415926535";
ui->lineEdit->setPlainText(displayText);
ui->lineEdit->verticalScrollBar()->setValue(
ui->lineEdit->verticalScrollBar()->maximum());
}

View file

@ -39,5 +39,8 @@ private slots:
void keyPressEvent(QKeyEvent *event);
void on_ansBtn_clicked();
void on_actionc_triggered();
void on_actionpi_triggered();
void on_actionShow_History_H_triggered();
};
#endif // MAINWINDOW_H

View file

@ -6,12 +6,12 @@
<rect>
<x>0</x>
<y>0</y>
<width>406</width>
<height>439</height>
<width>414</width>
<height>479</height>
</rect>
</property>
<property name="windowTitle">
<string>Calculator_v3_4490</string>
<string>Calculator_v6_4490</string>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextOnly</enum>
@ -168,13 +168,6 @@
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QPushButton" name="perBtn">
<property name="text">
<string>%</string>
</property>
</widget>
</item>
<item row="7" column="3">
<widget class="QPushButton" name="cBtn">
<property name="text">
@ -189,6 +182,13 @@
</property>
</widget>
</item>
<item row="8" column="2" colspan="2">
<widget class="QPushButton" name="equalBtn">
<property name="text">
<string>=</string>
</property>
</widget>
</item>
<item row="8" column="1">
<widget class="QPushButton" name="ansBtn">
<property name="text">
@ -196,10 +196,10 @@
</property>
</widget>
</item>
<item row="8" column="2" colspan="2">
<widget class="QPushButton" name="equalBtn">
<item row="8" column="0">
<widget class="QPushButton" name="sciBtn">
<property name="text">
<string>=</string>
<string>*10^</string>
</property>
</widget>
</item>
@ -210,7 +210,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>406</width>
<width>414</width>
<height>30</height>
</rect>
</property>
@ -220,8 +220,17 @@
</property>
<addaction name="actionPaste"/>
<addaction name="actionClearHistroy"/>
<addaction name="actionShow_History_H"/>
</widget>
<widget class="QMenu" name="menuConstant">
<property name="title">
<string>Constant</string>
</property>
<addaction name="actionc"/>
<addaction name="actionpi"/>
</widget>
<addaction name="menuMenu"/>
<addaction name="menuConstant"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionPaste">
@ -234,12 +243,30 @@
</action>
<action name="actionClearHistroy">
<property name="text">
<string>Clear Histroy(U)</string>
<string>Clear Buffer(U)</string>
</property>
<property name="shortcut">
<string>Ctrl+U</string>
</property>
</action>
<action name="actionc">
<property name="text">
<string>c</string>
</property>
</action>
<action name="actionpi">
<property name="text">
<string>pi</string>
</property>
</action>
<action name="actionShow_History_H">
<property name="text">
<string>Show History(H)</string>
</property>
<property name="shortcut">
<string>Ctrl+H</string>
</property>
</action>
</widget>
<resources/>
<connections/>