Qt中的第一个简单if语句:两个字符的比较

时间:2013-05-27 21:16:18

标签: c++ qt qt-signals

我的第一个Qt程序有问题(应该是一个简单的计算器)。每当我的计算器显示器(lineEditDisplay)中已经有一个操作员并且我再次按下操作员时,我想计算。 我的问题是他认出'+'而不是' - '或'*',我不知道为什么。

例如,如果我按4,然后 - ,然后2,显示屏显示4-2,但是现在如果我按下发布的+按钮,我会显示6++的2+。所以它接缝在第一个if语句中并计算firstNumber + secondNumber。

以下是添加按钮的单击插槽:

void Calculator::on_pushButtonAddition_clicked()
{
    QString inLineEditDisplay=ui->lineEditDisplay->text(); //Get whats in display
    //Loop through display string
    for(int i=0; i<inLineEditDisplay.length(); i++)
    {
        //Check if there is already a operator -> if yes then calculate first so
        //we can add a new operator; first check for +
        if(inLineEditDisplay[i]=='+')
        {
            //Get the two numbers; in front of and behind the operator
            QString firstPart=inLineEditDisplay.left(i);
            QString secondPart=inLineEditDisplay.right(inLineEditDisplay.length()-i);
            //Change from QString to int, so we can calculate with
            int firstPartAsInt=firstPart.toInt();
            int secondPartAsInt=secondPart.toInt();
            inLineEditDisplay=QString::number(firstPartAsInt+secondPartAsInt);
        }
        //Now check for -
        if(inLineEditDisplay[i]=='-')
        {
            //Get the two numbers; in front of and behind the operator
            QString firstPart=inLineEditDisplay.left(i);
            QString secondPart=inLineEditDisplay.right(inLineEditDisplay.length()-i);
            //Change from QString to int, so we can calculate with
            int firstPartAsInt=firstPart.toInt();
            int secondPartAsInt=secondPart.toInt();
            inLineEditDisplay=QString::number(firstPartAsInt-secondPartAsInt);
        }
        //Now check for *
        if(inLineEditDisplay[i]=='*')
        {
            //Get the two numbers; in front of and behind the operator
            QString firstPart=inLineEditDisplay.left(i);
            QString secondPart=inLineEditDisplay.right(inLineEditDisplay.length()-i);
            //Change from QString to int, so we can calculate with
            int firstPartAsInt=firstPart.toInt();
            int secondPartAsInt=secondPart.toInt();
            inLineEditDisplay=QString::number(firstPartAsInt*secondPartAsInt);
        }
        //Now check for /
        if(inLineEditDisplay[i]=='/')
        {
            //Get the two numbers; in front of and behind the operator
            QString firstPart=inLineEditDisplay.left(i);
            QString secondPart=inLineEditDisplay.right(inLineEditDisplay.length()-i);
            //Change from QString to int, so we can calculate with
            int firstPartAsInt=firstPart.toInt();
            int secondPartAsInt=secondPart.toInt();
            inLineEditDisplay=QString::number(firstPartAsInt/secondPartAsInt);
        }
    }
    inLineEditDisplay.append('+');
    ui->lineEditDisplay->setText(inLineEditDisplay);
}

我也用

测试了它
if(inLineEditDisplay[i]==QLatin1Char('+'))

但是没有改变任何东西(看不到任何不同的行为)

@ https://stackoverflow.com/users/2422324/user2422324

calculator.cpp - &gt; http://pastebin.com/1bsUgg3Y

calculator.h - &gt; http://pastebin.com/F0kbkx4g

main.cpp - &gt; http://pastebin.com/keCu6Gcr

calculator.ui - &gt; http://pastebin.com/nTEauYAH

2 个答案:

答案 0 :(得分:0)

仅在触发Calculator::on_pushButtonAddition_clicked()按钮时才会调用函数+。您确定将此功能与所有其他操作员按钮相关联吗?

应该有之类的功能(我不知道它们是否完全被称为)

Calculator::on_pushButtonSubtraction_clicked() Calculator::on_pushButtonDivision_clicked() Calculator::on_pushButtonMultiplication_clicked()

答案 1 :(得分:0)

嗯,这是一个愚蠢的......

QString.length()返回元素数量,i循环遍历i=0; i<QString.length; i++;,直到我找到一个运算符。

现在,运营商的左侧是QString[0;i[QString.left(i),右侧(错误的位置)QString]i; QString.length()]QString.right(QString.length()-(i+1))而非QString.length().right.length()-i