编写一段qt简易计算器的代码
时间: 2024-04-21 16:16:39 浏览: 151
Qt简易计算器代码.doc
3星 · 编辑精心推荐
以下是一个简单的 Qt 计算器的代码示例:
```
#include <QtWidgets>
class Calculator : public QWidget
{
Q_OBJECT
public:
Calculator(QWidget *parent = 0);
private slots:
void digitClicked();
void unaryOperatorClicked();
void additiveOperatorClicked();
void multiplicativeOperatorClicked();
void equalClicked();
void pointClicked();
void changeSignClicked();
void backspaceClicked();
void clear();
void clearAll();
private:
void abortOperation();
bool calculate(double rightOperand, const QString &pendingOperator);
QDoubleSpinBox *display;
QString pendingAdditiveOperator;
QString pendingMultiplicativeOperator;
QString pendingUnaryOperator;
double factorSoFar;
bool waitingForOperand;
bool operandClicked;
};
Calculator::Calculator(QWidget *parent)
: QWidget(parent), pendingAdditiveOperator(""), pendingMultiplicativeOperator(""), pendingUnaryOperator(""), factorSoFar(0.0), waitingForOperand(true), operandClicked(false)
{
display = new QDoubleSpinBox();
display->setRange(-999999999.0, 999999999.0);
display->setDecimals(10);
display->setSingleStep(0.1);
display->setReadOnly(true);
QPushButton *button0 = new QPushButton(tr("0"));
QPushButton *button1 = new QPushButton(tr("1"));
QPushButton *button2 = new QPushButton(tr("2"));
QPushButton *button3 = new QPushButton(tr("3"));
QPushButton *button4 = new QPushButton(tr("4"));
QPushButton *button5 = new QPushButton(tr("5"));
QPushButton *button6 = new QPushButton(tr("6"));
QPushButton *button7 = new QPushButton(tr("7"));
QPushButton *button8 = new QPushButton(tr("8"));
QPushButton *button9 = new QPushButton(tr("9"));
QPushButton *pointButton = new QPushButton(tr("."));
QPushButton *changeSignButton = new QPushButton(tr("\302\261"));
QPushButton *backspaceButton = new QPushButton(tr("Backspace"));
QPushButton *clearButton = new QPushButton(tr("Clear"));
QPushButton *clearAllButton = new QPushButton(tr("Clear All"));
QPushButton *divisionButton = new QPushButton(tr("\303\267"));
QPushButton *timesButton = new QPushButton(tr("\303\227"));
QPushButton *minusButton = new QPushButton(tr("-"));
QPushButton *plusButton = new QPushButton(tr("+"));
QPushButton *equalButton = new QPushButton(tr("="));
QGridLayout *mainLayout = new QGridLayout;
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
mainLayout->addWidget(display, 0, 0, 1, 4);
mainLayout->addWidget(button1, 1, 0);
mainLayout->addWidget(button2, 1, 1);
mainLayout->addWidget(button3, 1, 2);
mainLayout->addWidget(divisionButton, 1, 3);
mainLayout->addWidget(button4, 2, 0);
mainLayout->addWidget(button5, 2, 1);
mainLayout->addWidget(button6, 2, 2);
mainLayout->addWidget(timesButton, 2, 3);
mainLayout->addWidget(button7, 3, 0);
mainLayout->addWidget(button8, 3, 1);
mainLayout->addWidget(button9, 3, 2);
mainLayout->addWidget(minusButton, 3, 3);
mainLayout->addWidget(button0, 4, 0);
mainLayout->addWidget(pointButton, 4, 1);
mainLayout->addWidget(changeSignButton, 4, 2);
mainLayout->addWidget(plusButton, 4, 3);
mainLayout->addWidget(backspaceButton, 5, 0, 1, 2);
mainLayout->addWidget(clearButton, 5, 2, 1, 1);
mainLayout->addWidget(clearAllButton, 5, 3, 1, 1);
mainLayout->addWidget(equalButton, 6, 3);
setLayout(mainLayout);
connect(button0, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(button1, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(button2, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(button3, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(button4, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(button5, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(button6, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(button7, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(button8, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(button9, SIGNAL(clicked()), this, SLOT(digitClicked()));
connect(pointButton, SIGNAL(clicked()), this, SLOT(pointClicked()));
connect(changeSignButton, SIGNAL(clicked()), this, SLOT(changeSignClicked()));
connect(backspaceButton, SIGNAL(clicked()), this, SLOT(backspaceClicked()));
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
connect(clearAllButton, SIGNAL(clicked()), this, SLOT(clearAll()));
connect(divisionButton, SIGNAL(clicked()), this, SLOT(multiplicativeOperatorClicked()));
connect(timesButton, SIGNAL(clicked()), this, SLOT(multiplicativeOperatorClicked()));
connect(minusButton, SIGNAL(clicked()), this, SLOT(additiveOperatorClicked()));
connect(plusButton, SIGNAL(clicked()), this, SLOT(additiveOperatorClicked()));
connect(equalButton, SIGNAL(clicked()), this, SLOT(equalClicked()));
}
void Calculator::digitClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
int digitValue = clickedButton->text().toInt();
if (display->value() == 0.0 && !waitingForOperand) {
display->setValue(digitValue);
waitingForOperand = true;
} else {
display->setValue(display->value() * 10 + digitValue);
}
}
void Calculator::unaryOperatorClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
QString clickedOperator = clickedButton->text();
double operand = display->value();
if (clickedOperator == tr("Sqrt")) {
if (operand < 0.0) {
abortOperation();
return;
}
operand = std::sqrt(operand);
}
display->setValue(operand);
waitingForOperand = true;
operandClicked = true;
}
void Calculator::additiveOperatorClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
QString clickedOperator = clickedButton->text();
double operand = display->value();
if (!pendingMultiplicativeOperator.isEmpty()) {
if (!calculate(operand, pendingMultiplicativeOperator)) {
abortOperation();
return;
}
operand = factorSoFar;
factorSoFar = 0.0;
pendingMultiplicativeOperator = "";
}
if (!pendingAdditiveOperator.isEmpty()) {
if (!calculate(operand, pendingAdditiveOperator)) {
abortOperation();
return;
}
display->setValue(factorSoFar);
} else {
factorSoFar = operand;
}
pendingAdditiveOperator = clickedOperator;
waitingForOperand = true;
operandClicked = true;
}
void Calculator::multiplicativeOperatorClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
QString clickedOperator = clickedButton->text();
double operand = display->value();
if (!pendingMultiplicativeOperator.isEmpty()) {
if (!calculate(operand, pendingMultiplicativeOperator)) {
abortOperation();
return;
}
operand = factorSoFar;
factorSoFar = 0.0;
}
if (!pendingAdditiveOperator.isEmpty()) {
if (!calculate(operand, pendingAdditiveOperator)) {
abortOperation();
return;
}
display->setValue(factorSoFar);
} else {
factorSoFar = operand;
}
pendingMultiplicativeOperator = clickedOperator;
waitingForOperand = true;
operandClicked = true;
}
bool Calculator::calculate(double rightOperand, const QString &pendingOperator)
{
if (pendingOperator == tr("+")) {
factorSoFar += rightOperand;
} else if (pendingOperator == tr("-")) {
factorSoFar -= rightOperand;
} else if (pendingOperator == tr("\303\227")) {
factorSoFar *= rightOperand;
} else if (pendingOperator == tr("\303\267")) {
if (rightOperand == 0.0) {
return false;
}
factorSoFar /= rightOperand;
}
return true;
}
void Calculator::equalClicked()
{
double operand = display->value();
if (!pendingMultiplicativeOperator.isEmpty()) {
if (!calculate(operand, pendingMultiplicativeOperator)) {
abortOperation();
return;
}
operand = factorSoFar;
factorSoFar = 0.0;
pendingMultiplicativeOperator = "";
}
if (!pendingAdditiveOperator.isEmpty()) {
if (!calculate(operand, pendingAdditiveOperator)) {
abortOperation();
return;
}
pendingAdditiveOperator = "";
} else {
factorSoFar = operand;
}
display->setValue(factorSoFar);
factorSoFar = 0.0;
waitingForOperand = true;
operandClicked = true;
}
void Calculator::pointClicked()
{
if (!waitingForOperand) {
display->setValue(display->value() + ".");
waitingForOperand = true;
}
}
void Calculator::changeSignClicked()
{
display->setValue(display->value() * -1);
}
void Calculator::backspaceClicked()
{
if (waitingForOperand) {
return;
}
QString text = QString::number(display->value());
text.chop(1);
if (text.isEmpty() || text == "-") {
text = "0";
}
display->setValue(text.toDouble());
}
void Calculator::clear()
{
if (waitingForOperand) {
return;
}
display->setValue(0.0);
waitingForOperand = true;
}
void Calculator::clearAll()
{
display->setValue(0.0);
pendingAdditiveOperator = "";
pendingMultiplicativeOperator = "";
pendingUnaryOperator = "";
factorSoFar = 0.0;
waitingForOperand = true;
}
void Calculator::abortOperation()
{
clearAll();
display->setText(tr("####"));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Calculator calc;
calc.show();
return app.exec();
}
```
这里使用了 QGridLayout 来布局界面,使用 QDoubleSpinBox 来显示计算结果。各个按钮的点击事件被连接到了相应的槽函数,实现了计算器的基本功能。
阅读全文