项目一 计算器

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
#include <QTextEdit>
#include <QStack>

class MainWindow : public QMainWindow
{
    Q_OBJECT

protected:
    QLineEdit *data;
    QLineEdit *expression;
    QTextEdit *history;

    QPushButton *button_0;
    QPushButton *button_1;
    QPushButton *button_2;
    QPushButton *button_3;
    QPushButton *button_4;
    QPushButton *button_5;
    QPushButton *button_6;
    QPushButton *button_7;
    QPushButton *button_8;
    QPushButton *button_9;
    QPushButton *button_dot;
    QPushButton *button_add;
    QPushButton *button_sub;
    QPushButton *button_mul;
    QPushButton *button_div;
    QPushButton *button_pow;
    QPushButton *button_Lbrac;
    QPushButton *button_Rbrac;
    QPushButton *button_BIN;
    QPushButton *button_OCT;
    QPushButton *button_HEX;
    QPushButton *button_equal;
    QPushButton *button_BACK;
    QPushButton *button_C;

    QString s;    //算术表达式
    QString history_text;    //每次运算后加入历史记录的文本
    int dot_cnt;    //小数点后的位数
    double n;    //暂存数字栈的栈顶元素
    int brac_cnt;    //左括号与右括号的数量之差
    int finish_flag;    //标记是否完成一次运算

    QStack<double> num;
    QStack<char> op;

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

public:
    void calc_layout();    //计算器界面布局
    void My_connect();    //连接信号与槽函数
    void My_disconnect();    //取消信号与槽函数的连接
    void init();    //初始化计算器
    bool is_number(QString s, int i);    //判断字符是否为数字或小数点
    bool is_new(QString s, int i);    //判断数字是否为新的数字
    double calc(double x, double y, char op);    //双目运算
    bool is_prior(char newop, char op);    //比较newop的运算符优先级是否高于op
    QString check(QString s);    //对输入时算术表达式的合法性进行检查
    QString equal_check(QString s);    //对点击等号时算术表达式的合法性进行检查
    bool is_num(char c);    //判断字符是否为数字
    bool is_op(char c);    //判断字符是否为运算符
    void calculator();    //双栈算符优先级法对算术表达式进行计算

private slots:
    void button_0_clicked();    //按键事件
    void button_1_clicked();
    void button_2_clicked();
    void button_3_clicked();
    void button_4_clicked();
    void button_5_clicked();
    void button_6_clicked();
    void button_7_clicked();
    void button_8_clicked();
    void button_9_clicked();
    void button_dot_clicked();
    void button_add_clicked();
    void button_sub_clicked();
    void button_mul_clicked();
    void button_div_clicked();
    void button_pow_clicked();
    void button_Lbrac_clicked();
    void button_Rbrac_clicked();
    void button_BIN_clicked();
    void button_OCT_clicked();
    void button_HEX_clicked();
    void button_equal_clicked();
    void button_BACK_clicked();
    void button_C_clicked();
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    w.setFixedSize(640, 440);
    w.calc_layout();
    return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include <QPushButton>
#include <QLineEdit>
#include <QTextEdit>
#include <QGridLayout>
#include <QStack>
#include <QDebug>
#include <QtCore>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
}

MainWindow::~MainWindow()
{
}

