2016-03-03 53 views
-2

这是我的程序,但我觉得我的语法只有一个主要问题,我无法弄清楚。我正在写一个程序,检查给定日期的“正确性”。我哪里做错了?

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

procedure Project_3 is 

    ------------------------------------------------------------------------- 
    -- Haleigh Graham 
    -- 02/26/2015 
    -- 
    -- 
    -- 
    -- 
    -- 
    -- 
    ------------------------------------------------------------------------- 


type Day_28 is array (1..28) of Natural; 
type Day_29 is array (1..29) of Natural; 
type Day_30 is array (1..30) of Natural; 
type Day_31 is array (1..31) of Natural; 

Date_Input : String (1 .. 10); 
Month : constant String := Date_Input (1 .. 2); 
Day : constant String := Date_Input (4 .. 5); 
Year : constant String := Date_Input (7 .. 10); 
Valid_Date : Boolean := False; 
Valid_Month : Boolean :=False; 
type Long_Month is array (1..31) of Natural; 
type Short_Month is array (1..30) of Natural; 
Index : Positive; 



    -- Is_February function 
function Is_February (Month : in integer) return Boolean is 

begin 
    if Month = 2 then 
     return True; 
    else 
     return False; 
    end if; 
end Is_February; 


    -- Is_Leap_Year function 
function Is_Leap_Year (Year : in integer) return boolean is 

begin -- Is_Leap_Year 
    if Year rem 100 = 0 then 
     if Year rem 400 = 0 then 
     return true; 
     else 
     return false; 
     end if; 
    else 
     if Year rem 4 = 0 then 
     return true; 
     else 
     return false; 
     end if; 
    end if; 
end Is_Leap_Year; 

    -- Is_Valid_Day_31 function 
function Is_Valid_Day_31 (Day : in Integer) return Boolean is 

begin -- Is_Valid_Day_31 
    if Day = Day_31 then 
     return true; 
    else 
     return False; 
    end if; 
end Is_Valid_Day_31; 

    -- Is_Valid_Day_30 function 
function Is_Valid_Day_30 (Day : in Integer) return Boolean is 

begin -- Is_Valid_Day_30 
    if Day = Day_30 then 
     return true; 
    else 
     return False; 
    end if; 
end Is_Valid_Day_30; 

    -- Is_Valid_Day_29 function 
function Is_Valid_Day_29 (Day : in Integer) return Boolean is 

begin -- Is_Valid_Day_29 
    if Day = Day_29 then 
     return true; 
    else 
     return False; 
    end if; 
end Is_Valid_Day_29; 

    -- Is_Valid_Day_28 function 
function Is_Valid_Day_28 (Day : in Integer) return Boolean is 

begin -- Is_Valid_Day_28 
    if Day = Day_28 then 
     return true; 
    else 
     return False; 
    end if; 
end Is_Valid_Day_28; 

    -- Is_Valid_Year function 
function Is_Valid_Year (Year : in Integer) return Boolean is 

begin -- Is_Valid_Year 
    if Year > 1700 and Year < 2300 then 
     return true; 
    else 
     return False; 
    end if; 
end Is_Valid_Year; 

    -- Is_Long_Month function 
function Is_Long_Month (Month : in Integer) return Boolean is 

begin -- Is_Long_Month function 
    if Month = Long_Month(Integer) then 
     return True; 
    else 
     return False; 
    end if; 
end Is_Long_Month; 

    -- Is_Short_Month function 
function Is_Short_Month (Month : in Integer) return Boolean is 

begin -- Is_Short_Month function 
    if Month = Short_Month(Integer) then 
     return True; 
    else 
     return False; 
    end if; 
end Is_Short_Month; 


    -- Main Program 

begin 




    Ada.Text_IO.Put ("Enter the date you wish to check (MM/DD/YYYY): "); 
    Ada.Integer_Text_IO.Get (Item => Date_Input); 
    Ada.Text_IO.New_Line; 


    if Is_Valid_Year = True then 
     if Is_February = True then 
     if Is_Leap_Year = True then 
      if Is_Valid_Day_28 = True then 
       Valid_Date := True; 
      else 
       Valid_Date := False; 
      end if; 
     else 
      if Is_Valid_Day_29 = True then 
       Valid_Date := True; 
      else 
       Valid_Date := False; 
      end if; 
     end if; 
     elsif Is_Long_Month = True then 
     if Is_Valid_Day_31 = True then 
      Valid_Date := True; 
     else 
      Valid_Date := False; 
     end if; 
     elsif Is_Short_Month = True then 
     if Is_Valid_Day_30 = True then 
      Valid_Date := True; 
     else 
      Valid_Date := False; 
     end if; 
     end if; 
    end if; 

end Project_3; 

我在做什么错在这里?我有很多错误信息,但我觉得他们是由一个问题引起的。

+0

为什么你会写'Is_Short_Month = True'这样的表达式?它们相当于更易读的'Is_Short_Month'。 –

+0

也许你应该尝试减少你的例子,并一次删除一个问题? –

回答

1

当我尝试编译您的示例时,获得的每条错误消息都来自源文本中的唯一错误。

考虑使用这些参数为gnatmake,当编译:

gnatmake -gnata -gnato -fstack-check -gnat12 -gnatyO -gnatv -gnati1 -gnatf -gnatn 
1

我建议您用-gnatl编译 - 它点缀与代码清单错误消息。

很多你的错误消息都像

71. function Is_Valid_Day_31 (Day : in Integer) return Boolean is 
72. 
73. begin -- Is_Valid_Day_31 
74. if Day = Day_31 then 
       | 
    >>> subtype name cannot be used as operand 

,你必须

25. type Day_31 is array (1..31) of Natural; 

我不知道为什么你编码Day_31作为一个数组;例如,您想要在Day_31 (13)处存储什么?这将是更正常的申报

subtype Day_31 is Natural range 1 .. 31; 

,然后你可以写

function Is_Valid_Day_31 (Day : in Integer) return Boolean is 
begin -- Is_Valid_Day_31 
    return Day in Day_31; 
end Is_Valid_Day_31; 

在另一方面,如果你真的需要这些阵列,你可以写

function Is_Valid_Day_31 (Day : in Integer) return Boolean is 
begin -- Is_Valid_Day_31 
    return Day in Day_31'Range; 
end Is_Valid_Day_31; 

然后,你有一个Text_IO的问题,我会一个人留下来,还有一堆问题,如

155. if Is_Valid_Year = True then 
      | 
    >>> missing argument for parameter "Year" in call to "Is_Valid_Year" declared at line 110 

这应该是显而易见的。

相关问题