1 Star 0 Fork 0

yycloudywind/Calculator Qt5

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
makefile 19.96 KB
一键复制 编辑 原始数据 按行查看 历史
yycloudywind 提交于 2022-07-27 03:42 . update with anti-encoding files
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
#ifndef DIALOG_H
#define DIALOG_H
#include <QWidget>
#include <QDebug>
#include <QPushButton>
#include <QDialog>
#include <QLabel>
class dialog : public QDialog
{
Q_OBJECT
public:
dialog(QWidget *parent = nullptr);
~dialog();
protected:
QPushButton qd;
QPushButton qx;
QLabel *detail;
protected slots:
void qd_clicked();
void qx_clicked();
};
#endif // DIALOG_H
#ifndef WIDGET_H
#define WIDGET_H
#include <QMainWindow>
#include <QMenuBar>
#include <QMenuBar>
#include <QMenu>
#include <QIcon>
#include <QKeySequence>
#include <QPushButton>
#include <QLabel>
#include <QDebug>
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <stack>
#include <sstream>
#include "dialog.h"
using namespace std;
namespace Ui {
class Widget;
}
class Widget : public QMainWindow
{
Q_OBJECT
public:
explicit Widget(QMainWindow *parent = nullptr);
~Widget();
bool uiInit();
QMenuBar* menuBar = new QMenuBar;
bool updateText(QString _text);
bool wordAnalysis(vector<pair<string, int>>& word, QString expr);
vector<pair<string, int>> word;
QString _text_ = "";
int idx = 0;
int sym;
int err = 0; // 错误
void E();
void E1();
void T();
void T1();
void F();
void Next();
QPoint getPoi(int x, int y);
bool checkText(QString _text);
int prior(int token);
bool isOperator(int token);
vector<pair<string, int>> getPostfix(vector<pair<string, int>> expr);
void popTwoNum(stack<double> &numStack, double &first, double &second);
double getAnswer(const vector<pair<string, int>> &postfix);
private:
Ui::Widget *ui;
QPushButton *num1;
QPushButton *num2;
QPushButton *num3;
QPushButton *num4;
QPushButton *num5;
QPushButton *num6;
QPushButton *num7;
QPushButton *num8;
QPushButton *num9;
QPushButton *num0;
QPushButton *_dot;
QPushButton *_back;
QPushButton *_equal;
QPushButton *_add;
QPushButton *_sub;
QPushButton *_mul;
QPushButton *_div;
QPushButton *_leftbracket;
QPushButton *_clean;
QPushButton *_rightbracket;
QLabel *_output;
QString _zero = "0";
private slots:
void getNum1();
void getNum2();
void getNum3();
void getNum4();
void getNum5();
void getNum6();
void getNum7();
void getNum8();
void getNum9();
void getNum0();
void getAdd();
void getSub();
void getMul();
void getDiv();
void getLBracket();
void getRBracket();
void getBackOption();
void getDot();
void getClean();
void calculate();
void Btn_Clicked();
};
#endif // WIDGET_H
#include "dialog.h"
dialog::dialog(QWidget *parent):
QDialog(parent), qd(this), qx(this)
{
qd.setText("确定");
qd.move(20, 120);
qd.resize(100, 30);
qx.setText("取消");
qx.move(140, 120);
qx.resize(100, 30);
//初始化对话框的大小以及设置其名字
this->resize(260, 170);
this->setWindowTitle("恭喜");
detail = new QLabel(this);
detail->setFixedSize(250, 110);
detail->move(5, 5);
detail->setWordWrap(true);
detail->setText("你发现了一个彩蛋,然而并没有什么用");
detail->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
detail->setFont(QFont("Microsoft YaHei", 10, 75));
//信号与槽的映射
connect(&qd, SIGNAL(clicked()), this, SLOT(qd_clicked()));
connect(&qx, SIGNAL(clicked()), this, SLOT(qx_clicked()));
}
dialog::~dialog() {}
//按钮1的槽
void dialog::qd_clicked()
{
qDebug() << "qd_Clicked()";
done(Accepted);
}
//按钮2的槽
void dialog::qx_clicked()
{
qDebug() << "qx_Clicked()";
done(Rejected);
}
==================================================================================================================
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
==================================================================================================================
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QMainWindow *parent) :
QMainWindow(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
if(!uiInit())
{
qDebug("calculator init error!");
return;
}
connect(num1, SIGNAL(clicked()), this, SLOT(getNum1()));
connect(num2, SIGNAL(clicked()), this, SLOT(getNum2()));
connect(num3, SIGNAL(clicked()), this, SLOT(getNum3()));
connect(num4, SIGNAL(clicked()), this, SLOT(getNum4()));
connect(num5, SIGNAL(clicked()), this, SLOT(getNum5()));
connect(num6, SIGNAL(clicked()), this, SLOT(getNum6()));
connect(num7, SIGNAL(clicked()), this, SLOT(getNum7()));
connect(num8, SIGNAL(clicked()), this, SLOT(getNum8()));
connect(num9, SIGNAL(clicked()), this, SLOT(getNum9()));
connect(num0, SIGNAL(clicked()), this, SLOT(getNum0()));
connect(_add, SIGNAL(clicked()), this, SLOT(getAdd()));
connect(_sub, SIGNAL(clicked()), this, SLOT(getSub()));
connect(_mul, SIGNAL(clicked()), this, SLOT(getMul()));
connect(_div, SIGNAL(clicked()), this, SLOT(getDiv()));
connect(_leftbracket, SIGNAL(clicked()), this, SLOT(getLBracket()));
connect(_rightbracket, SIGNAL(clicked()), this, SLOT(getRBracket()));
connect(_dot, SIGNAL(clicked()), this, SLOT(getDot()));
connect(_clean, SIGNAL(clicked()), this, SLOT(getClean()));
connect(_back, SIGNAL(clicked()), this, SLOT(getBackOption()));
connect(_equal, SIGNAL(clicked()), this, SLOT(calculate()));
}
Widget::~Widget()
{
delete ui;
}
bool Widget::uiInit()
{
this->resize(265, 340);
this->setWindowTitle("Calculator(retain 6 digits)");
this->setMenuBar(menuBar);
QMenu *fileMenu = new QMenu("文件(&F)", menuBar);
menuBar->addMenu(fileMenu);
fileMenu->addAction("你猜哪个有用");
fileMenu->addAction("其实全都没用");
fileMenu->addAction("不过还有希望");
fileMenu->addAction("试试总不会错");
QMenu *editMenu = menuBar->addMenu("编辑(&E)");
menuBar->addMenu(editMenu);
editMenu->addAction("这是假的");
editMenu->addAction("随便试试");
editMenu->addAction("爱信不信", this, SLOT(Btn_Clicked()));
editMenu->addAction("不信你来");
num7 = new QPushButton("7", this);
num7->setFixedSize(60,40);
num7->move(getPoi(0, 1));
num8 = new QPushButton("8", this);
num8->setFixedSize(60,40);
num8->move(getPoi(1, 1));
num9 = new QPushButton("9", this);
num9->setFixedSize(60,40);
num9->move(getPoi(2, 1));
num4 = new QPushButton("4", this);
num4->setFixedSize(60,40);
num4->move(getPoi(0, 2));
num5 = new QPushButton("5", this);
num5->setFixedSize(60,40);
num5->move(getPoi(1, 2));
num6 = new QPushButton("6", this);
num6->setFixedSize(60,40);
num6->move(getPoi(2, 2));
num1 = new QPushButton("1", this);
num1->setFixedSize(60,40);
num1->move(getPoi(0, 3));
num2 = new QPushButton("2", this);
num2->setFixedSize(60,40);
num2->move(getPoi(1, 3));
num3 = new QPushButton("3", this);
num3->setFixedSize(60,40);
num3->move(getPoi(2, 3));
num0 = new QPushButton("0", this);
num0->setFixedSize(60,40);
num0->move(getPoi(1, 4));
_dot = new QPushButton(".", this);
_dot->setFixedSize(60,40);
_dot->move(getPoi(2, 4));
_back = new QPushButton("退格", this);
_back->setFixedSize(60,40);
_back->move(getPoi(0, 4));
_equal = new QPushButton("=", this);
_equal->setFixedSize(60,40);
_equal->move(getPoi(3, 4));
_leftbracket = new QPushButton("(", this);
_leftbracket->setFixedSize(60,40);
_leftbracket->move(getPoi(1, 0));
_clean = new QPushButton("AC", this);
_clean->setFixedSize(60,40);
_clean->move(getPoi(0, 0));
_rightbracket = new QPushButton(")", this);
_rightbracket->setFixedSize(60,40);
_rightbracket->move(getPoi(2, 0));
_add= new QPushButton("+", this);
_add->setFixedSize(60,40);
_add->move(getPoi(3, 3));
_sub = new QPushButton("-", this);
_sub->setFixedSize(60,40);
_sub->move(getPoi(3, 2));
_mul = new QPushButton("*", this);
_mul->setFixedSize(60,40);
_mul->move(getPoi(3, 1));
_div = new QPushButton("/", this);
_div->setFixedSize(60,40);
_div->move(getPoi(3, 0));
_output = new QLabel("0", this);
_output->setFixedSize(255, 60);
_output->move(5, 35);
_output->setAlignment(Qt::AlignRight | Qt::AlignBottom);
_output->setStyleSheet("QLabel { background-color : white}");
_output->setFont(QFont("Microsoft YaHei", 15, 75));
return true;
}
QPoint Widget::getPoi(int x, int y)
{
return QPoint(5+65*x, 110+45*y);
}
bool Widget::updateText(QString _text)
{
_output->setText(_text);
return true;
}
void Widget::getNum1()
{
if(checkText(_text_))
_text_ += "1";
updateText(_text_);
}
void Widget::getNum2()
{
if(checkText(_text_))
_text_ += "2";
updateText(_text_);
}
void Widget::getNum3()
{
if(checkText(_text_))
_text_ += "3";
updateText(_text_);
}
void Widget::getNum4()
{
if(checkText(_text_))
_text_ += "4";
updateText(_text_);
}
void Widget::getNum5()
{
if(checkText(_text_))
_text_ += "5";
updateText(_text_);
}
void Widget::getNum6()
{
if(checkText(_text_))
_text_ += "6";
updateText(_text_);
}
void Widget::getNum7()
{
if(checkText(_text_))
_text_ += "7";
updateText(_text_);
}
void Widget::getNum8()
{
if(checkText(_text_))
_text_ += "8";
updateText(_text_);
}
void Widget::getNum9()
{
if(checkText(_text_))
_text_ += "9";
updateText(_text_);
}
void Widget::getNum0()
{
if(checkText(_text_))
_text_ += "0";
updateText(_text_);
}
void Widget::getAdd()
{
if(checkText(_text_))
_text_ += "+";
updateText(_text_);
}
void Widget::getSub()
{
if(checkText(_text_))
_text_ += "-";
updateText(_text_);
}
void Widget::getMul()
{
if(checkText(_text_))
_text_ += "*";
updateText(_text_);
}
void Widget::getDiv()
{
if(checkText(_text_))
_text_ += "/";
updateText(_text_);
}
void Widget::getLBracket()
{
if(checkText(_text_))
_text_ += "(";
updateText(_text_);
}
void Widget::getRBracket()
{
if(checkText(_text_))
_text_ += ")";
updateText(_text_);
}
void Widget::getDot()
{
if(_text_[_text_.size()-1] >= 0x30 && _text_[_text_.size()-1] <= 0x39)
{
if(checkText(_text_))
_text_ += ".";
updateText(_text_);
}
}
void Widget::getBackOption()
{
if(_text_.size() == 1)
{
_text_ = "";
updateText(_zero);
}
else
{
_text_ = _text_.left(_text_.size() - 1);
updateText(_text_);
}
}
void Widget::getClean()
{
updateText(_zero);
_text_ = "";
qDebug("clean text!");
}
bool Widget::checkText(QString _text)
{
if(_text.size() >= 15) return false;
else return true;
}
bool Widget::wordAnalysis(vector<pair<string, int>>& word, QString _text)
{
string expr = _text.toStdString();
for(unsigned int i=0; i<expr.length(); ++i)
{
// 如果是 + - * / ( )
if(expr[i] == '(' || expr[i] == ')' || expr[i] == '+'
|| expr[i] == '-' || expr[i] == '*' || expr[i] == '/')
{
string tmp;
tmp.push_back(expr[i]);
switch (expr[i])
{
case '+':
word.push_back(make_pair(tmp, 1));
break;
case '-':
word.push_back(make_pair(tmp, 2));
break;
case '*':
word.push_back(make_pair(tmp, 3));
break;
case '/':
word.push_back(make_pair(tmp, 4));
break;
case '(':
word.push_back(make_pair(tmp, 6));
break;
case ')':
word.push_back(make_pair(tmp, 7));
break;
}
}
// 如果是数字开头
else if(expr[i]>='0' && expr[i]<='9')
{
string tmp;
while(expr[i]>='0' && expr[i]<='9')
{
tmp.push_back(expr[i]);
++i;
}
if(expr[i] == '.')
{
++i;
if(expr[i]>='0' && expr[i]<='9')
{
tmp.push_back('.');
while(expr[i]>='0' && expr[i]<='9')
{
tmp.push_back(expr[i]);
++i;
}
}
else
{
return false; // .后面不是数字,词法错误
}
}
word.push_back(make_pair(tmp, 5));
--i;
}
// 如果以.开头
else
{
return false; // 以.开头,词法错误
}
}
return true;
}
void Widget::Next()
{
if(idx < word.size())
sym = word[idx++].second;
else
sym = 0;
}
// E TE'
void Widget::E()
{
T();
E1();
}
// E' +TE' | -TE' | ε
void Widget::E1()
{
if(sym == 1)
{
Next();
T();
E1();
}
else if(sym == 2)
{
Next();
T();
E1();
}
else if(sym != 7 && sym != 0)
{
err = -1;
}
}
// T FT'
void Widget::T()
{
F();
T1();
}
// T' *FT' | /FT' | ε
void Widget::T1()
{
if(sym == 3)
{
Next();
F();
T1();
}
else if(sym == 4)
{
Next();
F();
T1();
}
else if(sym != 1 && sym != 2 && sym != 7 && sym != 0)
{
err = -1;
}
}
// F (E) | d
void Widget::F()
{
if(sym == 5)
{
Next();
}
else if(sym == 6)
{
Next();
E();
if(sym == 7)
{
Next();
}
else
{
err = -1;
}
}
else
{
err = -1;
}
}
int Widget::prior(int token)
{
switch(token)
{
case 1:
case 2:
return 1;
case 3:
case 4:
return 2;
default:
return 0;
}
}
bool Widget::isOperator(int token)
{
switch(token)
{
case 1:
case 2:
case 3:
case 4:
return true;
default:
return false;
}
}
vector<pair<string, int>> Widget::getPostfix(vector<pair<string, int>> expr)
{
vector<pair<string, int>> output;
stack<pair<string, int>> op;
for(unsigned int i = 0; i < expr.size(); i++)
{
pair<string, int> p = expr[i];
if(isOperator(p.second))
{
while(!op.empty() && isOperator(op.top().second) && prior(op.top().second) >= prior(p.second))
{
output.push_back(op.top());
op.pop();
}
op.push(p);
}
else if (p.second == 6)
{
op.push(p);
}
else if (p.second == 7)
{
while(op.top().second != 6)
{
output.push_back(op.top());
op.pop();
}
op.pop();
}
else
{
output.push_back(p);
}
}
while(!op.empty())
{
output.push_back(op.top());
op.pop();
}
return output;
}
void Widget::popTwoNum(stack<double> &numStack, double &first, double &second)
{
first = numStack.top();
numStack.pop();
second = numStack.top();
numStack.pop();
}
double strToDouble(const string &str)
{
double ans;
stringstream ss;
ss << str;
ss >> ans;
return ans;
}
double Widget::getAnswer(const vector<pair<string, int>> &postfix)
{
double first, second;
stack<double> num;
for(unsigned int i = 0; i < postfix.size(); i++)
{
pair<string, int> p = postfix[i];
switch(p.second)
{
case 1:
popTwoNum(num, first, second);
num.push(second + first);
break;
case 2:
popTwoNum(num, first, second);
num.push(second - first);
break;
case 3:
popTwoNum(num, first, second);
num.push(second * first);
break;
case 4:
popTwoNum(num, first, second);
num.push(second / first);
break;
default:
num.push(strToDouble(p.first));
break;
}
}
double result = num.top();
num.pop();
// qDebug() << "the result is: " << num.top() << endl;
return result;
}
void Widget::calculate()
{
word.clear(); idx = 0; err = 0;
if(_text_[0] == '-')
_text_ = "0" + _text_;
else
_text_ = "0+" + _text_;
int err_num = wordAnalysis(word, _text_);
cout << _text_.toStdString() << endl << "Word Analysis:" << endl;
if (-1 == err_num)
{
cout << "Word Error!" << endl;
}
else
{
// 测试输出
vector<pair<string, int>>::iterator beg = word.begin();
for(;beg!=word.end(); ++beg)
cout << " (" << beg->first << ", " << beg->second << ")" << endl;
// 词法正确,进行语法分析
Next();
E();
if (sym || err) // 注意要判断两个条件
{
cout << "Wrong Expression." << endl;
_text_ = "";
updateText("无效输入!");
}
else
{
cout << "Correct Expression." << endl;
vector<pair<string, int>> postfix = getPostfix(word);
beg = postfix.begin();
for(;beg!=postfix.end(); ++beg)
cout << " (" << beg->first << ", " << beg->second << ")" << endl;
double answer = getAnswer(postfix);
qDebug() << "the answer is: " << answer << endl;
string double_to_str = std::to_string(answer);
_text_ = QString::fromStdString(double_to_str);
while(_text_[_text_.size()-1] == '0')
_text_ = _text_.left(_text_.size() - 1);
if(_text_[_text_.size()-1] == '.')
_text_ = _text_.left(_text_.size() - 1);
if(_text_ == "nan")
{
cout << "calculate error" << endl;
_text_ = "";
updateText("结果未定义");
return;
}
else if(_text_ == "inf")
{
cout << "calculate error" << endl;
_text_ = "";
updateText("除数不能为零");
return;
}
updateText(_text_);
return;
}
}
}
//Widget内按钮的响应槽,该按钮按下后Winget会弹出对话框(Dialog)
void Widget::Btn_Clicked()
{
dialog d(this); //Dialog是对话框类,见下
d.exec(); //exec()表明d是个模态对话框,用户不操作该对话框,会阻塞于此。
}
==================================================================================================================
<?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>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yycloudywind/calculator-qt5.git
git@gitee.com:yycloudywind/calculator-qt5.git
yycloudywind
calculator-qt5
Calculator Qt5
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385