void MainWindow::calc_layout()    //计算器界面布局
{
    this->setWindowTitle("计算器");

    QWidget *centerWindow = new QWidget;
    this -> setCentralWidget(centerWindow);

    init();

    data = new QLineEdit("");
    expression = new QLineEdit("");
    history = new QTextEdit("");

    data -> setFont(QFont("宋体", 12));
    expression -> setFont(QFont("宋体", 12));
    history -> setFont(QFont("宋体", 12));

    data -> setReadOnly(true);
    expression -> setReadOnly(true);
    history -> setReadOnly(true);

    button_0 = new QPushButton("0");
    button_1 = new QPushButton("1");
    button_2 = new QPushButton("2");
    button_3 = new QPushButton("3");
    button_4 = new QPushButton("4");
    button_5 = new QPushButton("5");
    button_6 = new QPushButton("6");
    button_7 = new QPushButton("7");
    button_8 = new QPushButton("8");
    button_9 = new QPushButton("9");
    button_dot = new QPushButton(".");
    button_add = new QPushButton("+");
    button_sub = new QPushButton("-");
    button_mul = new QPushButton("*");
    button_div = new QPushButton("/");
    button_pow = new QPushButton("^");
    button_Lbrac = new QPushButton("(");
    button_Rbrac = new QPushButton(")");
    button_BIN = new QPushButton("BIN");
    button_OCT = new QPushButton("OCT");
    button_HEX = new QPushButton("HEX");
    button_equal = new QPushButton("=");
    button_BACK = new QPushButton("←");
    button_C = new QPushButton("C");

    button_0 -> setFont(QFont("黑体", 13));
    button_1 -> setFont(QFont("黑体", 13));
    button_2 -> setFont(QFont("黑体", 13));
    button_3 -> setFont(QFont("黑体", 13));
    button_4 -> setFont(QFont("黑体", 13));
    button_5 -> setFont(QFont("黑体", 13));
    button_6 -> setFont(QFont("黑体", 13));
    button_7 -> setFont(QFont("黑体", 13));
    button_8 -> setFont(QFont("黑体", 13));
    button_9 -> setFont(QFont("黑体", 13));
    button_dot -> setFont(QFont("黑体", 13));
    button_add -> setFont(QFont("黑体", 13));
    button_sub -> setFont(QFont("黑体", 13));
    button_mul -> setFont(QFont("黑体", 13));
    button_div -> setFont(QFont("黑体", 13));
    button_pow -> setFont(QFont("黑体", 13));
    button_Lbrac -> setFont(QFont("黑体", 13));
    button_Rbrac -> setFont(QFont("黑体", 13));
    button_BIN -> setFont(QFont("黑体", 12));
    button_OCT -> setFont(QFont("黑体", 12));
    button_HEX -> setFont(QFont("黑体", 12));
    button_equal -> setFont(QFont("黑体", 13));
    button_BACK -> setFont(QFont("黑体", 13));
    button_C -> setFont(QFont("黑体", 12));

    data -> setFixedHeight(35);
    expression -> setFixedHeight(35);
    history -> setFixedHeight(420);
    button_0 -> setMinimumHeight(50);
    button_1 -> setMinimumHeight(50);
    button_2 -> setMinimumHeight(50);
    button_3 -> setMinimumHeight(50);
    button_4 -> setMinimumHeight(50);
    button_5 -> setMinimumHeight(50);
    button_6 -> setMinimumHeight(50);
    button_7 -> setMinimumHeight(50);
    button_8 -> setMinimumHeight(50);
    button_9 -> setMinimumHeight(50);
    button_dot -> setMinimumHeight(50);
    button_add -> setMinimumHeight(50);
    button_sub -> setMinimumHeight(50);
    button_mul -> setMinimumHeight(50);
    button_div -> setMinimumHeight(50);
    button_pow -> setMinimumHeight(50);
    button_Lbrac -> setMinimumHeight(50);
    button_Rbrac -> setMinimumHeight(50);
    button_BIN -> setMinimumHeight(50);
    button_OCT -> setMinimumHeight(50);
    button_HEX -> setMinimumHeight(50);
    button_equal -> setMinimumHeight(50);
    button_BACK -> setMinimumHeight(50);
    button_C -> setMinimumHeight(50);

    My_connect();

    connect(button_C, SIGNAL(clicked(bool)), this, SLOT(button_C_clicked()));

    QGridLayout *layout = new QGridLayout;

    layout -> addWidget(data, 1, 1, 1, 4, Qt::Alignment());
    layout -> addWidget(expression, 2, 1, 2, 4, Qt::Alignment());

    layout -> addWidget(button_BIN, 4, 1, Qt::Alignment());
    layout -> addWidget(button_OCT, 4, 2, Qt::Alignment());
    layout -> addWidget(button_HEX, 4, 3, Qt::Alignment());
    layout -> addWidget(button_C, 4, 4, Qt::Alignment());

    layout -> addWidget(button_Lbrac, 5, 1, Qt::Alignment());
    layout -> addWidget(button_Rbrac, 5, 2, Qt::Alignment());
    layout -> addWidget(button_BACK, 5, 3, Qt::Alignment());
    layout -> addWidget(button_pow, 5, 4, Qt::Alignment());

    layout -> addWidget(button_7, 6, 1, Qt::Alignment());
    layout -> addWidget(button_8, 6, 2, Qt::Alignment());
    layout -> addWidget(button_9, 6, 3, Qt::Alignment());
    layout -> addWidget(button_div, 6, 4, Qt::Alignment());

    layout -> addWidget(button_4, 7, 1, Qt::Alignment());
    layout -> addWidget(button_5, 7, 2, Qt::Alignment());
    layout -> addWidget(button_6, 7, 3, Qt::Alignment());
    layout -> addWidget(button_mul, 7, 4, Qt::Alignment());

    layout -> addWidget(button_1, 8, 1, Qt::Alignment());
    layout -> addWidget(button_2, 8, 2, Qt::Alignment());
    layout -> addWidget(button_3, 8, 3, Qt::Alignment());
    layout -> addWidget(button_sub, 8, 4, Qt::Alignment());

    layout -> addWidget(button_dot, 9, 1, Qt::Alignment());
    layout -> addWidget(button_0, 9, 2, Qt::Alignment());
    layout -> addWidget(button_equal, 9, 3, Qt::Alignment());
    layout -> addWidget(button_add, 9, 4, Qt::Alignment());

    layout -> addWidget(history, 1, 5, 9, 5, Qt::Alignment());

    centerWindow -> setLayout(layout);
}

