Example #1
0
//! [20]
void Calculator::equalClicked()
{
    double operand = display->text().toDouble();

    if (!pendingMultiplicativeOperator.isEmpty()) {
        if (!calculate(operand, pendingMultiplicativeOperator)) {
            abortOperation();
            return;
        }
        operand = factorSoFar;
        factorSoFar = 0.0;
        pendingMultiplicativeOperator.clear();
    }
    if (!pendingAdditiveOperator.isEmpty()) {
        if (!calculate(operand, pendingAdditiveOperator)) {
            abortOperation();
            return;
        }
        pendingAdditiveOperator.clear();
    } else {
        sumSoFar = operand;
    }

    display->setText(QString::number(sumSoFar));
    sumSoFar = 0.0;
    waitingForOperand = true;
}
Example #2
0
 void Calculator::additiveOperatorClicked()
 {
     Button *clickedButton = qobject_cast<Button *>(sender());
     QString clickedOperator = clickedButton->text();
     double operand = display->text().toDouble();

     if (!pendingMultiplicativeOperator.isEmpty()) {
         if (!calculate(operand, pendingMultiplicativeOperator)) {
             abortOperation();
             return;
         }
         display->setText(QString::number(factorSoFar));
         operand = factorSoFar;
         factorSoFar = 0.0;
         pendingMultiplicativeOperator.clear();
     }

     if (!pendingAdditiveOperator.isEmpty()) {
         if (!calculate(operand, pendingAdditiveOperator)) {
             abortOperation();
             return;
         }
         display->setText(QString::number(sumSoFar));
     } else {
         sumSoFar = operand;
     }

     pendingAdditiveOperator = clickedOperator;
     waitingForOperand = true;
 }
Example #3
0
//! [8]
void Calculator::unaryOperatorClicked()
//! [8] //! [9]
{
    Button *clickedButton = qobject_cast<Button *>(sender());
    QString clickedOperator = clickedButton->text();
    double operand = display->text().toDouble();
    double result = 0.0;

    if (clickedOperator == tr("Sqrt")) {
        if (operand < 0.0) {
            abortOperation();
            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) {
            abortOperation();
            return;
        }
        result = 1.0 / operand;
    }
    display->setText(QString::number(result));
    waitingForOperand = true;
}
Example #4
0
/********************************************************************************
 * Description      : Méthode permettant d'effectuer les opérations de calcul
                      pouvant être effectué par la calculatrice.
 * Paramètres       : const QString &text, const char *member
 * Type de retour   : bouton*
 *******************************************************************************/
bool Calculatrice::calculate(double rightOperand, const QString &pendingOperator)
{
    if(pendingOperator == tr("+"))
    {
        sumSoFar +=rightOperand;
    }
    else if(pendingOperator == tr("-"))
    {
        sumSoFar -= rightOperand;
    }
    else if(pendingOperator == tr("\327"))
    {
        factorSoFar *= rightOperand;
    }
    else if(pendingOperator == tr("\367"))
    {
        try
        {
            if (rightOperand == 0.0)
            {
                throw divisionParZero();
                abortOperation();
                return false;
            }
            else
                factorSoFar /= rightOperand;
        }
        catch(logic_error const& e)
        {
            QMessageBox::critical(this, "Error", e.what());
        }
    }
    return true;
}
Example #5
0
/********************************************************************************
 * Description      : unaryOperatorClicked() slot est appellé lorsque un des boutons
                      d'opérateur unaire est activé.
 * Paramètres       : Aucun
 * Type de retour   : void
 *******************************************************************************/ 
