qt实现一个简单的计算器

Posted 高傲的monkey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qt实现一个简单的计算器相关的知识,希望对你有一定的参考价值。

1、计算器的界面如下图所示

技术分享

dalog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include<QPushButton>
#include<QDebug>
#include<QtMath>
    const char *a;
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    connect(ui->pushButton_58,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton_59,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton_53,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton_54,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton_55,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton_68,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton_69,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton_70,SIGNAL(clicked()),this,SLOT(showdigit()));
    connect(ui->pushButton_71,SIGNAL(clicked()),this,SLOT(caculate()));
    connect(ui->pushButton_72,SIGNAL(clicked()),this,SLOT(caculate()));
    connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(caculate()));
    connect(ui->pushButton_37,SIGNAL(clicked()),this,SLOT(caculate()));
    connect(ui->pushButton_57,SIGNAL(clicked()),this,SLOT(caculate()));
    connect(ui->pushButton_56,SIGNAL(clicked()),this,SLOT(caculate()));
    connect(ui->pushButton_62,SIGNAL(clicked()),this,SLOT(caculate()));
    connect(ui->pushButton_61,SIGNAL(clicked()),this,SLOT(caculate()));
    connect(ui->pushButton_60,SIGNAL(clicked()),this,SLOT(result()));
}
Dialog::~Dialog()
{
    delete ui;
}
void Dialog::showdigit()
{

    QString tmp = ui->label->text();
    if(ui->pushButton_58==(QPushButton *)sender())
    {
        tmp +="0";
        ui->label->setText(tmp);
    }
    if(ui->pushButton_59==(QPushButton *)sender())
    {
        tmp +=".";
        ui->label->setText(tmp);
    }
    if(ui->pushButton_53==(QPushButton *)sender())
    {
        tmp +="1";
        ui->label->setText(tmp);
    }
    if(ui->pushButton_54==(QPushButton *)sender())
    {
        tmp +="2";
        ui->label->setText(tmp);
    }
    if(ui->pushButton_55==(QPushButton *)sender())
    {
        tmp +="3";
        ui->label->setText(tmp);
    }
    if(ui->pushButton==(QPushButton *)sender())
    {
        tmp +="4";
        ui->label->setText(tmp);
    }
    if(ui->pushButton_2==(QPushButton *)sender())
    {
        tmp +="5";
        ui->label->setText(tmp);
    }
    if(ui->pushButton_3==(QPushButton *)sender())
    {
        tmp +="6";
        ui->label->setText(tmp);
    }
    if(ui->pushButton_68==(QPushButton *)sender())
    {
        tmp +="7";
        ui->label->setText(tmp);
    }
    if(ui->pushButton_69==(QPushButton *)sender())
    {
        tmp +="8";
        ui->label->setText(tmp);
    }
    if(ui->pushButton_70==(QPushButton *)sender())
    {
        tmp +="9";
        ui->label->setText(tmp);
    }

}
void Dialog::caculate()
{
    QString tmp = ui->label->text();
    d1 = tmp.toDouble();
    if(ui->pushButton_71==(QPushButton *)sender())
    {
        a = "+";
    }
            if(ui->pushButton_72==(QPushButton *)sender())
            {
                a = "/";
            }
            if(ui->pushButton_4==(QPushButton *)sender())
            {
                a = "s";//sqrt
            }
            if(ui->pushButton_37==(QPushButton *)sender())
            {
                a = "-";
            }
            if(ui->pushButton_57==(QPushButton *)sender())
            {
                a = "x";//x^2
            }
            if(ui->pushButton_56==(QPushButton *)sender())
            {
                a = "*";
            }
            if(ui->pushButton_62==(QPushButton *)sender())
            {
                a = "%";
            }
            if(ui->pushButton_61==(QPushButton *)sender())
            {
                ui->label->setText(" ");
            }
            ui->label->setText("");

}
void Dialog::result()
{
//qDebug()<<"hello";
QString tmp = ui->label->text();
d2 = tmp.toDouble();
QString tmp1;

        if(a=="-")
        {
            tmp1.setNum((d1-d2));
            ui->label->setText(tmp1);
        }
    if(a=="+")
    {
        tmp1.setNum((d1+d2));
        ui->label->setText(tmp1);
    }
    if(a=="%")
    {
        tmp1.setNum(((int)d1%(int)d2));
        ui->label->setText(tmp1);
    }
    if(a=="x")
    {
        tmp1.setNum((d1*d1));
        ui->label->setText(tmp1);
    }
    if(a=="*")
    {
        tmp1.setNum((d1*d2));
        ui->label->setText(tmp1);
    }
    if(a=="s")
    {
        tmp1.setNum((qSqrt(d1)));
        ui->label->setText(tmp1);
    }
    if(a=="/")
    {
        tmp1.setNum((d1/d2));
        ui->label->setText(tmp1);
    }

main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec();
}

代码结构:

技术分享

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
     Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    Ui::Dialog *ui;

    double d1,d2;
public slots:
        void showdigit();
        void caculate();
        void result();
};

#endif // DIALOG_H

编译代码:

#-------------------------------------------------
#
# Project created by QtCreator 2016-03-25T20:06:17
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = caculate
TEMPLATE = app


SOURCES += main.cpp        dialog.cpp

HEADERS  += dialog.h

FORMS    += dialog.ui

 

以上是关于qt实现一个简单的计算器的主要内容,如果未能解决你的问题,请参考以下文章

qt_计算器的简单实现

golang代码片段(摘抄)

QT 实用代码片段

QT之计算器代码重构

利用QT制作我们自己的一个计算器

如何用VB做一个简单计算器 要求实现加减乘除乘方开方功能