Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University), 1998–2018

 

Examples | C++ | PT4 types and functions

PrevNext


Additional types and functions

Types and functions described below will be available in a program if pt4.cpp file is included (any template project created for task solving on C++ includes this file).

Task initialization, data input-output


void Task(char* name);

This function initializes a task named Name. It must be called at the beginning of the Solve function that contains task solution. If the Solve function does not contain the Task function call then the program output the error message "The Task procedure with a task name 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 function 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 function 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 function 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).


void GetB(bool& a); void GetN(int& a); void GetD(double& a); void GetC(char& a); void GetS(char* a); void GetS(string& a); void GetP(TNode*& a);

These functions must be used to input initial data values in the program. They must be called after the Task function, 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 function 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 function will be called for input a string.

Note that string data item can be read to a variable of char* or string type.

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".


void PutB(bool a); void PutN(int a); void PutD(double a); void PutC(char a); void PutS(char* a); void PutS(string a); void PutP(TNode* a);

These functions 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 function, 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-functions, parameters of the Put-functions can be expressions (in particular, constants of a corresponding type). The used function should correspond to output data item, otherwise the program output the error message "Invalid type is used for an output data item".

Note that the PutS function can receive parameters of two types: char* and string.

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".


Input-output stream pt

A special input-output stream called pt may be used for input-output operations instead of Get- and Put-functions. For instance, function calls GetN(a); GetD(b); GetS(s); may be replaced by one stream-read statement as follows: pt >> a >> b >> s;.

Dynamic structures processing


struct TNode { int Data; TNode* Next; TNode* Prev; TNode* Left; TNode* Right; TNode* Parent; }; typedef TNode* PNode;

PNode and TNode types are used in the Dynamic and Tree group tasks. In the introductory tasks of the Dynamic group (Dynamic1–Dynamic2) and in the tasks devoted to stacks and queues (Dynamic3–Dynamic28) the Data and Next fields of the TNode structure are used. In the tasks devoted to lists (Dynamic29–Dynamic80) 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 (Tree48–Tree56 and Tree70–Tree71).

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.


void DeleteNode(TNode* p);

This function provides freeing the memory allocated for a structure of TNode type (see the TNode description above). It must be used in the tasks of the Dynamic and Tree groups. Since the version 4.11 you may use the C++ delete operator instead the DeleteNode function.

Output debug info

Functions described below are intended to output some debug data into the debug panel located below the task panels of the Programming Taskbook window.


void Show(char* s); void Show(string s);

Shows a text string S in the debug panel. The string S may contain explicit line-breaks as follows: '\r' (carriage return), '\n' (new line) or their combination "\r\n".


void Show([char* s,] int a[, int w]); void Show([char* s,] double a[, int w]); void Show([string s,] int a[, int w]); void Show([string s,] double a[, int w]);

Overloaded variants of the Show function 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 function described below.


void ShowLine([char* s]); void ShowLine([string s]); void ShowLine([char* s,] int a[, int w]); void ShowLine([char* s,] double a[, int w]); void ShowLine([string s,] int a[, int w]); void ShowLine([string s,] double a[, int w]);

The ShowLine function additionally break the current screen line.


void HideTask();

The HideTask function 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 function 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.


void SetPrecision(int n);

The function 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. If the parameter n is negative or is equal to 0 then a number is output in the exponential numeric format with the amount of fractional digits that is equal to the absolute value of n (if n = 0 then the amount of fractional digits has the default value 6).

Before the first call of the SetPrecision function real numbers are output in the fixed-point format with 2 fractional digits.


PrevNext

 

  Рейтинг@Mail.ru

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
06.05.2018