1

我的第一个 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

计算器.cpp -> http://pastebin.com/1bsUgg3Y

计算器.h -> http://pastebin.com/F0kbkx4g

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

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

4

2 回答 2

0

该函数Calculator::on_pushButtonAddition_clicked()仅在+按钮被触发时被调用。您确定将此功能链接到所有其他操作员按钮吗?

应该有类似的功能(我不知道它们是否完全像那样调用)

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

于 2013-05-27T21:28:39.420 回答
0

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

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

现在运算符的左侧是QString[0;i[orQString.left(i)右侧(错误所在的位置)QString]i; QString.length()]or QString.right(QString.length()-(i+1))and notQString.length().right.length()-i

于 2013-06-02T22:13:19.590 回答