- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当读者开始阅读函数代码时,他应该已经非常清楚它是做什么的,它是如何做的,以及他可能会遇到什么问题。我正在尝试编写易于理解的干净、结构化、注释良好的代码。我正在阅读 Ada Style Guide 和一些我不太了解的东西,我可以为可选部分写什么 (例如:@Error_Handling, @Pre, @Post
)。
我想用一个例子来表示这个函数。使用上述指南,可以导出标准函数头:
-- ---------------------------------------------------------------------------
-- @Function: Arithmetic_Mean
--
-- @Description:
-- Function to find the mean of a numeric vector. The program should
-- work on a zero-length vector (with an answer of 0.0).
-- @Usage: (opt)
-- @Parameter:
-- +Num: Given array
-- @Return: Averages/Arithmetic mean or zero
-- @Error_Handling: (opt)
-- @Pre: (opt)
-- @Post (opt)
type Mean_Numbers is array (Natural range <>) of Float;
function Arithmetic_Mean (Num : Mean_Numbers) return Float is
Sum : Float := 0.0;
begin
if Num'Length > 0 then
while Num'First <= Num'Last loop
Sum := Sum + Num(Num'First );
end loop;
return Sum / Float (Num'Length);
end if;
return 0.0;
end Arithmetic_Mean;
-------------------------------------------------------------- ... --
-- @Function: Get_Index
-- @Description:
-- Returns the minimum index of Item in A.
-- @Parameters:
-- +A: the array
-- +Item: element searched for
-- @Return:
-- The minimum index of Item in A.
-- @Pre:
-- true
-- @Post:
-- if exists 1 <= I <= UPPER_BOUND: A(I) = Item then
-- result = min {1 <= k <= UPPER_BOUND | a(j) = item }
-- else
-- result = 0
最佳答案
@Pre
和 @Post
标签应该记录你的模块对Design by Contract的方法.正如您所观察到的,任何 precondition必须为 true 才能成功执行,并且任何 postcondition是由您的代码实现的 promise 。 @Error_Handling
标签标识您如何处理违反其他两项的行为。
作为一个具体的例子,你的 Arithmetic_Mean
的实现默默地忽略一个空输入数组,返回均值为零;它传播引发的任何异常。这些是应该记录的行为。
有几个好处:
关于function - Ada 函数头中的可选注释部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8821059/
如何在过程中编写迭代器?对不起我的转储问题,我是新手。感谢您的回答。 最佳答案 这完全取决于您需要迭代的内容。 数组?使用loop : plain, for, or while. predefined
我现在知道很多编程语言。回到我 18 岁的时候,我几乎加入了美国空军,并且对 Ada 进行了测试。那是十多年前的事了。 Ada 编程语言在军队中是否仍然像以前一样重要? 我想知道新的军事软件项目是否仍
在 Java 或 C# 中,您经常会拥有 final 的类成员。或 readonly - 它们设置一次,然后再也不碰。它们可以为类的不同实例保存不同的值。 艾达有没有类似的东西?我试图在 Ada 中创
即使使用这个简单的示例,我也无法让动态调度正常工作。我相信问题在于我如何设置类型和方法,但看不到在哪里! with Ada.Text_Io; procedure Simple is type A
我目前正在自学 Ada,尽管我可以从解决一些更传统的问题开始。 更具体地说,我尝试计算阶乘 n!,而 n>100。到目前为止,我的实现是: with Ada.Text_IO; with Ada.Int
目前正在学习 Ada 并真正享受它,有一件事情困扰着我:什么是 tagged类型?根据 John Barnes 的 Programming in Ada 2012,它表示实例化的对象在运行时带有标签。
你好 我正在尝试我在 Ada 中创建单人骰子游戏的第一个程序。 但面临着保持球员得分的问题。 目标:每个玩家有 10 个回合,如果 2 次掷骰总数为 7,则获得 10 分 问题:每次总分被重置并且 1
您可以通过让函数返回一个值来分配给变量: My_Int : Integer := My_Math_Func [(optional params)]; 或者你可以用一个过程来做到这一点(假设 My_In
我试图在 Ada 中将字符转换为整数,似乎没有任何效果,到目前为止我已经能够从 ASCII 返回 DEC,但我想返回 0(整数)。 Character'Pos('0'); 返回 48 --我希望它返回
假设我有以下常量来定义一个只接受其范围定义内的有效值的子类型: type Unsigned_4_T is mod 2**4; valid_1 : constant Unsigned_4_T :=
我正在尝试创建一个 tree-sitter解析器,以便 IDE(在本例中为 Vim)可以解析 Ada 程序文本并进行更高级的操作,例如 extract-subprogram 和 rename-vari
我正在写一篇关于 Ada 83 的论文。我们有一个作业,列出了论文的各个部分(历史、设计目标、语法等)。讲师提到我们中的一些人将有一些部分简单地说“此语言不支持此功能。” 其中两个部分是数据类型和
假设我有以下常量来定义一个只接受其范围定义内的有效值的子类型: type Unsigned_4_T is mod 2**4; valid_1 : constant Unsigned_4_T :=
我正在尝试创建一个 tree-sitter解析器,以便 IDE(在本例中为 Vim)可以解析 Ada 程序文本并进行更高级的操作,例如 extract-subprogram 和 rename-vari
我想声明一个元素类型为变体记录的数组。像这样: type myStruct (theType : vehicleType) is record ... when car => numOfWheels
我正在实例化一个带有枚举的通用包,以访问多个值之一并在子程序重载中使用。我想要一组定义明确、编译时检查过的值,我可以使用和查找。 generic -- Different types beca
我有以下包: ------------------- -- File: father.ads ------------------- package Father with SPARK_Mode =>
对于最后的程序,我从 gnat 收到以下错误消息: test2.adb:23:61: error: invalid operand types for operator "-" test2.adb:2
我编写了一个加密文件的 Ada 程序。它逐 block 读取它们以节省目标机器上的内存。不幸的是,Ada 的目录库读取 Long_Integer 中的文件大小,将读取限制为近 2GB 文件。尝试读取超
我想打印访问变量(指针)的地址以进行调试。 type Node is private; type Node_Ptr is access Node; procedure foo(n: in out No
我是一名优秀的程序员,十分优秀!