其他分享
首页 > 其他分享> > 用Qt设计一个简易计算器

用Qt设计一个简易计算器

作者:互联网

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

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QString>

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<QDebug>
#include<QString>
#include<stdlib.h>
#include<string.h>

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));
}

标签:Widget,clicked,Qt,void,简易,ui,计算器,lineEdit,btn
来源: https://blog.csdn.net/jhgkjasfg/article/details/102747729