Qt中的第一个简单if语句:两个字符的比较
First simple if statement in Qt: Comparison of two chars
我有问题与我的第一个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
- 比较字符数组
- 比较 c++ 中的字符数组
- 将 if 语句中的字符与 or 进行比较
- 将字符串字节与字符 c++ 进行比较
- 比较"A"、"C"、"G"'T'字符的最快方法
- 如何将字符串字符与结构数组进行比较?
- 字符* 与uint8_t的字符串比较
- 如何将32位字符与内联assembelyc++中的32位字符进行比较
- 如何比较文件中包含的下一个字符
- C++相互比较字符
- 为什么我不能将字符与"*"进行比较?
- 查找和比较两个字符串中的字符
- 将字符比较在一起
- 在数组、流和字符比较中定位
- 字符比较 if 语句无法正常工作
- c++中数字字符和字母字符之间的字符比较
- 字符比较错误
- 字符串,c++中的字符比较
- C++字符串和字符比较
- 根据每个字符比较同一数组中的两个字符串值.此结果仅显示Last OUT值的原因