用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

实现简单计算器

2014-07-07 作者: 云代码会员举报

[c++]代码库

  • 源代码打包下载Lzy.zip(免费下载)[35 次下载]
/*
 * ������
 * ʵ�ּ��������ù��� 
 * ������ʵ�ֺ������Ĵ���
 * ������Qt Creator 
 * Lzy		2011-8-26
 */

#include "widgetcounter.h"
#include "ui_widgetcounter.h"
#include <QObject>
#include <QString>

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.zip(免费下载)[35 次下载]


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...