質問

I have problems with my first Qt Program (should be a simple calculator). I would like to calculate everytime when there is already an operator in my calculator display (lineEditDisplay) and i press an operator again. My Problem is that he recognize a '+' but not a '-' or '*' and i don't know why.

For example if i press 4, then -, then 2, the display shows me 4-2, but now if i press the posted + button i get displayed 6+ insead of 2+. So it seams that it goes in the first if statement and calculate firstNumber+secondNumber.

Here is the clicked slot for the addition button:

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

I tested it also with

if(inLineEditDisplay[i]==QLatin1Char('+'))

but that didn't change anything (can't see any different behaviour)

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

calculator.cpp -> http://pastebin.com/1bsUgg3Y

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

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

calculator.ui -> http://pastebin.com/nTEauYAH

役に立ちましたか?

解決 2

Well, it was a stupid one...

QString.length() returns the number of elements and i loop over i=0; i<QString.length; i++; till i i found an operator.

Now the left side of the operator is QString[0;i[ or QString.left(i) and the right side (where the error was) QString]i; QString.length()] or QString.right(QString.length()-(i+1)) and not QString.length().right.length()-i

他のヒント

The function Calculator::on_pushButtonAddition_clicked() is only be called if the + button is triggered. Are you sure you linked this function to all the other operator buttons?

There should be functions like (I dont know whether they are called exactly like that)

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top