运用qt实现计算器页面

首先我们在界面文件中画出我们想要的 计算器的模型, 如下图所示:

运用qt实现计算器页面_第1张图片

然后,对于每一个按键进行修改名称,使之便于后续代码的书写。

运用qt实现计算器页面_第2张图片

之后我们对每一个按键转到槽,在源文件中进行对每个按键的功能进行书写:

对于除“=”以外的按键,都较容易实现他们的功能,对于“=”号按键,将在此按键中实现计算器加减乘除的所有功能。

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 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();

}

 注意头文件的书写

执行完成后的页面将如下图所示:

运用qt实现计算器页面_第3张图片

按等于键后将会出现结果。 

你可能感兴趣的:(运用qt实现计算器页面)