Additional types and functions for C++ before version 4.23
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".
bool GetBool();
int GetInt();
double GetDouble();
char GetChar();
string GetString();
TNode* GetNode();
These input functions have been added in version 4.22.
They are convenient to use in the situation when it is enough to pass the value
of source data element to the constructor of some object or to some function.
In addition, they allow you to combine the description and initialization
of variables associated with the input data.
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; .
In version 4.22, the capabilities of the pt output stream have been significantly extended.
Variables of the pair type and any container type can now be specified as output data for the pt stream,
and the container elements can be data of scalar types, pair types, and container types.
Components of the pair type can also have any scalar type, pair type, and container type.
Input iterator ptin_iterator<T>
Output iterator ptout_iterator<T>
Since version 4.15, specialized iterators ptin_iterator<T> and ptout_iterator<T>
can be used for input and output elements of sequences of type T, respectively.
These iterators have the same properties as the standard stream iterators istream_iterator<T> and ostream_iterator<T>.
Let us list the properties of the ptin_iterator iterator that coincide with those of the stream iterators:
- type T defines the type of sequence elements;
- the reading of an element of the sequence from the pt stream is performed at the initial moment
of working with the iterator, and then at each increment operation ++;
- there are two variants of the ++ operation: prefix increment (++i) and postfix increment (i++);
- the * operation returns the last read value; this operation can be used repeatedly to get the same value;
- the end-of-sequence iterator is created using the constructor without parameters;
- when reaching the end of the sequence, the iterator becomes equal to the end-of-sequence iterator,
the subsequent calls of the increment operation are ignored,
and the value of the last read element of the sequence is always returned as a result of calling the * operation.
Now let us list ptout_iterator properties that coincide with the similar properties of stream iterators:
- there is no special constructor for creating the end-of-sequence iterator;
- * and ++ operations do not perform any actions and just return the iterator itself;
- the assignment operation of the form o = expression (where o is the name of the ptout_iterator variable)
writes the value of the expression to the pt stream.
Unlike the standard istream_iterator iterator,
the constructor for ptin_iterator (except for the end-of-sequence iterator constructor)
has one parameter of unsigned int type that defines the size of the sequence to be read (in elements).
In case of a special parameter value equal to 0, the size of the sequence is read from the pt stream
itself just before the first element of the sequence is read.
If, when reading the size, it turns out that the data element read is not an integer or the number is not positive,
the iterator immediately goes to the end of the sequence (i. e., it becomes equal to the iterator ptin_iterator<T>()).
This way of organizing ptin_iterator<T> iterators makes it easy to implement
reading multiple sequences from the pt input stream if their size is known beforehand or if the size is specified just
before the start of the sequence.
As an example of using iterators from the pt stream for input and output,
consider the Array7 task, where we need to input an array of real numbers of a specified size
and then output its elements in reverse order (note the use of standard header files <vector> and <algorithm>;
the vector container is used to store the array):
#include "pt4.h"
#include <vector>
#include <algorithm>
using namespace std;
void Solve()
{
Task("Array7");
vector<double> a(ptin_iterator<double>(0), ptin_iterator<double>());
copy(a.rbegin(), a.rend(), ptout_iterator<double>());
}
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 (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.
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.
Since version 4.22, the debug panel can contain text data with any Unicode characters.
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.
Debugging tools added in version 4.16
The debugging tools described in this section are intended primarily for use in
tasks from the Programming Taskbook for STL. At the same time, they can be used for any tasks,
because they are implemented in the basic variant of Programming Taskbook version 4.16.
template<typename T1, typename T2>
void Show([string s,] pair<T1, T2> a)
template<typename T1, typename T2>
void Show(wstring s, pair<T1, T2> a)
template<typename T1, typename T2>
void ShowLine([string s,] pair<T1, T2> a)
template<typename T1, typename T2>
void ShowLine(wstring s, pair<T1, T2> a)
Versions of Show and ShowLine function, that allow to output data of pair<T1, T2> type to debug panel;
in this case, the following types can be used as T1 and T2 types: the int, double, char, const char* and string types,
the pair type (the nesting depth of pair types can be arbitrary), the containers
of type vector, deque, list, set, multiset, map, multimap with data of the specified types.
An optional string comment s and an opening parenthesis are printed before the first element of the pair,
a comma is printed between the elements of the pair, and a closing parenthesis is printed
after the second element.
For example, a data element of type pair<int, pair<int, int>> with values 5, 2, and 9 and comment "p="
will be output as follows:
p=( 5 , ( 2 , 9 ) )
Functions with a string parameter s of wstring type have been added in version 4.22.
void Show(string s, string a)
void Show(string s, char a)
void ShowLine(string s, string a)
void ShowLine(string s, char a)
void Show(wstring s, string a)
void Show(wstring s, char a)
void ShowLine(wstring s, string a)
void ShowLine(wstring s, char a)
Versions of Show and ShowLine functions that allow output char and string data to the debug panel,
together with s string comments preceding them. Functions with a string parameter s of wstring
type are added in version 4.22.
template<typename InIter>
void Show(InIter first, InIter last[, string s])
template<typename InIter>
void ShowLine(InIter first, InIter last[, string s])
template<typename InIter>
void Show(InIter first, InIter last, wstring s)
template<typename InIter>
void ShowLine(InIter first, InIter last, wstring s)
Versions of the Show and ShowLine functions that allow you to display the elements of a sequence
in the debug panel, using the input iterators (first and last) associated with a sequence.
The elements of the sequence can have int, double, char, const char*, and string types,
as well as pair<T1, T2> (the nesting depth of pair types can be arbitrary);
besides, the elements themselves (or components of the pair)
can be vector, deque, list, set, multiset, map, multimap containers.
The optional last parameter s allows to specify a string comment, which is output before the output sequence.
Functions with a string parameter s of wstring type are added in version 4.22.
In particular, these functions can be used to output the elements of array a of size n:
Show(a, a + n);
The Show function displays the elements of the sequence on one line,
separating them with spaces; when the output is complete, it automatically moves to a new line.
The ShowLine function displays each element of the sequence on a new line;
the string comment is displayed on the same line as the first element.
Conversion of encodings (version 4.22)
string RuAnsi(wstring s);
This function appeared in version 4.22 and solves the problem related to character
data encoding mismatch. This problem occurs when using a code editor which supports
UTF-8 encoding (for example, the VS Code editor). Any string constants containing characters
other than ASCII characters should be represents as "long strings" in this editor
(of the wstring type, for example, L"String of the wstring type" ).
This does not make a problem when displaying them in the debug panel (because
the Show and ShowLine functions with wstring parameters appeared in version 4.22),
but does not allow to use them when generating output text data containing Russian letters
(because output functions, as well as input functions, operate with single-byte strings of string type).
Therefore, if you need to use string constants with Russian letters as
output data, you must first convert these constants (of the wstring type)
into one-byte ANSI 1251 encoding using the RuAnsi function. If the converted constants
contain Unicode symbols other than Russian letters and symbols included in the ASCII set,
those symbols are replaced by question marks "?".
Thus, the necessity of using the RuAnsi function arises only for tasks that require processing
character data in Russian and only in code editors that support UTF-8 encoding.
|