comp.lang.ada
 help / color / mirror / Atom feed
* Stopwatch project
@ 1999-04-24  0:00 Brian A Crawford
  1999-04-25  0:00 ` Matthew Heaney
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Brian A Crawford @ 1999-04-24  0:00 UTC (permalink / raw)


I am a beginner attempting to teach myself from a number of Ada books and
lurking on this newsgroup.
I am trying to write a programme that will make the computer act as a
stopwatch with lap/split time capabilities.
I propose to start the programme running then use a get_immediate e.g. 'S'
for start then a number of
get_immediates e.g. 'L'  for each lap then get 'T' for stop and 'R' for
reset.
I find very little in the way of detailed examples in the books I have; in
fact very few mention get_immediate or real time programming; I have also
seen the amount of discussion on get_immediate in this newsgroup and realise
that neither topic is beginner level.
Are there any working examples of this concept on-line?

I have found on page 914-915 of 'Ada as a Second Language'  a package called
Stopwatch but find no example on how to use it (and the fact that it is on
page 914 is not lost on a beginner :-) ).

Any help would be extremely greatly appreciated.
Thanks
Brian
brianc@billybob.demon.co.uk

This is the programme taken from the book
----------------------------------------------------------------------------
---------------------------------------------------------
   PACKAGE stopwatch IS
      PROCEDURE reset;
      PROCEDURE start;
      PROCEDURE stop ;
      FUNCTION total_time RETURN duration;
      started_error : EXCEPTION;
   END stopwatch;

   WITH ada.Calender; USE ada.calendar;
   PACKAGE BODY stopwatch IS
      no_time: CONSTANT time := time_of (year => 1901, month => 1, day => 1,
seconds => 0.0 );
      time_started : time := no_time;
      total : duration := 0.0;

      PROCEDURE reset  IS

      BEGIN
         time_started := no_time;
         total := 0.0;

      END reset;

      PROCEDURE start IS

      BEGIN
         IF time_started = no_time THEN
            time_started :=  clock;
         ELSE RAISE started_error;
         END IF;
      END start;
      PROCEDURE stop IS

      BEGIN
         IF time_started =no_time THEN
            RAISE started_error;
         ELSE
            total := total + (clock - time_started);
            time_started := no_time;
         END IF;
      END stop;

      FUNCTION total_time RETURN duration IS
      BEGIN
         IF time_started = no_time THEN
            RETURN total;
         ELSE
            RAISE started_error;
         END IF ;
      END total_time;
   END stopwatch;

----------------------------------------------------------------------------
------------------------------------------------------------






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

* Re: Stopwatch project
  1999-04-24  0:00 Stopwatch project Brian A Crawford
@ 1999-04-25  0:00 ` Matthew Heaney
  1999-04-25  0:00 ` Matthew Heaney
  1999-04-26  0:00 ` Jeffrey D. Cherry
  2 siblings, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1999-04-25  0:00 UTC (permalink / raw)


"Brian A Crawford" <brianc@billybob.demon.co.uk> writes:

> I am trying to write a programme that will make the computer act as a
> stopwatch with lap/split time capabilities.
>
> I propose to start the programme running then use a get_immediate
> e.g. 'S' for start then a number of get_immediates e.g. 'L' for each
> lap then get 'T' for stop and 'R' for reset.

Why do you want to use Get_Immediate?  What's wrong with Get?





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