void MainWindow::My_connect()    //连接信号与槽函数
{
    connect(button_0, SIGNAL(clicked(bool)), this, SLOT(button_0_clicked()));
    connect(button_1, SIGNAL(clicked(bool)), this, SLOT(button_1_clicked()));
    connect(button_2, SIGNAL(clicked(bool)), this, SLOT(button_2_clicked()));
    connect(button_3, SIGNAL(clicked(bool)), this, SLOT(button_3_clicked()));
    connect(button_4, SIGNAL(clicked(bool)), this, SLOT(button_4_clicked()));
    connect(button_5, SIGNAL(clicked(bool)), this, SLOT(button_5_clicked()));
    connect(button_6, SIGNAL(clicked(bool)), this, SLOT(button_6_clicked()));
    connect(button_7, SIGNAL(clicked(bool)), this, SLOT(button_7_clicked()));
    connect(button_8, SIGNAL(clicked(bool)), this, SLOT(button_8_clicked()));
    connect(button_9, SIGNAL(clicked(bool)), this, SLOT(button_9_clicked()));
    connect(button_dot, SIGNAL(clicked(bool)), this, SLOT(button_dot_clicked()));
    connect(button_add, SIGNAL(clicked(bool)), this, SLOT(button_add_clicked()));
    connect(button_sub, SIGNAL(clicked(bool)), this, SLOT(button_sub_clicked()));
    connect(button_mul, SIGNAL(clicked(bool)), this, SLOT(button_mul_clicked()));
    connect(button_div, SIGNAL(clicked(bool)), this, SLOT(button_div_clicked()));
    connect(button_pow, SIGNAL(clicked(bool)), this, SLOT(button_pow_clicked()));
    connect(button_BIN, SIGNAL(clicked(bool)), this, SLOT(button_BIN_clicked()));
    connect(button_OCT, SIGNAL(clicked(bool)), this, SLOT(button_OCT_clicked()));
    connect(button_HEX, SIGNAL(clicked(bool)), this, SLOT(button_HEX_clicked()));
    connect(button_Lbrac, SIGNAL(clicked(bool)), this, SLOT(button_Lbrac_clicked()));
    connect(button_Rbrac, SIGNAL(clicked(bool)), this, SLOT(button_Rbrac_clicked()));
    connect(button_equal, SIGNAL(clicked(bool)), this, SLOT(button_equal_clicked()));
    connect(button_BACK, SIGNAL(clicked(bool)), this, SLOT(button_BACK_clicked()));
}

