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

First simple if statement in Qt: Comparison of two chars

本文关键字:字符 比较 两个 第一个 简单 if 语句 Qt      更新时间:2023-10-16

我有问题与我的第一个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 -> http://pastebin.com/1bsUgg3Y

calculator.h -> http://pastebin.com/F0kbkx4g

main.cpp -> http://pastebin.com/keCu6Gcr

计算器。ui -> http://pastebin.com/nTEauYAH

函数Calculator::on_pushButtonAddition_clicked()只有在触发+按钮时才会被调用。您确定已将此功能与所有其他操作员按钮连接起来了吗?

应该有像这样的函数(我不知道它们是否被这样调用)

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

好吧,这是一个愚蠢的…

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