C 混淆';如果';陈述

C 混淆';如果';陈述,c,if-statement,C,If Statement,下面的陈述到底在做什么 这让我很困惑。我很惊讶C允许您在if语句中使用条件运算符。有更好的办法吗 我发现if语句中的条件运算符太容易混淆。我可以尝试使用&&和| |运算符,但我担心可能会出错 如何将下面的语句改写为更简单的形式 if ( (offset < 0) ? ( input->binData.bounds.lo >= (unsigned long)(-offset) ) : ( input->binData.bounds.hi

下面的陈述到底在做什么

这让我很困惑。我很惊讶C允许您在
if
语句中使用条件运算符。有更好的办法吗

我发现
if
语句中的条件运算符太容易混淆。我可以尝试使用&&和| |运算符,但我担心可能会出错

如何将下面的语句改写为更简单的形式

if ( (offset < 0) ?
         ( input->binData.bounds.lo >= (unsigned long)(-offset) ) :
         ( input->binData.bounds.hi < (unsigned long)(-offset) ) )
if((偏移量<0)?
(输入->binData.bounds.lo>=(无符号长)(-offset)):
(输入->binData.bounds.hi<(无符号长)(-offset)))

我在Bithugh开源软件包中找到了这行C代码。

您可以使用三值运算符的每个分辨率
三值条件(
offset<0
)及其对立面(
offset>=0
):

if ( ((offset < 0) && ( input->binData.bounds.lo >= (unsigned long)(-offset) )) ||
     ((offset >= 0) && ( input->binData.bounds.hi <  (unsigned long)(-offset) )) ) {
if((偏移量<0)和&(输入->binData.bounds.lo>=(无符号长)(-offset)))||
((偏移量>=0)和&(输入->binData.bounds.hi<(无符号长)(-offset))){

您可以使用三值运算符的每个分辨率
三值条件(
偏移量<0
)及其对立面(
偏移量>=0
):

if ( ((offset < 0) && ( input->binData.bounds.lo >= (unsigned long)(-offset) )) ||
     ((offset >= 0) && ( input->binData.bounds.hi <  (unsigned long)(-offset) )) ) {
if((偏移量<0)和&(输入->binData.bounds.lo>=(无符号长)(-offset)))||
((偏移量>=0)和&(输入->binData.bounds.hi<(无符号长)(-offset))){

以下是一种不那么令人困惑的思考方式:

 overallBool = 0;

if ( (offset < 0) {
       overallBool = input->binData.bounds.lo >= (unsigned long)(-offset);
} else {
       overallBool = input->binData.bounds.hi < (unsigned long)(-offset);
}

if (overallBool) {
    do your thing.
}
overallBool=0;
如果((偏移量<0){
总体布尔=输入->binData.bounds.lo>=(无符号长)(-offset);
}否则{
总体布尔=输入->binData.bounds.hi<(无符号长)(-偏移量);
}
if(总体布局){
做你的事。
}

但很明显,它在实现上要大得多。

以下是一种不那么令人困惑的思考方式:

 overallBool = 0;

if ( (offset < 0) {
       overallBool = input->binData.bounds.lo >= (unsigned long)(-offset);
} else {
       overallBool = input->binData.bounds.hi < (unsigned long)(-offset);
}

if (overallBool) {
    do your thing.
}
overallBool=0;
如果((偏移量<0){
总体布尔=输入->binData.bounds.lo>=(无符号长)(-offset);
}否则{
总体布尔=输入->binData.bounds.hi<(无符号长)(-偏移量);
}
if(总体布局){
做你的事。
}

但是很明显,它在实现上要大得多。

假设
input->binData.bounds.lo binData.bounds.lo<
输入->binData.bounds.hi-输入->binData.bounds.lo{…}

(参见4-1检查整数边界以获得正确性证明。)

假设
输入->binData.Bounds.lo binData.Bounds.lo<
输入->binData.bounds.hi-输入->binData.bounds.lo{…}

(有关正确性的证明,请参见4-1检查整数的边界。)

我完全同意你的观点,这段代码不必要的迟钝。虽然听起来你已经有了答案。我不知道足够的C语言来帮助解决这个问题,但我觉得整个条件应该被提取到它自己的函数和“父”中conditional应该只检查该函数的输出。我完全同意你的观点,这段代码不必要的迟钝。虽然听起来你已经有了答案。我不知道足够的C来帮助清理这个问题,但我觉得整个conditional应该被提取到它自己的函数和“父函数”中conditional应该只检查该函数的输出。您可能还希望为(unsigned long)(-offset)和input->binData.bounds创建一个临时for,以减少重复。这看起来与原始代码最接近。因此更安全。(unsigned long)(-offset)是奇怪的。我对代码的理解不够好,无法进行大的更改。您可能还需要临时设置for(unsigned long)(-offset)和input->binData.bounds以减少重复。这看起来与原始代码最接近。因此更安全。(unsigned long)(-offset)是奇怪的。我对代码的理解不够好,无法进行大的更改。