サーチ…
前書き
Adaの標準パッケージはすべての数値型の出力を提供します。出力形式はさまざまな方法で調整できます。
備考
汎用パッケージが数値型でインスタンス化されるたびに、どのように注意してください。また、インスタンス全体に設定されるデフォルトと、このパラメータでPutを呼び出すときにWidthをオーバーライドする方法の両方があります。
スペースを使って寛大に整数を出力する
Integer_IOインスタンスには、各出力番号が取る文字数である設定変数Default_Widthがあります。
with Ada.Text_IO; use Ada.Text_IO;
procedure Print_Integer is
subtype Count is Integer range -1_000_000 .. 1_000_000;
package Count_IO is new Integer_IO (Count);
X : Count;
begin
Count_IO.Default_Width := 12;
X := Count'First;
while X < Count'Last loop
Count_IO.Put (X);
Count_IO.Put (X + 1);
New_Line;
X := X + 500_000;
end loop;
end Print_Integer;
結果
-1000000
-500000
0
500000
16を使用して整数を出力する(16進数)
設定変数Default_Baseは、 Ada.Text_IO.Integer_IOのインスタンスに設定されます。また、出力が先頭のスペースを持つことができないようにDefault_Widthが設定されています。
with Ada.Text_IO; use Ada.Text_IO;
procedure Print_Hex is
subtype Count is Integer range -1_000_000 .. 1_000_000;
package Count_IO is new Integer_IO (Count);
X : Count;
begin
Count_IO.Default_Width := 1;
Count_IO.Default_Base := 16;
X := Count'First;
while X < Count'Last loop
Count_IO.Put (X);
New_Line;
X := X + 500_000;
end loop;
end Print_Hex;
結果
-16#F4240#
-16#7A120#
16#0#
16#7A120#
小数点固定小数点番号を印刷、つまりお金
Ada.Text_IO.Editingは、「ピクチャ文字列」を使用して10進の固定小数点値をフォーマットします。セパレータや通貨記号などに "魔法の"文字を使用した出力
with Ada.Text_IO.Editing; use Ada.Text_IO;
procedure Print_Value is
Max_Count : constant := 1_000_000;
type Fruit is (Banana, Orange, Pear);
subtype Count is Integer range -Max_Count .. +Max_Count;
type Money is delta 0.001 digits 10;
package Fruit_IO is new Enumeration_IO (Fruit);
package Money_IO is new Editing.Decimal_Output
(Money,
Default_Currency => "CHF",
Default_Separator => ''');
Inventory : constant array (Fruit) of Count :=
(Banana => +27_420,
Orange => +140_600,
Pear => -10_000);
Price_List : constant array (Fruit) of Money :=
(Banana => 0.07,
Orange => 0.085,
Pear => 0.21);
Format : constant Editing.Picture :=
Editing.To_Picture ("<###BZ_ZZZ_ZZ9.99>");
begin
Fruit_IO.Default_Width := 12;
for F in Inventory'Range loop
Fruit_IO.Put (F);
Put (" | ");
Money_IO.Put (Item => Inventory (F) * Price_List (F),
Pic => Format);
New_Line;
end loop;
end Print_Value;
結果
BANANA | CHF 1'919.40
ORANGE | CHF 11'951.00
PEAR | (CHF 2'100.00)
1行に複数の項目を印刷する
_IOパッケージのインスタンスを結合し、右側に数値型を使用します。
with Ada.Text_IO; use Ada.Text_IO;
procedure Print_Inventory is
type Fruit is (Banana, Orange, Pear);
subtype Count is Integer range -1_000_000 .. 1_000_000;
package Fruit_IO is new Enumeration_IO (Fruit);
package Count_IO is new Integer_IO (Count);
Inventory : constant array (Fruit) of Count :=
(Banana => 27_420,
Orange => 140_600,
Pear => -10_000);
begin
Fruit_IO.Default_Width := 12;
for F in Inventory'Range loop
Fruit_IO.Put (F);
Put (" | ");
Count_IO.Put (Inventory (F));
New_Line;
end loop;
end Print_Inventory;
結果
BANANA | 27420
ORANGE | 140600
PEAR | -10000
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow