Ada通用平均函数 [英] Ada Generic Averaging Function

查看:127
本文介绍了Ada通用平均函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,可以对记录数组中的某个数值进行平均。此值是自然增量值或枚举类型增量。我已经正确地将它们的值进行了汇总,但是我的问题是:如何将数组的长度转换为泛型类型,以便它可以将整数和增量类型数都划分?

I have a function which averages a certain numeric value from an array of records. This value is either a natural or an enumerated type delta. I have it summing up the values correctly but my question is this: how do I get the length of an array into a generic type, so that it can divide both integers and delta type numbers?

推荐答案

这有一些缺陷,但这是否更接近您想要的?

This has some flaws in it, but is this closer to what you wanted ?

NWS。

with Ada.Text_Io;

procedure Main is

   generic
      type Element_T is private;
      Zero : Element_T;
      One : Element_T;
      type Vec_T is array (Integer range <>) of Element_T;
      with function "+"(Left, Right : in Element_T) return Element_T is <>;
      with function "/"(Left, Right : in Element_T) return Element_T is <>;

   package Arrayops is
      function Sum (Vec : in Vec_T) return Element_T;
      function Count (Vec : in Vec_T) return Element_T;
      function Average (Vec : in Vec_T) return Element_T;
   end Arrayops;

   package body Arrayops is
      function Sum (Vec : in Vec_T) return Element_T is
         S : Element_T := Zero;
      begin
         for I in Vec'First .. Vec'Last loop
            S := S + Vec(I);
         end loop;
         return S;
      end Sum;

      function Count (Vec : in Vec_T) return Element_T is
         C : Element_T := Zero;
      begin
         for I in Vec'First .. Vec'Last loop
            C := C + One;
         end loop;
         return C;
      end Count;

      function Average (Vec : in Vec_T) return Element_T is
         S : constant Element_T := Sum (Vec);
         Len : constant Element_T := Count (Vec);
      begin
         return S / Len;
      end Average;
   end Arrayops;

   type Fl_Arr_T is array (Integer range <>) of Float;
   package Fl_Arr is new Arrayops (Element_T => Float,
                                   Zero => 0.0,
                                   One => 1.0,
                                   Vec_T => Fl_Arr_T);

   type Int_Arr_T is array (Integer range <>) of Integer;
   package Int_Arr is new Arrayops (Element_T => Integer,
                                    Zero => 0,
                                    One => 1,
                                    Vec_T => Int_Arr_T);


   My_Ints   : constant Int_Arr_T (1 .. 5) := (6,7,5,1,2);
   My_Floats : constant Fl_Arr_T (1 .. 7) := (6.1,7.2,5.3,1.4,2.5,8.7,9.7);

   Int_Sum   : constant Integer := Int_Arr.Sum (My_Ints);
   Int_Count : constant Integer := Int_Arr.Count (My_Ints);
   Int_Avg   : constant Integer := Int_Arr.Average (My_Ints);

   Float_Sum   : constant Float := Fl_Arr.Sum (My_Floats);
   Float_Count : constant Float := Fl_Arr.Count (My_Floats);
   Float_Avg   : constant Float := Fl_Arr.Average (My_Floats);

begin

   Ada.Text_Io.Put_Line ("Integers => Sum: " & Integer'Image (Int_Sum) & ", Count: " & Integer'Image (Int_Count) & ", Avg: " & Integer'Image (Int_Avg));
   Ada.Text_Io.Put_Line ("Floats   => Sum: " & Float'Image (Float_Sum) & ", Count: " & Float'Image (Float_Count) & ", Avg: " & Float'Image (Float_Avg));

end Main;

结果:

整数=>总和:21,计数:5,平均:4

Integers => Sum: 21, Count: 5, Avg: 4

浮点数=>总和:4.09000E + 01,计数:7.00000E + 00,平均:5.84286E + 00

Floats => Sum: 4.09000E+01, Count: 7.00000E+00, Avg: 5.84286E+00

这篇关于Ada通用平均函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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