减少Ada程序中语句的缩短方法? [英] Shortened method to reduce statements in an Ada procedure?

查看:122
本文介绍了减少Ada程序中语句的缩短方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用if语句执行决策的过程。

I am creating a procedure that uses an if statement to perform a decision.

我有四个变量: Altitude Velocity Angle 温度

I have four variables: Altitude, Velocity, Angle and Temperature.

程序如下:

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;

这个程序很小便,认为如果每个变量满足变量的所有临界值,每个变量然后它将打印一条消息。

This procedure is bassicaly taking the idea that if all the critcal values for the variables are met for each and every variable then it will print a message.

我希望能够有一个更短的方式来削减语句,所以我可以做到以下几点:

I want to be able to have a shorter way of paring up the statements so I can do the following:

如果我有4个变量:海拔高度,速度,角度和温度
我希望有一个声明,如果这些变量中至少有3个(无关紧要哪三个)都超过了他们的临界值
然后显示一条消息。

If I have 4 variables: Altitude, velocity, angle and temperature and I want to have a statement that says, If atleast 3 of these varibles (doesnt matter which three) are all exceeding their critical values then display a message.

甚至可以这样做吗?

我不愿意认为我必须为if语句编写每个可能的组合。

I would hate to think that I would have to write each and every possible combination for the if statements.

简而言之,我想要一个if语句,表示至少有3个变量显示在它们的标题值,所以打印一条消息。

In short, I want an if statement that says at least 3 of the variables shown are at their criticle value so print a message.

同样适用于这些变量中的至少2个。

The same would be good for atleast 2 of these variables as well.

推荐答案

首先,您应该尝试使用特定类型的高度,速度,角度和温度。通过使用不同的类型,您将利用Ada提供的强类型,避免混淆或比较高度和温度等错误。您的示例表明它们都是Integer。一个可能的定义可能是(还有许多其他):

First, you should try to use specific types for altitude, velocity, angle and temperature. By using different types you'll leverage strong typing provided by Ada and avoid mistakes such as mixing or comparing altitudes and temperatures. Your example suggests that all of them are Integer. One possible definition could be (there are many others):

type Temperature is digits 5 range -273.15 .. 300.0;
type Angle is digits 5 range -180.0 .. 180.0;

这种定义的优点是你可以定义值的范围和精度(绑定者都有有限精度)。

The advantage of such definition is that you define both the range of values and the precision (captors all have a finite precision).

计算错误数是一种方法。
在Ada 2012中,您可以写道:

Counting the number of errors is one way do that. In Ada 2012, you could write:

编辑

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程序中语句的缩短方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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