简体   繁体   English

如何使Ada中的程序包成为通用程序包?

[英]How to make a package in Ada a generic package?

I have an Ada program to calculate the average and standard deviation of 200 values taken from a file, and they are both working correctly. 我有一个Ada程序可以计算从文件中获取的200个值的平均值和标准偏差,它们都可以正常工作。 these packages are in float type, How to turn them into generic type? 这些包是浮点型的,如何将它们变成泛型?

The average package ads file is: 打包广告的平均文件为:

with Text_IO;
package avg is
  type int_array is Array (1..200) of integer;
  function avrage (ParArray: int_array) return float;
end avg;

and the average package body is: 平均包装体为:

with Text_IO;   
WITH Ada.Integer_Text_IO; USE Ada.Integer_Text_IO;
package body avg is
 function avrage (ParArray: int_array) return float is
  result: float :=0.0;
  final:float :=0.0;
  myarray: int_array:=ParArray;
 begin
  for v in myarray'Range loop
     result:= result + float(myarray(v));
  end loop;
  final:=result/200.0;
  return final;
 end avrage;
end avg;

I call this package in my main program by "with" and "use". 我在主程序中通过“ with”和“ use”来调用此程序包。 please tell me what to do 请告诉我应该怎么做

You don't say what you want your package to be generic in . 你不是说你想你的包是通用的。

I'm assuming that you want the input to be an array ( Input_Values below) of some type Input_Value indexed by Input_Index , and you want the output to be of some floating-point type Result_Value . 我假设你想要的输入是一个数组( Input_Values某种类型的下面) Input_Value由索引Input_Index ,并且希望的输出是一些浮点类型的Result_Value You'll need a function To_Result_Value to convert Input_Value to Result_Value . 您需要一个函数To_Result_ValueInput_Value转换为Result_Value

generic
   type Input_Value is private;
   type Input_Index is (<>);
   type Input_Values is array (Input_Index range <>) of Input_Value;
   type Result_Value is digits <>;
   with function To_Result_Value (X : Input_Value) return Result_Value;
package Statistics is
   function Mean (Input : Input_Values) return Result_Value;
end Statistics;

... with implementation: ...与实施:

package body Statistics is
   function Mean (Input : Input_Values) return Result_Value is
      Sum : Result_Value := 0.0;
   begin
      for I of Input loop
         Sum := Sum + To_Result_Value (I);
      end loop;
      return Sum / Result_Value (Input’Length);
   end Mean;
end Statistics;

... and a little demo: ...和一些演示:

with Ada.Text_IO;
with Statistics;
procedure Demo is
   type Arr is array (Integer range <>) of Integer;
   function To_Float (X : Integer) return Float is
   begin
      return Float (X);
   end To_Float;
   package Avg is new Statistics (Input_Value => Integer,
                                  Input_Index => Integer,
                                  Input_Values => Arr,
                                  Result_Value => Float,
                                  To_Result_Value => To_Float);
   A : Arr := (1, 2, 3, 4, 5);
   M : Float;
begin
   M := Avg.Mean (A);
   Ada.Text_IO.Put_Line ("mean is " & Float'Image (M));
end Demo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM