QT实现计算器

1、新建CalculatorStandard类和工程文件

QT实现计算器_第1张图片

2、使用水平布局和把按键依次添加

ui界面如下图所示:

QT实现计算器_第2张图片

3、在CalculatorStandard.h中声明用到的槽函数

private slots:
        void digitClicked ();//数字按键
        void addAndSubClicked();//加减按键
        void MulAndDerivativeClicked();//乘除按键
        void EqualClicked();//等号按键
        void clearAll();//清楚按键
        void clear();
        void backspaceClicked();//空格按键
        void pointClicked();//小数点按键
        void MCclicked();//清除记忆数据
        void MRclicked();//添加记忆数据
        void MSclicked();//读取记忆数据
        void MPlusclicked();//清除记忆数据
        void plusMinusClicked();//加减按键
        void otherOperatorclicked();//开方。平方,求导数等

4、在CalculatorStandard.cpp中实现槽函数

void CalculatorStandard::digitClicked()
{
    //当某一个Object emit一个signal的时候,它就是一个sender,系统会记录下当前是谁emit出这个signal的,
    //所以你在对应的slot里就可以通过 sender()得到当前是谁invoke了你的slot,对应的是QObject->d->sender.
    //有可能多个 Object的signal会连接到同一个signal(例如多个Button可能会connect到一个slot函数onClick()),
    //因此这是就 需要判断到底是哪个Object emit了这个signal,根据sender的不同来进行不同的处理.
    QPushButton *clickedButton = qobject_cast(sender());
    int digitValue = clickedButton->text().toInt();
    if (ui->lineEdit_display->text() == "0" && digitValue == 0.0)
    {
        return;
    }

    if (waitingForOperand) {
        ui->lineEdit_display->clear();
        waitingForOperand = false;
    }
    ui->lineEdit_display->setText(ui->lineEdit_display->text() + QString::number(digitValue));
}

void CalculatorStandard::unaryOperatorClicked()
{
    QPushButton *clickedButton = qobject_cast(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("1/X")) {
        if (operand == 0.0) {
            abortOperation();
            return;
        }
        result = 1.0 / operand;
    }
    ui->lineEdit_display->setText(QString::number(result));
    waitingForOperand = true;
}

void CalculatorStandard::additiveOperatorClicked()
{
    QPushButton *clickedButton = qobject_cast(sender());
    QString clickedOperator = clickedButton->text();
    double operand = ui->lineEdit_display->text().toDouble();

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

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

    pendingAdditiveOperator = clickedOperator;

    waitingForOperand = true;
}

void CalculatorStandard::multiplicativeOperatorClicked()
{
    QPushButton *clickedButton = qobject_cast(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;
}

void CalculatorStandard::equalClicked()
{
    double operand = ui->lineEdit_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;
    }

    ui->lineEdit_display->setText(QString::number(sumSoFar));
    sumSoFar = 0.0;
    waitingForOperand = true;
}

void CalculatorStandard::pointClicked()
{
    if (waitingForOperand)
        ui->lineEdit_display->setText("0");
    if (!ui->lineEdit_display->text().contains("."))
        ui->lineEdit_display->setText(ui->lineEdit_display->text() + tr("."));
    waitingForOperand = false;
}

void CalculatorStandard::changeSignClicked()
{
    QString text = ui->lineEdit_display->text();
    double value = text.toDouble();

    if (value > 0.0) {
        text.prepend(tr("-"));
    } else if (value < 0.0) {
        text.remove(0, 1);
    }
    ui->lineEdit_display->setText(text);
}

void CalculatorStandard::backspaceClicked()
{
    if (waitingForOperand)
        return;

    QString text = ui->lineEdit_display->text();
    text.chop(1);
    if (text.isEmpty()) {
        text = "0";
        waitingForOperand = true;
    }
    ui->lineEdit_display->setText(text);
}

void CalculatorStandard::clear()
{
    if (waitingForOperand)
        return;

    ui->lineEdit_display->setText("0");
    waitingForOperand = true;
}

void CalculatorStandard::clearAll()
{
    sumSoFar = 0.0;
    factorSoFar = 0.0;
    pendingAdditiveOperator.clear();
    pendingMultiplicativeOperator.clear();
    ui->lineEdit_display->setText("0");
    waitingForOperand = true;
}

void CalculatorStandard::clearMemory()
{
    sumInMemory = 0.0;
}

void CalculatorStandard::readMemory()
{
    ui->lineEdit_display->setText(QString::number(sumInMemory));
    waitingForOperand = true;
}

void CalculatorStandard::setMemory()
{
    equalClicked();
    sumInMemory = ui->lineEdit_display->text().toDouble();
}

void CalculatorStandard::addToMemory()
{
    equalClicked();
    sumInMemory += ui->lineEdit_display->text().toDouble();
}

void CalculatorStandard::abortOperation()
{
    clearAll();
    ui->lineEdit_display->setText(tr("####"));
}

bool CalculatorStandard::calculate(double rightOperand, const QString &pendingOperator)
{
    if (pendingOperator == tr("+")) {
        sumSoFar += rightOperand;
    } else if (pendingOperator == tr("-")) {
        sumSoFar -= rightOperand;
    } else if (pendingOperator == tr("*")) {
        factorSoFar *= rightOperand;
    } else if (pendingOperator == tr("/")) {
    if (rightOperand == 0.0)
        return false;
    factorSoFar /= rightOperand;
    }
    return true;
}

5、运行结果如下图所示

QT实现计算器_第3张图片

你可能感兴趣的:(C++,QT)