qt实现一个简单的计算器

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

qt实现一个简单的计算器

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 +="";
ui->label->setText(tmp);
}
if(ui->pushButton_59==(QPushButton *)sender())
{
tmp +=".";
ui->label->setText(tmp);
}
if(ui->pushButton_53==(QPushButton *)sender())
{
tmp +="";
ui->label->setText(tmp);
}
if(ui->pushButton_54==(QPushButton *)sender())
{
tmp +="";
ui->label->setText(tmp);
}
if(ui->pushButton_55==(QPushButton *)sender())
{
tmp +="";
ui->label->setText(tmp);
}
if(ui->pushButton==(QPushButton *)sender())
{
tmp +="";
ui->label->setText(tmp);
}
if(ui->pushButton_2==(QPushButton *)sender())
{
tmp +="";
ui->label->setText(tmp);
}
if(ui->pushButton_3==(QPushButton *)sender())
{
tmp +="";
ui->label->setText(tmp);
}
if(ui->pushButton_68==(QPushButton *)sender())
{
tmp +="";
ui->label->setText(tmp);
}
if(ui->pushButton_69==(QPushButton *)sender())
{
tmp +="";
ui->label->setText(tmp);
}
if(ui->pushButton_70==(QPushButton *)sender())
{
tmp +="";
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();
}

代码结构:

qt实现一个简单的计算器

dialog.h

#ifndef DIALOG_H
#define DIALOG_H #include <QDialog> namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
Dialog(QWidget *parent = );
~Dialog(); private:
Ui::Dialog *ui; double d1,d2;
public slots:
void showdigit();
void caculate();
void result();
}; #endif // DIALOG_H

编译代码:

#-------------------------------------------------
#
# Project created by QtCreator --25T20::
#
#------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, ): QT += widgets TARGET = caculate
TEMPLATE = app SOURCES += main.cpp\
dialog.cpp HEADERS += dialog.h FORMS += dialog.ui
上一篇:openvswitch 2.7 安装过程记录 总结


下一篇:免费空间上的mysql数据库怎么连接?