Additional types and procedures
The types and procedures described on this page will be available in the program
if the PT4 unit is included with the uses clause. Since version 4.15,
Free Pascal Lazarus uses PT4Tasks as its unit name, and PT4 is the name reserved for the PascalABC.NET unit.
This section describes the common features for all supported Pascal implementations (including PascalABC.NET).
Task initialization, data input-output
procedure Task(Name: string);
This procedure initializes a task named Name. It must be called at the beginning of the program.
If the source code does not contain the Task procedure call then the program output the error
message "The Task procedure is not called".
A task name must contain a name of a task group and an
order number (for example, 'Begin3' ).
If the group name is invalid then the program output the error message "Invalid task group".
If the task number is invalid then the program output
the error message with information about the range of the available task numbers for this group.
The symbol "?" placed after the task name (for example, 'Begin3?' )
means that the program will be run in the demo mode.
Starting from the version 4.8, the Task procedure may be also used for creating and displaying an html-page
containing a description of a task or a task group.
In this case the name of the required task or the task group ended by the "#" symbol must be used for the Name parameter,
for example, 'Begin3#' or 'Begin#' .
Starting from the version 4.13, the Programming Taskbook provides a program testing
on several sets of input data during a single running of the program. You should place
the sumbol "!" after the task name (for example, 'Begin3!' ) in order to switch off this new feature.
All subsequent calls of the Task procedure are ignored with the following exception:
if several calls are used for creation of an html-page containing a description of various tasks
or task groups then all these calls will be taken into account.
Starting from the version 4.12, the Name parameter may include the "_en" or "_ru" suffix that determines
the interface language (English or Russian respectively) for Programming Taskbook window and the task itself.
The "?", "#", and "!" symbols should be placed before the suffix, for example,
'Begin3#_en' .
If several calls of the Task procedure are used (for creation of an html-page)
then only the suffix of the first call will be taken into account.
If the suffix is absent then the default language is applied (the default language may be changed by means
of a pop-up menu of the PT4Load tool; in the PascalABC.NET IDE the default language is determined by the
IDE option "Language"see "Tools | Options" menu command).
procedure GetB(var A: boolean);
procedure GetN(var A: integer);
procedure GetR(var A: real);
procedure GetC(var A: char);
procedure GetS(var A: string);
procedure GetP(var A: PNode);
These procedures must be used to input initial data values in the program.
They must be called after the Task procedure, otherwise the program output the error message
"The Task procedure with a task name is not called at the beginning of the program".
Each input procedure must correspond to the type of data item,
otherwise the program output the following message: "Invalid type is used for an input data item" .
For example, this message will be output if GetN procedure will be called for input a string.
In the case of input of more data than the task requires, the program output
the error message "An attempt to input superfluous data".
In the case of input of less data than the task requires, the program output
the error message "Some required data are not input".
In the PascalABC.NET IDE you may also use standard Pascal procedure read(a, b, c, ...)
for data input. Also input functions without parameters are available:
ReadBoolean, ReadInteger, ReadReal, ReadChar, ReadString, ReadPNode, ReadNode
(each function returns the next element of input data).
Starting from the version 4.13, you may use the overridden Get(A) procedure for data input
in the Free Pascal Lazarus IDE. Also input functions without parameters
are available in these IDEs: GetBoolean (or GetBool), GetInteger (or GetInt), GetReal (or GetDouble), GetChar, GetString, GetPNode.
Each function returns the next element of input data.
procedure PutB(A: boolean);
procedure PutN(A: integer);
procedure PutR(A: real);
procedure PutC(A: char);
procedure PutS(A: string);
procedure PutP(A: PNode);
These procedures must be used to output results on the screen and compare obtained results with control data
(i.e., correct results).
They must be called after the Task procedure, otherwise the program output the error message
"The Task procedure with a task name is not called at the beginning of the program".
Unlike the Get-procedures, parameters of the Put-procedures can be expressions
(in particular, constants of a corresponding type). The used procedure should
correspond to output data item, otherwise the program output the error message
"Invalid type is used for an output data item".
In the case of output of more data than the task requires, the program output
the error message "An attempt to output superfluous data".
In the case of output of less data than the task requires, the program output
the error message "Some data are not output".
In the PascalABC.NET IDE you may also use standard Pascal procedure write(a, b, c, ...)
for data output.
Starting from the version 4.13, you may use the overridden Put(A) procedure for data output
in the Free Pascal Lazarus IDE.
Dynamic structures processing
type
PNode = ^TNode;
TNode = record
Data: Integer;
Next: PNode;
Prev: PNode;
end;
PNode and TNode types are used in the Dynamic and Tree group tasks.
In the introductory tasks of the Dynamic group (Dynamic1Dynamic2) and in the tasks
devoted to stacks and queues (Dynamic3Dynamic28) the Data
and Next fields of the TNode structure are used. In the tasks
devoted to lists (Dynamic29Dynamic80)
all fields (Data, Next, Prev) of TNode structure are used.
In the most tasks devoted to binary trees the Data, Left, and Right fields of the TNode structure are used.
The Parent field is used in the tasks devoted to doubly linked binary trees
(Tree48Tree56 and Tree70Tree71).
All initial and resulting pointers have PNode type in the tasks. They
must be input and output by GetP and PutP functions respectively or by the pt stream.
Students should not redefine the PNode and TNode types
in their programs.
procedure Dispose(var P: PNode);
This procedure overloads the standard Dispose procedure; it must be used
in the tasks of the Dynamic and Tree groups (see also PNode type description above).
Output debug info
Procedures described below are intended to output some debug data into the
debug panel located below the task panels of the Programming Taskbook window.
Since version 4.22, the debug panel can contain text data with any Unicode characters.
procedure Show(S: string);
Shows a text string S in the debug panel. The string S may contain explicit line-breaks as follows:
#13 (carriage return), #10 (new line) or their combination
#13#10 .
procedure Show([S: string;] A: integer[; W: integer]);
procedure Show([S: string;] A: real[; W: integer]);
Overloaded variants of the Show procedure that are intended to numeric data output.
The parameters in square brackets [ ] are optional.
The string parameter s defines an optional comment.
The numeric parameter a defines an output number.
The optional integer parameter w defines the minimum field width.
If actual length of output data exceeds the value of w (or if the parameter w is omitted)
then the field width is defined automatically. Output data are right-aligned.
The decimal separator in string representation of any real number is the decimal point.
Real numbers are output by default in the fixed-point format with two fractional digits.
You can change format by means of the SetPrecision procedure described below.
procedure ShowLine([S: string]);
procedure ShowLine([S: string;] A: integer[; W: integer]);
procedure ShowLine([S: string;] A: real[; W: integer]);
The ShowLine procedure
additionally breaks the current screen line.
procedure HideTask;
The HideTask procedure hides all sections of the Programming Taskbook window
with the exception of the debug section (and therefore increases the debug panel height).
If the debug panel contains no data then the call of the HideTask function is ignored.
All subsequent calls of this procedure are ignored too.
After displaying the Programming Taskbook window
you can hide/restore task sections of this window
by means of the space key or the command of the pop-up menu.
procedure SetPrecision(N: integer);
The procedure defines the output format of real numbers in the debug panel.
If the parameter n is positive then this parameter defines the amount of fractional digits
and a number is output in the fixed-point numeric format.
Otherwise a number is output in the exponential
numeric format with the amount of fractional digits that is determined by the field width
(that is, by the parameter W of the Show or ShowLine procedure).
Before the first call of the SetPrecision procedure real numbers are output
in the fixed-point format with 2 fractional digits.
Conversion of encodings (version 4.22)
function RuAnsi(s: string): string;
function RuUtf8(s: string): string;
These functions appeared in version 4.22 for the Lazarus environment.
They solve two problems associated with a mismatch of character data encodings.
These problems are due to the fact that Free Pascal uses the same AnsiString
(synonymous to string) for storing single-byte encoded strings and UTF-8 encoded strings.
The Lazarus editor uses UTF-8 encoding, so all string constants defined
in the program text are UTF-8 encoded strings. Strings in this encoding correctly
handled by debug output procedures (Show and ShowLine), but can not be used by
procedures PutS and Put for output data, containing Russian text,
because the input-output functions use text data in a one-byte ANSI encoding (code page 1251).
Therefore, if you want to use string constants defined in your program that
contain Russian letters when outputting data, you have to convert those constants
to the one-byte encoding using the RuAnsi function beforehand.
If converted constants contain Unicode symbols other than Russian letters and symbols included in the ASCII set,
those symbols are replaced by question marks "?".
Another problem arises when trying to apply the Show or ShowLine procedure to string data,
which were obtained by input procedures or functions (GetS, Get, GetString), if this data contains Russian letters.
Since the input data is represented in a one-byte ANSI-encoding, Russian letters in it will be incorrectly
displayed in the debug panel. To solve this problem, you can use the RuUtf8 function,
which allows you to convert a string in 1251 ANSI single-byte encoding to UTF-8 encoding.
The need to use the RuAnsi and RuUtf8 functions arises only in Lazarus,
and only when task includes input and/or output string data in Russian.
The PascalABC.NET language has built-in tools for working with Unicode and, in particular,
for automatic conversion of encodings.
|