void MainWindow::My_disconnect()    //取消信号与槽函数的连接
{
    disconnect(button_0, SIGNAL(clicked(bool)), this, SLOT(button_0_clicked()));
    disconnect(button_1, SIGNAL(clicked(bool)), this, SLOT(button_1_clicked()));
    disconnect(button_2, SIGNAL(clicked(bool)), this, SLOT(button_2_clicked()));
    disconnect(button_3, SIGNAL(clicked(bool)), this, SLOT(button_3_clicked()));
    disconnect(button_4, SIGNAL(clicked(bool)), this, SLOT(button_4_clicked()));
    disconnect(button_5, SIGNAL(clicked(bool)), this, SLOT(button_5_clicked()));
    disconnect(button_6, SIGNAL(clicked(bool)), this, SLOT(button_6_clicked()));
    disconnect(button_7, SIGNAL(clicked(bool)), this, SLOT(button_7_clicked()));
    disconnect(button_8, SIGNAL(clicked(bool)), this, SLOT(button_8_clicked()));
    disconnect(button_9, SIGNAL(clicked(bool)), this, SLOT(button_9_clicked()));
    disconnect(button_dot, SIGNAL(clicked(bool)), this, SLOT(button_dot_clicked()));
    disconnect(button_add, SIGNAL(clicked(bool)), this, SLOT(button_add_clicked()));
    disconnect(button_sub, SIGNAL(clicked(bool)), this, SLOT(button_sub_clicked()));
    disconnect(button_mul, SIGNAL(clicked(bool)), this, SLOT(button_mul_clicked()));
    disconnect(button_div, SIGNAL(clicked(bool)), this, SLOT(button_div_clicked()));
    disconnect(button_pow, SIGNAL(clicked(bool)), this, SLOT(button_pow_clicked()));
    disconnect(button_BIN, SIGNAL(clicked(bool)), this, SLOT(button_BIN_clicked()));
    disconnect(button_OCT, SIGNAL(clicked(bool)), this, SLOT(button_OCT_clicked()));
    disconnect(button_HEX, SIGNAL(clicked(bool)), this, SLOT(button_HEX_clicked()));
    disconnect(button_Lbrac, SIGNAL(clicked(bool)), this, SLOT(button_Lbrac_clicked()));
    disconnect(button_Rbrac, SIGNAL(clicked(bool)), this, SLOT(button_Rbrac_clicked()));
    disconnect(button_equal, SIGNAL(clicked(bool)), this, SLOT(button_equal_clicked()));
    disconnect(button_BACK, SIGNAL(clicked(bool)), this, SLOT(button_BACK_clicked()));
}

void MainWindow::init()    //初始化计算器
{
    op.clear();
    num.clear();
    dot_cnt = 0;
    n = 0;
    brac_cnt = 0;
    s = "";
    finish_flag = 0;
}

bool MainWindow::is_number(QString s, int i)    //判断字符是否为数字或小数点
{
    if((s[i]>='0' && s[i]<='9') || s[i]=='.') return 1;
    return 0;
}

bool MainWindow::is_new(QString s, int i)    //判断数字是否为新的数字
{
    if(i==0 || (!is_number(s, i-1))) return 1;
    return 0;
}

double MainWindow::calc(double x, double y, char op)    //双目运算
{
    switch(op)
    {
        case '+' : return (x+y);
        case '-' : return (x-y);
        case '*' : return (x*y);
        case '/' : return (x/y);
        case '^' : return qPow(x, y);
    }
    return 0;
}

bool MainWindow::is_prior(char newop, char op)    //比较newop的运算符优先级是否高于op
{
    if(newop=='^' && (op=='+' || op=='-' || op=='*' || op=='/')) return 1;
    if((newop=='*' || newop=='/') && (op=='+' || op=='-')) return 1;
    if(op=='@' || op=='(') return 1;
    return 0;
}

QString MainWindow::check(QString s)    //对输入时算术表达式的合法性进行检查
{
    int l = s.length();

    if(l==1)
    {
        if(!is_num(s[0].toLatin1()) && s[0].toLatin1()!='(') return "";
        if(s[0].toLatin1()=='(') brac_cnt++;
        return s;
    }

    else if(is_num(s[l-1].toLatin1()))
    {
        if(s[l-1-1].toLatin1()==')') return s.mid(0, l-1);
    }

    else if(is_op(s[l-1].toLatin1()))
    {
        if(s[l-1].toLatin1()=='-' && s[l-1-1].toLatin1()=='(') return s;
        if(is_op(s[l-1-1].toLatin1()) || s[l-1-1].toLatin1()=='(' || s[l-1-1].toLatin1()=='.') return s.mid(0, l-1);
    }

    else if(s[l-1].toLatin1()=='(')
    {
        if(is_num(s[l-1-1].toLatin1()) || s[l-1-1].toLatin1()==')' || s[l-1-1].toLatin1()=='.') return s.mid(0, l-1);
        brac_cnt++;
    }

    else if(s[l-1].toLatin1()==')')
    {
        if(is_op(s[l-1-1].toLatin1()) || s[l-1-1].toLatin1()=='(' || s[l-1-1].toLatin1()=='.') return s.mid(0, l-1);
        if(brac_cnt==0) return s.mid(0, l-1);
        brac_cnt--;
    }

    else if(s[l-1].toLatin1()=='.')
    {
        if(is_op(s[l-1-1].toLatin1()) || s[l-1-1].toLatin1()=='(' || s[l-1-1].toLatin1()==')') return s.mid(0, l-1);

        for(int i=l-1-1; i>=0; i--)
        {
            if(s[i].toLatin1()=='.') return s.mid(0, l-1);
            if(is_op(s[i].toLatin1())) break;
        }
    }

    return s;
}

