代码拉取完成,页面将自动刷新
#include "calculatordialog.h"
#include "ui_calculatordialog.h"
#include <QScriptEngine>
#include <QRegExp>
#include <QDebug>
// 构造函数
CalculatorDialog::CalculatorDialog(QWidget *parent) :
QDialog(parent),
_ui(new Ui::CalculatorDialog)
{
_ui->setupUi(this);
// 设置显示框为无边框样式
_ui->LineEdt_Top->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
_ui->LineEdt_Down->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
// 添加按钮的快捷键
_ui->ButtonNum_0->setShortcut(Qt::Key_0);
_ui->ButtonNum_1->setShortcut(Qt::Key_1);
_ui->ButtonNum_2->setShortcut(Qt::Key_2);
_ui->ButtonNum_3->setShortcut(Qt::Key_3);
_ui->ButtonNum_4->setShortcut(Qt::Key_4);
_ui->ButtonNum_5->setShortcut(Qt::Key_5);
_ui->ButtonNum_6->setShortcut(Qt::Key_6);
_ui->ButtonNum_7->setShortcut(Qt::Key_7);
_ui->ButtonNum_8->setShortcut(Qt::Key_8);
_ui->ButtonNum_9->setShortcut(Qt::Key_9);
_ui->ButtonEqual->setShortcut(Qt::Key_Enter); // 小键盘的回车
_ui->ButtonPlus->setShortcut(Qt::Key_Plus);
_ui->ButtonMinus->setShortcut(Qt::Key_Minus);
_ui->ButtonTimes->setShortcut(Qt::Key_Asterisk);
_ui->ButtonDivision->setShortcut(Qt::Key_Slash);
_ui->ButtonDot->setShortcut(Qt::Key_Period);
_ui->ButtonClear->setShortcut(Qt::Key_Space);
_ui->ButtonPercent->setShortcut(Qt::Key_Percent);
_ui->ButtonDel->setShortcut(Qt::Key_Backspace);
display();
}
CalculatorDialog::~CalculatorDialog()
{
delete _ui;
}
void CalculatorDialog::display( bool iCompute /*= false*/ )
{
QString strUp,strDown;
if( !iCompute )
{
strDown = _strCurInput;
}
else
{
strUp = _strCurInput;
strDown =_strResult;
// 将运算结果作为当前输入
_strCurInput = _strResult;
// 计算并显示之后,将结果和公式内容清空
_strResult.clear();
_strEquation.clear();
}
// 如果字符串长度超过控件宽度,会自动换行,导致后面的输入看不到
_ui->LineEdt_Top->setText(strUp);
_ui->LineEdt_Down->setText(strDown);
}
void CalculatorDialog::on_ButtonNum_0_clicked()
{
if( _strCurInput == "0" )
return;
_strCurInput += "0";
display();
}
void CalculatorDialog::on_ButtonNum_1_clicked()
{
if( _strCurInput == "0" )
_strCurInput.clear();
_strCurInput += "1";
display();
}
void CalculatorDialog::on_ButtonNum_2_clicked()
{
if(_strCurInput == "0" )
_strCurInput.clear();
_strCurInput += "2";
display();
}
void CalculatorDialog::on_ButtonNum_3_clicked()
{
if(_strCurInput == "0" )
_strCurInput.clear();
_strCurInput += "3";
display();
}
void CalculatorDialog::on_ButtonNum_4_clicked()
{
if(_strCurInput == "0" )
_strCurInput.clear();
_strCurInput += "4";
display();
}
void CalculatorDialog::on_ButtonNum_5_clicked()
{
if(_strCurInput == "0" )
_strCurInput.clear();
_strCurInput += "5";
display();
}
void CalculatorDialog::on_ButtonNum_6_clicked()
{
if(_strCurInput == "0" )
_strCurInput.clear();
_strCurInput += "6";
display();
}
void CalculatorDialog::on_ButtonNum_7_clicked()
{
if(_strCurInput == "0" )
_strCurInput.clear();
_strCurInput += "7";
display();
}
void CalculatorDialog::on_ButtonNum_8_clicked()
{
if(_strCurInput == "0" )
_strCurInput.clear();
_strCurInput += "8";
display();
}
void CalculatorDialog::on_ButtonNum_9_clicked()
{
if(_strCurInput == "0" )
_strCurInput.clear();
_strCurInput += "9";
display();
}
void CalculatorDialog::on_ButtonPlus_clicked()
{
if( _strCurInput.isEmpty() || _strCurInput.endsWith('+') || _strCurInput.endsWith('-') ||
_strCurInput.endsWith("×") || _strCurInput.endsWith("÷") )
{
return;
}
_strCurInput += "+";
display();
}
void CalculatorDialog::on_ButtonMinus_clicked()
{
if( _strCurInput.isEmpty() || _strCurInput.endsWith('+') || _strCurInput.endsWith('-') ||
_strCurInput.endsWith("×") || _strCurInput.endsWith("÷") )
{
return;
}
_strCurInput += "-";
display();
}
void CalculatorDialog::on_ButtonTimes_clicked()
{
if( _strCurInput.isEmpty() || _strCurInput.endsWith('+') || _strCurInput.endsWith('-') ||
_strCurInput.endsWith("×") || _strCurInput.endsWith("÷") )
{
return;
}
_strCurInput += "×";
display();
}
void CalculatorDialog::on_ButtonDivision_clicked()
{
if( _strCurInput.isEmpty() || _strCurInput.endsWith('+') || _strCurInput.endsWith('-') ||
_strCurInput.endsWith("×") || _strCurInput.endsWith("÷") )
{
return;
}
_strCurInput += "÷";
display();
}
void CalculatorDialog::on_ButtonDot_clicked()
{
QString strLastNum = _strCurInput;
strLastNum.replace("×","*");
strLastNum.replace("÷","/");
// 最近的数字中如果有小数点,则不能继续添加点
int lastOperSign = strLastNum.lastIndexOf( QRegExp("[+-*/]") );
if( -1 != lastOperSign )
{
strLastNum = strLastNum.right(strLastNum.length()-lastOperSign);
}
if( strLastNum.contains('.') )
return;
_strCurInput += ".";
display();
}
void CalculatorDialog::on_ButtonEqual_clicked()
{
if( _strCurInput.isEmpty() || _strCurInput.endsWith('+') || _strCurInput.endsWith('-') ||
_strCurInput.endsWith("×") || _strCurInput.endsWith("÷") )
{
return;
}
// 以.结束时,补个零
if( _strCurInput.endsWith('.') )
_strCurInput += '0';
_strEquation = _strCurInput;
_strCurInput = _strCurInput + "=";
_strEquation.replace("×","*");
_strEquation.replace("÷","/");
QScriptEngine scriptEng;
QScriptValue scriptVal = scriptEng.evaluate(_strEquation);
if( scriptEng.hasUncaughtException() )
{
qDebug() << "The equation is: " << _strEquation;
qDebug() << scriptVal.toString();
_strResult = "Error";
}
else
_strResult = scriptVal.toString();
display(true);
}
void CalculatorDialog::on_ButtonClear_clicked()
{
_strCurInput = "0";
_strResult.clear();
display();
}
void CalculatorDialog::on_ButtonDel_clicked()
{
_strCurInput.remove( _strCurInput.length()-1, 1 );
if( _strCurInput.length() == 0 )
_strCurInput = "0";
display();
}
void CalculatorDialog::on_ButtonPercent_clicked()
{
if( _strCurInput == "0" )
return;
bool isNum = false;
double curVal = _strCurInput.toDouble(&isNum);
if( !isNum )
return;
_strCurInput += "%";
curVal /= 100.;
_strResult = QString::number(curVal);
display(true);
}
void CalculatorDialog::keyPressEvent(QKeyEvent *ev)
{
if( !ev )
return;
qDebug() << "keyboard input idenfiter number : " << ev->key();
switch( ev->key() )
{
case Qt::Key_Return: // 主键盘上的回车
case Qt::Key_Equal: // 主键盘上的等号
{
on_ButtonEqual_clicked();
}
break;
default:
break;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。