如何在if语句中简化许多OR? [英] how to simplify many OR in if statement?

查看:68
本文介绍了如何在if语句中简化许多OR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在if语句中有很多OR,有什么方法可以简化吗?


if(val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)

// ....


谢谢

I have many OR in the if statement, any method to simplify?

if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....

thank you

推荐答案



" Alan" <人************* @ sinatown.com> schrieb im Newsbeitrag

新闻:bs ********** @ news.hgc.com.hk ...

"Alan" <al*************@sinatown.com> schrieb im Newsbeitrag
news:bs**********@news.hgc.com.hk...
我在if中有很多OR声明,任何简化方法?

if(val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....
I have many OR in the if statement, any method to simplify?

if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....




ITYM

if(val == 5 || val == 9 ... ...


如果没有系统的val值,我唯一可以想到的是

是一个开关:


开关(val)

{

案例5:

案例9:

案例34:

....

案例234:

<在这里做你的东西>

休息;

}


但是如果它需要如此大量的或者'

$我会重新考虑这种方法b $ b Robert



ITYM
if(val == 5 || val == 9......

If there is no systematic in the values of val, the only thing I can think
of is a switch:

switch(val)
{
case 5:
case 9:
case 34:
....
case 234:
<do your stuff here>
break;
}

But I''d rethink the approach if it requires such a mass of or''s

Robert


Alan写道:
我在if语句中有很多OR,有什么方法可以简化吗?

if(val == 5 | val == 9 | val == 34 | val == 111 | val == 13 1 | .......)
// ....

谢谢
I have many OR in the if statement, any method to simplify?

if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....

thank you




请不要''交叉发布。


我假设您希望能够做到这样的事情:


int main()

{

int var = 3; //初始化为任何...


if(in_set(var))

{

// ... < br $>
}

}


长或声明并不是一个糟糕的方式,除非你有一个真正的,非常大的数字要检查的

。这里有几个

替代品:


/ * C或C ++ * /


int in_set(int i)

{

int result = 0;


switch(i)

{

案例5:

案例9:

案例34:

案例111:

案例131:

结果= 1;

}


返回结果;

}

/ *仅限C ++ * /


命名空间

{

int values [] = {5 ,9,34,111,131 / *,... * /};

int num_values = sizeof values / sizeof * values;


std: :设置< int> value_set(values,values + num_values);


inline bool in_set(int i)

{

return value_set.find( i)!= value_set.end();

}

}



Please don''t cross-post.

I assume you want to be able to do something like this:

int main( )
{
int var = 3; // Initialize to whatever...

if( in_set( var ) )
{
// ...
}
}

The long "or" statement is not a bad way to go, unless you have a
really, really large set of numbers to check. Here is a couple of
alternatives:

/* C or C++ */

int in_set( int i )
{
int result = 0;

switch( i )
{
case 5:
case 9:
case 34:
case 111:
case 131:
result = 1;
}

return result;
}
/* C++ only */

namespace
{
int values[ ] = { 5, 9, 34, 111, 131 /* , ... */ };
int num_values = sizeof values / sizeof *values;

std::set< int > value_set( values, values + num_values );

inline bool in_set( int i )
{
return value_set.find( i ) != value_set.end( );
}
}


" Alan" ; <人************* @ sinatown.com>在留言中写道

新闻:bs ********** @ news.hgc.com.hk ...
"Alan" <al*************@sinatown.com> wrote in message
news:bs**********@news.hgc.com.hk...
我在if中有很多OR声明,任何简化方法?

if(val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....
I have many OR in the if statement, any method to simplify?

if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......)
// ....




不知道任何明显的简化,假设

问题的逻辑实际上*需要*一些常用代码才能执行那些很多

不同的'val'值。


可以*使用De Morgan *重申*定理:


if(!(val<> 5&& val<>&& val<>&&&& ; val<> 111&& val<>&

........)

//普通代码。


轻弹OR',但唉不再简单!


你可以创建一个布尔数组,并使用val作为它的索引,

数组元素表示5的是 ,9,34,111,131 ......(和''不''

否则)。


if(boolean_array [val] == true)

//常用代码。


然后一个还需要内存用于数组,并且一些初始化

it''要启动的内容。


逻辑表达式的上下文是什么?


-


干杯

-

Hewson :: Mike

这封信比往常长,因为我没时间做这件事

更短 - Blaise Pascal



Don''t know of any evident simplification, assuming the logic of the
problem actually *requires* some common code to be executed for those many
different values of ''val''.

One can *restate* using De Morgan''s theorem:

if( !(val <> 5 && val <> 9 && val <> 34 && val <> 111 && val <> 131 &&
........) )
// Common code.

which flicks the OR''s, but alas no more simply!

You could create a boolean array, and use val as an index for it, with
the array elements indicating a ''yes'' for 5,9,34,111,131...... ( and ''no''
otherwise ).

if( boolean_array[val] == true )
// Common code.

but then one also needs memory for the array, and some initialization of
it''s contents to boot.

What is the context of your logical expression?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal


这篇关于如何在if语句中简化许多OR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