Qt(C++)实现的简易计算器

软件工程的作业,让写一个计算器,为此花两天抽时间看了看Qt入门,然后就开搞了。

环境Qt 5.13.1,MinGw 7.3.0 32-bit,IDE:Qt Creator 4.10.0

新建Qt Widgets 应用,项目名称Calculator,基类选择GWidget,类名MyWidget。

1.首先是页面,直接拖拖拖,没用栅格,先垂直在水平布局了,改了一点参数,解决问题。

Qt(C++)实现的简易计算器_第1张图片

 2.编写信号和槽,并代码实现计算器功能

mywidget.h 最终更改如下:

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include 
#include 
#include 
#include 

QT_BEGIN_NAMESPACE
namespace Ui { class MyWidget; }
QT_END_NAMESPACE

class QErrorMessage;

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    MyWidget(QWidget *parent = nullptr);
    ~MyWidget();

private slots:
    void on_pushButtonCe_clicked();

    void on_pushButtonC_clicked();

    void on_pushButtonDe_clicked();

    void on_pushButtonChu_clicked();

    void on_pushButton7_clicked();

    void on_pushButton8_clicked();

    void on_pushButton9_clicked();

    void on_pushButtonChen_clicked();

    void on_pushButton4_clicked();

    void on_pushButton5_clicked();

    void on_pushButton6_clicked();

    void on_pushButtonJian_clicked();

    void on_pushButton1_clicked();

    void on_pushButton2_clicked();

    void on_pushButton3_clicked();

    void on_pushButtonJia_clicked();

    void on_pushButtonHuan_clicked();

    void on_pushButton0_clicked();

    void on_pushButtonDian_clicked();

    void on_pushButtonDenyu_clicked();

    void AddNum(int i);

    void ShowNum();

    void Operator(int i);

    void TwiceOperator();

    double OpreatorTrue();

    void DToStr(double c);

private:
    Ui::MyWidget *ui;
    QErrorMessage * errordlg;
    char showstr[100];
    double a = 0, b = 0;
    int cha = 0, wei = 10, showlen=0, oper;
    bool sign = false, signdian = false, starta = false, startb = false, signa = true, signb = true;
};
#endif // MYWIDGET_H

mywidget.cpp 最终更改如下:

#include "mywidget.h"
#include "ui_mywidget.h"
#include
#include
#include
#include

const double lin = 0.0000000001;

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    errordlg = new QErrorMessage(this);
}

MyWidget::~MyWidget()
{
    delete ui;
}

void MyWidget::DToStr(double c){
    sprintf(showstr, "%.8lf", c);
    showstr[8] = '\0';
    showlen = 8;
    for(int i=showlen-1; i>0; i--){
        if(showstr[i] == '0'){
            showstr[i] = '\0';
            showlen--;
        }else if(showstr[i] == '.'){
            showstr[i] = '\0';
            showlen--;
            break;
        }else{
            break;
        }
    }
}

void MyWidget::ShowNum(){
    qDebug() << showstr;
    if(showlen == 0){
        ui->lcdNumber->display(0);
    }else{
        ui->lcdNumber->display(showstr);
    }
}

void MyWidget::AddNum(int i){
    showstr[showlen++] = char(48+i);
    showstr[showlen] = '\0';
    if((signdian && showlen == 10) || (!signdian && showlen == 9)){
        errordlg->setWindowTitle(tr("错误提示"));
        errordlg->showMessage(tr("输入超限"));
        showstr[--showlen] = '\0';
        return;
    }
    cha = i;
    if(!sign){
        if(!starta){
            showstr[0] = char(48+i);
            showstr[1] = '\0';
            showlen = 1;
            starta = true;
        }
        if(signdian){
            a = a + double(i) / wei;
            wei *= 10;
        }else{
            a = a * 10 + i;
        }
    }else{
        if(!startb){
            showstr[0] = char(48+i);
            showstr[1] = '\0';
            showlen = 1;
            startb = true;
        }
        if(signdian){
            b = b + double(i) / wei;
            wei *= 10;
        }else{
            b = b * 10 + i;
        }
    }
    ShowNum();
}

void MyWidget::on_pushButtonCe_clicked()
{
    if(sign){
        TwiceOperator();
        b = 0;
        wei = 10;
        signdian = false;
        startb = false;
        DToStr(b);
        ShowNum();
    }else{
        a = 0;
        wei = 10;
        signdian = false;
        starta = false;
        DToStr(a);
        ShowNum();
    }
}

