写一个qt实现计算机的程序,包括头文件和cpp
时间: 2024-03-13 18:44:42 浏览: 132
好的,以下是一个简单的Qt计算器程序的头文件和cpp文件,供您参考:
calculator.h
```c++
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
class Calculator : public QWidget
{
Q_OBJECT
public:
Calculator(QWidget *parent = nullptr);
private slots:
void digitClicked();
void unaryOperatorClicked();
void binaryOperatorClicked();
void equalClicked();
void pointClicked();
void changeSignClicked();
void clearAll();
void clear();
void backspace();
private:
QPushButton *createButton(const QString &text, const char *member);
void calculate(double rightOperand, const QString &pendingOperator);
double sumSoFar;
double factorSoFar;
QString pendingAdditiveOperator;
QString pendingMultiplicativeOperator;
bool waitingForOperand;
QLineEdit *display;
QPushButton *digitButtons[10];
QPushButton *pointButton;
QPushButton *changeSignButton;
QPushButton *backspaceButton;
QPushButton *clearButton;
QPushButton *clearAllButton;
QPushButton *divisionButton;
QPushButton *timesButton;
QPushButton *minusButton;
QPushButton *plusButton;
QPushButton *equalButton;
};
#endif // CALCULATOR_H
```
calculator.cpp
```c++
#include "calculator.h"
#include <QtWidgets>
Calculator::Calculator(QWidget *parent)
: QWidget(parent), sumSoFar(0.0), factorSoFar(0.0),
pendingAdditiveOperator(""), pendingMultiplicativeOperator(""), waitingForOperand(true)
{
display = new QLineEdit("0");
display->setReadOnly(true);
display->setAlignment(Qt::AlignRight);
display->setMaxLength(15);
for (int i = 0; i < 10; ++i) {
digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));
}
pointButton = createButton(".", SLOT(pointClicked()));
changeSignButton = createButton("\302\261", SLOT(changeSignClicked()));
backspaceButton = createButton(tr("Backspace"), SLOT(backspace()));
clearButton = createButton(tr("Clear"), SLOT(clear()));
clearAllButton = createButton(tr("Clear All"), SLOT(clearAll()));
divisionButton = createButton(tr("\303\267"), SLOT(unaryOperatorClicked()));
timesButton = createButton(tr("\303\227"), SLOT(binaryOperatorClicked()));
minusButton = createButton(tr("-"), SLOT(binaryOperatorClicked()));
plusButton = createButton(tr("+"), SLOT(binaryOperatorClicked()));
equalButton = createButton(tr("="), SLOT(equalClicked()));
QGridLayout *mainLayout = new QGridLayout;
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
mainLayout->addWidget(display, 0, 0, 1, 6);
mainLayout->addWidget(backspaceButton, 1, 0, 1, 2);
mainLayout->addWidget(clearButton, 1, 2, 1, 2);
mainLayout->addWidget(clearAllButton, 1, 4, 1, 2);
for (int i = 1; i < 10; ++i) {
int row = ((9 - i) / 3) + 2;
int column = ((i - 1) % 3) + 1;
mainLayout->addWidget(digitButtons[i], row, column);
}
mainLayout->addWidget(digitButtons[0], 5, 1, 1, 2);
mainLayout->addWidget(pointButton, 5, 3);
mainLayout->addWidget(changeSignButton, 5, 4);
mainLayout->addWidget(divisionButton, 2, 5);
mainLayout->addWidget(timesButton, 3, 5);
mainLayout->addWidget(minusButton, 4, 5);
mainLayout->addWidget(plusButton, 5, 5);
mainLayout->addWidget(equalButton, 6, 5);
setLayout(mainLayout);
setWindowTitle(tr("Calculator"));
}
void Calculator::digitClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
int digitValue = clickedButton->text().toInt();
if (display->text() == "0" && digitValue == 0.0) {
return;
}
if (waitingForOperand) {
display->clear();
waitingForOperand = false;
}
display->setText(display->text() + QString::number(digitValue));
}
void Calculator::unaryOperatorClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
QString clickedOperator = clickedButton->text();
double operand = display->text().toDouble();
double result = 0.0;
if (clickedOperator == tr("Sqrt")) {
if (operand < 0.0) {
QMessageBox::warning(this, tr("Calculator"),
tr("Cannot compute the square root of a negative number."));
return;
}
result = std::sqrt(operand);
} else if (clickedOperator == tr("x\302\262")) {
result = std::pow(operand, 2.0);
} else if (clickedOperator == tr("1/x")) {
if (operand == 0.0) {
QMessageBox::warning(this, tr("Calculator"),
tr("Cannot divide by zero."));
return;
}
result = 1.0 / operand;
}
display->setText(QString::number(result));
waitingForOperand = true;
}
void Calculator::binaryOperatorClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
QString clickedOperator = clickedButton->text();
double operand = display->text().toDouble();
if (!pendingMultiplicativeOperator.isEmpty()) {
if (!calculate(operand, pendingMultiplicativeOperator)) {
return;
}
display->setText(QString::number(factorSoFar));
operand = factorSoFar;
factorSoFar = 0.0;
pendingMultiplicativeOperator = "";
}
if (!pendingAdditiveOperator.isEmpty()) {
if (!calculate(operand, pendingAdditiveOperator)) {
return;
}
display->setText(QString::number(sumSoFar));
} else {
sumSoFar = operand;
}
pendingAdditiveOperator = clickedOperator;
waitingForOperand = true;
}
void Calculator::equalClicked()
{
double operand = display->text().toDouble();
if (!pendingMultiplicativeOperator.isEmpty()) {
if (!calculate(operand, pendingMultiplicativeOperator)) {
return;
}
operand = factorSoFar;
factorSoFar = 0.0;
pendingMultiplicativeOperator = "";
}
if (!pendingAdditiveOperator.isEmpty()) {
if (!calculate(operand, pendingAdditiveOperator)) {
return;
}
pendingAdditiveOperator = "";
} else {
sumSoFar = operand;
}
display->setText(QString::number(sumSoFar));
sumSoFar = 0.0;
waitingForOperand = true;
}
void Calculator::pointClicked()
{
if (waitingForOperand) {
display->setText("0");
waitingForOperand = false;
}
if (!display->text().contains('.')) {
display->setText(display->text() + ".");
}
}
void Calculator::changeSignClicked()
{
QString text = display->text();
double value = text.toDouble();
if (value > 0.0) {
text.prepend(tr("-"));
} else if (value < 0.0) {
text.remove(0, 1);
}
display->setText(text);
}
void Calculator::clearAll()
{
sumSoFar = 0.0;
factorSoFar = 0.0;
pendingAdditiveOperator = "";
pendingMultiplicativeOperator = "";
display->setText("0");
waitingForOperand = true;
}
void Calculator::clear()
{
display->setText("0");
waitingForOperand = true;
}
void Calculator::backspace()
{
if (waitingForOperand) {
return;
}
QString text = display->text();
text.chop(1);
if (text.isEmpty()) {
text = "0";
waitingForOperand = true;
}
display->setText(text);
}
QPushButton *Calculator::createButton(const QString &text, const char *member)
{
QPushButton *button = new QPushButton(text);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
connect(button, SIGNAL(clicked()), this, member);
return button;
}
void Calculator::calculate(double rightOperand, const QString &pendingOperator)
{
if (pendingOperator == tr("+")) {
sumSoFar += rightOperand;
} else if (pendingOperator == tr("-")) {
sumSoFar -= rightOperand;
} else if (pendingOperator == tr("\303\227")) {
factorSoFar = rightOperand;
} else if (pendingOperator == tr("\303\267")) {
if (rightOperand == 0.0) {
QMessageBox::warning(this, tr("Calculator"),
tr("Cannot divide by zero."));
return;
}
factorSoFar /= rightOperand;
}
}
```
阅读全文