#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_nDisplayNum = 0;
m_nOpNum1 = 0; //第一个操作数
m_nOpNum2 = 0; //第二个操作数
m_nResult = 0; //计算的结果
bInputFlag = true; //第一次进来,置为true
setWindowTitle("简易计算器"); //窗口标题
setFixedSize(330,405); //固定窗口大小
//创建数字1-9按钮
for (int i=0;i<3;i++)
{
QPushButton* pBtn = new QPushButton(this);
pBtn->setFixedSize(50,50);
pBtn->move(20 + i*80,100);
pBtn->setText(QString::number(i+1));
connect(pBtn,SIGNAL(clicked()),this,SLOT(InputNums())); //QT4 写法
m_BtnList.append(pBtn);
// connect(pBtn,&QPushButton::clicked,this,&MainWindow::InputNums); //QT5 写法
}
for (int i=3;i<6;i++)
{
QPushButton* pBtn = new QPushButton(this);
pBtn->setFixedSize(50,50);
pBtn->move(20 + (i-3)*80,180);
pBtn->setText(QString::number(i+1));
connect(pBtn,SIGNAL(clicked()),this,SLOT(InputNums())); //QT4 写法
m_BtnList.append(pBtn);
}
for (int i=6;i<9;i++)
{
QPushButton* pBtn = new QPushButton(this);
pBtn->setFixedSize(50,50);
pBtn->move(20 + (i-6)*80,260);
pBtn->setText(QString::number(i+1));
connect(pBtn,SIGNAL(clicked()),this,SLOT(InputNums())); //QT4 写法
m_BtnList.append(pBtn);
}
QPushButton* pBtn = new QPushButton(this); //创建数字0按钮
pBtn->setFixedSize(50,50);
pBtn->move(20 + 80,340);
pBtn->setText(QString::number(0));
connect(pBtn,SIGNAL(clicked()),this,SLOT(InputNums())); //QT4 写法
m_BtnList.append(pBtn);
//以下创建功能按钮
m_btnAdd = new QPushButton(this); //创建数字+按钮
m_btnAdd->setFixedSize(50,50);
m_btnAdd->move(20 + 240,100);
m_btnAdd->setText("+");
connect(m_btnAdd,SIGNAL(clicked()),this,SLOT(InputNums())); //QT4 写法
m_BtnList.append(m_btnAdd);
m_btnSub = new QPushButton(this); //创建数字-按钮
m_btnSub->setFixedSize(50,50);
m_btnSub->move(20 + 240,180);
m_btnSub->setText("-");
connect(m_btnSub,SIGNAL(clicked()),this,SLOT(InputNums())); //QT4 写法
m_BtnList.append(m_btnSub);
m_btnMul = new QPushButton(this); //创建数字*按钮
m_btnMul->setFixedSize(50,50);
m_btnMul->move(20 + 240,260);
m_btnMul->setText("*");
connect(m_btnMul,SIGNAL(clicked()),this,SLOT(InputNums())); //QT4 写法
m_BtnList.append(m_btnMul);
m_btnDiv = new QPushButton(this); //创建数字÷按钮
m_btnDiv->setFixedSize(50,50);
m_btnDiv->move(20 + 240,340);
m_btnDiv->setText("÷");
connect(m_btnDiv,SIGNAL(clicked()),this,SLOT(InputNums())); //QT4 写法
m_BtnList.append(m_btnDiv);
m_btnCalc = new QPushButton(this); //创建数字=按钮
m_btnCalc->setFixedSize(50,50);
m_btnCalc->move(20 + 160,340);
m_btnCalc->setText("=");
connect(m_btnCalc,SIGNAL(clicked()),this,SLOT(InputNums())); //QT4 写法
m_BtnList.append(m_btnCalc);
m_btnClear = new QPushButton(this); //创建数字+按钮
m_btnClear->setFixedSize(50,50);
m_btnClear->move(20,340);
m_btnClear->setText("C");
connect(m_btnClear,SIGNAL(clicked()),this,SLOT(InputNums())); //QT4 写法
m_BtnList.append(m_btnClear);
//开始创建显示屏
int startx = m_btnClear->x(); //获得最左侧按钮左边的值
int endx= m_btnDiv->x()+m_btnDiv->width(); //获得最右侧按钮右边的值
int nWidth = endx - startx; //计算之间的宽度
m_editDisplay = new QLineEdit(this); //创建一个编辑框
m_editDisplay->setFixedSize(nWidth,40); //设置宽度和高度
m_editDisplay->move(startx,20); //设置位置
m_editDisplay->setReadOnly(true); //此窗口只读
m_editDisplay->setAlignment(Qt::AlignRight);
Display(); //显示屏幕
}
MainWindow::~MainWindow()
{
delete ui;
}
#include<QDebug>
void MainWindow::InputNums( )
{
QPushButton* pBtn = dynamic_cast<QPushButton*>(sender());
QString strTmp;
strTmp = pBtn->text(); //按钮上的文字
if(strTmp.compare("0")>=0 && strTmp.compare("9")<=0) //如果是数字键
{
//先判断上次数字输入是否已经输入结束
if(bInputFlag)
{
m_nDisplayNum = 0; //本次是新输入的数字
bInputFlag = false; //开始输入,输入并未结束
}
m_nDisplayNum = (m_nDisplayNum*10) + strTmp.toInt(); //每次按一下都是前一个数*10 加上刚按上的数
}
else //如果按了功能键
{
bInputFlag = true; //数字已经输入完了
if(strTmp == "+")
{
m_nOpType = 1;
m_nOpNum1 = m_nDisplayNum;
}
else if(strTmp == "-")
{
m_nOpType = 2;
m_nOpNum1 = m_nDisplayNum;
}
else if(strTmp == "*")
{
m_nOpType = 3;
m_nOpNum1 = m_nDisplayNum;
}
else if(strTmp == "÷")
{
m_nOpType = 4;
m_nOpNum1 = m_nDisplayNum;
}
else if(strTmp == "C") //如果按下清零键
{
m_nOpNum1 = 0;
m_nOpNum2 = 0;
m_nOpType = 0;
m_nDisplayNum = 0;
}
else if(strTmp == "=")
{
m_nOpNum2 = m_nDisplayNum; //将第二次输入的数据写入
//开始计算得出结果
m_nResult = Calculate();
m_nDisplayNum = m_nResult;
}
}
Display(); //屏幕刷新显示
}
void MainWindow::Display()
{
m_editDisplay->setText(QString::number(m_nDisplayNum));
}
int MainWindow::Calculate()
{
int result = 0;
switch (m_nOpType)
{
case 1:
result = m_nOpNum1 + m_nOpNum2;
break;
case 2:
result = m_nOpNum1 - m_nOpNum2;
break;
case 3:
result = m_nOpNum1 * m_nOpNum2;
break;
case 4:
if(m_nOpNum2 != 0)
result = m_nOpNum1 / m_nOpNum2;
break;
}
return result;
}
评论0