수색…


소개

Ada의 표준 패키지는 모든 숫자 유형의 출력을 제공합니다. 출력 형식은 다양한 방법으로 조정할 수 있습니다.

비고

제네릭 패키지가 숫자 유형으로 인스턴스화 될 때마다 또한 전체 인스턴스에 대해 기본값을 설정할 수 있으며 Put 매개 변수로 호출 할 때 Width 를 재정의하는 방법도 있습니다.

공간을 사용하여 정수를 인쇄하십시오.

Integer_IO 인스턴스에는 각 출력 x 호가 취할 문자 수인 설정 변수 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_BaseAda.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)

한 줄에 여러 항목 인쇄

_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