qt实现计算器
时间: 2023-07-03 20:25:40 浏览: 218
Qt简易计算器代码.doc
3星 · 编辑精心推荐
要在Qt中实现一个计算器,可以按照以下步骤进行:
1. 创建一个Qt Widgets应用程序项目。
2. 在主窗口中添加一个QLineEdit(用于显示计算结果)和多个QPushButton(用于输入数字和运算符)。
3. 为每个QPushButton添加一个单击事件处理程序,以便在QLineEdit中显示相应的数字或运算符。
4. 创建一个QStack用于存储中间计算结果。
5. 在等号按钮的单击事件处理程序中,将QLineEdit中的表达式转换为逆波兰表达式,并计算结果。
6. 在计算过程中,使用QStack来存储中间结果,以便在计算完成后可以撤销操作。
7. 在AC按钮的单击事件处理程序中,清空QLineEdit和QStack,以便开始新的计算。
以下是一个简单的示例代码,演示了如何在Qt中实现一个计算器:
```
#include <QtWidgets>
class Calculator : public QWidget
{
Q_OBJECT
public:
Calculator(QWidget *parent = nullptr);
private slots:
void digitClicked();
void unaryOperatorClicked();
void binaryOperatorClicked();
void equalClicked();
void clear();
private:
QLineEdit *display;
QStack<double> stack;
enum { NumDigitButtons = 10 };
QPushButton *digitButtons[NumDigitButtons];
QPushButton *addButton;
QPushButton *subButton;
QPushButton *mulButton;
QPushButton *divButton;
QPushButton *equalButton;
QPushButton *clearButton;
double calculate(double operand1, double operand2, const QString &op);
};
Calculator::Calculator(QWidget *parent)
: QWidget(parent)
{
display = new QLineEdit;
display->setReadOnly(true);
display->setAlignment(Qt::AlignRight);
display->setMaxLength(15);
for (int i = 0; i < NumDigitButtons; ++i) {
digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));
}
addButton = createButton(tr("+"), SLOT(binaryOperatorClicked()));
subButton = createButton(tr("-"), SLOT(binaryOperatorClicked()));
mulButton = createButton(tr("*"), SLOT(binaryOperatorClicked()));
divButton = createButton(tr("/"), SLOT(binaryOperatorClicked()));
equalButton = createButton(tr("="), SLOT(equalClicked()));
clearButton = createButton(tr("AC"), SLOT(clear()));
QGridLayout *mainLayout = new QGridLayout;
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
mainLayout->addWidget(display, 0, 0, 1, 4);
mainLayout->addWidget(digitButtons[7], 1, 0);
mainLayout->addWidget(digitButtons[8], 1, 1);
mainLayout->addWidget(digitButtons[9], 1, 2);
mainLayout->addWidget(addButton, 1, 3);
mainLayout->addWidget(digitButtons[4], 2, 0);
mainLayout->addWidget(digitButtons[5], 2, 1);
mainLayout->addWidget(digitButtons[6], 2, 2);
mainLayout->addWidget(subButton, 2, 3);
mainLayout->addWidget(digitButtons[1], 3, 0);
mainLayout->addWidget(digitButtons[2], 3, 1);
mainLayout->addWidget(digitButtons[3], 3, 2);
mainLayout->addWidget(mulButton, 3, 3);
mainLayout->addWidget(digitButtons[0], 4, 0);
mainLayout->addWidget(equalButton, 4, 1);
mainLayout->addWidget(divButton, 4, 3);
mainLayout->addWidget(clearButton, 4, 2);
setLayout(mainLayout);
}
void Calculator::digitClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
int digitValue = clickedButton->text().toInt();
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();
if (clickedOperator == tr("sqrt")) {
if (operand < 0.0) {
QMessageBox::warning(this, tr("Calculator"),
tr("Cannot compute the square root of a negative number."));
return;
}
double result = std::sqrt(operand);
display->setText(QString::number(result));
}
}
void Calculator::binaryOperatorClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
QString clickedOperator = clickedButton->text();
double operand = display->text().toDouble();
if (!stack.isEmpty()) {
double result = calculate(stack.pop(), operand, clickedOperator);
display->setText(QString::number(result));
stack.push(result);
} else {
stack.push(operand);
}
lastOperator = clickedOperator;
}
void Calculator::equalClicked()
{
double operand = display->text().toDouble();
double result = calculate(stack.pop(), operand, lastOperator);
display->setText(QString::number(result));
}
void Calculator::clear()
{
display->setText("");
stack.clear();
}
QPushButton *Calculator::createButton(const QString &text, const char *member)
{
QPushButton *button = new QPushButton(text);
connect(button, SIGNAL(clicked()), this, member);
return button;
}
double Calculator::calculate(double operand1, double operand2, const QString &op)
{
if (op == tr("+"))
return operand1 + operand2;
else if (op == tr("-"))
return operand1 - operand2;
else if (op == tr("*"))
return operand1 * operand2;
else if (op == tr("/"))
return operand1 / operand2;
return 0.0;
}
```
这个示例代码并不完整,但是可以帮助你了解Qt中如何实现一个简单的计算器。你可以根据需要进行修改和扩展。
阅读全文