用QT写一个C++的计算器
时间: 2023-07-04 18:05:09 浏览: 166
QT creator实现(简易版)计算器
好的,下面是一个简单的 C++ 计算器程序,使用 QT 框架:
首先,在 QT Creator 中创建一个新的 QT Widgets 应用程序项目。接下来,打开 mainwindow.cpp 文件并添加以下代码:
```
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 连接数字按钮的信号和槽
connect(ui->button_0, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(ui->button_1, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(ui->button_2, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(ui->button_3, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(ui->button_4, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(ui->button_5, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(ui->button_6, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(ui->button_7, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(ui->button_8, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(ui->button_9, SIGNAL(clicked()), this, SLOT(digitClicked()));
// 连接运算符按钮的信号和槽
connect(ui->button_add, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(ui->button_sub, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(ui->button_mul, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(ui->button_div, SIGNAL(clicked()), this, SLOT(operatorClicked()));
connect(ui->button_equal, SIGNAL(clicked()), this, SLOT(equalClicked()));
// 初始化计算器状态
m_state = STATE_FIRST_OPERAND;
m_operator = OPERATOR_NONE;
m_firstOperand = 0;
m_secondOperand = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::digitClicked()
{
QPushButton *button = (QPushButton *)sender();
int digitValue = button->text().toInt();
if (m_state == STATE_FIRST_OPERAND) {
m_firstOperand = m_firstOperand * 10 + digitValue;
ui->display->setText(QString::number(m_firstOperand));
} else {
m_secondOperand = m_secondOperand * 10 + digitValue;
ui->display->setText(QString::number(m_secondOperand));
}
}
void MainWindow::operatorClicked()
{
QPushButton *button = (QPushButton *)sender();
if (m_state == STATE_FIRST_OPERAND) {
m_operator = getOperator(button->text());
m_state = STATE_SECOND_OPERAND;
} else {
calculate();
m_operator = getOperator(button->text());
m_state = STATE_SECOND_OPERAND;
}
}
void MainWindow::equalClicked()
{
calculate();
m_state = STATE_FIRST_OPERAND;
m_operator = OPERATOR_NONE;
}
void MainWindow::calculate()
{
int result = 0;
switch (m_operator) {
case OPERATOR_ADD:
result = m_firstOperand + m_secondOperand;
break;
case OPERATOR_SUB:
result = m_firstOperand - m_secondOperand;
break;
case OPERATOR_MUL:
result = m_firstOperand * m_secondOperand;
break;
case OPERATOR_DIV:
result = m_firstOperand / m_secondOperand;
break;
default:
break;
}
ui->display->setText(QString::number(result));
m_firstOperand = result;
m_secondOperand = 0;
}
MainWindow::Operator MainWindow::getOperator(const QString &text)
{
if (text == "+") {
return OPERATOR_ADD;
} else if (text == "-") {
return OPERATOR_SUB;
} else if (text == "*") {
return OPERATOR_MUL;
} else if (text == "/") {
return OPERATOR_DIV;
} else {
return OPERATOR_NONE;
}
}
```
然后,在 mainwindow.h 文件中添加以下代码:
```
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
enum State {
STATE_FIRST_OPERAND,
STATE_SECOND_OPERAND
};
enum Operator {
OPERATOR_ADD,
OPERATOR_SUB,
OPERATOR_MUL,
OPERATOR_DIV,
OPERATOR_NONE
};
private slots:
void digitClicked();
void operatorClicked();
void equalClicked();
private:
void calculate();
Operator getOperator(const QString &text);
Ui::MainWindow *ui;
State m_state;
Operator m_operator;
int m_firstOperand;
int m_secondOperand;
};
#endif // MAINWINDOW_H
```
最后,在 UI 界面中添加数字按钮和运算符按钮,并将它们的信号连接到对应的槽函数。
现在,您可以编译并运行该程序,测试您的计算器是否正确工作。
阅读全文