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;
相關文章
相關標籤/搜索