用QT写一个C++的计算器
时间: 2023-07-04 08:04:41 浏览: 118
好的,以下是一个简单的基于QT和C++的计算器示例代码:
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:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btn_0_clicked();
void on_btn_1_clicked();
void on_btn_2_clicked();
void on_btn_3_clicked();
void on_btn_4_clicked();
void on_btn_5_clicked();
void on_btn_6_clicked();
void on_btn_7_clicked();
void on_btn_8_clicked();
void on_btn_9_clicked();
void on_btn_add_clicked();
void on_btn_sub_clicked();
void on_btn_mul_clicked();
void on_btn_div_clicked();
void on_btn_clear_clicked();
void on_btn_equal_clicked();
private:
Ui::MainWindow *ui;
QString m_input;
double m_num1;
double m_num2;
QString m_op;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_input = "";
m_num1 = 0;
m_num2 = 0;
m_op = "";
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btn_0_clicked()
{
m_input += "0";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_1_clicked()
{
m_input += "1";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_2_clicked()
{
m_input += "2";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_3_clicked()
{
m_input += "3";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_4_clicked()
{
m_input += "4";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_5_clicked()
{
m_input += "5";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_6_clicked()
{
m_input += "6";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_7_clicked()
{
m_input += "7";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_8_clicked()
{
m_input += "8";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_9_clicked()
{
m_input += "9";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_add_clicked()
{
m_num1 = m_input.toDouble();
m_input = "";
m_op = "+";
}
void MainWindow::on_btn_sub_clicked()
{
m_num1 = m_input.toDouble();
m_input = "";
m_op = "-";
}
void MainWindow::on_btn_mul_clicked()
{
m_num1 = m_input.toDouble();
m_input = "";
m_op = "*";
}
void MainWindow::on_btn_div_clicked()
{
m_num1 = m_input.toDouble();
m_input = "";
m_op = "/";
}
void MainWindow::on_btn_clear_clicked()
{
m_input = "";
m_num1 = 0;
m_num2 = 0;
m_op = "";
ui->textEdit->setText(m_input);
}
void MainWindow::on_btn_equal_clicked()
{
m_num2 = m_input.toDouble();
double result = 0;
if (m_op == "+")
{
result = m_num1 + m_num2;
}
else if (m_op == "-")
{
result = m_num1 - m_num2;
}
else if (m_op == "*")
{
result = m_num1 * m_num2;
}
else if (m_op == "/")
{
if (m_num2 == 0)
{
ui->textEdit->setText("Error");
return;
}
result = m_num1 / m_num2;
}
m_input = QString::number(result);
ui->textEdit->setText(m_input);
}
```
mainwindow.ui:
在QT设计器中布置UI界面,添加TextEdit和16个PushButton,分别为0、1、2、3、4、5、6、7、8、9、+、-、*、/、CE、=。为每个PushButton添加对应的槽函数。
在MainWindow类中,定义了一个QString类型的变量m_input,用于存储用户输入的数字和运算符;两个double型的变量m_num1和m_num2,用于存储计算过程中需要用到的两个数;一个QString类型的变量m_op,用于存储用户选择的运算符。在槽函数中,根据用户的输入更新m_input变量,并根据用户选择的运算符更新m_num1和m_op变量。当用户点击“=”时,根据m_op变量进行相应的运算,并将结果更新到m_input变量中,最后将结果显示在TextEdit中。
示例代码中还添加了一个特殊情况的处理:当用户选择除法运算时,如果m_num2为0,则会在TextEdit中显示“Error”。
希望这个示例对你有所帮助!
阅读全文