代码语言:javascript
复制
#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QDebug>
#include <stack>
using namespace std;
bool cmopareFun(QChar l,QChar r)
{
if(r == '*' || r == '/')
{
if(l == '*' || r== '/')
return false;
else
return true;
}
else
{
return false;
}
}
double calc(QString str)
{
double res = 0;
double temp = 0;
QString str1 = NULL;
QString str2 = NULL;
stack<QChar> stackFun;
stack<double> stackNum;
for(int i=0;i<str.length();i++)
{
if(str[i].isDigit())
{
str1.append(str[i]);
}
else
{
str1.append(' ');
if(stackFun.size() == 0)
{
stackFun.push(str[i]);
}
else if(::cmopareFun(stackFun.top(),str[i]))
{
stackFun.push(str[i]);
}
else
{
str1.append(stackFun.top());
stackFun.pop();
stackFun.push(str[i]);
}
str1.append(' ');
}
}
while(stackFun.size() >0 )
{
str1.append(' ');
str1.append(stackFun.top());
stackFun.pop();
}
qDebug() << str1;//
for(int i=0;i<str1.length();i++)
{
if(str1[i].isSpace())
{
if(str2.length()>0)
{
temp = str2.toDouble();
stackNum.push(temp);
str2.clear();
}
continue;
}
else if(str1[i].isDigit())
{
str2.append(str1[i]);
}
else
{
double t1,t2;
if(stackNum.size() == 0)
{
str2.append(str1[i]);
continue;
}
else if(stackNum.size() == 1)
{
t1 = 0;
t2 = stackNum.top();
stackNum.pop();
}
else
{
t2 = stackNum.top();
stackNum.pop();
t1 = stackNum.top();
stackNum.pop();
}
if(str1[i] == '+')
stackNum.push(t1 + t2);
if(str1[i] == '-')
stackNum.push(t1 - t2);
if(str1[i] == '*')
stackNum.push(t1 * t2);
if(str1[i] == '/')
stackNum.push(t1 / t2);
}
}
if(str2.length() > 0)
{
stackNum.push(str1.toDouble());
}
res = stackNum.top();
stackNum.pop();
return res;
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->pushButton,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_2,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_3,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_4,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_6,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_7,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_9,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_10,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_11,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_12,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_13,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_14,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_15,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_16,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_17,&QPushButton::pressed,this,dealNum);
connect(ui->pushButton_18,&QPushButton::pressed,this,dealNum);
}
Widget::~Widget()
{
delete ui;
}
void Widget::dealNum()
{
QPushButton* pSender = (QPushButton*)this->sender();
QString str = ui->lineEdit->text();
QString add = pSender->text();
double left = 0, right = 0, res = 0;
char calc = 0;
if(add == "c")
{
str.chop(1);
}
else if(isdigit(*add.toStdString().data()))
{
if(str == "0")
str.chop(1);
str += add;
}
else
{
//first function
// if(sscanf(str.toStdString().data(),"%lf%c%lf",&left,&calc,&right) != 3)
// goto RES;
// switch(calc)
// {
// case '+':
// res = left + right;
// break;
// case '-':
// res = left - right;
// break;
// case '*':
// res = left * right;
// break;
// case '/':
// res = left / right;
// break;
// default:
// break;
// }
// str = QString::number(res);
// RES:
// if(strcmp(add.toStdString().data(),"=") != 0)
// str += add;
//second function
if(*add.toStdString().data() == '=')
{
res = ::calc(str);
ui->lineEdit->setText(QString::number(res));
return;
}
else
{
str += add;
}
}
ui->lineEdit->setText(str);
}