खोज…


परिचय

Ada के मानक पैकेज सभी संख्यात्मक प्रकारों के आउटपुट के लिए प्रदान करते हैं। आउटपुट के प्रारूप को कई तरीकों से समायोजित किया जा सकता है।

टिप्पणियों

ध्यान दें कि हर बार एक सामान्य पैकेज एक संख्यात्मक प्रकार के साथ त्वरित कैसे होता है। इसके अलावा, दोनों उदाहरण पूरे उदाहरण के लिए सेट करने के लिए हैं, और Width को ओवरराइड करने के तरीके भी हैं, कहते हैं, जब Put को इस पैरामीटर के साथ बुलाते हैं।

प्रिंट पूर्णांक, उदारता से अंतरिक्ष का उपयोग कर

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 (हेक्साडेसिमल) का उपयोग कर प्रिंट इंटेगर,

एक सेटिंग चर 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 "चित्र स्ट्रिंग" का उपयोग करके दशमलव निश्चित बिंदु मानों को स्वरूपित करता है। ये विभाजकों, मुद्रा संकेतों आदि के लिए "जादुई" वर्णों का उपयोग करते हुए आउटपुट का वर्णन करते हैं।

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 पैकेज के उदाहरणों को _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