前言
主题程序参考了以下博客,再他的基础上增加了小数运算结果显示和清除操作。
https://www.jianshu.com/p/426f69a07957
UI设计
创建工程后进入ui文件,放置PushButton和LineEdit,效果如下:
选中控件,鼠标右键 选择‘转到槽’,生成对应的控件监听程序。
程序编写
mainwindow.h程序
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
int m_num1;
int m_num2;
float m_result;
char m_op;
int m_mark;
private slots:
void on_pushButton_1_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_5_clicked();
void on_pushButton_4_clicked();
void on_pushButton_7_clicked();
void on_pushButton_6_clicked();
void on_pushButton_8_clicked();
void on_pushButton_9_clicked();
void on_pushButton_11_clicked();
void on_pushButton_10_clicked();
void on_pushButton_12_clicked();
void on_pushButton_15_clicked();
void on_pushButton_13_clicked();
void on_pushButton_14_clicked();
void on_pushButton_16_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp程序
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <math.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
int m_num1;
int m_num2;
float m_result;
char m_op;
int m_mark;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_1_clicked()
{
if(m_mark==0)
{
m_num1=1;
}
else
{
m_num2=1;
}
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_1->text());
}
void MainWindow::on_pushButton_2_clicked()
{
if(m_mark==0)
{
m_num1=2;
}
else
{
m_num2=2;
}
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_2->text());
}
void MainWindow::on_pushButton_3_clicked()
{
if(m_mark==0)
{
m_num1=3;
}
else
{
m_num2=3;
}
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_3->text());
}
void MainWindow::on_pushButton_4_clicked()
{
if(m_mark==0)
{
m_num1=4;
}
else
{
m_num2=4;
}
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_4->text());
}
void MainWindow::on_pushButton_5_clicked()
{
if(m_mark==0)
{
m_num1=5;
}
else
{
m_num2=5;
}
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_5->text());
}
void MainWindow::on_pushButton_6_clicked()
{
if(m_mark==0)
{
m_num1=6;
}
else
{
m_num2=6;
}
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_6->text());
}
void MainWindow::on_pushButton_7_clicked()
{
if(m_mark==0)
{
m_num1=7;
}
else
{
m_num2=7;
}
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_7->text());
}
void MainWindow::on_pushButton_8_clicked()
{
if(m_mark==0)
{
m_num1=8;
}
else
{
m_num2=8;
}
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_8->text());
}
void MainWindow::on_pushButton_9_clicked()
{
if(m_mark==0)
{
m_num1=9;
}
else
{
m_num2=9;
}
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_9->text());
}
void MainWindow::on_pushButton_10_clicked()
{
if(m_mark==0)
{
m_num1=0;
}
else
{
m_num2=0;
}
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_10->text());
}
void MainWindow::on_pushButton_11_clicked()
{
m_op = '+';
m_mark = 1;
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_11->text());
}
void MainWindow::on_pushButton_12_clicked()
{
m_op = '-';
m_mark = 1;
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_12->text());
}
void MainWindow::on_pushButton_13_clicked()
{
m_op = '/';
m_mark = 1;
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_13->text());
}
void MainWindow::on_pushButton_14_clicked()
{
switch (m_op) {
case '+':
m_result = m_num1 + m_num2;
break;
case '-':
m_result = m_num1 - m_num2;
break;
case '/':
m_result = 1.0 * m_num1 / m_num2;
break;
case '*':
m_result = m_num1 * m_num2;
break;
default:
break;
}
QString temp;
if(fmod(m_result,1.0) >0)
{
temp=QString::number(m_result,'f',3);
}
else
{
temp=QString::number(m_result,'f',0);
}
temp=ui->lineEdit->text()+ui->pushButton_14->text()+temp;
ui->lineEdit->setText(temp);
m_num1=0;
m_num2=0;
m_result=0;
m_op='+';
m_mark=0;
}
void MainWindow::on_pushButton_15_clicked()
{
m_op = '*';
m_mark = 1;
ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_15->text());
}
void MainWindow::on_pushButton_16_clicked()
{
m_num1=0;
m_num2=0;
m_result=0;
m_op=NULL;
m_mark=0;
ui->lineEdit->clear();
}
结果