日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Ada语言(gnat)hello world

發布時間:2024/3/24 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ada语言(gnat)hello world 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

hello_world.adb

-- compile Instruction; -- $ gcc -c hello_world.adb -- $ gnatbind hello_world.ali -- $ gnatlink hello_world.aliwith Text_IO; procedure Hello_World isbeginText_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!

字符串類型及其簡單處理:

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 ispackage CL renames Ada.Command_Line; -- shorten the package namepackage 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 32fixed_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 timesfunction duplicate_string(str: string; occurs : integer) return string isresult : string := occurs * str;beginreturn result;end duplicate_string;-- create a string 'dynmically' by returning a string of any length -- specified at run timefunction produce_blank_string(length : integer) return string isresult : string(1..length) := length * ' ';beginreturn result;end produce_blank_string;procedure print_string (str : string) isbeginput_line(str);end print_string;procedure print_string (bounded_str : bounded_string) isbeginput_line(To_String(bounded_str));end print_string;procedure print_string (unbounded_str : unbounded_string) isbeginput_line(To_String(unbounded_str));end print_string;beginprint_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;

編譯運行:

$ 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 testrangewith Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;procedure testrange isVar : Integer;beginPut ("Enter an Integer number to confirm its range:");Get (Var);if Var in Integer'First .. -1 thenPut_Line ("It is a negative number");elsif Var in 1 .. Integer'Last thenPut_Line ("It is a positive number");elsePut_Line ("It is 0");end if; end testrange;

?

轉載于:https://my.oschina.net/u/2245781/blog/882700

總結

以上是生活随笔為你收集整理的Ada语言(gnat)hello world的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。