基于C++的Qt计算器
时间: 2023-07-10 12:27:35 浏览: 90
好的,以下是一个基于 C++ 的 Qt 计算器的示例代码:
mainwindow.h 文件:
```cpp
#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:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btn_num_0_clicked();
void on_btn_num_1_clicked();
void on_btn_num_2_clicked();
void on_btn_num_3_clicked();
void on_btn_num_4_clicked();
void on_btn_num_5_clicked();
void on_btn_num_6_clicked();
void on_btn_num_7_clicked();
void on_btn_num_8_clicked();
void on_btn_num_9_clicked();
void on_btn_op_add_clicked();
void on_btn_op_sub_clicked();
void on_btn_op_mul_clicked();
void on_btn_op_div_clicked();
void on_btn_clear_clicked();
void on_btn_del_clicked();
void on_btn_equal_clicked();
private:
Ui::MainWindow *ui;
QString m_str_num_left;
QString m_str_num_right;
QString m_str_op;
double m_num_left;
double m_num_right;
double m_result;
void reset();
void updateDisplay(QString str);
void calculate();
};
#endif // MAINWINDOW_H
```
mainwindow.cpp 文件:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
reset();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btn_num_0_clicked()
{
updateDisplay("0");
}
void MainWindow::on_btn_num_1_clicked()
{
updateDisplay("1");
}
void MainWindow::on_btn_num_2_clicked()
{
updateDisplay("2");
}
void MainWindow::on_btn_num_3_clicked()
{
updateDisplay("3");
}
void MainWindow::on_btn_num_4_clicked()
{
updateDisplay("4");
}
void MainWindow::on_btn_num_5_clicked()
{
updateDisplay("5");
}
void MainWindow::on_btn_num_6_clicked()
{
updateDisplay("6");
}
void MainWindow::on_btn_num_7_clicked()
{
updateDisplay("7");
}
void MainWindow::on_btn_num_8_clicked()
{
updateDisplay("8");
}
void MainWindow::on_btn_num_9_clicked()
{
updateDisplay("9");
}
void MainWindow::on_btn_op_add_clicked()
{
if (m_str_num_left.isEmpty()) {
return;
}
m_str_op = "+";
m_str_num_right.clear();
updateDisplay(m_str_op);
}
void MainWindow::on_btn_op_sub_clicked()
{
if (m_str_num_left.isEmpty()) {
return;
}
m_str_op = "-";
m_str_num_right.clear();
updateDisplay(m_str_op);
}
void MainWindow::on_btn_op_mul_clicked()
{
if (m_str_num_left.isEmpty()) {
return;
}
m_str_op = "*";
m_str_num_right.clear();
updateDisplay(m_str_op);
}
void MainWindow::on_btn_op_div_clicked()
{
if (m_str_num_left.isEmpty()) {
return;
}
m_str_op = "/";
m_str_num_right.clear();
updateDisplay(m_str_op);
}
void MainWindow::on_btn_clear_clicked()
{
reset();
}
void MainWindow::on_btn_del_clicked()
{
QString str = ui->label_display->text();
if (!str.isEmpty()) {
str.remove(str.length() - 1, 1);
ui->label_display->setText(str);
}
}
void MainWindow::on_btn_equal_clicked()
{
calculate();
}
void MainWindow::reset()
{
m_str_num_left.clear();
m_str_num_right.clear();
m_str_op.clear();
m_num_left = 0;
m_num_right = 0;
m_result = 0;
ui->label_display->setText("");
}
void MainWindow::updateDisplay(QString str)
{
QString display_str = ui->label_display->text();
display_str += str;
ui->label_display->setText(display_str);
if (m_str_op.isEmpty()) {
m_str_num_left += str;
} else {
m_str_num_right += str;
}
}
void MainWindow::calculate()
{
if (m_str_num_left.isEmpty() || m_str_num_right.isEmpty() || m_str_op.isEmpty()) {
return;
}
m_num_left = m_str_num_left.toDouble();
m_num_right = m_str_num_right.toDouble();
if (m_str_op == "+") {
m_result = m_num_left + m_num_right;
} else if (m_str_op == "-") {
m_result = m_num_left - m_num_right;
} else if (m_str_op == "*") {
m_result = m_num_left * m_num_right;
} else if (m_str_op == "/") {
if (m_num_right == 0) {
ui->label_display->setText("Error");
return;
}
m_result = m_num_left / m_num_right;
}
QString str_result = QString::number(m_result);
ui->label_display->setText(str_result);
}
```
在 Qt Creator 中新建一个项目,选择 "Qt Widgets Application",然后将以上代码分别复制到对应的文件中(注意文件名和类名要保持一致),编译运行即可。
阅读全文