码迷,mamicode.com
首页 > 其他好文 > 详细

Qt版科学计算器

时间:2015-02-27 21:35:21      阅读:931      评论:0      收藏:0      [点我收藏+]

标签:qt   科学计算器   

Qt版科学计算器

转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907

          之前做过《VC版科学计算器》,这也是我学VC++时的第一个大作业,通过科学计算器的开发使用我学到了很多东西,也让我逐渐喜欢上了编程。最近在学习Qt,所以将当时在VC下写过的一些东西在Qt下重写了一遍,其实主要还是与显示等有关的东西需要重写,要使用Qt的显示方式,而其他的核心的算法等都还是使用VC下C++的源码。

下面是Qt版的运行截图:

标准版:

技术分享

科学版:

技术分享


头文件中变量和槽的声明:

class CalculatorScientific : public QMainWindow
{
    Q_OBJECT

public:
    explicit CalculatorScientific(QWidget *parent = 0);
    ~CalculatorScientific();
private slots:
    void digitClicked();
    void unaryOperatorClicked();
    void additiveOperatorClicked();
    void multiplicativeOperatorClicked();
    void equalClicked();
    void pointClicked();
    void changeSignClicked();
    void backspaceClicked();
    void clear();
    void clearAll();
    void clearMemory();
    void readMemory();
    void setMemory();
    void addToMemory();
    void subToMemory();

private:
    Ui::CalculatorScientific *ui;

    double angleToArc(double angle);
    double getPi();
    void abortOperation();
    bool calculate(double rightOperand, const QString &pendingOperator);

    double sumInMemory;
    double sumSoFar;
    double factorSoFar;

    QString pendingAdditiveOperator;
    QString pendingMultiplicativeOperator;

    bool waitingForOperand;

};
转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907


信号和槽的关联代码:

 //NumberKey
    connect(ui->pushButton_Zero, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_One, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_Two, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_Three, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_Four, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_Five, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_Six, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_Seven, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_Eight, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_Nine, SIGNAL(clicked()), this, SLOT(digitClicked()));

    connect(ui->pushButton_A, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_B, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_C, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_D, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_E, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->pushButton_F, SIGNAL(clicked()), this, SLOT(digitClicked()));

    connect(ui->pushButton_Pi, SIGNAL(clicked()), this, SLOT(digitClicked()));

 //OperatorKey
    connect(ui->pushButton_Add, SIGNAL(clicked()), this, SLOT(additiveOperatorClicked()));
    connect(ui->pushButton_Sub, SIGNAL(clicked()), this, SLOT(additiveOperatorClicked()));
    connect(ui->pushButton_Mul, SIGNAL(clicked()), this, SLOT(multiplicativeOperatorClicked()));
    connect(ui->pushButton_Div, SIGNAL(clicked()), this, SLOT(multiplicativeOperatorClicked()));
    connect(ui->pushButton_Mod, SIGNAL(clicked()), this, SLOT(additiveOperatorClicked()));
    connect(ui->pushButton_And, SIGNAL(clicked()), this, SLOT(additiveOperatorClicked()));
    connect(ui->pushButton_Xor, SIGNAL(clicked()), this, SLOT(additiveOperatorClicked()));
    connect(ui->pushButton_X_y, SIGNAL(clicked()), this, SLOT(additiveOperatorClicked()));


再看一下计算函数的实现代码:

bool CalculatorScientific::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;
    } else if (pendingOperator == tr("%")) {
        sumSoFar = fmod(sumSoFar,rightOperand);
    } else if (pendingOperator == tr("And")) {
        sumSoFar = (int)sumSoFar & (int)rightOperand;
    } else if (pendingOperator == tr("Xor")) {
        sumSoFar = (int)sumSoFar ^ (int)rightOperand;
    } else if (pendingOperator == tr("x^y")) {
        sumSoFar = pow(sumSoFar,rightOperand);
    }
    return true;
}

单目运算:

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;
}
转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907



Qt版科学计算器

标签:qt   科学计算器   

原文地址:http://blog.csdn.net/u012027907/article/details/43971999

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!