嵌入式实验之多功能计算器的实现

嵌入式实验之多功能计算器的实现
嵌入式实验之多功能计算器的实现

/**************************************************************************** **

** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).

** All rights reserved.

** Contact: Nokia Corporation (qt-info@https://www.360docs.net/doc/2d14649548.html,)

**

** This file is part of the examples of the Qt Toolkit.

**

** $QT_BEGIN_LICENSE:LGPL$

** Commercial Usage

** Licensees holding valid Qt Commercial licenses may use this file in

** accordance with the Qt Commercial License Agreement provided with the

** Software or, alternatively, in accordance with the terms contained in

** a written agreement between you and Nokia.

**

** GNU Lesser General Public License Usage

** Alternatively, this file may be used under the terms of the GNU Lesser

** General Public License version 2.1 as published by the Free Software

** Foundation and appearing in the file LICENSE.LGPL included in the

** packaging of this file. Please review the following information to

** ensure the GNU Lesser General Public License version 2.1 requirements

** will be met: https://www.360docs.net/doc/2d14649548.html,/licenses/old-licenses/lgpl-2.1.html.

**

** In addition, as a special exception, Nokia gives you certain additional

** rights. These rights are described in the Nokia Qt LGPL Exception

** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.

**

** GNU General Public License Usage

** Alternatively, this file may be used under the terms of the GNU

** General Public License version 3.0 as published by the Free Software

** Foundation and appearing in the file LICENSE.GPL included in the

** packaging of this file. Please review the following information to

** ensure the GNU General Public License version 3.0 requirements will be

** met: https://www.360docs.net/doc/2d14649548.html,/copyleft/gpl.html.

**

** If you have questions regarding the use of this file, please contact

** Nokia at qt-info@https://www.360docs.net/doc/2d14649548.html,.

** $QT_END_LICENSE$

**

****************************************************************************/ #include

#include

#include "button.h"

#include "calculator.h"

//! [0]

Calculator::Calculator(QWidget *parent)

: QDialog(parent)

{

sumInMemory = 0.0;

sumSoFar = 0.0;

factorSoFar = 0.0;

waitingForOperand = true;

waitingForE = false;

//! [0]

//! [1]

display = new QLineEdit("0");

//! [1] //! [2]

display->setReadOnly(true);

display->setAlignment(Qt::AlignRight);

display->setMaxLength(15);

QFont font = display->font();

font.setPointSize(font.pointSize() + 8);

display->setFont(font);

//! [2]

//! [4]

for (int i = 0; i < NumDigitButtons; ++i) {

digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));

}

Button *pointButton = createButton(tr("."), SLOT(pointClicked()));

Button *changeSignButton = createButton(tr("\261"), SLOT(changeSignClicked()));

Button *backspaceButton = createButton(tr("Backspace"), SLOT(backspaceClicked()));

Button *clearButton = createButton(tr("Clear"), SLOT(clear()));

Button *clearAllButton = createButton(tr("Clear All"), SLOT(clearAll()));

Button *clearMemoryButton = createButton(tr("MC"), SLOT(clearMemory()));

Button *readMemoryButton = createButton(tr("MR"), SLOT(readMemory()));

Button *setMemoryButton = createButton(tr("MS"), SLOT(setMemory()));

Button *addToMemoryButton = createButton(tr("M+"), SLOT(addToMemory()));

Button *divisionButton = createButton(tr("\367"), SLOT(multiplicativeOperatorClicked()));

Button *timesButton = createButton(tr("\327"), SLOT(multiplicativeOperatorClicked()));

Button *minusButton = createButton(tr("-"), SLOT(additiveOperatorClicked()));

Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));

Button *squareRootButton = createButton(tr("Sqrt"), SLOT(unaryOperatorClicked()));

Button *powerButton = createButton(tr("x\262"), SLOT(unaryOperatorClicked()));

Button *reciprocalButton = createButton(tr("1/x"), SLOT(unaryOperatorClicked()));

Button *sinhButton = createButton(tr("sinh"), SLOT(unaryOperatorClicked()));

