QT实现精美计算机

▼魔方 西西 提交于 2019-12-13 03:42:35

QT简介

Qt [1] 是一个1991年由Qt Company开发的跨平台C++图形用户界面应用程序开发框架。它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器。Qt是面向对象的框架,使用特殊的代码生成扩展(称为元对象编译器(Meta Object Compiler, moc))以及一些宏,Qt很容易扩展,并且允许真正地组件编程。
使用QT制作精简计算机是初学者必备技能之一。读者可根据自己算法,如栈的知识来判断计算符号优先级考虑

使用QT实现计算机

首先先初步实现计算机所需功能模块目录
在这里插入图片描述

画精美计算机

此处放计算机示意图,读者可根据要求自行修改UI文件
在这里插入图片描述

特别注意!
在旁赋予Id与是否能编辑属性(读者根据要求自我调控)
在这里插入图片描述
在这里插入图片描述

工程文件

重点来了:
初始化各种代码,与函数声明`
dialog.h头文件

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPixmap>
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

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

private slots:
    void on_Button0_clicked();
    void on_Button1_clicked();
    void on_Button2_clicked();
    void on_Button3_clicked();
    void on_Button4_clicked();
    void on_Button5_clicked();
    void on_Button6_clicked();
    void on_Button7_clicked();
    void on_Button8_clicked();
    void on_Button9_clicked();
    void on_Button_add_clicked();
    void on_Button_multip_clicked();
    void on_Button_point_clicked();
    void on_Button_sub_clicked();
    void on_Button_pow_clicked();
    void on_Button_left_clicked();
    void on_Button_right_clicked();
    void on_Button_sin_clicked();
    void on_Button_yu2_clicked();
    void on_Button_yu_clicked();
    void on_Button_result_clicked();
    void on_Button_divide_clicked();
    void on_Button_delete_clicked();
    void on_ButtonC_clicked();
    long long Poww(long long a,long long b);
    long long f(int n);
    double getVal(int &s, int e, char str[]);
    double getVal1(int &s, int e, char str[]);
    double cal(int s, int e, char str[]);

private:
    Ui::Dialog *ui;
    QString tmp;

    int flag = 1;
    int flag_2 = 0;
    bool isNumber = true;//符号后面接数字
    bool isLeft = false;//左边括号
    int flag_divide = 1;//除法,被除数不能为0
    int flag_yu = 1;//取余数不能是double、float型
};
#endif // DIALOG_H

dialog.cpp

各种计算可以查看各种算法来进行优化,此处仅引入快速幂算法
并进行除余两数间不可不为整数、与符号后面跟数字、括号之间相对应之间的输入优化。
加上除数不能为0的温馨提示

#include "dialog.h"
#include "ui_dialog.h"
#include "calculator.h"
#include "math.h"
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

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


void Dialog::on_Button0_clicked()
{
    if(this->tmp != "")
    {
        this->tmp += this->ui->Button0->text();
        this->ui->info->setText(this->tmp);
        flag_2 = 1;
        isNumber = false;
    }

}

void Dialog::on_Button1_clicked()
{
    this->tmp += this->ui->Button1->text();
    this->ui->info->setText(this->tmp);
    flag_2 = 1;
    isNumber = false;
}



void Dialog::on_Button2_clicked()
{
    this->tmp += this->ui->Button2->text();
    this->ui->info->setText(this->tmp);
    flag_2 = 1;isNumber = false;
}

void Dialog::on_Button3_clicked()
{
    this->tmp += this->ui->Button3->text();
    this->ui->info->setText(this->tmp);
    flag_2 = 1;isNumber = false;
}

void Dialog::on_Button4_clicked()
{
    this->tmp += this->ui->Button4->text();
    this->ui->info->setText(this->tmp);
    flag_2 = 1;isNumber = false;
}

void Dialog::on_Button5_clicked()
{
    this->tmp += this->ui->Button5->text();
    this->ui->info->setText(this->tmp);
    flag_2 = 1;isNumber = false;
}

void Dialog::on_Button6_clicked()
{
    this->tmp += this->ui->Button6->text();
    this->ui->info->setText(this->tmp);
    flag_2 = 1;isNumber = false;
}

void Dialog::on_Button7_clicked()
{
    this->tmp += this->ui->Button7->text();
    this->ui->info->setText(this->tmp);
    flag_2 = 1;isNumber = false;
}

void Dialog::on_Button8_clicked()
{
    this->tmp += this->ui->Button8->text();
    this->ui->info->setText(this->tmp);
    flag_2 = 1;isNumber = false;
}

void Dialog::on_Button9_clicked()
{
    this->tmp += this->ui->Button9->text();
    this->ui->info->setText(this->tmp);
    flag_2 = 1;isNumber = false;
}

void Dialog::on_Button_pow_clicked()
{
    if(this->tmp != ""&&!isNumber)
    {
        this->tmp += this->ui->Button_pow->text();
        this->ui->info->setText(this->tmp);
        flag = 1;flag_2 = 0;
        isNumber = true;
    }
}

void Dialog::on_Button_point_clicked()
{
    if(this->tmp != ""&&flag == 1&&flag_2 == 1)
    {
        this->tmp += this->ui->Button_point->text();
        this->ui->info->setText(this->tmp);
        flag = 0;flag_2 = 0;

    }
}

void Dialog::on_Button_sin_clicked()
{
    this->tmp += this->ui->Button_sin->text();
    this->ui->info->setText(this->tmp);
    flag = 1;flag_2 = 0;
    isNumber = true;
}

void Dialog::on_Button_yu2_clicked()
{
    if(this->tmp != "")
    {
        this->tmp += this->ui->Button_yu2->text();
        this->ui->info->setText(this->tmp);
        flag = 1;flag_2 = 0;
        isNumber = true;
    }
}

void Dialog::on_Button_yu_clicked()
{
    if(this->tmp != ""&&!isNumber)
    {
        this->tmp += this->ui->Button_yu->text();
        this->ui->info->setText(this->tmp);
        flag = 1;flag_2 = 0;isNumber = true;
    }
}

void Dialog::on_Button_left_clicked()
{
    this->tmp += this->ui->Button_left->text();
    this->ui->info->setText(this->tmp);
    flag = 1;flag_2 = 0;isNumber = false;
    isLeft = true;
}

void Dialog::on_Button_right_clicked()
{
    if(isLeft)
    {
        this->tmp += this->ui->Button_right->text();
        this->ui->info->setText(this->tmp);
        flag = 1;flag_2 = 0;isNumber = true;
    }
}

void Dialog::on_ButtonC_clicked()
{
    if(tmp != ""){
        tmp.clear();
        this->ui->info->clear();
    }
}

void Dialog::on_Button_delete_clicked()
{
    if(this->tmp != "")
    {
        this->tmp = this->tmp.left(this->tmp.length()-1);
        this->ui->info->setText(this->tmp);
        isNumber = false;
    }
}

void Dialog::on_Button_divide_clicked()
{
    if(this->tmp != ""&&!isNumber)
    {
        this->tmp += this->ui->Button_divide->text();
        this->ui->info->setText(this->tmp);
        flag = 1;flag_2 = 0;isNumber = true;
    }
}

void Dialog::on_Button_multip_clicked()
{
    if(this->tmp != ""&&!isNumber)
    {
        this->tmp += this->ui->Button_multip->text();
        this->ui->info->setText(this->tmp);
        flag = 1;flag_2 = 0;
        isNumber = true;
    }
}

void Dialog::on_Button_sub_clicked()
{
    if(true)
    {
        this->tmp += this->ui->Button_sub->text();
        this->ui->info->setText(this->tmp);
        flag = 1;flag_2 = 0;isNumber = true;
    }
}

void Dialog::on_Button_add_clicked()
{
    if(this->tmp != "")
    {
        this->tmp += this->ui->Button_add->text();
        this->ui->info->setText(this->tmp);
        flag = 1;flag_2 = 0;isNumber = true;
    }
}

void Dialog::on_Button_result_clicked()
{
    if(tmp != ""){
        char str[2000];
        QByteArray qByteArray = tmp.toLatin1();
        char *Str = qByteArray.data();
        memcpy(str,Str,2000);
        double ans = cal(0,strlen(str),str);
        this->tmp.clear();
        if(flag_divide == 1&&flag_yu == 1){
            if(ans == ceil(ans)&&ans == floor(ans)){
                this->tmp =  QString::number(ans,10,0);
            }else{
                this->tmp =  QString::number(ans,10,5);
            }
        }else if(flag_divide ==0){
            this->ui->info->setPlaceholderText("被除数不能为0");
        }else{
            this->ui->info->setPlaceholderText("输入格式有误");
        }
        this->ui->info->setText(this->tmp);
        isNumber = false;
        flag_divide = 1;
        flag_yu = 1;
    }
}

long long Dialog::Poww(long long a,long long b)
{
    long long ans=1;long long base=a;
    while(b!=0)
    {
        if(b&1)ans*=base;
        base*=base;
        b>>=1;
    }
    return ans;//快速幂
}

long long Dialog::f(int n){
    long long sum;
    if(n==1)return 1;
    if(n>1)return n*f(n-1);//阶乘
}

double Dialog::getVal(int &s, int e, char str[])
{
    if (str[s] == '(')
    {
        double begin, mid, i;
        i = 1;
        begin = s + 1;
        while (i)
        {
            s++;
            if (s == e)
                break;
            if (str[s] == '(')
                i++;
            else if (str[s] == ')')
                i--;
        }
        mid = s - 1;
        s++;
        return cal(begin, mid, str);
    }
    double val;
    if(str[s]=='s')
    {
        s=s+3;
        val=sin(getVal(s,e,str));
    }
    if(str[s]=='c')
    {
        s=s+3;
        val=cos(getVal(s,e,str));
    }
    if(str[s]=='t')
    {
        s=s+3;
        val=tan(getVal(s,e,str));
    }

    if(str[s]>='0'&&str[s]<='9')
    {
        val = str[s++] - '0';
        while (s<e && str[s] >= '0'&&str[s] <= '9')
        {
            val *= 10;
            val += str[s++] - '0';//多位数
        }
        if(str[s]=='!'){
            int fn = (int)val;
            val=f(fn);//阶乘
            s++;
        }
        if(str[s]=='.')
        {
            s=s+1;//小数
            double val2=str[s++] - '0';
            while (s<e && str[s] >= '0'&&str[s] <= '9')
            {
                val2 *= 10;
                val2 += str[s++] - '0';
            }
            while(val2>=1)
            {
                val2/=10;
            }
            val=val+val2;
        }
    }
    return val;
}

double Dialog::getVal1(int &s, int e, char str[])
{
    double val;
    val = getVal(s, e, str);
    while (1)
    {
        if (s < e&&str[s] == '*') val*=getVal(++s,e,str);
        else if (s<e && str[s] == '/'){
            double cnt = getVal(++s,e,str);
            if(ceil(cnt) == 0&&floor(cnt) == 0){
                flag_divide = 0;
                return 0;
            }else{
                val /= cnt;
            }

        }
        else if (s<e && str[s] == '^')val=Poww(val,getVal(++s,e,str));
        else if (s<e && str[s] == '%'){
            double cnt = getVal(++s,e,str);
            if(ceil(cnt) == floor(cnt)&&(ceil(val) == floor(val))){
                int bbq = ceil(cnt),bbq2 = ceil(val);
                bbq2 %= bbq;
                val = bbq2;
            }else{
                flag_yu = 0;
                return 0;
            }
        }
        else return val;
    }
}
double Dialog::cal(int s, int e, char str[])
{
    double sum = 0;
    if (str[s] != '-')
        sum = getVal1(s, e, str);
    while (s < e)
    {
        if (str[s] == '+')
        {
            sum += getVal1(++s, e, str);
        }
        if (str[s] == '-')
        {
            sum -= getVal1(++s, e, str);
        }
    }
    return sum;
}



pro文件的修改

最后最后,记得修改pro文件。

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 \
    dialog.cpp

HEADERS += \
    dialog.h \
    floatnumber.h

FORMS += \
    dialog.ui

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

DISTFILES += \
    timg.jfif

RESOURCES += \
    image.qrc \
    image.qrc

下面是示意图。
在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!