#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 转换为QByteArraystrcpy(opt,ba.data()); //data可以把QByteArray转换为const char *//opt[i]还没进栈,看opt优先级与栈顶top的优先级情况,对栈进行操作,之后再操作optwhile(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;}
}