比较 Ada 中的无限字符串

Comparing unbounded strings in Ada

你好,我面临着在 Ada 中比较两个无界字符串并使用名称(字符串)作为键以排序方式排列数据值集的困境。我完全不知道如何比较 ada 中的两个字符串,或者更准确地说是确定哪个字符串按升序排在第一位。

    with Ada.Text_IO;
    with Ada.Integer_Text_IO;
    with Ada.Unchecked_Deallocation;
    with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
    with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;

    procedure Main is
       package TIO renames Ada.Text_IO;
       package IIO renames Ada.Integer_Text_IO;
       Name_Main1, Name_Main2, Name_Main3 : Ada.Strings.Unbounded.Unbounded_String;
    begin
       TIO.Put_Line("Enter the Name of the student :");
       Ada.Text_IO.Unbounded_IO.Get_Line(Name_Main1);
       TIO.Put_Line("Enter the Name of the student :");
       Ada.Text_IO.Unbounded_IO.Get_Line(Name_Main2);
       TIO.Put_Line("Enter the Name of the student :");
       Ada.Text_IO.Unbounded_IO.Get_Line(Name_Main3);

       if Name_Main1 > Name_Main2 then
          TIO.Put_Line("Some semblance");TIO.New_Line;
       else
          TIO.Put_Line("Cant be matched");
       end if;

    end Main; 

更多这些字符串中的大多数长度不同,这对任何方式都没有帮助。我有 C 背景,所以正在寻找 strcmp 类的功能。 如果可以的话请帮忙。网上关于 Ada 的这一重要功能的文档很少或没有。

Ada 参考手册

对于Unbounded_String:

http://www.adaic.org/resources/add_content/standards/12rm/html/RM-A-4-5.html

83 每个函数“=”、“<”、“>”、“<=”和“>=”returns 与应用于由 Left 和表示的字符串值的相应字符串操作相同的结果对。

对于字符串文字:

http://www.adaic.org/resources/add_content/standards/12rm/html/RM-3-6-3.html

5 为所有字符串类型定义了 54 个字符串文字(参见 2.6 和 4.2)。串联运算符 & 是为字符串类型预定义的,对于所有非限制的一维数组类型也是如此。排序运算符 <、<=、> 和 >= 是为字符串类型预定义的,就像所有一维离散数组类型一样;这些排序运算符对应于词典顺序(见 4.5.2)。

对于一维数组类型:

http://www.adaic.org/resources/add_content/standards/12rm/html/RM-4-5-2.html

26/3 对于离散数组类型,预定义排序运算符使用组件类型的预定义顺序关系对应于字典顺序:空数组在字典顺序上小于具有至少一个组件的任何数组。在非空数组的情况下,如果左操作数的第一个分量小于右操作数的第一个分量,则左操作数按字典顺序小于右操作数;否则,仅当左操作数的第一个组件相等并且左操作数的尾部在字典序上小于右操作数的尾部(尾部由第一个之后的剩余组件组成并且可以为空)时,左操作数在字典顺序上小于右操作数.

示例:

37

"" < "A" 和 "A" < "Aa" -- 正确

"Aa" < "B" 和 "A" < "A " -- 正确

如果需要对Unbounded_String进行排序,可以使用Containers.Generic_Array_Sort:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
with Ada.Containers.Generic_Array_Sort;

procedure Main is
   Name_Main1, Name_Main2, Name_Main3 : Unbounded_String :=
     Null_Unbounded_String;
begin
   Put_Line ("Enter the Name of the student :");
   Get_Line (Name_Main1);
   Put_Line ("Enter the Name of the student :");
   Get_Line (Name_Main2);
   Put_Line ("Enter the Name of the student :");
   Get_Line (Name_Main3);

   declare
      --  Array of Unbounded_Strings
      type Unbarr is array (Positive range <>) of Unbounded_String;

      --  Sort procedure
      procedure Unbsort is new Ada.Containers.Generic_Array_Sort
        (Positive, Unbounded_String, Unbarr, "<");

      --  Array of Unbounded_Strings
      Z : Unbarr := Name_Main1 & Name_Main2 & Name_Main3;
   begin
      --  Sort array
      Unbsort (Z);
      --  Output sorted array
      Put_Line ("Sorted:");
      for X of Z loop
         Put_Line (X);
      end loop;
   end;

end Main;.