Ada 中数组最大

发布于 2024-12-10 12:35:51 字数 78 浏览 0 评论 0原文

如何检查某个值是否是数组中最大的元素?这是 Ada 语言。我有查找最大元素的函数,并且它有效。现在我想运行一个后置条件来检查该值是否是最大的。

How do i check if a certain value is the largest element in an array? this is in the Ada language. I have the function finding the largest element, and it works.. now i want to run a post condition that check that that value is the largest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫相离 2024-12-17 12:35:51

循环遍历数组,如果找到更大的元素或没有找到相等的元素,则会失败?

function Check_Largest (In_Array : Array_Type; Value : Element_Type)
  return Boolean
is
   Matched : Boolean := False;
begin
   for J in In_Array'Range loop
      if In_Array (J) > Value then
         return False;
      elsif In_Array (J) = Value then
         Matched := True;
      end if;
   end loop;
   return Matched;
end Check_Largest;

在 2020 年、Ada 2012 之后重新审视这一点,一对量词可能会起到作用;请参阅 Largest_Member 的后置条件:

procedure Largest
with SPARK_Mode
is
   type Arr is array (Positive range <>) of Natural;

   function Largest_Member (Of_Array : Arr) return Natural
   with
     Pre => Of_Array'Length > 0,
     Post =>
       (for all E of Of_Array => E <= Largest_Member'Result)
       and
       (for some E of Of_Array => E = Largest_Member'Result);

   function Largest_Member (Of_Array : Arr) return Natural is
      Result : Integer := -1;
   begin
      for J in Of_Array'Range loop
         if Of_Array (J) > Result then
            Result := Of_Array (J);
         end if;
         pragma Loop_Invariant
           ((for all F of Of_Array (Of_Array'First .. J) => F <= Result)
            and
            (for some F of Of_Array (Of_Array'First .. J) => F = Result));
      end loop;
      return Result;
   end Largest_Member;
begin
   null;
end Largest;

Loop through the array, failing if you find an element that is larger or if you don't find an element that is equal?

function Check_Largest (In_Array : Array_Type; Value : Element_Type)
  return Boolean
is
   Matched : Boolean := False;
begin
   for J in In_Array'Range loop
      if In_Array (J) > Value then
         return False;
      elsif In_Array (J) = Value then
         Matched := True;
      end if;
   end loop;
   return Matched;
end Check_Largest;

Revisiting this in 2020, post Ada 2012, a pair of quantifiers might do the trick; see the postcondition of Largest_Member:

procedure Largest
with SPARK_Mode
is
   type Arr is array (Positive range <>) of Natural;

   function Largest_Member (Of_Array : Arr) return Natural
   with
     Pre => Of_Array'Length > 0,
     Post =>
       (for all E of Of_Array => E <= Largest_Member'Result)
       and
       (for some E of Of_Array => E = Largest_Member'Result);

   function Largest_Member (Of_Array : Arr) return Natural is
      Result : Integer := -1;
   begin
      for J in Of_Array'Range loop
         if Of_Array (J) > Result then
            Result := Of_Array (J);
         end if;
         pragma Loop_Invariant
           ((for all F of Of_Array (Of_Array'First .. J) => F <= Result)
            and
            (for some F of Of_Array (Of_Array'First .. J) => F = Result));
      end loop;
      return Result;
   end Largest_Member;
begin
   null;
end Largest;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文