这个三元表达式的等价if else语句是什么?
What would be the equivalent if else statement of this ternary expression?
bool wm(const char *s, const char *t)
{
return *t-'*' ? *s ? (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1) : !*t : wm(s,t+1) || *s && wm(s+1,t);
}
我在网上搜索了三进制/if else等价物,但这个似乎很奇怪,因为它在开头有一个返回。
来自cplusplus网站:(条件)?(if_true): (if_false)
if(a > b){
largest = a;
} else if(b > a){
largest = b;
} else /* a == b */{
std::cout << "Uh oh, they're the same!n";
}
谢谢
实际上是两个三元语句。
if (*t-'*' ) {
if (*s) {
return (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1);
} else {
return !*t;
}
} else {
return wm(s,t+1) || *s && wm(s+1,t);
}
开头的return只返回整个语句的结果。
在你的例子中,你可以这样写:
bool wm(const char *s, const char *t)
{
if(*t-'*')
{
if (*s)
return (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1);
else
return !*t;
}
else
return wm(s,t+1) || *s && wm(s+1,t);
}
return
不是三元表达式的一部分。你可以这样想:
return (
*t-'*' ? *s ? (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1) : !*t : wm(s,t+1) || *s && wm(s+1,t)
);
要将其复制为if
语句,您需要在单独的分支中放置return
语句:
if (*t-'*') {
if (*s) {
return (*t=='?') | (toupper(*s)==toupper(*t)) && wm(s+1,t+1);
} else {
return !*t;
}
} else {
return wm(s,t+1) || *s && wm(s+1,t);
}
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- 使用if-else将数字转换为单词
- 为什么这个案例陈述需要一个"if else"而不仅仅是一个"if"?
- C++ If/Else 语句被跳过
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 如何将 if else 语句重写为 switch 语句
- 在 C++ 中的 if-else if- else 语句期间更改变量
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 具有嵌套 if-else 的循环的时间复杂度
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 简化对两个布尔值的 4 个 if/else 检查
- 如何在C++中创建 if else 循环而不是多个嵌套?
- 为什么切换 if else 语句的顺序会导致错误?
- 如何使用 if else 语句
- if/else 语句输出由于加号或减号而未显示正确的消息
- 如何解释 #if/#else 位域?(VC++菜鸟)
- if-else 语句仅按特定顺序工作,我不知道为什么