QString MainWindow::equal_check(QString s)    //对点击等号时算术表达式的合法性进行检查
{
    int l = s.length();

    if(s=="")
    {
        return "0";
    }

    while(!is_num(s[l-1].toLatin1()) && s[l-1]!=')')
    {
        if(s[l-1]=='(')
        {
            brac_cnt--;
        }
        l--;
    }
    s = s.mid(0, l);

    for(int i=1; i<=brac_cnt; i++)
    {
        s += ")";
    }

    return s;
}

bool MainWindow::is_num(char c)    //判断字符是否为数字
{
    if(c>='0' && c<='9') return 1;
    return 0;
}

bool MainWindow::is_op(char c)    //判断字符是否为运算符
{
    if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^') return 1;
    return 0;
}

void MainWindow::calculator()    //双栈算符优先级法对算术表达式进行计算
{
    op.push('@');
    num.push(0);

    for(int i=0; i<s.length(); i++)
    {
        if(is_number(s, i))    //处理数字
        {
            if(is_new(s, i))
            {
                num.push(s[i].toLatin1()-'0');
            }

            else
            {
                if(s[i]=='.')
                {
                    dot_cnt = 1;
                    continue;
                }

                if(!dot_cnt)
                {
                    num.top() = num.top()*10+s[i].toLatin1()-'0';
                }

                else
                {
                    num.top() = num.top()+(s[i].toLatin1()-'0')/qPow(10, dot_cnt);
                    dot_cnt++;
                }
            }
        }

        else
        {
            dot_cnt = 0;

            if(s[i]=='(')    //处理左括号
            {
                op.push(s[i].toLatin1());

                if(s[i+1]=='-')
                {
                    num.push(0);
                }
            }

            else if(s[i]==')')    //处理右括号
            {
                while(op.top()!='(')
                {
                    n = num.top();
                    num.pop();
                    num.top() = calc(num.top(), n, op.top());
                    op.pop();
                }
                op.pop();
            }

            else if(s[i]=='=')    //处理等于号
            {
                while(op.top()!='@')
                {
                    n = num.top();
                    num.pop();
                    num.top() = calc(num.top(), n, op.top());
                    op.pop();
                }
            }

            else    //处理运算符
            {
                if(is_prior(s[i].toLatin1(), op.top()))
                {
                    op.push(s[i].toLatin1());
                }

                else
                {
                    while(!is_prior(s[i].toLatin1(), op.top()))
                    {
                        n = num.top();
                        num.pop();
                        num.top() = calc(num.top(), n, op.top());
                        op.pop();
                    }
                    op.push(s[i].toLatin1());
                }
            }
        }
    }
}

