在Ada中使用多维数组
在这段代码中,我需要帮助编写一个范围在2020-01-01到2119-12-31之间的多维数组。 我的代码可以工作,但正如您所看到的,其中没有数组。如何仅使用数组编写此代码在Ada中使用多维数组,ada,Ada,在这段代码中,我需要帮助编写一个范围在2020-01-01到2119-12-31之间的多维数组。 我的代码可以工作,但正如您所看到的,其中没有数组。如何仅使用数组编写此代码 with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; Procedure Date is type date_type is record Str
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Procedure Date is
type date_type is record
Str : string (1..8);
Length : Natural := 0; end record;
A: date_type;
begin
loop
Put ("Enter a date between 2020-01-01 to 2119-12-31 : ");
Get_Line (A.Str, A.Length);
exit when A.Length = 8;
Put_Line ("Wrong input. Try again.");
end loop;
Put_Line (A.Str (1 .. 4) & "-" & A.Str (5 .. 6) & "-" & A.Str (7 .. 8));
end Date;
也许不是一个多维数组,你应该考虑使用一个记录,比如
type Year_Number is range 1900..3000;
type Month_Number is range 1..12;
type Day_Number is range 1..31;
type Date_Rec is record
Year : Year_Number;
Month : Month_Number;
Day : Day_Number;
end record;
subtype Year_String is string (1..4);
subtype Month_String is string (1..2);
subtype Day_String is string (1..2);
function To_Date (Yr : Year_String; Mnth : Month_String; Dy : Day_String)
return Date_Rec is
Result : Date_Rec;
begin
Result.Year := Year_Number'Value (Yr);
Result.Month := Month_Number'Value (Mnth);
Result.Day := Day_Number'Value (Dy);
return Result;
end To_Date;
现在可以传递日期的实例_
记录你想做什么就做什么。
如果你走了这么远,你可能想考虑使用艾达语言参考手册第9.6和第9节中描述的时间类型。 < p>这里你没有问一个合理的问题,因为<代码>“我想使用数组”不是使用数组的好理由。
“这个问题最好用数组解决……但我该如何处理……?”
是一个合理的问题,但您还没有说明问题,更不用说需要数组的问题了
这一点很重要,因为“使用数组”是在解决方案领域进行思考,就像“使用凿子”。这不是用Ada(或任何语言的IMO)进行编程的方式
首先试着从问题的角度来思考:与其说“我想用凿子”,不如说“我想把这个铰链凹进去,这样门就能精确地安装起来”,而凿子是最整洁的方法
然后“我如何才能最好地验证日期?”
将是一个合理的问题,或者“我如何存储100年来每天发生的事件?”
第一个问题的答案可能在Ada.Calendar
包中。可能是Ada.Calendar.Formatting
中的Value
函数,带有一个异常处理程序,用于捕获不可理解的字符串并让用户重试
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar;
with Ada.Calendar.Formatting;
procedure date is
Date : Ada.Calendar.Time;
Done : Boolean;
begin
loop
Put ("Enter a date between 2020-01-01 to 2119-12-31 : ");
Done := TRUE;
declare
A : String := Get_Line & " 12:00:00";
begin
Date := Ada.Calendar.Formatting.Value(A); -- validate it's a date
Done := Ada.Calendar.Year(Date) >= 2020
and Ada.Calendar.Year(Date) < 2120; -- validate correct range
exception
when Constraint_Error => Done := False; -- Formatting.Value failed
end;
exit when Done;
Put("Try Again : ");
end loop;
end date;
使用字符串测试
2020-11-03
Ada数组索引值必须是离散类型,例如整数类型或枚举类型。它们不能是字符串。有关字符串、时间和日计数之间的转换,请参阅《Ada语言参考手册》第9.6节和第9.6.1节。您想对数组做什么?为什么它必须是多维数组?数组为代码提供了更好的结构,处理起来也更容易。因为使用数组会给您带来麻烦(为什么?问题出在哪里?),显然“不容易处理”。而且,使用不适当的语言特性并不能“使代码具有更好的结构”。我重复Jim的问题,你想用数组做什么?噢,2020-11-04
是9个字符,不是8个
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure date is
Date : Ada.Calendar.Time;
Done : Boolean := TRUE;
Event_Array : array(2020 .. 2119,
Ada.Calendar.Month_Number,
Ada.Calendar.Day_Number) of Unbounded_String;
begin
Event_Array := (others => (others => (others => Null_Unbounded_String)));
Event_Array(2020,11,3) := To_Unbounded_String("nothing much");
loop
Put ("Enter a date between 2020-01-01 to 2119-12-31 : ");
Done := TRUE;
declare
A : String := Get_Line & " 12:00:00";
begin
Date := Ada.Calendar.Formatting.Value(A);
Done := Ada.Calendar.Year(Date) >= 2020
and Ada.Calendar.Year(Date) < 2120;
exception
when Constraint_Error => Done := False;
end;
exit when Done;
Put("Try Again : ");
end loop;
Put_Line("Today " & Ada.Calendar.Formatting.Image(Date) & " : " &
To_String(Event_Array(Year(Date), Month(Date), Day(Date))) & " happened");
end date;