How To Make A Stopwatch Program In Pascal

Table of contents:

How To Make A Stopwatch Program In Pascal
How To Make A Stopwatch Program In Pascal

Video: How To Make A Stopwatch Program In Pascal

Video: How To Make A Stopwatch Program In Pascal
Video: Pascal - Timer programming 2024, May
Anonim
How to make a stopwatch program in Pascal
How to make a stopwatch program in Pascal

Necessary

PascalABC. NET or PascalTurbo programming environment

Instructions

Step 1

So, first, let's open the program. Let's create a new file and specify the plugins. In this case, we need a module for working with the console - CRT.

for this we will write:

uses

CRT;

Step 2

Let us indicate the variables i, s, m - variables of the real type.

var

i, s, m: Real;

Step 3

To start the program, write:

begin

And we will indicate the title of the console window:

SetWindowTitle ('Stopwatch');

Step 4

The TextColor procedure assigns a color to the text, and the Write statement displays the text to the screen:

TextColor (LightGreen);

WriteLn ('Press Enter to start stopwatch');

WriteLn ('Press again to stop');

Write ('Click again to start over');

The ending Ln moves to the next line.

Step 5

The ReadLn operator enters values from the keyboard, but in this case it just waits for the user to hit Enter:

ReadLn;

Step 6

We make an endless loop:

while (true) do

begin

The while (true) do begin construct translates as: While (condition) do (). Why is begin here?

In this case, we need a compound operator, which means that while the condition is true, several operators are executed. If it were not for begin, then after the While, only one statement was executed, which would lead to incorrect program operation. To end the While statement at the end, we write end.

Step 7

Now let's reset the counter:

i: = 0;

Step 8

The following statement translates as: until the do () key is pressed.

while not keypressed do

begin

The ClrScr operator clears the screen:

ClrScr;

Step 9

We set the condition: if the seconds are more than 60 and less than 3600 (this is necessary so that when the time is more than an hour, the program prints only what is after the third if) then:

if (i> 60) and (i <3600) then begin

The variable m (minutes) is equal to: seconds divided by 60 and rounded to the front.

m: = Int (i / 60);

And the variable s (seconds without minutes) is equal to: all seconds minus minutes multiplied by 60.

s: = i - m * 60;

Step 10

The Write statement writes how many minutes and seconds have passed, and the end statement ends the work of the begin following the if condition:

Write (m, 'minutes (s) and', s: 1: 2, 'seconds (s)')

end;

Function: 1: 2 means that seconds should be written with two decimal places.

Step 11

If the seconds are less than 60, then just write how many seconds have passed with two decimal places:

if i <60 then

Write ('', i: 1: 2, 'seconds (a / s)');

Step 12

If the seconds are more than 3600 (that is, more than an hour) then:

if i> 3600 then begin

m: = Int (i / 60);

s: = i - m * 60;

ch: = Int (m / 60);

m: = m - ch * 60;

Write (ch, 'hour (s)', m, 'minutes (s) and', s: 1: 2, 'seconds (s)');

end;

Step 13

So, the program wrote that 0 seconds have passed, now it increases the counter i by 10 milliseconds, and since the program does everything instantly, we make a delay for the same time:

i: = i + 0.01;

Delay (10);

Next, we end for the While (not keypressed) statement:

end;

If the user pressed the Enter key, then the program waits for him to press it again to start the stopwatch again:

Readln;

Readln;

It is no coincidence that we set the counter to zero after the While (true) statement, because when the user presses Enter a second time, the program will start from there, reset the counter and start counting again.

Next, we end the While and for the entire program:

end;

Step 14

Here is the complete program:

uses

CRT;

var

i: Real;

s: Real;

m: Real;

ch: Real;

begin

SetWindowTitle ('Stopwatch');

TextColor (LightGreen);

WriteLn ('Press Enter to start stopwatch');

WriteLn ('Press again to stop');

Write ('Click again to start over');

ReadLn;

while (true) do

begin

i: = 0;

while not keypressed do

begin

ClrScr;

if (i> 60) and (i <3600) then begin

m: = Int (i / 60);

s: = i - m * 60;

Write (m, 'minutes (s) and', s: 1: 2, 'seconds (s)')

end;

if i <60 then

Write ('', i: 1: 2, 'seconds (a / s)');

if i> 3600 then begin

m: = Int (i / 60);

s: = i - m * 60;

ch: = Int (m / 60);

m: = m - ch * 60;

Write (ch, 'hour (s)', m, 'minutes (s) and', s: 1: 2, 'seconds (s)');

end;

i: = i + 0.01;

Delay (10);

end;

Readln;

Readln;

end;

end.

Image
Image

Step 15

It turned out not very nice, but the program works correctly!

Recommended: