QT做的计算器,非常好用,要源代码的说!
工程文件放GITHUB上了,下载地址:https://github.com/ltxb/calculator
赞一个!:lol
发出来瞅瞅!:lol
好的,我把代码公布出来,大家看看代码中有什么 不足,共五个工程文件:
第一个文件calculator.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2016-12-26T08:50:09
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = calculator
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
calculatorwidget.cpp
HEADERS += calculatorwidget.h
FORMS += calculatorwidget.ui
第二个文件calculatorwidget.h
#ifndef CALCULATORWIDGET_H
#define CALCULATORWIDGET_H
#include <QWidget>
namespace Ui {
class calculatorWidget;
}
class calculatorWidget : public QWidget
{
Q_OBJECT
public:
explicit calculatorWidget(QWidget *parent = 0);
~calculatorWidget();
public slots:
void slot_NumberClicked(); //The slot function by clicked button "1,2,3,4,5,6,7,8,9,0,'+/-','.'".
void slot_OperatorClicked(); //The slot function by clicked button "+,-,*,/,%".
void slot_EqualClicked(); //The slot function by clicked button "=".
void slot_ClearClicked(); //The slot function by clicked button "AC".
private:
void AddToValue(QString &m_op,QString str); //According to string "str", add character to reference string "m_op".
int GetType(QString tmpStr); //According to string "str",get the value of the variable "m_type".
bool Calculate(); //According to the "m_op1" and "m_op2" and "m_type",calculate result.
void initialize(); //initialize all private variables.
void UpdateScreen(QString strFirst,QString strSecond,int symbol,QString strShow); //Update four QLable objects.
Ui::calculatorWidget *ui;
QString m_op1; //Used to store the first operand.
QString m_op2; //Used to store the second operand.
QString tmp_m_op1,tmp_m_op2; //Used to temporary store variable "m_op1" and "m_op2".
bool isFirst; //Used to indicate whether be operating the first number.
int m_type; //Used to store the type of operator.
};
#endif // CALCULATORWIDGET_H
第三个文件calculatorwidget.cpp
#include "calculatorwidget.h"
#include "ui_calculatorwidget.h"
calculatorWidget::calculatorWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::calculatorWidget)
{
ui->setupUi(this);
connect(ui->pB_0,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_1,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_2,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_3,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_4,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_5,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_6,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_7,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_8,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_9,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_dot,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_sign,SIGNAL(clicked()),this,SLOT(slot_NumberClicked()));
connect(ui->pB_add,SIGNAL(clicked()),this,SLOT(slot_OperatorClicked()));
connect(ui->pB_sub,SIGNAL(clicked()),this,SLOT(slot_OperatorClicked()));
connect(ui->pB_multiply,SIGNAL(clicked()),this,SLOT(slot_OperatorClicked()));
connect(ui->pB_divide,SIGNAL(clicked()),this,SLOT(slot_OperatorClicked()));
connect(ui->pB_residue,SIGNAL(clicked()),this,SLOT(slot_OperatorClicked()));
connect(ui->pB_equal,SIGNAL(clicked()),this,SLOT(slot_EqualClicked()));
connect(ui->pB_clear,SIGNAL(clicked()),this,SLOT(slot_ClearClicked()));
initialize();
}
calculatorWidget::~calculatorWidget()
{
delete ui;
}
void calculatorWidget::slot_NumberClicked()
{
QObject *tmpObj=sender();
QString tmpStr=tmpObj->objectName();
tmpStr=tmpStr.remove("pB_");
if(isFirst)
{
AddToValue(m_op1,tmpStr);
UpdateScreen(m_op1,m_op2,m_type,m_op1);
}
else
{
AddToValue(m_op2,tmpStr);
UpdateScreen(m_op1,m_op2,m_type,m_op2);
}
}
void calculatorWidget::AddToValue(QString &m_op,QString str)
{
if(str=="dot")
{
if(m_op.contains("."))
UpdateScreen(m_op1,m_op2,m_type,m_op1);
else if(m_op=="")
m_op="0.";
else if(m_op=="-")
m_op="-0.";
else
m_op+=".";
}
else if(str=="sign")
{
if(m_op[0]=='-')
m_op.remove('-');
else
m_op.insert(0,'-');
}
else
{
if(m_op=="0")
m_op=str;
else if(m_op=="-0")
m_op="-"+str;
else
m_op+=str;
}
}
void calculatorWidget::slot_OperatorClicked()
{
QObject *tmpObj=sender();
QString tmpStr=tmpObj->objectName();
if(m_op1=="-"||m_op1=="")
tmp_m_op1=m_op1;
else if(isFirst)
{
isFirst=false;
m_type=GetType(tmpStr);
UpdateScreen(m_op1,m_op2,m_type,m_op1);
}
else if(m_op1!=""&&m_op2=="")
{
m_type=GetType(tmpStr);
UpdateScreen(m_op1,m_op2,m_type,m_op1);
}
else if(m_op2!="")
{
bool isOk=Calculate();
if(isOk)
{
m_type=GetType(tmpStr);
UpdateScreen(m_op1,m_op2,m_type,m_op1);
}
else
{
UpdateScreen(tmp_m_op1,tmp_m_op2,m_type,"operation error!");
}
}
}
int calculatorWidget::GetType(QString tmpStr)
{
int x;
if(tmpStr=="pB_add")
x=1;
else if(tmpStr=="pB_sub")
x=2;
else if(tmpStr=="pB_multiply")
x=3;
else if(tmpStr=="pB_divide")
x=4;
else
x=5;
return x;
}
void calculatorWidget::slot_EqualClicked()
{
if(m_op1!=""&&m_op2!="")
{
bool isOk=Calculate();
if(isOk)
{
tmp_m_op2+=" =";
UpdateScreen(tmp_m_op1,tmp_m_op2,m_type,m_op1);
}
else
UpdateScreen(tmp_m_op1,tmp_m_op2,m_type,"operation error!");
initialize();
}
}
bool calculatorWidget::Calculate()
{
double op1=m_op1.toDouble();
double op2=m_op2.toDouble();
double result;
switch (m_type)
{
case 1:result=op1+op2;break;
case 2:result=op1-op2;break;
case 3:result=op1*op2;break;
case 4:
{
if(op2!=0)
{
result=op1/op2;
break;
}
else
{
initialize();
return false;
}
}
case 5:
{
if(!m_op1.contains(".")&&!m_op2.contains(".")&&op2!=0)
{
int x=m_op1.toInt();
int y=m_op2.toInt();
result=x%y;
}
else
{
initialize();
return false;
}
break;
}
}
tmp_m_op1=m_op1;
tmp_m_op2=m_op2;
m_op1=QString::number(result,'g',15);
m_op2="";
return true;
}
void calculatorWidget::slot_ClearClicked()
{
initialize();
UpdateScreen(m_op1,m_op2,m_type,"");
}
void calculatorWidget::initialize()
{
m_op1="";
m_op2="";
isFirst=true;
m_type=0;
tmp_m_op1=m_op1;
tmp_m_op2=m_op2;
}
void calculatorWidget::UpdateScreen(QString strFirst,QString strSecond,int symbol,QString strShow)
{
ui->label_first->setText(strFirst);
ui->label_second->setText(strSecond);
ui->lable_show->setText(strShow);
if(symbol==0)
ui->label_symbol->setText("");
else if(symbol==1)
ui->label_symbol->setText("+");
else if(symbol==2)
ui->label_symbol->setText("-");
else if(symbol==3)
ui->label_symbol->setText("*");
else if(symbol==4)
ui->label_symbol->setText("/");
else
ui->label_symbol->setText("%");
}
第四个文件main.cpp
#include "calculatorwidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
calculatorWidget w;
w.show();
return a.exec();
}
第五个文件calculatorwidget.ui,由于字节长度超过规定,发不上来
那就发附件发上来
赞!!!
第四个文件main
顶起,:handshake
:lol不错呀