我正在尝试在Ada中定义一个字符串数组来存储可变大小的字符串.我遇到的问题是我必须预先定义我在编译时不知道的字符串的大小,并且使用Unbounded_Strings,String_Split.Create将不起作用,因为它需要Standard.String.
下面是我的代码,我需要能够解析可变大小的字符串,而不仅仅是固定长度4.
提前致谢.
type StrArray is array(1..7) of String(1..4);
function Split(Line : String;Sep : String) return StrArray is
Tokens : String_Split.Slice_Set;
Output : StrArray;
Count : Natural := 0;
begin
String_Split.Create(s => Tokens,
From => Line,
Separators => Sep,
Mode => String_Split.Single);
For I in 1 .. String_Split.Slice_Count (Tokens) loop
Count := Count + 1;
Output(Count) := String_Split.Slice(Tokens,I); -- Not sure how to convert Slice_Count to Integer either!
end loop;
return Output;
end Split;
Run Code Online (Sandbox Code Playgroud) 我是Ada的新手。
我已经声明了新的任务类型,并将其中三个存储在一个池中。然后,我想循环运行每个任务。
预期的行为是所有它们都同时执行。
现实情况是,它们是一个接一个地执行的。因此,任务(2)的执行不会早于任务(1)的终止。实际上,由于选择约束而终止,因此task(2)将永远不会执行。
我的代码:
with Counter;
procedure Main is
task type CounterTask is
entry Execute(t:in Counter.Timeout; d:in Duration);
end CounterTask;
task body CounterTask is
begin MyLoop: loop
select
accept Execute(t:in Counter.Timeout;d:in Duration) do
Counter.Run(t, d);
end Execute;
or
delay 2.0;
exit;
end select;
end loop MyLoop;
end CounterTask;
tasks:Array(1..3) of CounterTask;
begin
for i in Integer range 1..3 loop
tasks(i).Execute(Counter.Timeout(10*i), Duration(0.5 * i));
end loop;
end Main;
Run Code Online (Sandbox Code Playgroud)
任何提示或想法将是最欢迎的!
我目前正在learning.adacore.com上进行Ada教程,现在是第二个示例:读取和输出整数。由于复制粘贴适用于不想学习语法的人,因此我手动键入了大部分代码(其中一些是由gnat-gps生成的,但现在使用的是vim)。
我编译并运行了该程序,令人惊讶的是,输出的第二行大约缩进了一个制表符。为什么?
这是代码:
With Ada.Text_IO;
Use Ada.Text_IO;
With Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;
procedure Main is
N : Integer;
begin
-- Insert code here.
Put("Enter an integer value: ");
Get(N);
if N>0 then
Put (N);
Put_Line(" is a positive number");
end if;
end Main;
Run Code Online (Sandbox Code Playgroud)
(如何获取语法突出显示?)
以下是输出示例(第一个输入):
Enter an integer value: 1
1 is a positive number
Run Code Online (Sandbox Code Playgroud) 当聚合只包含一个元素时,如下所示,位置表示法会导致编译错误,我们只能使用命名表示法.为什么?
type singleton is record
v : integer;
end record;
v1 : singleton := (0);
Run Code Online (Sandbox Code Playgroud)
导致编译器消息
check.adb:6:23: positional aggregate cannot have one component
check.adb:6:23: write instead "V => ..."
gnatmake: “check.adb" compilation error
Run Code Online (Sandbox Code Playgroud)
这没关系:
v2 : singleton := (v => 0);
Run Code Online (Sandbox Code Playgroud) 如何像C中的指针一样增加访问类型的地址?我是阿达新手......
procedure main is
type myarr is array(1..5)of integer; --creating array of 5 integer.
myarr_var:aliased myarr:=(2,5,7,9,0); --setting 5 values
type my_access is access all myarr; --creating access type (pointer)
var:my_access:=myarr_var'access; --holding address of 5 integer
begin;
-- since var holds the address of myarr_var now i want to increment
-- the address by adding 1 to print next value (5) as we do in c?
put((var+1).all); --???
-- i know this is bad but how to increment its base address …Run Code Online (Sandbox Code Playgroud) 在我知道的所有Ada源代码中,包括标准库(抱歉,我不知道在网上找到它)或小游戏实现,所有非保留字都以大写字母开头.(最后,堆栈,索引,...)
因为所有保留的关键字都被编辑器突出显示,我猜/希望它不是用于区分用户定义的标识符.
在Python,C++或Java中,大写通常用于某些事物(通常是类标识符和常量),以允许它们被人类读者快速识别.(案例提供有关意义的信息)
为什么"官方"Ada代码以不同方式执行此操作?为什么不使用类型,模块之类的大写字母?
是因为通过信件案件的信息被认为是一件坏事吗?
如果是这样,为什么?
如何在Ada中找到变量的数据类型?
例如,给定
INT : integer;
Run Code Online (Sandbox Code Playgroud)
如何为此变量打印"数据类型是整数"?
在Python中,type()可以用来查找类型.在Ada中是否有任何类似的函数来查找变量的数据类型?
我想在Ada把我的头围绕在OOP上.为了做到这一点,我需要了解如何使用gnatmake命名,编译和链接包文件.
这个网站(http://www.infres.enst.fr/~pautet/Ada95/chap22.htm)有很好的例子,但我不明白如何编译程序的各个部分.
我在看e_c22_p2.ada和e_c22_p3.ada.从这些文件中我创建了一个名为Conveyance1.ads的文件,并在其中添加了e_c22_p2的内容,以及一个名为Vehicle1.adb的文件,并在其中添加了e_c22_p3.ada的内容.我使用了gnatmake Vehicle1.adb,但是有编译错误.
e_c22_p2.ada包含:
-- Chapter 22 - Program 2
package Conveyance1 is
-- This is a very simple transportation type.
type TRANSPORT is
record
Wheels : INTEGER;
Weight : FLOAT;
end record;
procedure Set_Values(Vehicle_In : in out TRANSPORT;
Wheels_In : INTEGER;
Weight_In : FLOAT);
function Get_Wheels(Vehicle_In : TRANSPORT) return INTEGER;
function Get_Weight(Vehicle_In : TRANSPORT) return FLOAT;
-- This CAR type extends the functionality of the TRANSPORT type.
type CAR is new TRANSPORT;
function Tire_Loading(Vehicle_In : CAR) …Run Code Online (Sandbox Code Playgroud) 假设我有一个用Ada编写的"hello world"程序:
with Ada.Text_IO; -- Bibliothèque
-- Déclaration de la procédure "Hello"
procedure Hello is
begin
-- Imprimer "Hello, world!" à l'écran
Ada.Text_IO.Put_Line("Hello, world!");
end Hello;
Run Code Online (Sandbox Code Playgroud)
当我完成编译过程时,我得到了可执行文件; 我该怎么扭转这个来取回源代码?
我经常发现我需要指定一个变量从具有某种物理意义的范围(例如,SoC 上的特定核心)获取值。但我还需要能够将其设置为“无”,这意味着“目前它不持有真正的核心标识符”。我通常使用下面给出的两种模式之一来实现此目的,但每种模式都有缺点:
第一个需要定义额外的(而且确实不必要的)类型。
第二个要求手动保持两个字段(值和定义该值是否有效的字段)对齐。编译器无法检查这种对齐方式。
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
-- We want to specify a Unit Identifier in the range 1 .. 10.
-- However, sometimes we need to say that the Unit Identifier
-- being stored is invalid.
--
-- The first way of doing this is to define an extended range
-- and an Invalid Identifier.
type Extended_Core_Identifier is new Natural range 0 .. 10;
subtype Core_Identifier is Extended_Core_Identifier range 1 .. 10;
Invalid_Core : constant …Run Code Online (Sandbox Code Playgroud) ada ×10
coding-style ×1
concurrency ×1
decompiler ×1
format ×1
gnat ×1
output ×1
record ×1
string ×1