请用Qt重新实现上面的程序
时间: 2024-01-20 18:46:15 浏览: 48
Qt重启程序
由于题目没有提供完整的程序代码,以下是一个使用Qt实现的简单的计算器程序:
mainwindow.h文件:
```cpp
#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 slots:
void on_num0Button_clicked();
void on_num1Button_clicked();
void on_num2Button_clicked();
void on_num3Button_clicked();
void on_num4Button_clicked();
void on_num5Button_clicked();
void on_num6Button_clicked();
void on_num7Button_clicked();
void on_num8Button_clicked();
void on_num9Button_clicked();
void on_plusButton_clicked();
void on_minusButton_clicked();
void on_multiplyButton_clicked();
void on_divideButton_clicked();
void on_clearButton_clicked();
void on_equalsButton_clicked();
private:
Ui::MainWindow *ui;
QString currentNumber;
QString operation;
double result;
};
#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);
result = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_num0Button_clicked()
{
currentNumber.append("0");
ui->resultLabel->setText(currentNumber);
}
void MainWindow::on_num1Button_clicked()
{
currentNumber.append("1");
ui->resultLabel->setText(currentNumber);
}
void MainWindow::on_num2Button_clicked()
{
currentNumber.append("2");
ui->resultLabel->setText(currentNumber);
}
void MainWindow::on_num3Button_clicked()
{
currentNumber.append("3");
ui->resultLabel->setText(currentNumber);
}
void MainWindow::on_num4Button_clicked()
{
currentNumber.append("4");
ui->resultLabel->setText(currentNumber);
}
void MainWindow::on_num5Button_clicked()
{
currentNumber.append("5");
ui->resultLabel->setText(currentNumber);
}
void MainWindow::on_num6Button_clicked()
{
currentNumber.append("6");
ui->resultLabel->setText(currentNumber);
}
void MainWindow::on_num7Button_clicked()
{
currentNumber.append("7");
ui->resultLabel->setText(currentNumber);
}
void MainWindow::on_num8Button_clicked()
{
currentNumber.append("8");
ui->resultLabel->setText(currentNumber);
}
void MainWindow::on_num9Button_clicked()
{
currentNumber.append("9");
ui->resultLabel->setText(currentNumber);
}
void MainWindow::on_plusButton_clicked()
{
operation = "+";
result += currentNumber.toDouble();
currentNumber.clear();
}
void MainWindow::on_minusButton_clicked()
{
operation = "-";
result -= currentNumber.toDouble();
currentNumber.clear();
}
void MainWindow::on_multiplyButton_clicked()
{
operation = "*";
result *= currentNumber.toDouble();
currentNumber.clear();
}
void MainWindow::on_divideButton_clicked()
{
operation = "/";
result /= currentNumber.toDouble();
currentNumber.clear();
}
void MainWindow::on_clearButton_clicked()
{
currentNumber.clear();
result = 0;
operation.clear();
ui->resultLabel->setText("0");
}
void MainWindow::on_equalsButton_clicked()
{
if(operation == "+")
result += currentNumber.toDouble();
else if(operation == "-")
result -= currentNumber.toDouble();
else if(operation == "*")
result *= currentNumber.toDouble();
else if(operation == "/")
result /= currentNumber.toDouble();
currentNumber = QString::number(result);
ui->resultLabel->setText(currentNumber);
}
```
其中,ui_mainwindow.h文件是Qt自动生成的,包含了UI界面的设计布局和各个控件的定义和信号槽连接。
阅读全文