我正在编写一个程序来检查给定日期的“正确性”。我哪里做错了?

时间:2016-03-03 03:08:05

标签: ada

这是我的程序,但我觉得我的语法只有一个主要问题,我无法弄明白。

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;

我在这里做错了什么?我有很多错误信息,但我觉得它们源于一个问题。

2 个答案:

答案 0 :(得分:1)

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

在编译时考虑使用gnatmake的这些参数:

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

答案 1 :(得分: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

这应该是显而易见的。