Generics 如何编译这个通用包

Generics 如何编译这个通用包,generics,package,ada,Generics,Package,Ada,我让程序读取通用包的位置,但我不知道如何处理上下文子句以获得要打印的数字和/或从文件中读取的数字。在现在的客户端程序中,我知道元素类型应该是整数,但是我所有的声明都是元素类型,当没有Ada.Element\u text\u IO时,我该如何处理它; 请让我知道,我已经被困了很长时间了 客户端程序 WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; WITH Ada.Float_Text_IO; WITH Min_Max; PROCEDURE Min_M

我让程序读取通用包的位置,但我不知道如何处理上下文子句以获得要打印的数字和/或从文件中读取的数字。在现在的客户端程序中,我知道元素类型应该是整数,但是我所有的声明都是元素类型,当没有Ada.Element\u text\u IO时,我该如何处理它; 请让我知道,我已经被困了很长时间了

客户端程序

WITH Ada.Text_IO; 
WITH Ada.Integer_Text_IO; 
WITH Ada.Float_Text_IO; 
WITH Min_Max;

PROCEDURE Min_Max_Average_File IS
------------------------------------------------------------------
--| Finds and displays the minimum, maximum, and average
--| of a list of data items; the data items are read from a file.                                   
------------------------------------------------------------------

package Grades is new Min_Max(Element_Type => Integer);
use Grades;



  NumValues:    Element;  -- input - the number of items to be averaged
  CurrentValue: Element;   -- input - the next data item to be added   
  Smallest:     Element;   -- output - minimum of the data values
  Largest:      Element;   -- output - maximum of the data values
  Average:      Element;   -- output - average of the data values
  Sum:          Element;   -- program variable - the sum being accumulated   
  TestScores:   Ada.Text_IO.File_Type; 

BEGIN  -- Min_Max_Average_File
  -- Open the file and associate it with the file variable name
  Ada.Text_IO.Open
    (File => TestScores, Mode => Ada.Text_IO.In_File, Name => "scores.txt");

  -- Read from the file the number of items to be averaged
           Ada.Integer_Text_IO.Get(File => TestScores, Item => NumValues);  
           Ada.Text_IO.Put("The number of scores to be averaged is ");
           Ada.Integer_Text_IO.Put(Item => NumValues, Width => 1);  
           Ada.Text_IO.New_Line;
  -- Initialize program variables
  Smallest := Element'Last;
  Largest := Element'First;
  Sum := 0;

  -- Read each data item, log to the screen, add it to Sum, 
  -- and check if it is a new minimum or maximum
  FOR Count IN 1 .. NumValues LOOP
    Ada.Integer_Text_IO.Get(File => TestScores, Item => CurrentValue);
    Ada.Text_IO.Put("Score number ");
    Ada.Integer_Text_IO.Put(Item => Count, Width => 1);  
    Ada.Text_IO.Put(" is ");
    Ada.Text_IO.Put(Item => CurrentValue, Width => 1);  
    Ada.Text_IO.New_Line;
    Sum := Sum + CurrentValue;
    Smallest := Grades.Minimum(Value1 => Smallest, Value2 => CurrentValue);
    Largest  := Grades.Maximum(Value1 => Largest,  Value2 => CurrentValue);

  END LOOP;

  -- compute the average; since Sum and NumValues are integers,
  -- the average is truncated, that is, the fractional part is discarded
  Average := Sum / NumValues;

  -- display the results
  Ada.Text_IO.Put(Item => "The Smallest is ");
  Ada.Integer_Text_IO.Put(Item => Smallest, Width => 1);  
  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put(Item => "The Largest is ");
  Ada.Integer_Text_IO.Put(Item => Largest, Width => 1);  
  Ada.Text_IO.New_Line;
  Ada.Text_IO.Put(Item => "The Average is ");
  Ada.Integer_Text_IO.Put(Item => Average, Width => 1);  
  Ada.Text_IO.New_Line;

END Min_Max_Average_File;
包装规格

generic

  Type Element_Type is private;


PACKAGE Min_Max IS

------------------------------------------------------------------
--| specifications of functions provided by Min_Max package                                     
------------------------------------------------------------------
  Type Element is new Element_Type;
  FUNCTION Minimum (Value1, Value2: Element_Type) RETURN Element_Type;
  -- Pre:  Value1 and Value2 have been assigned values
  -- Post: Returns the smaller of the two input values

  FUNCTION Maximum (Value1, Value2: Element_Type) RETURN Element_Type;
  -- Pre:  Value1 and Value2 have been assigned values
  -- Post: Returns the larger of the two input values
包体

PACKAGE BODY Min_Max IS
------------------------------------------------------------------
--| bodies of functions provided by Min_Max package                                     
------------------------------------------------------------------

  FUNCTION Minimum (Value1, Value2: Element_Type) RETURN Element_Type IS
    Result: Element_Type;
  BEGIN

    IF Value1 < Value2 THEN
       Result := Value1;
    ELSE
       Result := Value2;
    END IF;
    RETURN Result;

  END Minimum;

  FUNCTION Maximum (Value1, Value2: Element_Type) RETURN Element_Type IS
    Result: Element_Type;
  BEGIN

    IF Value1 > Value2 THEN
       Result := Value1;
    ELSE
       Result := Value2;
    END IF;
    RETURN Result;

  END Maximum;

END Min_Max;
包体最小值\最大值为
------------------------------------------------------------------
--|Min_Max软件包提供的函数体
------------------------------------------------------------------
函数最小值(值1,值2:元素类型)返回元素类型为
结果:元素_型;
开始
如果值1小于值2,则
结果:=值1;
其他的
结果:=值2;
如果结束;
返回结果;
末端最小值;
函数最大值(值1,值2:元素类型)返回元素类型为
结果:元素_型;
开始
如果值1>值2,则
结果:=值1;
其他的
结果:=值2;
如果结束;
返回结果;
末端最大值;
结束最小值和最大值;

当我尝试编译
Min\u Max
的规范时,我收到错误消息:

min_max.ads:16:71: missing "end Min_Max;"
解决这个问题应该很简单。

当你说

package Grades is new Min_Max(Element_Type => Integer);
这意味着
等级
的功能是
Min\u Max
对于
整数

因此,您不应该声明
Element
类型的
numvalue
等,而应该声明类型
Integer
。这将解决您的读/写值问题

但是,您在编译泛型时会遇到问题。您已将
Element_Type
声明为
private
;这将限制您对这种类型的对象所能做的操作,以进行赋值和比较以获得相等性。它不支持“”

如果您愿意支持有符号整数类型,可以说()

类型元素\u类型为范围;
或者(假设您也希望能够支持浮点类型),您需要导入相关的操作():

类型元素\u类型是私有的;
函数为“”(L,R:Element_Type)时,返回布尔值为;

(如果有相同的名称,
is
意味着使用
Element\u Type
的操作)。

这就是我被赋予的全部工作,为什么还有其他东西?这实际上是我首先被赋予的,我添加了Type元素,我不应该这样做吗?我不明白。我应该把它放在哪里?我真的不知道怎么做,我的书对这个问题几乎毫无用处。当type元素是new element_type时,程序似乎导致更少的编译错误;包括在内。我不明白你在说什么,我需要这个程序做的只是将Ada.Integer_Text_IO和Ada.Float_Text_IO更改为适用于我声明的元素谢谢你,我需要它,但如果你愿意回答的话,我还有一个问题。如果我想让它同时适用于整数和浮点呢。我是否真的需要遍历并输入if-integer然后if-float,或者有更简单的方法吗
type Element_Type is range <>;
type Element_Type is private;
with function "<" (L, R : Element_Type) return Boolean is <>;
with function ">" (L, R : Element_Type) return Boolean is <>;