Ada 05 问题:搜索记录向量中的特定元素?

发布于 2024-11-07 12:24:18 字数 824 浏览 5 评论 0原文

我创建了一个 Ada 程序,将在 GNAT Gcc 编译器 4.3.0 中编译 我创建了一条包含姓名、电话、地址和生日的记录。 姓名、电话和地址将是无界字符串格式,生日是另一条记录。这里的目的是制作一个通讯录管理系统。(非常简单)

type birthday is record
    year  : Positive;
    month : Positive;
    day   : positive;
end record;

type contact_type is record
    name    : unbounded_string;
    phone   : unbounded_string;
    address : unbounded_string;
    bday    : birthday;
end record;

我已经制作了插入(使用追加)和删除以及列表功能。我需要一个搜索和排序功能来完成。

问题是我把这个记录(contact_type 记录)变成了一个向量。 现在我想从向量中搜索元素/子类之一(无论你正确地称呼它)。

例如搜索姓名...或地址等。 但是使用向量的 find_index 方法,我需要插入 contact_type 类型的另一条记录,并且只会返回完全相同的元素作为结果。

但我想要的是,如果我搜索一个名称,则返回具有该名称的所有元素...

并且是否可以根据名称、地址、生日对向量进行“通用排序”(所以我有三种不同的排序选项)

PS。 我正在编写的第一个 Ada 程序是地址簿管理器...... 我是一个完全的新手,我只有 C 和 Java 经验,所以有点困难...... (我花了很长时间才找到如何标准输入和输出哈哈)

I've created a Ada program that will be compiled in GNAT Gcc compiler 4.3.0
I created a record that consists of a name, phone, address and a birthday.
The name, phone and address will be in unbounded string format and the birthday another record. The purpose here is to make a addressbook management system.(Very simple one)

type birthday is record
    year  : Positive;
    month : Positive;
    day   : positive;
end record;

type contact_type is record
    name    : unbounded_string;
    phone   : unbounded_string;
    address : unbounded_string;
    bday    : birthday;
end record;

I've already made an insert(using the append) and delete, and list function. I need a search and sort function to finish off.

The thing is I made this record(the contact_type record) into a vector.
Now I want to search from the vector for one of the elements/subclass(whatever you call it properly).

For example search for a name... Or an address, etc.
But using the vector's find_index method, I need to insert another record of the type contact_type and only an exactly same element will be returned as the result.

But what I want is if I search a name, all the elements with the name is returned...

And will it be possible to "Generic Sort" the vector in terms of Name, Address, Bday(So i have three different sorting options)

PS.
The first Ada program I'm writing is an addressbook manager...
I'm a complete newbie, and I only have experience in C, and Java so it's kinda going rough...
(It took me a long time finding out how to Standard input and Output lol)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心清如水 2024-11-14 12:24:18

当我开始学习 Ada 时,我写了一篇关于 Ada.Containers.Vectors 的相当长的 Wiki 文章。它帮助我记住了使用此软件包时可以使用的所有选项。文章中提供了有关如何迭代向量、如何搜索向量以及如何对其进行排序的示例。

http://wiki.ada-dk.org/index.php/Ada .Containers.Vectors

尽情享受吧! :o)

When I started learning Ada, I wrote a rather lengthy Wiki article about Ada.Containers.Vectors. It helped me remember all the many options that are available to you when using this package. In the article there are examples on how to iterate a vector, how to search it and how to sort it.

http://wiki.ada-dk.org/index.php/Ada.Containers.Vectors

Enjoy! :o)

怼怹恏 2024-11-14 12:24:18

简单地循环浏览内容怎么样?

function Find_Name (V : Vector; Name : String) return Contact_Type is
   Position : Cursor := V.First;
begin
   while Position /= No_Element loop
      if Element (Position).Name = Name then
         return Element (Position);
      end if;
      Next (Position);
   end loop;
   return Empty_Contact;
end Find_Name;

how about simply looping through the content?

function Find_Name (V : Vector; Name : String) return Contact_Type is
   Position : Cursor := V.First;
begin
   while Position /= No_Element loop
      if Element (Position).Name = Name then
         return Element (Position);
      end if;
      Next (Position);
   end loop;
   return Empty_Contact;
end Find_Name;
如何视而不见 2024-11-14 12:24:18

使用 迭代 并向其传递一个子程序来检查每个访问的记录。

package Container is new Containers.Vectors (Natural, Contact_Type);
List : Container.Vector;
...
procedure Visit (Position : Container.Cursor) is
    C : Contact_Type := Container.Element(Position);
begin
    -- examine C
end Visit;
...
List.Iterate (Visit'Access);

您可以使用类似的模式来构造排序键。另请参阅 A.18.16 数组排序。

附录:正如 @Simon Wright 评论的那样, < Containers.Vectors 中的 code>Generic_Sorting 是更好的选择。

Instead of Find_Index, use Iterate and pass it a subprogram that examines each visited record.

package Container is new Containers.Vectors (Natural, Contact_Type);
List : Container.Vector;
...
procedure Visit (Position : Container.Cursor) is
    C : Contact_Type := Container.Element(Position);
begin
    -- examine C
end Visit;
...
List.Iterate (Visit'Access);

You can use a similar pattern to construct your sort keys. See also A.18.16 Array Sorting.

Addendum: As @Simon Wright comments, Generic_Sorting in Containers.Vectors is a better choice.

被你宠の有点坏 2024-11-14 12:24:18

只要只有一个匹配元素,@oenone 的答案就可以。否则,你可以尝试类似的事情

function Find_Name (V : Vector; Name : String) return Vector is
   Position : Cursor := V.First;
   Result : Vector;
begin
   while Position /= No_Element loop
      if Element (Position).Name = Name then
         Append (Result, Element (Position));
      end if;
      Next (Position);
   end loop;
   return Result;
end Find_Name;

@oenone's answer is fine so long as there's only one matching element. Otherwise, you might try something like

function Find_Name (V : Vector; Name : String) return Vector is
   Position : Cursor := V.First;
   Result : Vector;
begin
   while Position /= No_Element loop
      if Element (Position).Name = Name then
         Append (Result, Element (Position));
      end if;
      Next (Position);
   end loop;
   return Result;
end Find_Name;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文