我有程序来读取通用包,但是我无法弄清楚如何处理上下文子句以获取要打印和/或从文件中读取的数字。在目前的客户端程序中,我认为Element_Type应该是一个整数,但我的所有声明都是类型元素,当没有Ada.Element_text_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;
答案 0 :(得分:1)
当我尝试编译Min_Max
的规范时,收到错误消息:
min_max.ads:16:71: missing "end Min_Max;"
修复这应该是非常简单的。
答案 1 :(得分:1)
当你说
时package Grades is new Min_Max(Element_Type => Integer);
这意味着Grades
的功能属于Min_Max
,代表Integers
。
因此,您不应声明类型为NumValues
的{{1}}等,而应声明Element
类型。这将解决您能够读/写值的问题。
但是,编译泛型时会遇到问题。您已将Integer
声明为Element_Type
;这限制了您可以对此类型的对象执行分配和等同性比较。它确实不支持&#34;&lt;&#34;,&#34;&lt; =&#34;,&#34;&gt; =&#34;,&#34; &GT;&#34;
如果您愿意支持有符号整数类型,可以说(ARM 12.5.2(3))
private
或者(假设您也希望能够支持浮点类型),您需要导入相关操作(ARM 12.6(18)):
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 <>;
表示使用具有相同名称的is <>
操作(如果有)。