我已经定义了
subtype String10 is String(1..10);
Run Code Online (Sandbox Code Playgroud)
我试图获得键盘输入,而无需在输入之前手动输入空格.我尝试了get_line()但是由于某些原因它在输出get put()命令之前实际上不会等待输入,而且我也认为它会在那之前留下字符串中的内容并且不用空格填充它.
我知道并使用了Bounded_String和Unbounded_String,但我想知道是否有办法使这项工作.
我试过为它做一个函数:
--getString10--
procedure getString10(s : string10) is
c : character;
k : integer;
begin
for i in integer range 1..10 loop
get(c);
if Ada.Text_IO.End_Of_Line = false then
s(i) := c;
else
k := i;
exit;
end if;
end loop;
for i in integer range k..10 loop
s(i) := ' ';
end loop;
end getString10;
Run Code Online (Sandbox Code Playgroud)
但是,在这里,我知道这s(i)不起作用,我不认为
"if Ada.Text_IO.End_Of_Line = false then"
Run Code Online (Sandbox Code Playgroud)
做我希望它会做什么.它只是一个占位符,而我正在寻找实际的方法.
我一直在搜索几个小时,但是Ada文档不像其他语言那样可用或清晰.我找到了很多关于获取字符串的内容,但不是我正在寻找的内容.
我想在一个包内创建一个标记类型,描述一个2D离散空间,其大小由运行时间决定.(背景:生命游戏的实施)
我找到的第一种方法是通用性:
generic
Size : Natural;
package Worlds is
type World_Type is tagged private;
type World is access World_Type'Class;
subtype Coordinate is Positive range 1..Size;
private
type World_Array is array (Coordinate, Coordinate) of Boolean;
type World_Type is tagged record
Content : World_Array;
end record;
end Worlds;
Run Code Online (Sandbox Code Playgroud)
但是,在为世界实现访问者时,通用性成为一个大问题:
with Worlds;
package World_Visitors is
type World_Visitor_Type is tagged private;
type World_Visitor is access World_Visitor_Type'Class;
procedure Visite(v : World_Visitor_Type;
w : in out Worlds.World); -- ERROR: invalid prefix in selected component …Run Code Online (Sandbox Code Playgroud) 作为学习Ada的一部分,我正在处理一些基本的编码挑战。我遇到了一种情况,我想创建一个固定大小的2D整数数组(大小在运行时确定)。我的想法是要有一个小的实用程序函数,该函数可以传递数组的大小,创建它,填充它并返回它以便在其他函数中使用。
我该怎么做呢?我看到的其他答案将创建的数组保留在function-scope中,并且不返回它。
到目前为止,这是我的主要过程:
with Ada.Integer_Text_IO;
with Ada.Text_IO;
with Coord;
with Grid;
procedure test is
boundary : Coord.Box;
-- This is the array I want to create and fill
-- Note sure about what I put here for the "initial" size
new_grid : Grid.GridType (0 .. 1, 0 .. 1);
begin
-- This is just for the example, actually these
-- values are calculated from values in a text file
Ada.Text_IO.Put ("X Min?");
Ada.Integer_Text_IO.Get (boundary.min.x);
Ada.Text_IO.Put ("X Max?");
Ada.Integer_Text_IO.Get (boundary.max.x);
Ada.Text_IO.Put …Run Code Online (Sandbox Code Playgroud) 正如问题所说,我想在Ada中创建一个动态分配的数组.像C++这样的东西std::vector,我不希望将数组的长度存储在一个单独的变量中,就像在这里完成一样.由于Ada支持泛型,是否可以std::vector在Ada中创建类似的功能?