void MainWindow::button_0_clicked()    //按键事件
{
    s += "0";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_1_clicked()
{
    s += "1";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_2_clicked()
{
    s += "2";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_3_clicked()
{
    s += "3";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_4_clicked()
{
    s += "4";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_5_clicked()
{
    s += "5";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_6_clicked()
{
    s += "6";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_7_clicked()
{
    s += "7";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_8_clicked()
{
    s += "8";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_9_clicked()
{
    s += "9";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_dot_clicked()
{
    s += ".";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_add_clicked()
{
    s += "+";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_sub_clicked()
{
    s += "-";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_mul_clicked()
{
    s += "*";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_div_clicked()
{
    s += "/";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_pow_clicked()
{
    s += "^";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_BIN_clicked()
{
    int num_flag = 1;

    for(int i=0; i<s.length(); i++)
    {
        if(!is_number(s, i) || s[i]=='.')    //判断输入的内容是否仅包含数字
        {
            num_flag = 0;
        }
    }

    if(num_flag)    //十进制整数转换为二进制
    {
        calculator();
        s = "";
        int tmp_n = num.top();
        QString tmp_s;

        if(num.top()==0) s = "0";

        while(tmp_n>0)
        {
            tmp_s += QString::number(tmp_n%2);
            tmp_n /= 2;
        }

        int l = tmp_s.length();
        while(l!=0)
        {
            s += tmp_s[l-1];
            tmp_s = tmp_s.mid(0, l-1);
            l--;
        }

        data -> setText("("+s+")BIN");
        expression -> setText(QString::number(num.top())+"=");
        history_text = QString::number(num.top())+"=("+s+")BIN";
        history -> append(history_text);
        My_disconnect();
        finish_flag = 1;
    }
}

void MainWindow::button_OCT_clicked()
{
    int num_flag = 1;

    for(int i=0; i<s.length(); i++)
    {
        if(!is_number(s, i) || s[i]=='.')
        {
            num_flag = 0;
        }
    }

    if(num_flag)
    {
        calculator();
        s = "";
        int tmp_n = num.top();
        QString tmp_s;

        if(num.top()==0) s = "0";

        while(tmp_n>0)
        {
            tmp_s += QString::number(tmp_n%8);
            tmp_n /= 8;
        }

        int l = tmp_s.length();
        while(l!=0)
        {
            s += tmp_s[l-1];
            tmp_s = tmp_s.mid(0, l-1);
            l--;
        }

        data -> setText("("+s+")OCT");
        expression -> setText(QString::number(num.top())+"=");
        history_text = QString::number(num.top())+"=("+s+")OCT";
        history -> append(history_text);
        My_disconnect();
        finish_flag = 1;
    }
}

void MainWindow::button_HEX_clicked()
{
    int num_flag = 1;

    for(int i=0; i<s.length(); i++)
    {
        if(!is_number(s, i) || s[i]=='.')
        {
            num_flag = 0;
        }
    }

    if(num_flag)
    {
        calculator();
        s = "";
        int tmp_n = num.top();
        QString tmp_s;

        if(num.top()==0) s = "0";

        while(tmp_n>0)
        {
            if(tmp_n%16<10)
            {
                tmp_s += QString::number(tmp_n%16);
            }
            if(tmp_n%16==10) tmp_s += "A";
            if(tmp_n%16==11) tmp_s += "B";
            if(tmp_n%16==12) tmp_s += "C";
            if(tmp_n%16==13) tmp_s += "D";
            if(tmp_n%16==14) tmp_s += "E";
            if(tmp_n%16==15) tmp_s += "F";
            tmp_n /= 16;
        }

        int l = tmp_s.length();
        while(l!=0)
        {
            s += tmp_s[l-1];
            tmp_s = tmp_s.mid(0, l-1);
            l--;
        }

        data -> setText("("+s+")HEX");
        expression -> setText(QString::number(num.top())+"=");
        history_text = QString::number(num.top())+"=("+s+")HEX";
        history -> append(history_text);
        My_disconnect();
        finish_flag = 1;
    }
}

void MainWindow::button_Lbrac_clicked()
{
    s += "(";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_Rbrac_clicked()
{
    s += ")";
    s = check(s);
    data -> setText(s);
}

void MainWindow::button_equal_clicked()
{
    s = equal_check(s);
    s += "=";
    calculator();
    data -> setText(QString::number(num.top()));
    expression -> setText(s);
    history_text = s+QString::number(num.top());
    history -> append(history_text);
    My_disconnect();
    finish_flag = 1;
}

void MainWindow::button_BACK_clicked()
{
    if(s.length()!=0 && s[s.length()-1]=="(")
    {
        brac_cnt--;
    }
    if(s.length()!=0 && s[s.length()-1]==")")
    {
        brac_cnt++;
    }
    s = s.mid(0, s.length()-1);
    data -> setText(s);
}

void MainWindow::button_C_clicked()
{
    if(finish_flag==1)
    {
        My_connect();
    }
    init();
    data -> setText(s);
    expression -> setText(s);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值