Qt实现计算器界面和基本运算

用Qt来实现一个简单的计算器。

Qt实现计算器界面和基本运算

 

 按钮设置

//按键
private slots:
    void on_oneButton_clicked();

    void on_twoButton_clicked();

    void on_threeButton_clicked();

    void on_fireButton_clicked();

    void on_fourButton_clicked();

    void on_sixButton_clicked();

    void on_sevenButton_clicked();

    void on_eightButton_clicked();

    void on_nineButton_clicked();

    void on_zeroButton_clicked();

    void on_addButton_clicked();

    void on_subButton_clicked();

    void on_mulButton_clicked();

    void on_divButton_clicked();

    void on_leftButton_clicked();

    void on_rightButton_clicked();

    void on_clearButton_clicked();

    void on_deleteButton_clicked();

    void on_equalButton_clicked();

 

//运算
void Widget::on_equalButton_clicked()
{
    QStack<int>  s_num, s_opt;
    char opt[128]={0};
    int i=0,tmp=0,num1,num2;

    //把QString 转换成char*
        QByteArray ba;
        ba = expression.toLatin1();
        strcpy(opt,ba.data());

    while(opt[i] != '\0' || s_opt.empty() != true)
    {
        if(opt[i] >= '0' && opt[i] <= '9')
        {
            tmp = tmp *10 + opt[i] - '0';
            i++;
            if(opt[i] < '0' || opt[i] > '9')
            {
                s_num.push(tmp);
                tmp = 0;
            }
        }
        else//操作符
        {
            if(s_opt.empty() == true||Priority(opt[i]) >=Priority(s_opt.top()) || (s_opt.top() == '(' && opt[i] != ')'))
            {
                s_opt.push(opt[i]);
                i++;
                continue;
            }
            if(s_opt.top() == '(' && opt[i] == ')')
            {
                s_opt.pop();
                i++;
                continue;
            }
            if(Priority(opt[i]) <= Priority(s_opt.top())||(opt[i] == ')'&& s_opt.top() !='(')|| (opt[i]=='\0'&& s_opt.empty()!=true))
            {
                char ch = s_opt.top();
                s_opt.pop();
                switch(ch)
                {
                case '+':
                    num1=s_num.top();
                    s_num.pop();
                    num2=s_num.top();
                    s_num.pop();
                    s_num.push(num1+num2);
                    break;
                case '-':
                    num1=s_num.top();
                    s_num.pop();
                    num2=s_num.top();
                    s_num.pop();
                    s_num.push(num2-num1);
                    break;
                case '*':
                    num1=s_num.top();
                    s_num.pop();
                    num2=s_num.top();
                    s_num.pop();
                    s_num.push(num1*num2);
                    break;
                case '/':
                    num1=s_num.top();
                    s_num.pop();
                    num2=s_num.top();
                    s_num.pop();
                    s_num.push(num2/num1);
                    break;
                }
            }
        }
    }
    ui->mainLineEdit->setText(QString::number(s_num.top()));
    expression.clear();
}

int Widget::Priority(char ch)
{
    switch (ch)
    {
    case '(':
         return 3;
    case '*':
    case '/':
        return 2;
     case '+':
     case '-':
        return 1;
      default:
        return 0;

    }
}

 

上一篇:Java多线程(priority)


下一篇:C++第10课 STL容器 (七)