#include<string>
#include<vector>
#include<algorithm>
#include<stack>
#include<stdlib.h>
#include "qtwidgetsapplication1.h"
using namespace std;
string format(string str)
{
for (int i = 0; i < str.length(); i++)
{
if (str[i] == '-')
{
if (i == 0)
{
str.insert(0, 1, '0');
}
else if (str[i - 1] == '(')
{
str.insert(i, 1, '0');
}
}
return str;
}
}
int prior(char c)
{
if (c == '+' || c == '-')
{
return 1;
}
else if (c == '*' || c == '/')
{
return 2;
}
else
{
return 0;
}
}
vector<string> hz(string str)
{
vector<string> vs;
stack<char> st;
for (int i = 0; i < str.length(); i++)
{
string tmp = "";
switch (str[i])
{
case'+':
case'-':
case'*':
case'/':
if (st.empty() || st.top() == '(')
{
st.push(str[i]);
}
else
{
while (!st.empty() && prior(st.top()) >= prior(str[i]))
{
tmp += st.top();
vs.push_back(tmp);
st.pop();
tmp = "";
}
st.push(str[i]);
}
break;
case'(':
st.push(str[i]);
break;
case')':
while (st.top() != '(')
{
tmp += st.top();
vs.push_back(tmp);
st.pop();
tmp = "";
}
st.pop();
break;
default:
if ((str[i] >= '0' && str[i] <= '9'))
{
tmp += str[i];
while (i + 1 < str.size() && str[i + 1] >= '0' && str[i + 1] <= '9' || str[i + 1] == '.')
tmp = str[i + 1];
++i;
}
vs.push_back(tmp);
}
}
while (!st.empty())
{
string tmp = "";
tmp += st.top();
vs.push_back(tmp);
st.pop();
}
return vs;
}
double result(vector<string> bh)
{
stack<double> st;
double num, op1, op2;
for (int i = 0; i < bh.size(); i++)
{
string tmp = bh[i];
if (tmp[0] >= '0' && tmp[0] <= '9')
{
num = atof(tmp.c_str());
st.push(num);
}
else if (bh[i] == "+")
{
op2 = st.top();
st.pop();
op1 = st.top();
st.pop();
st.push(op1 + op2);
}
else if (bh[i] == "-")
{
op2 = st.top();
st.pop();
op1 = st.top();
st.pop();
st.push(op1 - op2);
}
else if (bh[i] == "*")
{
op2 = st.top();
st.pop();
op1 = st.top();
st.pop();
st.push(op1 * op2);
}
else if (bh[i] == "/")
{
op2 = st.top();
st.pop();
op1 = st.top();
st.pop();
st.push(op1 / op2);
}
return st.top();
}
}
double solve(string str)
{
str = format(str);
vector<string>bh = hz(str);
double k = result(bh);
return k;
}
void QtWidgetsApplication1::numbuttonPress()
{
int flag;
QString expression;
expression = ui.lineEdit->text();
QPushButton* btn = qobject_cast<QPushButton*>(sender());
QString number = btn->text();
flag = expression.indexOf("=");
if (expression == "0.0")
ui.lineEdit->setText(number);
else if (flag != -1)
{
ui.lineEdit->setText(expression.right(expression.size() - 1 - flag));
ui.lineEdit->insert(number);
}
else
ui.lineEdit->insert(number);
}
void QtWidgetsApplication1::clearPress()
{
ui.lineEdit->setText("0.0");
}
void QtWidgetsApplication1::backPress()
{
QString expression;
expression = ui.lineEdit->text();
if (expression == "0.0")
return;
else if (expression == "")
{
ui.lineEdit->setText("0.0");
}
else
{
expression = expression.left(expression.size() - 1);
ui.lineEdit->setText(expression);
}
}
void QtWidgetsApplication1::eqlbtnPress()
{
ui.lineEdit->insert("=");
QString expression;
expression = ui.lineEdit->text();
double result;
result = solve(expression.toStdString());
if ((int)result == result)
{
ui.lineEdit->insert(QString::number((int)result, 10, 0));
}
else
{
ui.lineEdit->insert(QString::number((int)result, 10, 6));
}
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/365457953496164.png "#left")