1. 程式人生 > >運用qt實現計算器頁面

運用qt實現計算器頁面

首先我們在介面檔案中畫出我們想要的 計算器的模型, 如下圖所示:

然後,對於每一個按鍵進行修改名稱,使之便於後續程式碼的書寫。

之後我們對每一個按鍵轉到槽,在原始檔中進行對每個按鍵的功能進行書寫:

對於除“=”以外的按鍵,都較容易實現他們的功能,對於“=”號按鍵,將在此按鍵中實現計算器加減乘除的所有功能。

void Widget::on_oneButton_clicked()
{
    expression +="1";
    ui->mainLineEdit->setText(expression);
}


void Widget::on_twoButton_clicked()
{
    expression +="2";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_threeButton_clicked()
{
    expression +="3";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_fourButton_clicked()
{
    expression +="4";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_fiveButton_clicked()
{
    expression +="5";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_sixButton_clicked()
{
    expression +="6";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_sevenButton_clicked()
{
    expression +="7";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_eightButton_clicked()
{
    expression +="8";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_nineButton_clicked()
{
    expression +="9";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_addButton_clicked()
{
    expression +="+";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_decButton_clicked()
{
    expression +="-";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_mulButton_clicked()
{
    expression +="*";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_decButton_2_clicked()
{
    expression +="/";
    ui->mainLineEdit->setText(expression);
}


void Widget::on_leftButton_clicked()
{
    expression +="(";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_rightButton_clicked()
{
    expression +=")";
    ui->mainLineEdit->setText(expression);
}

void Widget::on_clearButton_clicked()
{

    ui->mainLineEdit->clear();
    expression.clear();
}

void Widget::on_delButton_2_clicked()
{
    expression.chop(1);
    ui->mainLineEdit->setText(expression);
}

void Widget::on_zeroButton_clicked()
{
    expression +="0";
    ui->mainLineEdit->setText(expression);
}
int Widget::Priority(char ch)
{
    switch(ch)
    {
        case '(':
            return 3;
        case '*':
        case '/':
            return 2;
        case '+':
        case '-':
            return 1;
        default:
            return 0;
    }

}
void Widget::on_equButton_clicked()
{
    int i=0,tmp=0,num1,num2,result;
    QStack<int> s_opt, s_num;
    QByteArray data=expression.toLatin1();
    char *opt=data.data();
    char ch;
    while(opt[i]!='\0'||s_opt.empty()!=true)
    {
        if(opt[i]>='0'&&opt[i]<='9')
        {
            tmp=tmp*1+opt[i]-'0';
            i++;
            if(opt[i]>'9'||opt[i]<'0')
            {
                s_num.push(tmp);
                tmp=0;
            }
        }
        else
        {
            //操作符進棧
            if(s_opt.empty()||(s_opt.top()=='('&&opt[i]!=')')||Priority(opt[i])>Priority(s_opt.top()))
            {
                s_opt.push(opt[i]);
                i++;
                continue;
            }
            if(s_opt.top()=='('&&opt[i]==')')
            {
                s_opt.pop();
                i++;
                continue;
            }
            if((opt[i]==')'&&s_opt.top()!='(')||Priority(opt[i])<=Priority(s_opt.top()))
            {
                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;
                  }
            }
         }
      }
        result = s_num.top();
        ui->mainLineEdit->setText(QString::number(result));
        expression.clear();

}

 注意標頭檔案的書寫

執行完成後的頁面將如下圖所示:

按等於鍵後將會出現結果。