gpt4 book ai didi

ada - 如何使用 Ada 双向链表

转载 作者:行者123 更新时间:2023-12-02 09:03:32 24 4
gpt4 key购买 nike

我正在使用 Ada 编程语言开展一个学校项目。教授要我们用不熟悉的语言来写。我需要使用 Ada 双向链表来编写一个文本编辑器程序。该程序在调用时将采用一个可选的命令行参数,该参数将指定默认文件名。如果该文件已经存在,则应将其内容加载到文本缓冲区中。保存文件后,缓冲区的内容将转储到具有指定名称的文件中,覆盖任何现有文件。

例如,如果我输入

a -- 追加命令

你好

世界

.

文件会有

你好

世界

附加到文档末尾

如果我输入

3个

你好

世界

.

然后在文档的第 3 行之后追加同一行。

这是我到目前为止编写的代码,但我也不知道如何使用字符串作为我的 case 语句的条件。

有人可以帮我做这个项目吗?

with Ada.Containers.Doubly_Linked_Lists;
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;


procedure main is
package String_List is new Ada.Containers.Doubly_Linked_Lists(Unbounded_String);
use String_List;
--Userinput : String(1 .. 10) := (others => ' '); --This string length is 10
Last: Integer; --This is use to count input string length
currentAddress: Integer := 0;
text : List; -- List is from Doubly_Linked_Lists
type Userinput is (a, c, p, e);
--size: Integer; --This is use to count input string length


procedure Print(Position : Cursor) is -- this subprogram print all string from list
begin
Put_Line(To_String(Element(Position)));
--Put_Line("K");
end Print;



begin
loop

Put( Integer'Image (currentAddress) & " >> " );

Get_Line(Userinput);

case Userinput is

when a =>
Put( Integer'Image (currentAddress) & " >> " );
Get_Line(Userinput);
text.Append(To_Unbounded_String(Userinput)); -- if letter is a add it to the doubly link list

when c =>
--text.Insert(Before => text.Find(To_Unbounded_String(Userinput)), New_Item => To_Unbounded_String( ? ));

when p =>
text.Iterate(Print'access);
when e =>
Put_Line("Program Exit");
exit;
when others =>
Put_Line ("No command found ");

end case;

end loop;

end main;

最佳答案

您不能将 String 用作 case 语句 ( ARM 5.4(4) ) 的“选择表达式”,因为它不是离散类型(不仅仅是实数或记录, 是)。

您可以尝试使用下面示例中的枚举(这留下了如何处理输入的问题,例如您的示例 3 a):

with Ada.Text_IO;
with Ada.IO_Exceptions;
procedure Alan is
type Command is (A, C, P, E);
package Command_IO is new Ada.Text_IO.Enumeration_IO (Command);
begin
loop
declare
Cmd : Command;
begin
Command_IO.Get (Cmd);
Ada.Text_IO.Skip_Line;
Ada.Text_IO.Put_Line ("read " & Cmd'Image); -- ' to sort out the not-fully-Ada-aware syntax highlighting
case Cmd is
when A => ...
...
when E => exit;
end case;
exception
when Ada.IO_Exceptions.Data_Error =>
Ada.Text_IO.Put_Line ("unrecognised command");
end;
end loop;
end Alan;

通常我会使用 Ada.Text_IO;;为了清楚起见,不在这里。

关于ada - 如何使用 Ada 双向链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61151618/

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