Ada-如何定义任务参数



我是Ada的新手。

我的问题是:

用程序设计语言ADA:创建

这里有6个湖泊。每个湖泊都有:

  • 4只天鹅,羽毛数量:100、200、300、400和

  • 7羽鹅:50060070080090010001100。

有10个猎人。每个猎人计划猎杀3只天鹅和5只鹅。每个猎人都会在尽可能多的不同湖泊中狩猎(同一湖泊中的第二只鸟只有在尝试在其他湖泊中狩猎成功或失败后才会狩猎(。每次猎人选择从他要寻找的类型的鸟中寻找羽毛最多的鸟。

2名猎人首先寻找天鹅(只有在他们狩猎了所需数量的天鹅或湖中不再有天鹅时才能寻找鹅(,8名猎人首先搜寻鹅。当猎人获得所需数量的天鹅和鹅时,他会说出他狩猎的羽毛总数。这位猎人没有获得所需数量的羽毛,他说他失踪了多少只天鹅和多少只鹅。

说明:将猎人和湖泊描述为ADA中的TASK阵列;如果猎人x想从y湖狩猎一只天鹅,猎人会打电话给他在同步期间收到的羽毛数量的湖,或者如果湖中没有天鹅,他会被拒绝。必须通过在标准输出中打印输出数据来公开输出数据。

所有的猎人必须同时完成他们的工作。程序中的所有TASK和程序本身都必须退出工作。禁止使用时间控制来确保程序停止工作。

关于猎人的活动、他们狩猎的鸟类以及天鹅或鹅在每个湖泊中的剩余信息,禁止保存在全局变量或狩猎或湖泊以外的任何其他结构中。仅在消息交换的基础上在主应用程序和任务簿之间交换信息;任务不使用对其外部定义的变量的访问权限。

首先,我想了解如何定义规则。我现在的解决方案是:

with Ada.Text_IO;
use Ada.Text_IO;
procedure Birds is
type Hunter (Swans, Geese);
type Swans(100,200,300,400) of Integer;
type Geese(500,600,700,800,900,1000,1100) of Integer;
Hunter_Number_Of_Birds : INTEGER := 0;
Lake_Number_Of_birds_In_Lake : INTEGER := 66;
-- The main program adds and deletes birds to hunters and from the lakes.
task Hunter is
entry Hunts_Swans(dates: in Integer);  -- This adds swans to hunter
entry Hunts_Geese(dates: in Integer);  -- This adds geese to hunter
end Hunter;
task Lake is
entry Swans(dates: in Integer);  -- This deletes swans from lake
entry Geese(dates: in Integer);  -- This deletes geese from lake
end Lake;

task body Hunter is
begin
for Index in 1..8 loop
select
when   Hunter_Number_Of_Birds =>8
accept Hunts_Swans do
Hunter_Number_Of_Birds :=   Hunter_Number_Of_Birds + 1;
Put_line("Hunter adds swans, count =");
Put(Hunter_Number_Of_Birds);
New_Line;
end Hunts_Swans;
or
when Hunter_Number_Of_Birds =>8
accept Hunts_Geese do
Hunter_Number_Of_Birds :=   Hunter_Number_Of_Birds + 1;
Put_Line("Hunter adds geese, count =");
Put(Hunter_Number_Of_Birds);
end Hunts_Geese;
end select;
end loop;
end Hunter;
task body Lake is
begin
for Index in 1..5 loop
select
when  Lake_Number_Of_birds_In_Lake =>66
accept Swans do
Lake_Number_Of_birds_In_Lake :=  Lake_Number_Of_birds_In_Lake - 1;
Put_line("Hunter hunt swans, remaining number of swans in the lake =");
Put(Lake_Number_Of_birds_In_Lake);
New_Line;
end Swans;
or
when Lake_Number_Of_birds_In_Lake =>66
accept Geese do
Lake_Number_Of_birds_In_Lake:=  Lake_Number_Of_birds_In_Lake - 1;
Put_line("Hunter hunt geese, remaining number of geese in the lake =");
Put(Lake_Number_Of_birds_In_Lake);
end Geese;
end select;
end loop;
end Lake;

我不知道如何定义:

  • 猎人和湖泊的数量
  • 每个胡须的羽毛数量
  • 2名猎人先寻找天鹅,8名猎人先搜寻鹅
  • 数数羽毛
Number of hunters and lakes;

如评论中所述,将您的任务声明更改为任务类型:

task type Hunter is
entry Hunts_Swans(dates: in Integer);  -- This adds swans to hunter
entry Hunts_Geese(dates: in Integer);  -- This adds geese to hunter
end Hunter;
task type Lake is
entry Swans(dates: in Integer);  -- This deletes swans from lake
entry Geese(dates: in Integer);  -- This deletes geese from lake
end Lake;

然后您只需要将它们声明为数组:

type Hunter_Array is array(1..10) of Hunter;
type Lake_Array is array(1..6) of Lake;
Hunters : Hunter_Array;
Lakes   : Lake_Array;
number of feathers of each beards;

在这里,我将创建一个birds包并使用Ada.Containers.Boundd_Vectors:

package Birds is
type Bird is (Swan,Goose);
type Feather_Count is new Natural;
end Birds;
use type Birds.Feather_Count;
package Vectors is new Ada.Containers.Bounded_Vectors
(Index_Type   => Positive,
Element_Type => Birds.Feather_Count);
use type Vectors.Vector;
Swans : aliased Vectors.Vector 
:= Vectors.To_Vector(100,1) & 200 & 300 & 400;
Geese : aliased Vectors.Vector
:= Vectors.To_Vector(500,1) & 600 & 700 & 800 & 900 & 1000 & 1100;

使用矢量可以跟踪湖中剩下的鸟类数量(例如,你可以称之为天鹅。长度(,当被猎杀时,你可以通过调用来获取羽毛

Feathers := Swans.Last_Element;
Swans.Delete_Last;  -- Takes it out of the vector

本质上使用向量作为堆栈。

2 hunters first look for swans and 8 hunters first look for geese;

为此,我会将您的猎人任务定义更改为:

task type Hunter(Prefers : Birds.Bird := Birds.Swan) is
entry Hunts_Swans(dates: in Integer);  -- This adds swans to hunter
entry Hunts_Geese(dates: in Integer);  -- This adds geese to hunter
end Hunter;

并为其添加一个构造函数:

function Swan_Hunter return Hunter is
begin
return Result : Hunter(Birds.Swan);
end Swan_Hunter;
function Goose_Hunter return Hunter is
begin
return Result : Hunter(Birds.Goose);
end Goose_Hunter;

然后你可以将你的猎人阵列声明更改为:

Hunters : Hunter_Array := 
(1 => Swan_Hunter, 
2 => Swan_Hunter, 
others => Goose_Hunter);

在那之后,你只需要添加逻辑来跟踪他们目前是否在狩猎鹅或天鹅,并将其与他们的偏好进行比较,以了解如何继续。

count the feathers

当你在湖中狩猎时,输入还返回羽毛数量。然后你可以在每次成功的时候把它们加在一起,让猎人为自己保持一个连续的总数。

最新更新