void Calculatrice::unaryOperatorClicked()
{
    Bouton* clickedButton = qobject_cast<Bouton*>(sender());
    QString clickedOperator = clickedButton->text();
    double operand = display->text().toDouble();
    double result = 0.0;

    try
    {
        if(clickedOperator == tr("Sqrt"))
        {
             if(operand < 0.0)
             {
                 throw racineNeg();
                 abortOperation();
                 return;
             }
            result = sqrt(operand);
        }
        else if(clickedOperator == tr("x\262"))
        {
            result = pow(operand, 2.0);
        }
        else if(clickedOperator == tr("1/x"))
        {
             if(operand == 0.0)
             {
                 throw inverseZero();
                 abortOperation();
                 return;
             }
              result = 1.0 / operand;
        }
    }
    catch(logic_error const& e)
    {
        QMessageBox::critical(this, "Error", e.what());
    }
    display->setText(QString::number(result));
    waitingForOperand = true;

}
Example #6
0
void CSVDoc::Operation::initWidgets()
{
    mProgressBar = new QProgressBar ();
    mAbortButton = new QPushButton("Abort");
    mLayout = new QHBoxLayout();

    mLayout->addWidget (mProgressBar);
    mLayout->addWidget (mAbortButton);

    connect (mAbortButton, SIGNAL (clicked()), this, SLOT (abortOperation()));
}
Example #7
0
//! [10]
void Calculator::additiveOperatorClicked()
//! [10] //! [11]
{
    Button *clickedButton = qobject_cast<Button *>(sender());
    QString clickedOperator = clickedButton->text();
    double operand = display->text().toDouble();

//! [11] //! [12]
    if (!pendingMultiplicativeOperator.isEmpty()) {
//! [12] //! [13]
        if (!calculate(operand, pendingMultiplicativeOperator)) {
            abortOperation();
            return;
        }
        display->setText(QString::number(factorSoFar));
        operand = factorSoFar;
        factorSoFar = 0.0;
        pendingMultiplicativeOperator.clear();
    }

//! [13] //! [14]
    if (!pendingAdditiveOperator.isEmpty()) {
//! [14] //! [15]
        if (!calculate(operand, pendingAdditiveOperator)) {
            abortOperation();
            return;
        }
        display->setText(QString::number(sumSoFar));
    } else {
        sumSoFar = operand;
    }

    calculateCounter++;

//! [15] //! [16]
    pendingAdditiveOperator = clickedOperator;
//! [16] //! [17]
    waitingForOperand = true;
}
void CalculatorScientific::multiplicativeOperatorClicked()
{
    QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
    QString clickedOperator = clickedButton->text();
    double operand = ui->lineEdit_display->text().toDouble();

    if (!pendingMultiplicativeOperator.isEmpty()) {
        if (!calculate(operand, pendingMultiplicativeOperator)) {
            abortOperation();
        return;
        }
        ui->lineEdit_display->setText(QString::number(factorSoFar));
    } else {
        factorSoFar = operand;
    }

    pendingMultiplicativeOperator = clickedOperator;
    waitingForOperand = true;
}
Example #9
0
void MiniCalc::multiplicativeOperatorClicked()
{
    QPushButton *clickedButton = (QPushButton*)sender();
    QString clickedOperator = clickedButton->text();
    double operand = display->text().toDouble();

    if (!pendingMultiplicativeOperator.isEmpty()) {
        if (!calculate(operand, pendingMultiplicativeOperator)) {
            abortOperation();
            return;
        }
        display->setText(QString::number(factorSoFar));
    } else {
        factorSoFar = operand;
    }

    pendingMultiplicativeOperator = clickedOperator;
    waitingForOperand = true;
}
Example #10
0
void MiniCalc::unaryOperatorClicked()
{
    QPushButton *clickedButton = (QPushButton*)sender();
    QString clickedOperator = clickedButton->text();
    double operand = display->text().toDouble();
    double result = 0.0;


    if (clickedOperator == tr("x\262")) {
        result = pow(operand, 2.0);
    } else if (clickedOperator == tr("1/X")) {
        if (operand == 0.0) {
            abortOperation();
            return;
        }
        result = 1.0 / operand;
    }
    display->setText(QString::number(result));
    waitingForOperand = true;
}
Example #11
0
void CSVDoc::Operation::abortOperation()
{
    emit abortOperation (mType);
}
void CalculatorScientific::unaryOperatorClicked()
{
    QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
    QString clickedOperator = clickedButton->text();
    double operand = ui->lineEdit_display->text().toDouble();
    double result = 0.0;

    if (clickedOperator == tr("Sqrt")) {
        if (operand < 0.0) {
            abortOperation();
            return;
        }
        result = sqrt(operand);
    } else if (clickedOperator == tr("x^2")) {
        result = pow(operand, 2.0);
    } else if (clickedOperator == tr("x^3")){
        result = pow(operand, 3.0);
    } else if (clickedOperator == tr("1/X")) {
        if (operand == 0.0) {
            abortOperation();
            return;
        }
        result = 1.0 / operand;
    } else if (clickedOperator == tr("ln")) {
        if (operand < 0.0) {
            abortOperation();
            return;
        }
        result = log(operand);
    } else if (clickedOperator == tr("log")) {
        if (operand < 0.0) {
            abortOperation();
            return;
        }
        result = log10(operand);
    } else if (clickedOperator == tr("sin")) {
        if(ui->radioButton_Arc->isChecked())
            result = sin(operand);
        else
            result = sin(angleToArc(operand));
    } else if (clickedOperator == tr("sinh")) {

        result = sinh(operand);
    } else if (clickedOperator == tr("cos")) {
        if(ui->radioButton_Arc->isChecked())
            result = cos(operand);
        else
            result = cos(angleToArc(operand));
    } else if (clickedOperator == tr("cosh")) {

        result = cosh(operand);
    } else if (clickedOperator == tr("tan")) {
        if(ui->radioButton_Arc->isChecked())
            result = tan(operand);
        else
            result = tan(angleToArc(operand));
    } else if (clickedOperator == tr("tanh")) {

        result = tanh(operand);
    } else if (clickedOperator == tr("n!")) {
        if (operand < 0.0) {
            abortOperation();
            return;
        }
        result = 1;
        for(int i = 1; i <= (int)operand; i++)
            result *= i;
    } else if (clickedOperator == tr("e^x")) {
        result = exp(operand);
    } else if (clickedOperator == tr("10^x")) {
        result = pow(10,operand);
    } else if (clickedOperator == tr("Int")) {
        result = (int)(operand);
    } else if (clickedOperator == tr("Not")) {
        result = ~(int)(operand);
    }
    ui->lineEdit_display->setText(QString::number(result));
    waitingForOperand = true;
}