サーチ…


前書き

サブタイプ属性'Image'Valueはそれぞれスカラー値と文字列をとり、それぞれ文字列とスカラー値を返します。 'Imageの結果は'Image元の値を取得するため'Valueに入力できます。逆も真です。

__Scalar_Object__'Image属性は、Ada 2012-TC-1以降のオブジェクトで直接使用できます。

構文

  • 関数Scalar'Image(引数:Scalar'Base)は文字列を返します。
  • 関数Discrete'Image(引数:Discrete'Base)戻り文字列;
  • function Integer'Image(引数:Integer'Base)戻り値文字列。
  • function Enumeration'Image(Argument:Enumeration'Base)戻り値文字列です。
  • 関数Real'Image(引数:Real'Base)は文字列を返します。
  • 関数Numeric'Image(引数:Numeric'Base)戻り文字列;
  • 関数Scalar'Value(引数:String)はスカラベースを返します。
  • 関数Discrete'Value(引数:文字列)return Discrete'Base;
  • function Integer'Value(引数:文字列)戻り値Integer'Base;
  • function Enumeration'Value(引数:String)戻り値Enumeration'Base;
  • 関数Real'Value(引数:String)はReal'Baseを返します。
  • 関数Scalar_Object 'イメージ戻り文字列;

備考

ことに注意してください'Imageために必要ないくつかのグラフィック文字とき、すなわち、実装定義された結果(RM 3.5)を招くことができますString結果がで定義されていないCharacter 。大きなレパートリー考えてみましょう'Wide_Image'Wide_Wide_Image

Ada 2012(TC-1)

__Scalar_Object__'Image属性をオブジェクトに直接使用する権限がAda 2012-TC-1(2016年4月)に追加されました。

Image属性を使用してfloatを出力する

Ada 2012(TC-1)
with Ada.Text_IO;

procedure Main is
   type Some_Float digits 8 range 0.0 .. 10.0;
   X : Some_Float := 2.71;
begin
   Ada.Text_IO.Put_Line (X'Image);
end Main;

結果

2.71000E+00

Image属性を使用して整数を出力する

Ada 2012(TC-1)
with Ada.Text_IO;

procedure Main is
   type Some_Integer is range -42 .. 42;
   X : Some_Integer := 17;
begin
   Ada.Text_IO.Put_Line (X'Image);
end Main;

結果

17

Image属性を使用して列挙を出力する

Ada 2012(TC-1)
with Ada.Text_IO;

procedure Main is
   type Fruit is (Banana, Orange, Pear);
   X : Fruit := Orange;
begin
   Ada.Text_IO.Put_Line (X'Image);
   Ada.Text_IO.Put_Line (Pear'Image);
end Main;

結果

ORANGE
PEAR

属性Imageを使用して列挙を出力する

with Ada.Text_IO;

procedure Main is
   type Fruit is (Banana, Orange, Pear);
   X : Fruit := Orange;
begin
   Ada.Text_IO.Put_Line (Fruit'Image (X));
end Main;

結果

ORANGE

属性Imageを使用して整数を出力する

with Ada.Text_IO;

procedure Main is
   X : Integer := 17;
begin
   Ada.Text_IO.Put_Line (Integer'Image (X));
end Main;

結果

17

属性Imageを使用してフロートをプリントアウトする

with Ada.Text_IO;

procedure Main is
   X : Float := 2.71;
begin
   Ada.Text_IO.Put_Line (Float'Image (X));
end Main;

結果

2.71000E+00

インバースとして

with Ada.Text_IO;

procedure Image_And_Value is
   type Fruit is (Banana, Orange, Pear);
   X  : Fruit := Orange;
begin
   Ada.Text_IO.Put_Line (Boolean'Image
      (Fruit'Value (Fruit'Image (X)) = X
          and
       Fruit'Image (Fruit'Value ("ORANGE")) = "ORANGE"));
end Image_And_Value;

結果

TRUE


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow