用Qt设计一个简易计算器

Qt工作原理:
事件驱动,信号与槽原理。
要实现简易计算器的设计需要将界面按钮转到槽,具体程序如下:
Widget.h :

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
protected slots:
    void on_btn_0_clicked();
    void on_btn_1_clicked();
    void on_btn_2_clicked();
    void on_btn_3_clicked();
    void on_btn_4_clicked();
    void on_btn_5_clicked();
    void on_btn_6_clicked();
    void on_btn_7_clicked();
    void on_btn_8_clicked();
    void on_btn_9_clicked();
    void on_btn_point_clicked();
    void on_btn_res_clicked();
    void on_btn_add_clicked();
    void on_btn_sub_clicked();
    void on_btn_mul_clicked();
    void on_btn_div_clicked();
    void on_btn_div2_clicked();
    void on_btn__clicked();
    void on_btn_ac_clicked();

private:
    Ui::Widget *ui;

    int a, b;
    char func;
};

#endif // WIDGET_H

Widget.cpp 程序如下:

#include "widget.h"
#include "ui_widget.h"
#include
#include
#include
#include

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}
Widget::~Widget()
{
    delete ui;
}

void Widget::on_btn_0_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"0");
}

void Widget::on_btn_1_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"1");
}
void Widget::on_btn_2_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"2");
}
void Widget::on_btn_3_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"3");
}
void Widget::on_btn_4_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"4");
}
void Widget::on_btn_5_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"5");
}
void Widget::on_btn_6_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"6");
}
void Widget::on_btn_7_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"7");
}
void Widget::on_btn_8_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"8");
}
void Widget::on_btn_9_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"9");
}
void Widget::on_btn_point_clicked()
{
    ui->lineEdit_2->setText(ui->lineEdit_2->text()+"0");
}
void Widget::on_btn_add_clicked()
{
    ui->lineEdit->setText(ui->lineEdit_2->text()+"+");
    ui->lineEdit_2->clear();
}
void Widget::on_btn_sub_clicked()
{
    ui->lineEdit->setText(ui->lineEdit_2->text()+"-");
    ui->lineEdit_2->clear();
}
void Widget::on_btn_mul_clicked()
{
    ui->lineEdit->setText(ui->lineEdit_2->text()+"*");
    ui->lineEdit_2->clear();
}
void Widget::on_btn_div_clicked()
{
    ui->lineEdit->setText(ui->lineEdit_2->text()+"/");
    ui->lineEdit_2->clear();
}
void Widget::on_btn_div2_clicked()
{
    ui->lineEdit->setText(ui->lineEdit_2->text()+"%");
    ui->lineEdit_2->clear();
}
void Widget::on_btn__clicked()
{

}
void Widget::on_btn_ac_clicked()
{
    static int i = 0;
    if (i == 0){
        ui->lineEdit_2->clear();
        i++;
    }else {
        ui->lineEdit_2->clear();
        ui->lineEdit->clear();
        i--;
    }
}

void Widget::on_btn_res_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text()+ui->lineEdit_2->text());
    ui->lineEdit_2->clear();
    QString data = ui->lineEdit->text();

    sscanf(data.toStdString().data(), "%d%c%d", &a, &func, &b);
    float sum;
    switch (func) {
    case '+':
            sum = a + b;
        break;
    case '-':
            sum = a - b;
        break;
    case '*':
            sum = a * b;
        break;
    case '/':
            sum = a / b;
        break;
    case '%':
            sum = a % b;
        break;
    default:
        break;
    }

    ui->lineEdit->setText(data+"=");
    ui->lineEdit_2->setText(QString::number(sum));
}

Ui的界面设计如下:


![在这里插入图片描述](https://img-blog.csdnimg.cn/20191028112505157.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2poZ2tqYXNmZw==,size_16,color_FFFFFF,t_70)

数字1-9设置命名为btn_0 - btn_9;
加减乘除分别命名为btn_add、 btn_sub、btn_mul、btn_div/div2;
清除键和等号键分别命名为btn_ac、btn_res;

你可能感兴趣的:(用Qt设计一个简易计算器)