void MyWidget::on_pushButtonC_clicked()
{
    a = 0; b = 0;
    cha = 0; wei = 10; showlen=0; oper=0;
    showstr[0] = '\0';
    sign = false; signdian = false; starta = false; startb = false; signa = true; signb = true;
    DToStr(0.0);
    ShowNum();
}

void MyWidget::on_pushButtonDe_clicked()
{
    if(!sign){
        if(signdian){
            a = a * wei / 10;
            a -= cha;
            a /= wei;
            wei /= 10;
            if(wei == 1){
                signdian = false;
                wei = 10;
                //showstr[--showlen] = '\0';
            }
        }else{
            a -= cha;
            a /= 10;
            cha = int(a) % 10;
        }
    }else{
        if(signdian){
            b = b * wei;
            b -= cha;
            cha = (int(b) % 100) / 10;
            b /= wei;
            wei /= 10;
            if(wei == 1){
                signdian = false;
                wei = 10;
            }
        }else{
            b -= cha;
            b /= 10;
            cha = int(b) % 10;
        }
    }
    if(showlen>0){
        showstr[--showlen] = '\0';
    }
    ShowNum();
}

double MyWidget::OpreatorTrue(){
    switch (oper) {
    case 1 : return a + b;
    case 2 : return a - b;
    case 3 : return a * b;
    case 4 : {
        if(b>=-lin && b<=lin){
            errordlg->setWindowTitle(tr("错误提示"));
            errordlg->showMessage(tr("除数不能为0"));
            return a;
        } return a / b;
    }
    }
    return a;
}

void MyWidget::TwiceOperator(){
    b = 0;
    signb = true;
    signa = true;
    starta = true;
    startb = false;
    wei = 10;
    showlen=0;
    showstr[0] = '\0';
    signdian = false;
}

void MyWidget::Operator(int i){
    switch(i){
    case 1:{
        if(sign){
            if(!signa) a = 0 - a;
            if(!signb) b = 0 - b;
            a = OpreatorTrue();
            TwiceOperator();
            oper = 1;
            DToStr(a);
            ShowNum();
        }else{
            if(!signa) a = 0 - a;
            oper = 1;
            sign = true;
            TwiceOperator();
        }
    } break;
    case 2:{
        if(sign){
            if(!startb){
                startb = true;
                signb = false;
                showstr[0] = '-';
                showstr[++showlen] = '\0';
                ShowNum();
            }else{
                if(!signa) a = 0 - a;
                if(!signb) b = 0 - b;
                a = OpreatorTrue();
                TwiceOperator();
                oper = 2;
                DToStr(a);
                ShowNum();
            }
        }else{
            if(!starta){
                starta = true;
                signa = false;
                showstr[0] = '-';
                showstr[++showlen] = '\0';
                ShowNum();
            }else{
                if(!signa) a = 0 - a;
                oper = 2;
                TwiceOperator();
                sign = true;
            }
        }
    } break;
    case 3:{
        if(sign){
            if(!signa) a = 0 - a;
            if(!signb) b = 0 - b;
            a = OpreatorTrue();
            TwiceOperator();
            oper = 3;
            DToStr(a);
            ShowNum();
        }else{
            if(!signa) a = 0 - a;
            oper = 3;
            TwiceOperator();
            sign = true;
        }
    } break;
    case 4:{
        if(sign){
            if(!signa) a = 0 - a;
            if(!signb) b = 0 - b;
            a = OpreatorTrue();
            TwiceOperator();
            oper = 4;
            DToStr(a);
            ShowNum();
        }else{
            if(!signa) a = 0 - a;
            oper = 4;
            TwiceOperator();
            sign = true;
        }
    } break;
    }
}

void MyWidget::on_pushButtonHuan_clicked()
{
    if(sign){
        if(startb){
            if(signb){
                signb = false;
                for(int i=(++showlen); i>0; i--){
                    showstr[i] = showstr[i-1];
                }
                showstr[0] = '-';
                ShowNum();
            }else{
                signb = true;
                for(int i=0; i0; i--){
                    showstr[i] = showstr[i-1];
                }
                showstr[0] = '-';
                ShowNum();
            }else{
                signa = true;
                for(int i=0; i

 3.到这就没啥了,改一下项目文件啥的换个图标

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mywidget.cpp

HEADERS += \
    mywidget.h

FORMS += \
    mywidget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RC_ICONS = Calculator.ico

TARGET = Calculator
TEMPLATE = app

最后放效果图

Qt(C++)实现的简易计算器_第2张图片

 

Qt(C++)实现的简易计算器_第3张图片

最后附上所有文件的资源链接:https://download.csdn.net/download/alfa_jin/11790101 

你可能感兴趣的:(Qt,C++,计算器,作业)