Qt Creator (C++)计算器简单实现

//widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

#include <QPushButton>
#include <QLineEdit>
#include <QDebug>
class Widget : public QWidget
{
    Q_OBJECT
public slots:
    //按键捕捉
    void num_pushed(){
        QPushButton *digit = static_cast<QPushButton *>(sender());
        qDebug() << digit->text();
        QString data = le->text();
        le->setText(data + digit->text());
    }
    //操作捕捉
    void ope_pushed(){
        QPushButton * op = static_cast<QPushButton *>(sender());
        qDebug() << op->text();
        Operate = op->text();
        QString data = le->text();
        data1 = data.toDouble();
        le->setText(data+op->text());
    }
    // 小数点
    void dot(){
        QPushButton *dots = static_cast<QPushButton *>(sender());
        qDebug() <<dots->text();
        QString data = le->text();
        le->setText(data+dots->text());
    }
    //等号
    void cal(){
        QPushButton *den = static_cast<QPushButton *>(sender());
        qDebug() << den->text();
        QString  data = le->text();
        int start,length;
        for(int k1 = 0;k1<data.length();k1++){
            if (data[k1] != '.' && !isdigit(data[k1].toLatin1())){
                start = k1;break;
            }
        }
        length = data.length()-1;
        QString result = data.mid(start,length);
        data2 = result.toDouble();
        double sum;
        switch (Operate.toStdString().c_str()[0]){
            case '+': sum = data1 + data2; break;
            case '-': sum = data1 - data2; break;
            case '/': sum = data1 / data2; break;
            case '*': sum = data1 * data2; break;
        }
        le->clear();
        le->setText(QString::number(sum));
    }
    //退位
    void tuiwei(){
        QPushButton *den = static_cast<QPushButton *>(sender());
        qDebug() << den->text();
        QString  data = le->text();
        data.chop(1);
        le->setText(data);
    }
    // 清除
    void Clear(){
        QPushButton *C = static_cast<QPushButton *>(sender());
        qDebug() << C->text();
        le->clear();
    }
public:
    Widget(QWidget *parent = 0);
    ~Widget();
    QPushButton *numbt[10];
    QPushButton *addbt,*subbt,*mulbt,*debt,*dotbt,*calbt,*delbt,*clearbt;
    QLineEdit *le;
    QString Operate;
    double data1,data2;
};

#endif // WIDGET_H

 

// widget.cpp
#include "widget.h"
#include <QHBoxLayout>
#include <QGridLayout>
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    // 控件部分
    le = new QLineEdit;
    le->setAlignment(Qt::AlignRight);
    delbt = new QPushButton("<");
    addbt = new QPushButton("+");
    subbt = new QPushButton("-");
    mulbt = new QPushButton("*");
    debt = new QPushButton("/");
    dotbt = new QPushButton(".");
    calbt = new QPushButton("=");
    calbt->setMinimumHeight(63);
    clearbt = new QPushButton("C");
    for(int k1 = 0;k1<10;k1++){
        numbt[k1] = new QPushButton(QString::number(k1));
    }
    QHBoxLayout *hbox = new QHBoxLayout;
    hbox->addWidget(le);
    QGridLayout *grid = new QGridLayout;
    grid->addWidget(numbt[7],0,0,1,1);
    grid->addWidget(numbt[8],0,1,1,1);
    grid->addWidget(numbt[9],0,2,1,1);
    grid->addWidget(debt,0,3,1,1);
    grid->addWidget(clearbt,0,4,1,1);
    grid->addWidget(numbt[4],1,0,1,1);
    grid->addWidget(numbt[5],1,1,1,1);
    grid->addWidget(numbt[6],1,2,1,1);
    grid->addWidget(mulbt,1,3,1,1);
    grid->addWidget(delbt,1,4,1,1);
    grid->addWidget(numbt[1],2,0,1,1);
    grid->addWidget(numbt[2],2,1,1,1);
    grid->addWidget(numbt[3],2,2,1,1);
    grid->addWidget(subbt,2,3,1,1);
    grid->addWidget(calbt,2,4,2,1);
    grid->addWidget(numbt[0],3,0,1,1);
    grid->addWidget(dotbt,3,1,1,2);
    grid->addWidget(addbt,3,3,1,1);
    QVBoxLayout *mainbox = new QVBoxLayout;
    mainbox->addLayout(hbox);
    mainbox->addLayout(grid);
    setLayout(mainbox);
    connect(delbt, SIGNAL(clicked(bool)), this, SLOT(tuiwei()));
    for(int k1 = 0;k1<10;k1++){
        connect(numbt[k1],SIGNAL(clicked(bool)),this,SLOT(num_pushed()));
    }
    connect(addbt,SIGNAL(clicked(bool)),this,SLOT(ope_pushed()));
    connect(subbt,SIGNAL(clicked(bool)),this,SLOT(ope_pushed()));
    connect(mulbt,SIGNAL(clicked(bool)),this,SLOT(ope_pushed()));
    connect(debt,SIGNAL(clicked(bool)),this,SLOT(ope_pushed()));
    connect(dotbt,SIGNAL(clicked(bool)),this,SLOT(dot()));
    connect(calbt,SIGNAL(clicked(bool)),this,SLOT(cal()));
    connect(clearbt,SIGNAL(clicked(bool)),this,SLOT(Clear()));

}

Widget::~Widget()
{

}

http://t.csdn.cn/tOmymicon-default.png?t=N5K3http://t.csdn.cn/tOmym 程序参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值