前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自己在Qt上做的辣鸡计算器

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

作者头像
_gongluck
发布2018-03-09 15:42:41
1.3K0
发布2018-03-09 15:42:41
举报
文章被收录于专栏:转载gongluck的CSDN博客
代码语言:javascript
复制
#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);
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年05月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档