我遇到了这段代码,但我无法理解递归如何用于以下代码,因为在 return
语句中使用了 bool OR 运算符,这是我以前从未见过的。它应该如何工作?
bool match(char *first, char * second)
{
if (*first == '\0' && *second == '\0')
return true;
if (*first == '*' && *(first+1) != '\0' && *second == '\0')
return false;
if (*first == '?' || *first == *second)
return match(first+1, second+1);
if (*first == '*')
return match(first+1, second) || match(first, second+1); //How does it work?
return false;
}
最佳答案
它像所有其他析取一样工作:
首先评估左侧,match(first+1, second)
。
如果为 true
,则整个表达式的结果为 true
,并且不会计算任何其他内容。
如果为 false
,则计算右侧 match(first, second+1)
并成为整个表达式的结果。
关于c++ - 由于在 return 语句中使用了 bool OR 运算,因此递归如何用于以下代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38077076/