comp.lang.ada
 help / color / mirror / Atom feed
* My first solution
@ 2011-06-15  6:01 juanmiuk
  2011-06-15  6:09 ` juanmiuk
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: juanmiuk @ 2011-06-15  6:01 UTC (permalink / raw)


with Ada.Strings.Fixed;
use  Ada.Strings.Fixed;

procedure Test_2 is

     subtype My_Num is Integer range 1 .. 10;

     function Format_Num ( Width_Num  : Integer;
                                       The_Number : Integer ) return
String is

        Temp_Length : My_Num := My_Num'Image(The_Number)'Length;
        Separador      : String := (Width_Num - Temp_Length) * ' ';

        begin
             return (Separador & My_Num'Image(The_Number));
        end Format_Num;

begin
     for I in My_Num range 1 .. 10 loop
        declare
            Output : String := Format_Num(My_Num'Width, I);
        begin
            Text_IO.Put_Line(Output);
        end;
    end loop;
end Test_2;



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: My first solution
  2011-06-15  6:01 My first solution juanmiuk
@ 2011-06-15  6:09 ` juanmiuk
  2011-06-15  6:39 ` Ludovic Brenta
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: juanmiuk @ 2011-06-15  6:09 UTC (permalink / raw)


I want to thanks for the comments done.

Juan Miguel




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: My first solution
  2011-06-15  6:01 My first solution juanmiuk
  2011-06-15  6:09 ` juanmiuk
@ 2011-06-15  6:39 ` Ludovic Brenta
  2011-06-15 18:39 ` onox
  2011-06-15 19:43 ` Jeffrey Carter
  3 siblings, 0 replies; 5+ messages in thread
From: Ludovic Brenta @ 2011-06-15  6:39 UTC (permalink / raw)


juanmiuk <juanmiuk@googlemail.com> writes:
> with Ada.Strings.Fixed;
> use  Ada.Strings.Fixed;
>
> procedure Test_2 is
>
>      subtype My_Num is Integer range 1 .. 10;
>
>      function Format_Num ( Width_Num  : Integer;
>                                        The_Number : Integer ) return
> String is

OK, now you can create a procedure with 3 integer parameters that writes
all 3 parameters to the console.  Inside this procedure, you can use

procedure Ada.Integer_Text_IO.Put(Item  : in Num;
                                  Width : in Field := Default_Width;
                                  Base  : in Number_Base := Default_Base);

defined in A.10.8(11).

-- 
Ludovic Brenta.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: My first solution
  2011-06-15  6:01 My first solution juanmiuk
  2011-06-15  6:09 ` juanmiuk
  2011-06-15  6:39 ` Ludovic Brenta
@ 2011-06-15 18:39 ` onox
  2011-06-15 19:43 ` Jeffrey Carter
  3 siblings, 0 replies; 5+ messages in thread
From: onox @ 2011-06-15 18:39 UTC (permalink / raw)


On Jun 15, 8:01 am, juanmiuk <juanm...@googlemail.com> wrote:
>      for I in My_Num range 1 .. 10 loop

Assuming you want to iterate over the whole range of My_Num, you can
write instead:

for I in My_Num loop

Following also seems to work:

for I in My_Num'Range loop



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: My first solution
  2011-06-15  6:01 My first solution juanmiuk
                   ` (2 preceding siblings ...)
  2011-06-15 18:39 ` onox
@ 2011-06-15 19:43 ` Jeffrey Carter
  3 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2011-06-15 19:43 UTC (permalink / raw)


On 06/14/2011 11:01 PM, juanmiuk wrote:
>
>       function Format_Num ( Width_Num  : Integer;

What does it mean to call this function with Width_Num not in Positive? Using an 
appropriate subtype in cases like this helps document how the subprogram should 
be used.

>                                         The_Number : Integer ) return
> String is
>
>          Temp_Length : My_Num := My_Num'Image(The_Number)'Length;
>          Separador      : String := (Width_Num - Temp_Length) * ' ';

These are both never assigned to, so they can be declared as constants. While 
this can assist the compiler in optimization, its primary benefit is letting the 
reader know you never change these.

Note that the Left parameter of Ada.Strings.Fixed."*" is subtype Natural. This 
will raise Constraint_Error if Width_Num - Temp_Length < 0. This helps define 
what subtype Width_Num should be, indicates some comments are probably needed to 
describe a precondition [Width_Num >= My_Num'Image (The_Number)'Length], and 
perhaps points to some ways the function can be improved [if Width_Num < 
My_Num'Image (The_Number)'Length, return My_Num'Image (The_Number)?]

"Separador" would probably be named "Padding" in English.

>       for I in My_Num range 1 .. 10 loop

for I in My_Num loop

What you have done here is effectively duplicate the functionality of 
Ada.Text_IO.Integer_IO.Put.

-- 
Jeff Carter
"Spam! Spam! Spam! Spam! Spam! Spam! Spam! Spam!"
Monty Python's Flying Circus
53



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-06-15 19:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-15  6:01 My first solution juanmiuk
2011-06-15  6:09 ` juanmiuk
2011-06-15  6:39 ` Ludovic Brenta
2011-06-15 18:39 ` onox
2011-06-15 19:43 ` Jeffrey Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox