自己在Qt上做的辣鸡计算器

#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QDebug>
#include <stack>
using namespace std;

bool cmopareFun(QChar l,QChar r)
{
    if(r == '*' || r == '/')
    {
        if(l == '*' || r== '/')
            return false;
        else
            return true;
    }
    else
    {
            return false;
    }
}

double calc(QString str)
{
    double res = 0;
    double temp = 0;
    QString str1 = NULL;
    QString str2 = NULL;
    stack<QChar> stackFun;
    stack<double> stackNum;

    for(int i=0;i<str.length();i++)
    {
        if(str[i].isDigit())
        {
            str1.append(str[i]);
        }
        else
        {
            str1.append(' ');
            if(stackFun.size() == 0)
            {
                stackFun.push(str[i]);
            }
            else if(::cmopareFun(stackFun.top(),str[i]))
            {
                stackFun.push(str[i]);
            }
            else
            {
                str1.append(stackFun.top());
                stackFun.pop();
                stackFun.push(str[i]);
            }
            str1.append(' ');
        }
    }
    while(stackFun.size() >0 )
    {
        str1.append(' ');
        str1.append(stackFun.top());
        stackFun.pop();
    }

    qDebug() << str1;//

    for(int i=0;i<str1.length();i++)
    {
        if(str1[i].isSpace())
        {
            if(str2.length()>0)
            {
                temp = str2.toDouble();
                stackNum.push(temp);
                str2.clear();
            }
            continue;
        }
        else if(str1[i].isDigit())
        {
            str2.append(str1[i]);
        }
        else
        {
            double t1,t2;
            if(stackNum.size() == 0)
            {
                str2.append(str1[i]);
                continue;
            }
            else if(stackNum.size() == 1)
            {
                t1 = 0;
                t2 = stackNum.top();
                stackNum.pop();
            }
            else
            {
                t2 = stackNum.top();
                stackNum.pop();
                t1 = stackNum.top();
                stackNum.pop();
            }

            if(str1[i] == '+')
                stackNum.push(t1 + t2);
            if(str1[i] == '-')
                stackNum.push(t1 - t2);
            if(str1[i] == '*')
                stackNum.push(t1 * t2);
            if(str1[i] == '/')
                stackNum.push(t1 / t2);
        }
    }
    if(str2.length() > 0)
    {
        stackNum.push(str1.toDouble());
    }

    res = stackNum.top();
    stackNum.pop();

    return res;
}

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(ui->pushButton,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_2,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_3,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_4,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_6,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_7,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_9,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_10,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_11,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_12,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_13,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_14,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_15,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_16,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_17,&QPushButton::pressed,this,dealNum);
    connect(ui->pushButton_18,&QPushButton::pressed,this,dealNum);
}

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

void Widget::dealNum()
{
    QPushButton* pSender = (QPushButton*)this->sender();
    QString str = ui->lineEdit->text();
    QString add = pSender->text();

    double left = 0, right = 0, res = 0;
    char calc = 0;

    if(add == "c")
    {
        str.chop(1);
    }
    else if(isdigit(*add.toStdString().data()))
    {
        if(str == "0")
            str.chop(1);
        str += add;
    }
    else
    {
        //first function
//        if(sscanf(str.toStdString().data(),"%lf%c%lf",&left,&calc,&right) != 3)
//            goto RES;
//        switch(calc)
//        {
//        case '+':
//            res = left + right;
//            break;
//        case '-':
//            res = left - right;
//            break;
//        case '*':
//            res = left * right;
//            break;
//        case '/':
//            res = left / right;
//            break;
//        default:
//            break;
//        }
//        str = QString::number(res);
//        RES:
//        if(strcmp(add.toStdString().data(),"=") != 0)
//            str += add;

        //second function
        if(*add.toStdString().data() == '=')
        {
            res = ::calc(str);
            ui->lineEdit->setText(QString::number(res));
            return;
        }
        else
        {
            str += add;
        }
    }

    ui->lineEdit->setText(str);
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏linux驱动个人学习

高通Audio中ASOC的machine驱动

ASoC被分为Machine、Platform和Codec三大部分,其中的Machine驱动负责Platform和Codec之间的耦合以及部分和设备或板子特定的...

9784
来自专栏搞前端的李蚊子

Html5模拟通讯录人员排序(sen.js)

// JavaScript Document  var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_...

5976
来自专栏余生开发

echarts太阳分布图-饼图来回穿梭

var dom = document.getElementById("container");

1202
来自专栏一个会写诗的程序员的博客

java.base.jmod

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods$ jmod list java....

1112
来自专栏图形学与OpenGL

8.6.4编程实例-纹理映射

GLubyte stripeImage[4*stripeImageWidth];

1032
来自专栏Golang语言社区

Knapsack problem algorithms for my real-life carry-on knapsack

I'm a nomad and live out of one carry-on bag. This means that the total weight o...

1142
来自专栏Petrichor的专栏

Dataset 列表:机器学习研究

In computer vision, face images have been used extensively to develop face recog...

1541
来自专栏哲学驱动设计

线程同步:System.Core中新的读写锁

读写锁是进程同步中经常使用的锁。 在System.Core中ReaderWriterLockSlim类比较好用,它是基于写优先策略的。它还支持从读锁升级到写锁,...

1937
来自专栏专知

2018年SCI期刊最新影响因子排行,最高244,人工智能TPAMI9.455

2018年6月26日,最新的SCI影响因子正式发布,涵盖1万2千篇期刊。CA-Cancer J Clin 依然拔得头筹,其影响因子今年再创新高,达244.585...

1282
来自专栏逍遥剑客的游戏开发

写的第一个Shader

26210

扫码关注云+社区