-- 本程序从命令行读取一个标准的时间(ISO date),如: -- 年 月 日,其中: -- -- * 年的数值在区间 [1983,2019]内 -- * 月 [1,12] -- * 日 [1,31] -- -- 经过转换,输出中文显示的时间。 -- 如果您输入的时间恰好是当年的春节,该程序还会为您献上新春的祝福。 -- 虽然16比特的字符串就可以满足中文格式的需要,但本程序统一使用32比特的字符串。 with Ada.Characters.Conversions; use Ada.Characters.Conversions; with Ada.Command_Line; with Ada.Wide_Wide_Text_IO; use Ada.Wide_Wide_Text_IO; -- 注视:我们在这里使用了“Ada日历格式”(Ada.Calendar.Formatting)。 procedure 春节快乐 is 抛出异常 : exception; -- 当命令行输入格式不符合(年 月 日)要求时,抛出异常。 subtype 中文字符串 is Wide_Wide_String; -- 重命名Wide_Wide_String为中文字符串。 function 字符串转换 (待转换串 : String) return 中文字符串 renames To_Wide_Wide_String; -- 将将String格式字符串转换为Wide_Wide_String格式。 function 数值转字符串 (数值 : Integer) return 中文字符串; -- 将一个正数值转换为字符串格式。 function 获得正数值 (字符 : String; 下限, 上限 : Positive) return Positive; -- 输入一个字符(年,月或日), 和一对下限,上限值,返回该字符所对应的正数值。 -- 如果该字符对应的不是一个正数值,或者该正数值不在区间[下限 .. 上限]内,打印 -- 错误信息并抛出异常。 -- (此处用于核对,输入的“年”在[1983,2019]内,“月”在[1,12],“日”在[1,31]) ----------------- -- 数值转字符串 -- ----------------- function 数值转字符串 (数值 : Integer) return 中文字符串 is begin return 字符串转换 (数值'Img); end 数值转字符串; --------------- -- 获得正数值 -- --------------- function 获得正数值 (字符 : String; 下限, 上限 : Positive) return Positive is 数值 : Integer; begin 数值 := Integer'Value (字符); if 数值 in 下限 .. 上限 then return 数值; else Put_Line ("'" & 字符串转换 (字符) & "': 数值不在规定范围内 " & 数值转字符串 (下限) & " .. " & 数值转字符串 (上限)); raise 抛出异常; end if; exception when Constraint_Error => Put_Line ("'" & 字符串转换 (字符) & "': 时间格式不正确"); raise 抛出异常; end 获得正数值; subtype 年_数字值 is Positive range 1983 .. 2019; subtype 月_数字值 is Positive range 1 .. 12; subtype 日_数字值 is Positive range 1 .. 31; 年 : 年_数字值; 月 : 月_数字值; 日 : 日_数字值; type 春节 is record 月 : 月_数字值; 日 : 日_数字值; end record; 春节表 : constant array (年_数字值) of 春节 := (1983 => (02, 13), 1984 => (02, 02), 1985 => (02, 20), 1986 => (02, 09), 1987 => (01, 29), 1988 => (02, 17), 1989 => (02, 06), 1990 => (01, 27), 1991 => (02, 15), 1992 => (02, 04), 1993 => (01, 23), 1994 => (02, 10), 1995 => (01, 31), 1996 => (02, 19), 1997 => (02, 07), 1998 => (01, 28), 1999 => (02, 16), 2000 => (02, 05), 2001 => (01, 24), 2002 => (02, 12), 2003 => (02, 01), 2004 => (01, 22), 2005 => (02, 09), 2006 => (01, 29), 2007 => (02, 18), 2008 => (02, 07), 2009 => (01, 26), 2010 => (02, 14), 2011 => (02, 03), 2012 => (01, 23), 2013 => (02, 10), 2014 => (01, 31), 2015 => (02, 19), 2016 => (02, 08), 2017 => (01, 28), 2018 => (02, 16), 2019 => (02, 05)); begin if Ada.Command_Line.Argument_Count /= 3 then raise 抛出异常; end if; 年 := 获得正数值 (Ada.Command_Line.Argument (1), 年_数字值'First, 年_数字值'Last); 月 := 获得正数值 (Ada.Command_Line.Argument (2), 月_数字值'First, 月_数字值'Last); 日 := 获得正数值 (Ada.Command_Line.Argument (3), 日_数字值'First, 日_数字值'Last); -- 将输入的标准时间转换为中文格式并对照春节表, -- 如果输入时间恰好是春节,打印新春祝福。 Put (数值转字符串(年) & "年 " & 数值转字符串(月)& "月 " & 数值转字符串(日) & "日"); if 月 = 春节表 (年).月 and 日 = 春节表 (年).日 then Put_Line (" - 春节快乐!"); else New_Line; end if; exception when 抛出异常 => Put_Line ("您输入的格式不正确!"); Put_Line (" 格式提示: " & "(./)春节快乐" & " 年 月 日"); Put_Line (" 年: " & 数值转字符串(年_数字值'First) & " .. " & 数值转字符串(年_数字值'Last)); Put_Line (" 月 : " & 数值转字符串(月_数字值'First) & " .. " & 数值转字符串(月_数字值'Last)); Put_Line (" 日 : " & 数值转字符串(日_数字值'First) & " .. " & 数值转字符串(日_数字值'Last)); end 春节快乐;