下图为运行效果:
Qt version:5.14
IDE:Qt内置编译器
C++
表达式求值方法:中缀表达式转后缀表达式,后缀表达式求值。
后缀表达式求值比较简单,只需要熟练使用栈即可,就不展开描述了
源代码如下:带注释
畅所欲言,欢迎指正
mainwindow.h
#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 = 0);//有参构造(含默认值)
~MainWindow();
void calculate();
int judge(char a, char b);
private:
QString s;
};
#endif // MAINWINDOW_H
cal.h
#ifndef FLOATNUMBER_H
#define FLOATNUMBER_H
#endif // FLOATNUMBER_H
#include
using namespace std;
struct node{
int flag;
union{
double fg; //flag==0时,为数字
char op; //flag==1时,为符号
}exp;
};
mainwindow.cpp
#include "mainwindow.h"
#include //按钮控件的头文件
#include
#include
#include
#include
#include
#include
#include "cal.h"
#define NUM_OF_NUMBER 20
#define NUMBERS 0
#define OPERATORS 1
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
s = "";
setFixedSize(500,600);//固定窗口大小
setWindowTitle("简易计算器");
//声明按钮 设置字体颜色大小
// QPushButton *button_delete = new QPushButton(this);
// button_delete->setText("Del");
QPushButton *button_delete = new QPushButton("DEL",this);
button_delete->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_0 = new QPushButton("0",this);
button_0->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_1 = new QPushButton("1",this);
button_1->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_2 = new QPushButton("2",this);
button_2->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_3 = new QPushButton("3",this);
button_3->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_4 = new QPushButton("4",this);
button_4->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_5 = new QPushButton("5",this);
button_5->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_6 = new QPushButton("6",this);
button_6->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_7 = new QPushButton("7",this);
button_7->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_8 = new QPushButton("8",this);
button_8->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_9 = new QPushButton("9",this);
button_9->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_add = new QPushButton("+",this);
button_add->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_dec = new QPushButton("-",this);
button_dec->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_mul = new QPushButton("*",this);
button_mul->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_div = new QPushButton("/",this);
button_div->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_clear = new QPushButton("CE",this);
button_clear->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_equal = new QPushButton("=",this);
button_equal->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_point = new QPushButton(".",this);
button_point->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_left = new QPushButton("(",this);
button_left->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
QPushButton *button_right = new QPushButton(")",this);
button_right->setStyleSheet("QPushButton{font-family:'宋体';font-size:32px;color:rgb(0,0,0,255);}");
//设置显示窗口
QTextEdit *text = new QTextEdit(this);
text->resize(440,90);//文本框重置大小
text->move(30,10);//移动至此
text->setFontPointSize(16);
//设置显示学号
QLabel *label=new QLabel("学号:2019******",this);
label->move(20,540);
label->resize(300,30);
QFont font;
font.setPointSize(12);
label->setFont(font);
//显示校名图片
// QPixmap myPix("D:/A_Qt/Project/Calculate/hfut.png");
QLabel *label_8=new QLabel(this);
//label_8->setPixmap(myPix);
label_8->move(230,525);
label_8->resize(250,60);
label_8->setStyleSheet("QLabel{border-image: url(:/ahut.png);}");
label_8->setScaledContents(true);
//布局
button_1->resize(80,80);//按钮重置大小
button_1->move(30,130);
button_2->resize(80,80);
button_2->move(120,130);
button_3->resize(80,80);
button_3->move(210,130);
button_add->resize(80,80);//3
button_add->move(300,130);
button_4->resize(80,80);
button_4->move(30,230);
button_5->resize(80,80);
button_5->move(120,230);
button_6->resize(80,80);
button_6->move(210,230);
button_mul->resize(80,80);//7
button_mul->move(300,230);
button_7->resize(80,80);
button_7->move(30,330);
button_8->resize(80,80);
button_8->move(120,330);
button_9->resize(80,80);
button_9->move(210,330);
button_left->resize(80,80);
button_left->move(300,330);
button_0->resize(80,80);
button_0->move(30,430);
button_point->resize(80,80);
button_point->move(120,430);
button_equal->resize(80,80);
button_equal->move(210,430);
button_delete->resize(80,80);
button_delete->move(300,430);
button_dec->resize(80,80);
button_dec->move(390,130);
button_div->resize(80,80);
button_div->move(390,230);
button_right->resize(80,80);
button_right->move(390,330);
button_clear->resize(80,80);
button_clear->move(390,430);
//连接、处理信号
//lambda表达式:用于定义并创建匿名的函数对象
//信号的发送者 发送的信号 信号的接受者 处理的槽函数
connect(button_0,&QPushButton::clicked,
[=]()
{
//button_0->setText("123");
s+="0";
text->setText(s);
}
);
connect(button_1,&QPushButton::clicked,
[=]()
{
s+="1";
text->setText(s);
}
);
connect(button_2,&QPushButton::clicked,
[=]()
{
s+="2";
text->setText(s);
}
);
connect(button_3,&QPushButton::clicked,
[=]()
{
s+="3";
text->setText(s);
}
);
connect(button_4,&QPushButton::clicked,
[=]()
{
s+="4";
text->setText(s);
}
);
connect(button_5,&QPushButton::clicked,
[=]()
{
s+="5";
text->setText(s);
}
);
connect(button_6,&QPushButton::clicked,
[=]()
{
s+="6";
text->setText(s);
}
);
connect(button_7,&QPushButton::clicked,
[=]()
{
s+="7";
text->setText(s);
}
);
connect(button_8,&QPushButton::clicked,
[=]()
{
s+="8";
text->setText(s);
}
);
connect(button_9,&QPushButton::clicked,
[=]()
{
s+="9";
text->setText(s);
}
);
connect(button_add,&QPushButton::clicked,
[=]()
{
s+="+";
text->setText(s);
}
);
connect(button_dec,&QPushButton::clicked,
[=]()
{
s+="-";
text->setText(s);
}
);
connect(button_mul,&QPushButton::clicked,
[=]()
{
s+="*";
text->setText(s);
}
);
connect(button_div,&QPushButton::clicked,
[=]()
{
s+="/";
text->setText(s);
}
);
connect(button_clear,&QPushButton::clicked,
[=]()
{
s="";
text->setText(s);
}
);
connect(button_left,&QPushButton::clicked,
[=]()
{
s+="(";
text->setText(s);
}
);
connect(button_right,&QPushButton::clicked,
[=]()
{
s+=")";
text->setText(s);
}
);
connect(button_point,&QPushButton::clicked,
[=]()
{
s+=".";
text->setText(s);
}
);
connect(button_delete,&QPushButton::clicked,
[=]()
{
s=s.left(s.length()-1);
text->setText(s);
}
);
connect(button_equal,&QPushButton::clicked,
[=]() //[=](){}实现函数内部的信号接收
{
MainWindow::calculate();
text->setText(s);
}
);
}
MainWindow::~MainWindow()
{
}
void MainWindow::calculate()
{
node Node[NUM_OF_NUMBER];
double num1=0;
int len=s.length();
int i=0; //标记字符串位置
int num_of_point=0; //记录小数点个数
int num_and_op=0; //记录浮点数和操作符的个数
int isfloat=0; //标记为小数部分
//读取表达式
while(i<len){
if((s[i]>='0'&&s[i]<='9')){
if(!isfloat){ //整数部分
num1=num1*10+(s[i].toLatin1()-'0');//s[i].toLatin1():QString类型转换为const char*(toLatin1)
i++;
}
else{ //小数部分
double per=1;
for(int k=0;k<num_of_point;k++)
per*=0.1;
num1+=(s[i].toLatin1()-'0')*per;
num_of_point++;
i++;
}
}
else if(s[i].toLatin1()=='.'){ //读到小数点
isfloat=1;
num_of_point=1;
i++;
}
else{ //读到操作符
if(num1){
Node[num_and_op].flag=NUMBERS;
Node[num_and_op].exp.fg=num1;
num1=0;
num_and_op++;
isfloat=0;
num_of_point=0;
}
Node[num_and_op].flag=OPERATORS;
Node[num_and_op].exp.op=s[i].toLatin1();
++num_and_op;
++i;
}
}
if(num1){
Node[num_and_op].flag=NUMBERS;
Node[num_and_op].exp.fg=num1;
++num_and_op;
num1=0;
}
//计算
//中缀表达式转后缀表达式
QStack<node> stack;
node Node_1[NUM_OF_NUMBER];
int j = 0;
for(int i = 0; i < num_and_op;){
if(Node[i].flag){//符号
if(stack.isEmpty())
stack.push_back(Node[i++]);
else{
node tem=stack.top();
int ret=judge(tem.exp.op,Node[i].exp.op);
switch (ret) {
case -1:
stack.push_back(Node[i++]);
break;
case 1:
Node_1[j++]=stack.top();
stack.pop();
break;
default:
stack.pop();
i++;
break;
}
}
}
else{
Node_1[j++]=Node[i++];
}
}
for(;!stack.isEmpty();){
node tem=stack.top();
if(tem.exp.op != '('&& tem.exp.op != ')')
Node_1[j++]=tem;
stack.pop();
}
QStack<double> stack_1;
double d1,d2;
for(int i = 0; i < j; i++){
if(Node_1[i].flag){ //操作符
d2=stack_1.top();
stack_1.pop();
d1=stack_1.top();
stack_1.pop();
switch (Node_1[i].exp.op) {
case '+':
d1+=d2;
break;
case '-':
d1-=d2;
break;
case '*':
d1*=d2;
break;
case '/':
d1/=d2;
break;
default: //相当于#
break;
}
stack_1.push_back(d1);
}
else{
stack_1.push_back(Node_1[i].exp.fg);
}
}
qDebug() << d1;
s += '=';
s += QString::number(d1);
}
//判断两个符号的优先级
int MainWindow::judge(char a, char b)
{
char aim[7][8] = { { ">><<<>>" },{ ">><<<>>" },{ ">>>><>>" },{ ">>>><>>" },{ "<<<<<=1" },{ ">>>>1>>" },{ "<<<<<1=" } };
char sta[7] = { '+','-','*','/','(',')','#' };
char _result;
int i, pa, pb;
for (i = 0; i<6; i++) {
if (a == sta[i])pa = i;
if (b == sta[i])pb = i;
}
_result = aim[pa][pb];
if (_result == '>')return 1;
else if (_result == '<')return -1;
else return 0;
}
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();//以顶层的方式弹出窗口控件
return a.exec();
}
若嫌复制粘贴代码麻烦且会出错,这里有完整的项目文件
CS·恰烂钱·恶心·DN 居然会根据下载量自动涨积分!!!
而且更改后又自动涨积分
祝~早日倒闭
资源链接点这里~
既来之~
则赞之~