* Re: Stopwatch project
  1999-04-24  0:00 Stopwatch project Brian A Crawford
  1999-04-25  0:00 ` Matthew Heaney
@ 1999-04-25  0:00 ` Matthew Heaney
  1999-04-26  0:00 ` Jeffrey D. Cherry
  2 siblings, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1999-04-25  0:00 UTC (permalink / raw)


"Brian A Crawford" <brianc@billybob.demon.co.uk> writes:

> I have found on page 914-915 of 'Ada as a Second Language'  a package called
> Stopwatch but find no example on how to use it (and the fact that it is on
> page 914 is not lost on a beginner :-) ).
> 
> Any help would be extremely greatly appreciated.

Here is a test driver.  It does something similar to what you want, but
without using Get_Immediate.

My run-time is broken, so when I run this program, it dumps core.  Try
it yourself and see if it works.

Matt
<mailto:matthew_heaney@acm.org>



--STX
with Stopwatch;
with Ada.Text_IO; use Ada.Text_IO;

procedure Test_Stopwatch is

   C : Character;

begin

   Main:
   loop

      Get (C);

      case C is
         when 'r' | 'R' =>
            Stopwatch.Reset;
            Put_Line ("Stopwatch has been reset.");

         when ' ' =>
            Stopwatch.Start;
            Put_Line ("Stopwatch has been started.");

         when 's' | 'S' =>
            Stopwatch.Stop;
            Put_Line
              ("Stopwatch stopped; total time is " &
               Duration'Image (Stopwatch.Total_Time) &
               ".");

         when 'x' | 'X' =>
            exit Main;

         when others =>
            Put_Line ("Bad input.");

      end case;

   end loop Main;

end Test_Stopwatch;




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

* Re: Stopwatch project
  1999-04-24  0:00 Stopwatch project Brian A Crawford
  1999-04-25  0:00 ` Matthew Heaney
  1999-04-25  0:00 ` Matthew Heaney
@ 1999-04-26  0:00 ` Jeffrey D. Cherry
  2 siblings, 0 replies; 4+ messages in thread
From: Jeffrey D. Cherry @ 1999-04-26  0:00 UTC (permalink / raw)


Mr. Heaney's test driver does indeed work using GNAT 3.11p under Win95. 
To demonstrate the differences between Get and Get_Immediate, I've
copied Mr. Heaney's test driver and replaced the appropriate procedure
call.  The result is included below.  Try both programs on your system
and observe the difference in behavior.  On my system the behavior is as
follows:
   For the Get version -- The user must press a letter command and then
the 
                          <enter> key before the test driver responds to
the
                          input.  The letter key pressed is echoed to
the 
                          display as is the newline sequence in response
to
                          the <enter> key.
   For the Get_Immediate version -- The test driver responds to a letter
                          command as soon as the key is pressed.  The
letter
                          is not echoed to the display.

Note that both versions have advantages and disadvantages.  You should
perform additional testing on your system to determine which approach
will meet your requirements.  I would recommend you test both drivers
with multiple character inputs.  For example, type in several letter
commands before pressing the <enter> key for the Get version, and enter
the same keys for the Get_Immediate version.  Try invalid keys, function
keys, and redirected input.  Note the behavior in each case.  This
should assist in choosing the most appropriate implementation.

Regards,
Jeffrey D. Cherry
Logicon Geodynamics

-----
with Stopwatch;
with Ada.Text_IO; use Ada.Text_IO;

procedure Test_Stopwatch is
   C : character;
begin -- Test_Stopwatch
   loop
      Get_Immediate(C);
      case C is
         when 'r' | 'R' =>
            Stopwatch.Reset;
            Put_Line ("Stopwatch has been reset.");
         when ' ' =>
            Stopwatch.Start;
            Put_Line ("Stopwatch has been started.");
         when 's' | 'S' =>
            Stopwatch.Stop;
            Put_Line("Stopwatch stopped; total time is " &
               duration'image(Stopwatch.Total_Time) & ".");
         when 'x' | 'X' =>
            exit;
         when others =>
            null;
      end case;
   end loop;
end Test_Stopwatch;
-----




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

end of thread, other threads:[~1999-04-26  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-04-24  0:00 Stopwatch project Brian A Crawford
1999-04-25  0:00 ` Matthew Heaney
1999-04-25  0:00 ` Matthew Heaney
1999-04-26  0:00 ` Jeffrey D. Cherry

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