#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
sum="";
ui->setupUi(this);
connect(ui->pushButton_1,SIGNAL(clicked()),this,SLOT(clickNum1()));
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(clickNum2()));
connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(clickNum3()));
connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(clickNum4()));
connect(ui->pushButton_5,SIGNAL(clicked()),this,SLOT(clickNum5()));
connect(ui->pushButton_6,SIGNAL(clicked()),this,SLOT(clickNum6()));
connect(ui->pushButton_7,SIGNAL(clicked()),this,SLOT(clickNum7()));
connect(ui->pushButton_8,SIGNAL(clicked()),this,SLOT(clickNum8()));
connect(ui->pushButton_9,SIGNAL(clicked()),this,SLOT(clickNum9()));
connect(ui->pushButton_0,SIGNAL(clicked()),this,SLOT(clickNum0()));
connect(ui->pushButton_point,SIGNAL(clicked()),this,SLOT(clickPoint()));
connect(ui->pushButton_lbracket,SIGNAL(clicked()),this,SLOT(clickLbracket()));
connect(ui->pushButton_rbracket,SIGNAL(clicked()),this,SLOT(clickRbracket()));
connect(ui->pushButton_clean,SIGNAL(clicked()),this,SLOT(clickClean()));
connect(ui->pushButton_delete,SIGNAL(clicked()),this,SLOT(clickDelete()));
connect(ui->pushButton_plus,SIGNAL(clicked()),this,SLOT(clickPlus()));
connect(ui->pushButton_minus,SIGNAL(clicked()),this,SLOT(clickMinus()));
connect(ui->pushButton_multi,SIGNAL(clicked()),this,SLOT(clickMulti()));
connect(ui->pushButton_div,SIGNAL(clicked()),this,SLOT(clickDiv()));
connect(ui->pushButton_equl,SIGNAL(clicked()),this,SLOT(clickEqul()));
}
void Widget::clickNum1()//按钮1的槽函数。
{
QString num1="1";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickNum2()//按钮2的槽函数。
{
QString num1="2";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickNum3()//按钮3的槽函数。
{
QString num1="3";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickNum4()//按钮4的槽函数。
{
QString num1="4";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickNum5()//按钮5的槽函数。
{
QString num1="5";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickNum6()//按钮6的槽函数。
{
QString num1="6";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickNum7()//按钮7的槽函数。
{
QString num1="7";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickNum8()//按钮8的槽函数。
{
QString num1="8";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickNum9()//按钮9的槽函数。
{
QString num1="9";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickNum0()//按钮0的槽函数。
{
QString num1="0";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickPoint()//按钮point的槽函数。
{
QString num1=".";
QString num2="0.";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num2);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickLbracket()//按钮lbracket的槽函数。
{
QString num1="(";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickRbracket()//按钮rbracket的槽函数。
{
QString num1=")";
QString str=ui->lineEdit->text();
str=str+num1;
if(ui->lineEdit->text()=="0")
{
ui->lineEdit->setText(num1);
}
else
{
ui->lineEdit->setText(str);
}
}
void Widget::clickClean()//按钮clean的槽函数。
{
QString num1="0";
ui->lineEdit->setText(num1);
}
void Widget::clickDelete()//按钮delete的槽函数。
{
QString a="0";
QString str=ui->lineEdit->text();
if(str.length()==1)
{
ui->lineEdit->setText(a);
}
else if(str.length()>1)
{
str=str.left(str.length()-1);//从左边取长度减去1的字符串。
ui->lineEdit->setText(str);
}
}
void Widget::clickPlus()//按钮+的槽函数。
{
QString num1="+";
QString str=ui->lineEdit->text();
sum=str+num1;
ui->lineEdit->setText(sum);
}
void Widget::clickMinus()//按钮-的槽函数。
{
QString num1="-";
QString str=ui->lineEdit->text();
sum=str+num1;
ui->lineEdit->setText(sum);
}
void Widget::clickMulti()//按钮*的槽函数。
{
QString num1="*";
QString str=ui->lineEdit->text();
sum=str+num1;
ui->lineEdit->setText(sum);
}
void Widget::clickDiv()//按钮/的槽函数。
{
QString num1="/";
QString str=ui->lineEdit->text();
sum=str+num1;
ui->lineEdit->setText(sum);
}
void Widget::clickEqul()//按钮=的槽函数。
{
QString str=ui->lineEdit->text();
QScriptEngine engine;//QScriptEngine类为程序提供一个嵌入式脚本环境,每一个引擎都是一个轻量级自包含的虚拟机。
QScriptValue result = engine.evaluate(str);//调用脚本引擎的evaluate()函数可以执行脚本,此处即可算出之前输入的字符串算式的结果。
ui->lineEdit->setText(result.toString());//调用QScriptValue类的toString()函数将QScriptValue类的执行结果转换成string型。
}
Widget::~Widget()
{
delete ui;
}