QT实现四则运算计算器
2023-12-16 18:38:27
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setMaximumSize(240,300);
this->setMinimumSize(240,300);
this->setWindowTitle("计算器");
QFont f("仿宋",14);
ui->lineEdit->setFont(f);
//按钮上放图片
// QIcon icon=("D:\\");
// ui->back->setIcon(icon);
//改变按钮背景颜色
ui->equal->setStyleSheet("background:green");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btn0_clicked()
{
expression+="0";
ui->lineEdit->setText(expression);
}
void Widget::on_btn1_clicked()
{
expression+="1";
ui->lineEdit->setText(expression);
}
void Widget::on_btn2_clicked()
{
expression+="2";
ui->lineEdit->setText(expression);
}
void Widget::on_btn3_clicked()
{
expression+="3";
ui->lineEdit->setText(expression);
}
void Widget::on_btn4_clicked()
{
expression+="4";
ui->lineEdit->setText(expression);
}
void Widget::on_btn5_clicked()
{
expression+="5";
ui->lineEdit->setText(expression);
}
void Widget::on_btn6_clicked()
{
expression+="6";
ui->lineEdit->setText(expression);
}
void Widget::on_btn7_clicked()
{
expression+="7";
ui->lineEdit->setText(expression);
}
void Widget::on_btn8_clicked()
{
expression+="8";
ui->lineEdit->setText(expression);
}
void Widget::on_btn9_clicked()
{
expression+="9";
ui->lineEdit->setText(expression);
}
void Widget::on_add_clicked()
{
expression+="+";
ui->lineEdit->setText(expression);
}
void Widget::on_sub_clicked()
{
expression+="-";
ui->lineEdit->setText(expression);
}
void Widget::on_mul_clicked()
{
expression+="*";
ui->lineEdit->setText(expression);
}
void Widget::on_div_clicked()
{
expression+="/";
ui->lineEdit->setText(expression);
}
void Widget::on_clear_clicked()
{
expression.clear();
ui->lineEdit->clear();
}
void Widget::on_back_clicked()
{
expression.chop(1);
ui->lineEdit->setText(expression);
}
void Widget::on_left_clicked()
{
expression+="(";
ui->lineEdit->setText(expression);
}
void Widget::on_right_clicked()
{
expression+=")";
ui->lineEdit->setText(expression);
}
void Widget::on_equal_clicked()
{
QStack<int> s_num,s_opt;
char opt[128]={0};
int i=0,temp=0,num1,num2;
QByteArray ba=expression.toLocal8Bit(); //QString 转换为QByteArray
strcpy(opt,ba.data()); //data可以把QByteArray转换为const char *
//opt[i]还没进栈,看opt优先级与栈顶top的优先级情况,对栈进行操作,之后再操作opt
while(opt[i]!='\0'||s_opt.empty()!=true)
{
if(opt[i]>='0'&&opt[i]<='9') //操作数
{
temp=temp*10+opt[i]-'0';
i++;
if(opt[i]<'0'||opt[i]>'9')
{
s_num.push(temp);
temp=0;
}
}
else //操作符
{
if(s_opt.empty()==true|| //操作符入栈情况
Priority(opt[i])>Priority(s_opt.top())|| //1、将要入栈的操作符比栈顶操作符优先级大
(s_opt.top()=='('&&opt[i]!=')')) //2、已经有(入栈,将要入栈的不是)而是其他()的操作符
{ //3、操作符栈为空
s_opt.push(opt[i]);
i++;
continue;
}
if(s_opt.top()=='('&&opt[i]==')') //(!opt)
{ //处理完()内的运算符,剩余(),则直接出栈(
s_opt.pop();
i++;
continue;
}
if(Priority(opt[i])<=Priority(s_opt.top())|| //操作符出栈,i无++
(opt[i]==')'&&s_opt.top()!='(')|| //1、将要入栈的操作符比栈顶操作符优先级小,则先计算已在栈中的
(opt[i]=='\0'&&s_opt.empty()!=true) //2、将要入栈的操作符为),则计算()内的运算符
) //3、没有要进行入栈的操作符了
{
char ch=s_opt.top();
s_opt.pop();
switch(ch)
{
case '+':
num1=s_num.top();
s_num.pop();
num2=s_num.top();
s_num.pop();
s_num.push(num1+num2);
break;
case '-':
num1=s_num.top();
s_num.pop();
num2=s_num.top();
s_num.pop();
s_num.push(num1-num2);
break;
case '*':
num1=s_num.top();
s_num.pop();
num2=s_num.top();
s_num.pop();
s_num.push(num1*num2);
break;
case '/':
num1=s_num.top();
s_num.pop();
num2=s_num.top();
s_num.pop();
s_num.push(num1/num2);
break;
}
}
}
}
ui->lineEdit->setText(QString::number(s_num.top()));
expression.clear();
}
int Widget::Priority(char ch)
{
switch(ch)
{
case '(':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
?
文章来源:https://blog.csdn.net/m0_70732442/article/details/135035244
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!