If statement Ada过程中减少语句的缩短方法?
我正在创建一个使用if语句执行决策的过程 我有四个变量:If statement Ada过程中减少语句的缩短方法?,if-statement,ada,If Statement,Ada,我正在创建一个使用if语句执行决策的过程 我有四个变量:高度,速度,角度和温度 程序如下: procedure Test3 is begin if (Integer(Status_System.Altitude_Measured)) >= Critical_Altitude and (Integer(Status_System.Velocity_Measured)) >= Critical_Velocity and (Integer(Status_
高度
,速度
,角度
和温度
程序如下:
procedure Test3 is
begin
if (Integer(Status_System.Altitude_Measured)) >= Critical_Altitude
and (Integer(Status_System.Velocity_Measured)) >= Critical_Velocity
and (Integer(Status_System.Angle_Measured)) >= Critical_Angle
and (Integer(Status_System.Temperature_Measured)) >= Critical_Temperature
then
DT_Put ("message 1");
else
null;
end if;
end Test3;
此过程基本上采用了这样的思想:如果每个变量都满足所有变量的临界值,那么它将打印一条消息
我希望能够用一种较短的方式对语句进行裁剪,以便我可以执行以下操作:
如果我有4个变量:高度,速度,角度和温度
我想有一个声明,如果这些变量中至少有3个(不管是哪三个)都超过了临界值
然后显示一条消息
甚至有可能做到这一点吗
我不愿意认为我必须为if语句编写每一个可能的组合
简言之,我需要一个if语句,其中至少有3个变量处于临界值,所以打印一条消息
这同样适用于至少两个变量。首先,您应该尝试使用海拔、速度、角度和温度的特定类型。通过使用不同的类型,您可以利用Ada提供的强类型,避免诸如混合或比较海拔和温度之类的错误。您的示例表明它们都是整数。一个可能的定义是(还有许多其他定义): 这种定义的优点是可以定义值的范围和精度(捕获者都有有限的精度) 计算错误数是一种方法。 在Ada 2012中,您可以写: 编辑
Errors : Natural := 0;
...
Errors := Errors + (if Altitude_Measured > Critical_Altitude then 1 else 0);
Errors := Errors + (if Velocity_Measured > Critical_Velocity then 1 else 0);
Errors := Errors + (if Angle_Measured > Critical_Angle then 1 else 0);
Errors := Errors + (if Temperature_Measured > Critical_Temperature then 1 else 0);
if Errors >= 2 then
...
end if;
首先,您应该尝试使用特定类型的高度、速度、角度和温度。通过使用不同的类型,您可以利用Ada提供的强类型,避免诸如混合或比较海拔和温度之类的错误。您的示例表明它们都是整数。一个可能的定义是(还有许多其他定义): 这种定义的优点是可以定义值的范围和精度(捕获者都有有限的精度) 计算错误数是一种方法。 在Ada 2012中,您可以写: 编辑
Errors : Natural := 0;
...
Errors := Errors + (if Altitude_Measured > Critical_Altitude then 1 else 0);
Errors := Errors + (if Velocity_Measured > Critical_Velocity then 1 else 0);
Errors := Errors + (if Angle_Measured > Critical_Angle then 1 else 0);
Errors := Errors + (if Temperature_Measured > Critical_Temperature then 1 else 0);
if Errors >= 2 then
...
end if;
Boolean
是一种枚举类型,其值为(False,True)
。与任何枚举类型一样,'Pos
属性可用于获取值在枚举文本列表中的位置。因此,如果B
为假,Boolean'Pos(B)
等于0,如果B
为真,则等于1
你可以这样说
True_Count := Boolean'Pos(Integer(Status_System.Altitude_Measured) >= Critical_Altitude)
+ Boolean'Pos(Integer(Status_System.Velocity_Measured) >= Critical_Velocity)
+ Boolean'Pos(Integer(Status_System.Angle_Measured) >= Critical_Angle)
+ Boolean'Pos(Integer(Status_System.Temperature_Measured)) >= Critical_Temperature);
Boolean
是一种枚举类型,其值为(False,True)
。与任何枚举类型一样,'Pos
属性可用于获取值在枚举文本列表中的位置。因此,如果B
为假,Boolean'Pos(B)
等于0,如果B
为真,则等于1
你可以这样说
True_Count := Boolean'Pos(Integer(Status_System.Altitude_Measured) >= Critical_Altitude)
+ Boolean'Pos(Integer(Status_System.Velocity_Measured) >= Critical_Velocity)
+ Boolean'Pos(Integer(Status_System.Angle_Measured) >= Critical_Angle)
+ Boolean'Pos(Integer(Status_System.Temperature_Measured)) >= Critical_Temperature);
您可以计算临界条件的数量,并根据最终计数做一些事情。如果您可以将变量和临界值安排在数组中,您甚至可以在数组上循环,而不是单独测试每个临界条件。您可以计算临界条件的数量,并根据最终计数执行某些操作。如果你能将变量和临界值安排在数组中,你甚至可以在数组上循环,而不是单独测试每个临界条件。这太难看了-(我知道
if条件\u N then Count:=Count+1;end if;
更长,但我很确定它也更容易阅读。这太难看了。)-(我知道if条件\u N then Count:=Count+1;end if;
更长,但我很确定它也更容易阅读。我会用零初始化错误
,然后使所有四个增量相等。我会用零初始化错误
,然后使所有四个增量相等。