很明显,我在使用有效的表达式运算符时遇到了一些麻烦。从理论上讲,我理解首先检查范围开放器的概念,如果你看到一个没有范围开启器的范围更近,那么它就是无效的。我不太确定它的图表是我绘制的还是什么但是仍然像我说的那样,在编码时使用算法时,我只是有点困惑,任何提示?另外,我的教授告诉我在这里使用开关?究竟怎么会让我受益?我不需要为示波器开关和闭门器提供3种不同的外壳开关,而实际上我需要的是if语句吗?
//=============use case switch here
void check_expression(char expression[80], bool& valid)
{
Stack symbStack; //stack to hold scope openers
char symbol, //current symbol in the expression being investigated
top_symb; //current scope opener at the top of the stack
int i = 0; //subscript to element in expression array, initialized to
//first element
valid = true;
symb = expression[i];
while(symb != '\0') //while symbol in expression is not the null terminator
{
if( symbol == '{' || symbol == '[' || symbol == '(' )
{
symbStack.Push(symbol);
}
else if( symbol == '}' || symbol == ']' || symbol == ')' )
{
if(symbStack.IsEmpty())
{
cout << "Expression is invalid!";
valid == false;
}
else
{
top_symb = symbStack.StackTop();
symbStack.Pop();
if( (top_symb == '(' && symbol != ')') ||
(top_symb == '[' && symbol != ']') ||
(top_symb == '{' && symbol != '}') )
{
valid = false;
}
}
}
i++; //incrememt the subscript to the next character in the expression.
symb = expression[i]; //assign symb to the next character in expression.
}
//Check to see if the stack is not empty. If it is not empty, then
//the expression is invalid, in which case you want to assign valid to false
return;
}
答案 0 :(得分:0)
valid == false;
^^^^
你的罪魁祸首。摆脱其中一个=
,你应该做得很好。
作为旁注,如果您发现自己正在查看无效的表达,您可以立即返回!在此之后,表达式无法变为有效。因此,您可以在将return;
设置为valid
后添加false
。