【发布时间】:2017-10-06 20:03:23
【问题描述】:
我正在尝试创建一个查看'-' 符号的函数,并根据它前面是否有' '(空格)来检查是减号还是负号。我这样做是通过比较我拥有的当前字符 (infix[x]) 并与infix[x+1] 进行比较;我不断收到错误,但我不确定是因为我没有正确通过还是其他原因?
for(unsigned x = 0; x < infix.length(); ++x)
{
// place numbers (standard, decimal, & negative)
// numbers onto the 'postfix' string
if((isdigit(infix[x])) || (infix[x] == '.'))
{
postfix += infix[x];
}
else if ((infix[x] == '-'))
{
if(checkfornegative(infix[x], infix)== 1)) // error: expected primary-expression before ‘)’ token
if(checkfornegative(infix[x], infix)== 1))
{
postfix+= " ";
}
else if(checkfornegative(infix[x], infix)== 0)) //error: expected primary-expression before ‘)’ token
if(checkfornegative(infix[x], infix)== 1))
{
postfix += infix[x];
}
}
// This is the function in using
bool checkfornegative(char C, string& QQ)
{
bool status;
if((C == '-') && (QQ[C+1] == ' '))
{
status = true;
}
else if((C == '-') && (QQ[C+1] != ' '))
{
status = false;
}
return status;
}
【问题讨论】:
-
这里有一些关于一个最小的、完整的例子的上下文将有很大帮助。这些函数是如何调用的?您提供什么数据?
-
你的括号不平衡,这不可能是你正在运行的代码。
-
QQ[C+1]没有意义。C是一个字符,而不是字符串中的索引。
标签: c++ string function parsing