Ada语言(gnat)hello world

hello_world.adblinux

-- compile Instruction; 
-- $ gcc -c hello_world.adb 
-- $ gnatbind hello_world.ali
-- $ gnatlink hello_world.ali

with Text_IO;
procedure Hello_World is
  begin
	Text_IO.Put_line("Hello World!");
  end Hello_World;

编译运行:数组

$ gnatmake hello_world
gcc-6 -c hello_world.adb
gnatbind-6 -x hello_world.ali
gnatlink-6 hello_world.ali
$ ./hello_world 
Hello World!函数

字符串类型及其简单处理:code

ADA语言内建的字符串类型是定长类型,基本接近至关于C的静态字符数组。对ADA而言,String也彻底是经过字符数组的严格定义派生出来的(可参见wikibooks关于ADA类型系统的条目;关于ADA的复杂的类型系统须要另行撰文)。定长字符类型对应的操做包在Ada.Strings.Fixed中。另外String类型也有不少这个类型的Attribute。这类字符串一般在编译期决定长度(由其界限参数指定或所附值决定);但对ADA而言,这种长度肯定也能够宽松一点:字符串和全部数据变量实例同样都定义在变量定义区,它能够不限定长度,而由一个函数返回的字符串肯定;另外一方面,对于一个子程序变量定义区的字符串,它能够用一个数值变量为其在运行时配置长度,这有点像扩展后C语言中的栈空间分配同样(其实背后运行机理也是这样,而这个特性也适用于ADA的其余如数组Array上)。而在字符串做为参数传给子程序的时候,这个形参的类型必须定义成string,而不含长度节点,这个也很容易理解。至此,对于通常的字符串处理,充分利用这种栈空间分配的特性,合理建立字符变量,这种定长字符串类型已经足够了。
另外ADA的类库也提供了字符串类型,至关因而一种字符串对象了,主要有两种,一种是Bounded(有界)类型,另外一种是Unbounded(无界)类型。Bounded类型的使用须要实例化定义它的一个泛型包,而这个泛型包的参数只是一个数值,用于指定这个包提供的Bounded字符串的最大长度,这样估计包里的功能根据这个长度进行一些存储准备或策略配置等,而这个包产生每一个字符串实例的长度也就不能超过这个界限。而无界类型是不须要作这个指定而字符串也能无限增加。
ADA字符串操做符除了已经见到的乘法用于复制串之外,对字符串链接则用&,而不是通常语言中用的+(固然用户能够去改,但这不符合ADA的一般实践)。须要注意的是,若是链接两个定长字符类型变量,若是赋值给一个定长类型变量,这个变量必须正正好好和这两个链接起来的字符串的长度相等,这个比较麻烦,但这是没办法的;除了链接外,ADA还提供了一些字符函数的插入替换操做等(Replace_Slice, Overwrite, Insert等),也是比较方便的。ADA的定长类型求子字符串只须要在数组下标中指定便可;但若是在字符串对象上进行的话,因为ADA不支持数组下标的运算符重载,因此必须使用函数如Slice/Replace_Slice来提取设置子串或Element/Replace_Element来提取设置单个字符。而string,Bounded_String和Unbounded_String之间转换目前彷佛只提供了string和后二者之间的互转,对于这些ADA库可能还有待更新扩展。对象

-- $ gnatmake adastrings 

with Ada.Text_IO;

use Ada.Text_IO;

 

with Ada.Strings.Fixed;

use Ada.Strings.Fixed;

 

with Ada.Strings.Bounded;

with Ada.Strings.Unbounded;

 

with Ada.Command_Line;

 

procedure adastrings is

  

  package CL renames Ada.Command_Line; -- shorten the package name

  package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length(Max => 128);

  use Bounded;

  use Ada.Strings.Unbounded;

 

  fixed_str_without_sizespec : string := "size determined by designated value";

  fixed_str_from_cmd_line : string := CL.Argument(1);

  fixed_str : string(1..32) := 32 * "0"; -- has to be 32

  fixed_str2 : string(1..32) := 32 * "1";

  fixed_str3 : string(1..64);

  bounded_str : Bounded_String;

  unbounded_str : UnBounded_String;

    

  -- create a string 'dynamically' by duplicating a string given times

  function duplicate_string(str: string; occurs : integer) return string is

    result : string := occurs * str;

  begin

    return result;

  end duplicate_string;

  

  -- create a string 'dynmically' by returning a string of any length 

  -- specified at run time

  function produce_blank_string(length : integer) return string is

    result : string(1..length) := length * ' ';

  begin

    return result;

  end produce_blank_string;

 

  procedure print_string (str : string) is

  begin

    put_line(str);

  end print_string;

 

  procedure print_string (bounded_str : bounded_string) is

  begin

    put_line(To_String(bounded_str));

  end print_string;

 

  procedure print_string (unbounded_str : unbounded_string) is

  begin

    put_line(To_String(unbounded_str));

  end print_string;

 

begin

  print_string("demo program for ada string types");

  print_string(fixed_str);

 

  fixed_str3 := fixed_str2 & fixed_str;

 

  print_string(fixed_str3);

 

  print_string(fixed_str2(1..16));

 

  fixed_str := Overwrite(fixed_str, 17, fixed_str2(1..16));

 

  print_string(fixed_str);

  print_string(fixed_str_without_sizespec);

  print_string(fixed_str_from_cmd_line);

 

  bounded_str := bounded_str & fixed_str;

 

  print_string(bounded_str);

 

  unbounded_str := To_Unbounded_String(To_String(bounded_str));

  unbounded_str := unbounded_str & "123";

 

  print_string(unbounded_str);

 

  Replace_Slice(unbounded_str, 3, 10, "replaced");

  print_string(unbounded_str);

 

  print_string(duplicate_string("Sample", 5));

  print_string(produce_blank_string(12));

  

end adastrings;

编译运行:ci

$ gnatmake adastrings
x86_64-linux-gnu-gcc-7 -c adastrings.adb
x86_64-linux-gnu-gnatbind-7 -x adastrings.ali
x86_64-linux-gnu-gnatlink-7 adastrings.ali
$ ./adastrings argument1
demo program for ada string types
00000000000000000000000000000000
1111111111111111111111111111111100000000000000000000000000000000
1111111111111111
00000000000000001111111111111111
size determined by designated value
argument1
00000000000000001111111111111111
00000000000000001111111111111111123
00replaced0000001111111111111111123
SampleSampleSampleSampleSample

IF语句:字符串

-- filename: ifInteger.adb
-- gnatmake testrange


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

procedure testrange is
      Var : Integer;

begin
      Put ("Enter an Integer number to confirm its range:");
      Get (Var);

     if Var in Integer'First .. -1 then
            Put_Line ("It is a negative number");
     elsif Var in 1 .. Integer'Last then
           Put_Line ("It is a positive number");
     else
            Put_Line ("It is 0");
     end if;
end testrange;
相关文章
相关标签/搜索