Button *coshButton = createButton(tr("cosh"), SLOT(unaryOperatorClicked()));

Button *tanhButton = createButton(tr("tanh"), SLOT(unaryOperatorClicked()));

Button *sinButton = createButton(tr("sin"), SLOT(unaryOperatorClicked()));

Button *cosButton = createButton(tr("cos"), SLOT(unaryOperatorClicked()));

Button *tanButton = createButton(tr("tan"), SLOT(unaryOperatorClicked()));

Button *mi2Button = createButton(tr("x^2"), SLOT(unaryOperatorClicked()));

Button *miyButton = createButton(tr("x^y"), SLOT(multiplicativeOperatorClicked()));

Button *mi3Button = createButton(tr("x^3"), SLOT(unaryOperatorClicked()));

Button *factorButton = createButton(tr("n!"), SLOT(unaryOperatorClicked()));

Button *genyButton = createButton(tr("x^1/y"), SLOT(multiplicativeOperatorClicked()));

Button *gen3Button = createButton(tr("x^1/3"), SLOT(unaryOperatorClicked()));

Button *expButton = createButton(tr("Exp"), SLOT(expOperatorClicked()));

Button *modButton = createButton(tr("Mod"), SLOT(multiplicativeOperatorClicked()));

Button *logButton = createButton(tr("log"), SLOT(unaryOperatorClicked()));

Button *mi10Button = createButton(tr("10^x"), SLOT(unaryOperatorClicked()));

Button *equalButton = createButton(tr("="), SLOT(equalClicked()));

//! [4]

//! [5]

QGridLayout *mainLayout = new QGridLayout;

//! [5] //! [6]

mainLayout->setSizeConstraint(QLayout::SetFixedSize);

mainLayout->addWidget(display, 0, 0, 1, 10);

mainLayout->addWidget(backspaceButton, 1, 0, 1, 4);

mainLayout->addWidget(clearButton, 1, 4, 1, 3);

mainLayout->addWidget(clearAllButton, 1, 7, 1, 3);

mainLayout->addWidget(clearMemoryButton, 2, 0);

mainLayout->addWidget(readMemoryButton, 3, 0);

mainLayout->addWidget(setMemoryButton, 4, 0);

mainLayout->addWidget(addToMemoryButton, 5, 0);

for (int i = 1; i < NumDigitButtons; ++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);

mainLayout->addWidget(pointButton, 5, 2);

mainLayout->addWidget(changeSignButton, 5, 3);

mainLayout->addWidget(divisionButton, 2, 4);

mainLayout->addWidget(timesButton, 3, 4);

mainLayout->addWidget(minusButton, 4, 4);

mainLayout->addWidget(plusButton, 5, 4);

mainLayout->addWidget(squareRootButton, 2, 5);

mainLayout->addWidget(powerButton, 3, 5);

mainLayout->addWidget(reciprocalButton, 4, 5);

mainLayout->addWidget(equalButton, 5, 5);

mainLayout->addWidget(sinhButton, 2, 6);

mainLayout->addWidget(coshButton, 3, 6);

mainLayout->addWidget(tanhButton, 4, 6);

mainLayout->addWidget(expButton, 5, 6);

mainLayout->addWidget(sinButton, 2, 7);

mainLayout->addWidget(cosButton, 3, 7);

mainLayout->addWidget(tanButton, 4, 7);

mainLayout->addWidget(modButton, 5, 7);

mainLayout->addWidget(mi2Button, 2, 8);

mainLayout->addWidget(miyButton, 3, 8);

mainLayout->addWidget(mi3Button, 4, 8);

mainLayout->addWidget(logButton, 5, 8);

mainLayout->addWidget(factorButton, 2, 9);

mainLayout->addWidget(genyButton, 3, 9);

mainLayout->addWidget(gen3Button, 4, 9);

mainLayout->addWidget(mi10Button, 5, 9);

setLayout(mainLayout);

setWindowTitle(tr("Calculator"));

}

//! [6]

//! [7]

void Calculator::digitClicked()

{

Button *clickedButton = qobject_cast

相关文档
最新文档