gpt4 book ai didi

string - 存储和使用变长字符串 (ADA)

转载 作者:行者123 更新时间:2023-12-04 00:19:24 25 4
gpt4 key购买 nike

我正在解决一个问题,我需要根据输入的数字制作一组框,其中每个框都有一个唯一的名称。我已经设法创建了这些盒子,但我只设法在所有盒子上插入一个名字,因为我的名字在名字收集过程中被覆盖了。

这是代码 https://pastebin.com/FBMvvrn4

with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure exercise is

N : Integer;
Names : String(1..10);
L : Integer;

procedure Objectcatcha (N: out Integer) is

begin

Put("Enter amount of objects: ");
Get(N);

end Objectcatcha;

procedure Namescatcha (Names: out string; L: out integer) is

begin
for I in 1..N loop
Get_Line(Names, L);
end loop;

end Namescatcha;

procedure SpaceBox(Names: in String; L: in Integer; N : in integer) is

begin

for I in 1..N loop
Put("+-----------+ ");
end loop;
New_Line;

for I in 1..N loop
Put("! ");
Put(Names(1..L));
for J in (L+1)..10 loop
Put(" ");
end loop;
Put("!");

if I = N then
Put("");
else
Put("<>---");
end if;
end loop;
New_Line;

for I in 1..N loop
Put("+-----------+ ");
end loop;

end SpaceBox;

begin

Objectcatcha(N);

Put("Enter the name of the objects: ");
Namescatcha(Names, L);

SpaceBox(Names,L, N);

end exercise;

我在这方面坐了很久,如果有人能帮我找到一种方法来单独命名每个盒子,我会很高兴。

提前致谢!

最佳答案

只要你可以,(你可以在这里),只需声明一个精确大小的变量来保存你正在使用的名称。这可以通过将其声明为不定数组并使用正确的名称对其进行初始化来完成。

所以你的主程序可能是:

 Objectcatcha(N);    
For I in 1 to N loop
Put("Enter the name of the next object: ");
Declare
Name : String := Namescatcha;
Begin
SpaceBox(Name, Name'Length, N);
End;
End loop;

Namescatcha 现在是一个返回正确大小的字符串的函数:

function Namescatcha return String is
begin
return Getline;
end Namescatcha;

你可能应该重写没有 L 的 Spacebox(你总是可以使用 Name'Length 来查看 Name 的长度)

关于string - 存储和使用变长字符串 (ADA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61719343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com