2012年(1008)
分类:
2012-08-01 11:07:52
原文地址:Qt 实现计算器常用功能 作者:luozhiyong131
/*
* 计算器
* 实现计算器常用功能
* 计算器实现函数核心代码
* 环境:Qt Creator
* Lzy 2011-8-26
*/
#include "widgetcounter.h"
#include "ui_widgetcounter.h"
#include
#include
WidgetCounter::WidgetCounter(QWidget *parent) :
QWidget(parent),
ui(new Ui::WidgetCounter)
{
ui->setupUi(this);
ui->lEditPlay->setText("0"); //初始化 显示0
num = num1 = num2 = 0;
f_point = f_num = false;
op = 0;
times = 1;
QObject::connect(this,SIGNAL(NumClick(int)), this, SLOT(Counter(int))); //信号与槽处理数字
}
WidgetCounter::~WidgetCounter()
{
delete ui;
}
void WidgetCounter::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
/*数值处理*/
void WidgetCounter::Counter(int i)
{
if(!f_point)
{
if(!f_num)
{
num1 = num1 * 10 + i;
ui->lEditPlay->setText(QString::number(num1));
}
else
{
num2 = num2 * 10 + i;
ui->lEditPlay->setText(QString::number(num2));
}
}
else
{
times *= 10;
if(!f_num)
{
num1 = num1 + i/times;
ui->lEditPlay->setText(QString::number(num1));
}
else
{
num2 = num2 + i/times;
ui->lEditPlay->setText(QString::number(num2));
}
}
}
/*数字健按下*/
void WidgetCounter::on_Btn1_clicked()
{
emit NumClick(1);
}
void WidgetCounter::on_Btn2_clicked()
{
emit NumClick(2);
}
void WidgetCounter::on_Btn3_clicked()
{
emit NumClick(3);
}
void WidgetCounter::on_Btn4_clicked()
{
emit NumClick(4);
}
void WidgetCounter::on_Btn5_clicked()
{
emit NumClick(5);
}
void WidgetCounter::on_Btn6_clicked()
{
emit NumClick(6);
}
void WidgetCounter::on_Btn7_clicked()
{
emit NumClick(7);
}
void WidgetCounter::on_Btn8_clicked()
{
emit NumClick(8);
}
void WidgetCounter::on_Btn9_clicked()
{
emit NumClick(9);
}
void WidgetCounter::on_Btn0_clicked()
{
emit NumClick(0);
}
/*功能键按下*/
void WidgetCounter::on_BtnDev_clicked()
{
op = '/';
f_num = true;
f_point = false;
times = 1;
}
void WidgetCounter::on_BtnMul_clicked()
{
op = '*';
f_num = true;
f_point = false;
times = 1;
}
void WidgetCounter::on_BtnSub_clicked()
{
op = '-';
f_num = true;
f_point = false;
times = 1;
}
void WidgetCounter::on_BtnPlus_clicked()
{
op = '+';
f_num = true;
f_point = false;
times = 1;
}
/*输出结果*/
void WidgetCounter::on_BtnEql_clicked()
{
switch(op)
{
case '+':
num = num1 + num2;
break;
case '-':
num = num1 - num2;
break;
case '*':
num = num1 * num2;
break;
case '/':
num = num1 / num2;
break;
case 0:
num = num1;
}
ui->lEditPlay->setText(QString::number(num));
num1 = num;
num2 = 0;
op = 0;
times = 1;
}
/*小数点按下*/
void WidgetCounter::on_BtnPoint_clicked()
{
f_point = true;
}
/*清除键按下*/
void WidgetCounter::on_BtnC_clicked()
{
ui->lEditPlay->setText("0");
num1 = 0;
num2 = 0;
op = 0;
f_point = f_num = false;
times = 1;
}
void WidgetCounter::on_BtnBack_clicked()
{
if(!f_num)
{
ui->lEditPlay->backspace();
num1 = ui->lEditPlay->text().toDouble();
ui->lEditPlay->setText(QString::number(num1));
}
else
{
ui->lEditPlay->backspace();
num2 = ui->lEditPlay->text().toDouble();
ui->lEditPlay->setText(QString::number(num2));
}
}
void WidgetCounter::on_BtnCE_clicked()
{
if(f_num)
{
num2 = 0;
ui->lEditPlay->setText(QString::number(num2));
}
else if(op != 0)
op = 0;
else
{
num1 = 0;
ui->lEditPlay->setText(QString::number(num1));
}
}
功程完整代码: Lzy.rar