如何检查元素是属于一个子类型还是另一个子类型?

时间:2012-01-28 13:36:18

标签: types comparison ada

我刚学会了Ada中的Enums和Types,并决定编写一个小程序来练习:

with Ada.Text_IO;                       use Ada.Text_IO;
with Ada.Integer_Text_IO;       use Ada.Integer_Text_IO;

procedure Day is 

    type Day_Of_The_Week is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

    subtype Weekday is Day_Of_The_Week range Monday..Friday;

    subtype Weekend is Day_Of_The_Week range Saturday..Sunday;

        function is_Weekday ( dayOfTheWeek: in Day_Of_The_Week) return Boolean is
        begin
            if(--?--)
        end is_Weekday;

    selected_day_value  :   Integer;
    selected_day                :   Day_Of_The_Week;

begin
    Put_Line("Enter the number co-responding to the desired day of the week:");
    Put_Line("0 - Monday");
    Put_Line("1 - Tuesday");
    Put_Line("2 - Wednesday");
    Put_Line("3 - Thursday");
    Put_Line("4 - Friday");
    Put_Line("5 - Saturday");
    Put_Line("6 - Sunday");
    Get(selected_day_value);
    selected_day = Day_Of_The_Week'pos(selected_day_value);

    if( is_Weekday(selected_day))
        Put_Line( Day_Of_The_Week'Image(selected_day) & " is a weekday." );
    else
        Put_Line( Day_Of_The_Week'Image(selected_day) & " is a weekday." );

end Day;

我遇到了if语句的问题。如何检查dayOfTheWeek是否处于工作日子类型或周末子类型?

2 个答案:

答案 0 :(得分:9)

你想要

function is_Weekday ( dayOfTheWeek: in Day_Of_The_Week) return Boolean is
begin
    return dayoFTheWeek in Weekday;
end is_Weekday;

此外,您希望

中的’Val不是’Pos
selected_day := Day_Of_The_Week'val(selected_day_value);

您可以查看第二个Put_Line中的字词!

答案 1 :(得分:2)

您无需检查此功能。在这种情况下,函数只会模糊发生的事情:

if Selected_Day in Weekday then
  do stuff..
else
  do other stuff...
end if;