#include "MyCalculator.h"
#include<iostream>
using
namespace
std;
#include<stack>
#include<vector>
#include<cstdlib>
#include<limits.h>
MyCalculator::MyCalculator(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(
this
);
setWindowTitle(QStringLiteral(
"计算器"
));
ui.textBrowser->setFontPointSize(28);
connect(ui.pushButton0, SIGNAL(clicked()),
this
, SLOT(on_number_Button_clicked()));
connect(ui.pushButton1, SIGNAL(clicked()),
this
, SLOT(on_number_Button_clicked()));
connect(ui.pushButton2, SIGNAL(clicked()),
this
, SLOT(on_number_Button_clicked()));
connect(ui.pushButton3, SIGNAL(clicked()),
this
, SLOT(on_number_Button_clicked()));
connect(ui.pushButton4, SIGNAL(clicked()),
this
, SLOT(on_number_Button_clicked()));
connect(ui.pushButton5, SIGNAL(clicked()),
this
, SLOT(on_number_Button_clicked()));
connect(ui.pushButton6, SIGNAL(clicked()),
this
, SLOT(on_number_Button_clicked()));
connect(ui.pushButton7, SIGNAL(clicked()),
this
, SLOT(on_number_Button_clicked()));
connect(ui.pushButton8, SIGNAL(clicked()),
this
, SLOT(on_number_Button_clicked()));
connect(ui.pushButton9, SIGNAL(clicked()),
this
, SLOT(on_number_Button_clicked()));
connect(ui.pushButton_add, SIGNAL(clicked()),
this
, SLOT(on_action_Button_clicked()));
connect(ui.pushButton_div, SIGNAL(clicked()),
this
, SLOT(on_action_Button_clicked()));
connect(ui.pushButton_mul, SIGNAL(clicked()),
this
, SLOT(on_action_Button_clicked()));
connect(ui.pushButton_sub, SIGNAL(clicked()),
this
, SLOT(on_action_Button_clicked()));
connect(ui.pushButton_right, SIGNAL(clicked()),
this
, SLOT(on_action_Button_clicked()));
connect(ui.pushButton_left, SIGNAL(clicked()),
this
, SLOT(on_action_Button_clicked()));
connect(ui.pushButton_dian, SIGNAL(clicked()),
this
, SLOT(on_comma_Button_clicked()));
connect(ui.pushButton_del, SIGNAL(clicked()),
this
, SLOT(on_action_Button_d_clicked()));
connect(ui.pushButton_re, SIGNAL(clicked()),
this
, SLOT(on_action_Button_c_clicked()));
connect(ui.pushButton_equ, SIGNAL(clicked()),
this
, SLOT(on_action_Button_e_clicked()));
}
void
MyCalculator::on_number_Button_clicked()
{
QPushButton *btn = qobject_cast<qpushbutton*>(sender());
QString number = btn->text();
QString ss = ui.textBrowser->toPlainText();
ui.textBrowser->clear();
ui.textBrowser->append(ss + number);
}
void
MyCalculator::on_action_Button_clicked()
{
QPushButton *btn = qobject_cast<qpushbutton*>(sender());
QString action = btn->text();
QString ss = ui.textBrowser->toPlainText();
ui.textBrowser->clear();
ui.textBrowser->append(ss + action);
}
void
MyCalculator::on_comma_Button_clicked()
{
QPushButton *btn = qobject_cast<qpushbutton*>(sender());
QString action = btn->text();
QString ss = ui.textBrowser->toPlainText();
ui.textBrowser->clear();
ui.textBrowser->append(ss + action);
}
void
MyCalculator::on_action_Button_c_clicked()
{
ui.textBrowser->clear();
}
void
MyCalculator::on_action_Button_d_clicked()
{
QString ss = ui.textBrowser->toPlainText();
ss = ss.left(ss.length() - 1);
ui.textBrowser->clear();
ui.textBrowser->setText(ss);
}
bool
isNum(QString ch)
{
if
(ch >=
'0'
&& ch <=
'9'
|| ch ==
'.'
)
return
true
;
else
return
false
;
}
bool
isOperate(QString ch)
{
if
(ch ==
'+'
|| ch ==
'-'
|| ch ==
'*'
|| ch ==
'/'
|| ch ==
'('
|| ch ==
')'
)
return
true
;
else
return
false
;
}
int
level(QString ch) {
if
(ch ==
'('
)
return
5;
else
if
(ch ==
'*'
|| ch==
'/'
)
return
4;
else
if
(ch ==
'+'
|| ch ==
'-'
)
return
3;
else
if
(ch ==
')'
)
return
2;
}
double
calcu(
double
a ,
double
b, QString c)
{
double
result = 0;
if
(c ==
'+'
)
result = b + a;
else
if
(c ==
'-'
)
result = b - a;
else
if
(c ==
'*'
)
result = b * a;
else
if
(c ==
'/'
)
result = b / a;
else
result = INT_MAX;
return
result;
}
double
getjieguo(QString input)
{
QStack<
double
> Num;
QStack<qstring> Act;
int
a = input.length();
for
(
int
i=0;i<a;i++) {=
""
int
=
""
flag=
"0;//用来判断是否是数字"
xiaoshu=
"1;//用来判断是否是小数部分"
double
=
""
number=
"0;//暂存数字"
qstring=
""
frist=
"input.left(1);"
while
=
""
(isnum(frist))=
""
连续的数字转化为一个整数=
""
if
=
""
(frist=
"="
'.'
=
""
||=
""
2)=
""
10=
""
+=
""
number;=
""
}=
""
else
=
""
*=
""
frist.toint();=
""
input=
"input.right(input.length()"
-=
""
1);=
""
(flag=
"="
1)=
""
num.push(number);=
""
(isoperate(frist))=
""
(!act.empty()=
""
&&=
""
(level(act.top())=
""
> level(frist)))
{
double
a = Num.pop();
double
b = Num.pop();
QString c = Act.pop();
double
result = calcu(a, b, c);
Num.push(result);
if
(frist !=
')'
)
Act.push(frist);
input = input.right(input.length() - 1);
frist = input.left(1);
}
else
if
(!Act.empty() && (level(Act.top()) <= level(frist)))
{
if
(frist !=
'('
)
Act.push(frist);
input = input.right(input.length() - 1);
frist = input.left(1);
}
else
if
(Act.empty())
{
Act.push(frist);
input = input.right(input.length() - 1);
frist = input.left(1);
}
}
if
(frist ==
'='
)
{
Num.clear();
Act.clear();
input = input.right(input.length() - 1);
frist = input.left(1);
}
if
(i + 1 >= a)
{
while
(!Act.empty())
{
double
a1 = Num.pop();
double
b1 = Num.pop();
QString c1 = Act.pop();
double
result = calcu(a1, b1, c1);
Num.push(result);
}
}
}
return
Num.top();
}
void
MyCalculator::on_action_Button_e_clicked()
{
QString input = ui.textBrowser->toPlainText();
double
value = getjieguo(input);
ui.textBrowser->clear();
ui.textBrowser->append(input +
"="
+ QString::number(value));
}
</qpushbutton*></qpushbutton*></a;i++)></qstring></
double
></qpushbutton*></qpushbutton*></qpushbutton*></limits.h></cstdlib></vector></stack></iostream>