End of table. 2

TRUNC(X) ROUND(X) Isolate the integer part of a number Round X to the nearest integer real whole
PRED(X) SUCC(X) Finding the preceding value (in its type) Finding the succeeding element (the value following X in its ordinal type) integer, boolean or character
ORD(X) Determining the ordinal number of the character X in a character set or its enumerated type symbolic whole
CHR(X) Determining the meaning of a symbol by its serial number whole symbolic
ODD(X) Logic function odd parity whole logical
Frac(X) Int(X) Fraction numbers Whole part numbers real real

Integer types in Pascal ABC

Pascal ABC has three standard type integers: Byte, Integer and Word. The range of their possible values ​​depends on the internal representation of the number, which can occupy one, two or four bytes. Table 3 shows the characteristics of these integer types.

Table 3

Classification of integer types

Compiler Pascal language ABC defines the maximum value of MAXINT for data of type INTEGER. In this case, the following ratio will be valid:

MAXINT<= K <= MAXINT

Variables of integer types are described in the VAR descriptions section:

Var <имя переменной> : <целый тип>;

Where<whole type> can be specified as Byte, Integer or Word.

For example:

VAR K : INTEGER;

I, J : BYTE;

The following operations are defined for integer types: addition, subtraction (have lower priority), multiplication and division - DIV, MOD. The functions from Table 4 are applicable to integer types. The TRUNC(X) and ROUND(X) functions with a real argument also produce an integer result (see Table 2). Standard mathematical functions, when given an integer argument, always return real values ​​(see Table 2).

When using procedures and functions with integer parameters, you should be guided by the rule of “nesting” of types, i.e. Wherever a WORD type is used as an argument, the BYTE type can be used. For arithmetic operations, if the operands are of different integer types, the result type has the maximum cardinality of the range, i.e. For Byte and Word operand types in an expression, the result type will be Word.

Table 4

Built-in procedures and functions for integer types

Real types

Unlike integer types, the values ​​of real types define a number with a decimal point only with some precision, depending on the internal representation of the number. A real number is stored in PC memory in a format consisting of sign, mantissa and order of the number. The decimal point is implied to be placed before the left (most significant) digit of the mantissa (such a mantissa is called normalized). When performing operations, the decimal point is shifted by multiplying the mantissa by E(exponent symbol) with order.

For example, writing a number in the form: <знак> <мантисса>E<порядок> corresponds to number representation<зnak><мантисса>*10 < порядок > .

Pascal ABC uses only two real types - REAL and COMPLEX, the characteristics of which are given in Table 5.

Table 5

Real types

Variables of real types are declared in the variable declaration section of VAR, similarly to variables of integer types in the form:

Var <переменная> : <вещественный тип>;

Where<real type> can only be specified Real or Complex.

To work with real types, standard functions are defined: sin, cos, arctan, ln, exp, sqr, abs, sqrt (Table 2), as well as functions of real type (Table 6).

Table 6

Built-in functions of real type

In this case, the operations +, -, *, / in an arithmetic expression give a real result if at least one of the operands is real.

In programming, as in any science (although it is also an art), experience and methods for solving various problems accumulate over the course of historical time. The solution to many problems is quite universal. There is no need to write an algorithm to solve it every time if it was already written many years ago and approved by the programming community. Such algorithms are designed as functions and modules, and then used in programs that are written here and now.

The function or procedure may already be included in the programming language itself, or it may be part of a module that needs to be “connected” to the program.

The standard (included in the language) functions of the Pascal programming language are described below.

Arithmetic functions

Arithmetic functions can only be used with integer and real values.

Function Purpose Result type
abs(x) absolute value of the argument matches argument type
sqr(x) square of the argument matches argument type
sqrt(x) square root of the argument real
cos(x) cosine of the argument real
sin(x) sine of argument real
arctan(x) arctangent of the argument real
exp(x) e x real
ln(x) natural logarithm real
int(x) the integer part of number real
frac(x) fractional part of a number real

Type Conversion Functions

These functions are designed to convert value types, such as character to integer, real number to integer, etc.

Expressions

Constants and Variables

Constants and variables can take values ​​of any of the allowed data types within specified ranges.

Constants define values ​​that are known before the program begins execution. Attempting to assign a new value to a constant while the program is running will result in an error. Variables, unlike constants, can take on different numerical values ​​during calculations.

In a PC, each variable corresponds to a specific memory area into which its value is stored.

Expression is a syntactic unit of language that defines how a certain value is calculated. The expression may contain constants, variables, standard functions, arithmetic operations signs, and parentheses.

The calculation of expression values ​​is performed in a certain order, shown in Table 2.3.

Table 2.3

To calculate the most frequently used functions, the Pascal library contains corresponding subroutines (Table 2.4) that calculate the values ​​of the main standard functions. Constants, variables, and expressions can be used as function arguments.

For example: Sin(X) + Cos(A/2+Z) - Log(7).

When using standard functions, please note the following:

1) the name of the function must strictly correspond to the name;

2) the argument must be enclosed in parentheses, and its type must match the type specified in Table 2.4.

Table 2.4

Recording in Pascal Function name Argument type Result type
Abs(X) Absolute value Integer Real Same as type X
Sqr(X) Calculates X in the 2nd degree Integer Real Same as type X
Sin(X) Cos(X) ArcTan(X) Sine, cosine and arctangent X Integer Real Real
Exp(X) Exponential function e x Integer Real Real
Ln(X) Natural logarithm Real or integer Real
Exp10(X) Log(X) These functions are similar to Exp( X) and Ln( X), but in base 10 Real or integer Real
Sqrt(X) Square root of a number X Real or integer Real
Odd(X) Returns True if X odd and False if X honest Whole Boolean
Trunc(X) Returns the whole part X, the fractional part is discarded Real Whole
Round(X) Rounds X to the nearest integer Real Whole

Note. Tangent x is calculated as ; Exponentiation is carried out using the following formula: a x =Exp (x*Ln(a)) .

I/O routines

I/O is associated with the exchange of information between RAM and external storage media (input/output terminal, ADPU, HDD (floppy disk), LMD (hard drive) and other devices).

In the Pascal language, the standard means of communication between a person and a computer are predefined Input and Output files, which by default are program parameters. The program receives input data from the Input file and places the processing result in the Output file. By default, the Input file is assigned to the keyboard, and the Output file is assigned to the terminal screen.

There are the following types of input operator (reading procedures):

READ (A1,A2,A3,...,AN)

READLN (A1,A2,A3....AN)

where A are the variables to which input values ​​are sequentially assigned.

During program execution, as soon as the READ (READLN) statement is encountered, the computer “stops” and waits for input of numeric, symbolic values. When values ​​are entered and the Enter key is pressed, program execution continues. The enter key is pressed after entering data for each reading procedure. Values ​​are entered after (at least) one space after typing the entire program and running it for execution.

The READLN (A1,A2,...AN) operator first enters the values ​​of the variables, and then, unlike the READ (Al, ...,AN) operator, moves to a new line.

Using an input statement without READLN parameters simply advances to a new input line. The READLN (A1....AN) operator is equivalent to using 2 READ (Al.....AN) and READLN operators.

For example:

1) VAR A, B.: REAL

C. D: INTEGER;

READ(A.B);

READ(C,D);

READLN(A,B);

READLN(C,D);

READ(A.B);

READ(C.D);

In the first case, after typing each pair of data in one line, press the Enter key. In the second case, the reading procedure is similar. The difference is that after reading the values ​​A and B by the first reading procedure, the data for the next reading procedure will be read from the beginning of the new line, i.e. the data set for variables A, B, C, D for the first and second cases, respectively, will look like this:

1) 4.83 E - fZ 35.71 E + f1Enteg 51 2134 Enter

2) 4.83 E - f3 35.71 E + f1Ep1eg

Integer, real, and character data can be entered. Entering character data has its own peculiarities, since a space, like any character in the Pascal language, is classified as character data. Character data is entered as a continuous line. For example:

VAR A.B.C.D: CHAR;

READ(A.B.C.D);

Type Kyiv on the keyboard and press the Enter key. The variables will receive the following values: A-"K", B-"I", C-"E", D-"B". Another feature of entering character data is that pressing the Enter key itself is perceived as a space character, so for correct input it is recommended to put a READLN operator before each operator for entering character data so that they are always entered on a new line.



There are the following types of inference operator.

WRITE (A1,A2.....AN);

WRITELN (A1,A2,...,AN);

WRITELN;

The parameters A1, A2,..., AN can be integer, real, character (string) and logical variables.

Formatted output and data output with formats that determine the width of the selection field are allowed.

With formatless output, a standard (defined for a specific class of machines) number of positions is allocated for the output values ​​of variables of various types. For example, for PC EC computers, PC IBM.

The total length of the field for the value of a real Type variable occupies 18 positions, and the fractional part of the number itself takes 10 positions.

Output formats in the output statement are specified with a colon after the output variable. For real numbers, the format can consist of two quantities. The first one denotes the general field of the output value, the second - the field of the fractional part. In this case, the general field includes the number sign, the decimal point and the number of digits in the integer and fractional parts.

Here are some examples of data output with formats for the above variable values.

When outputting the value of a real variable B, in the first case, a floating point format (normalized format) is used using a decimal factor - the Latin letter E (which separates the mantissa of the number from the order). The minimum length of the output field is 8 characters. In the second case, the value of B is output in fixed-point format.

If the format field selected is greater than the number of positions occupied by the number, then the corresponding number of spaces will be allocated before the integer part, and the corresponding number of zeros will be allocated after the fractional part.

The first part of the last remark also applies to cases of outputting the values ​​of character, string and logical variables

Function Purpose Argument type Function type
Abs(x) Calculation of the absolute value of X R, I R, I
Sin(x) Calculate the value of the function SIN arg. X R, I R, R
Cos(x) Calculate the value of the function COS arg. X R, I R, R
Arctan(x) Calculate the value of the ARCTG function args. X R, I R, R
SQR(x) Calculates the value of the square of the argum. R, I R, I
SQRT(x) Calculates the value of the square root of X R, I R, R
EXP(x) Calculation of the value of the exponential function of the argument. R, I R, R
EXP10(x) Calculating 10 to the power of X R, I R, R
Ln(x) Calculation of the value of the function of the natural logarithm of an argument. R, I R, R
LOG(x) Calculating the value of the decimal logarithm of an argument R, I R, R
TRUNC(x) Finding the integer part of X R, I I, I
INT(x) Evaluating the integer part of an argument R, R R, R
ROUND(x) Rounds X to the nearest integer. R, I I, I
FRAC(x) Calculates the fractional part of an argument R R
ODD(x) TRUE if X is an odd number; FALSE if X is an even number; I B
ORD(x) 1. Finding the number of a value of an enumerated type 2. Finding the number of a Pascal language symbol (in the decimal system) Enumeration C I I
CHR(x) Determination of a Pascal language symbol by its serial number. I C

GENERAL TASK

1. Study on your own

a) building the simplest program structure in Pascal;

b) data types, description of constants, variables, standard functions;

c) rules for writing arithmetic expressions.

2. Get acquainted with general information and methodological instructions for this laboratory work.

3. Create an algorithm and program for solving the proposed problem.

4. Debug the working program on the PC. Print the program listing, input data and calculation result.

Individual tasks

Calculate on a computer:

  1. , with c=0.7; m=0.3´10 -2 ; a=5; n=1.2
  2. , with r=5; k=1.24´10 -7 ; t=0.1´10 -6 ;z=0.5´10 2
  3. ; , with a=0.1; b=1.4; a=0.02; z=3´10 -3 ; k=4.5
  4. ; , with a=3.4; b=1.1; c=9
  5. ; , at δ=0.8; b=1.5; a=3; a=0.394
  6. ; , with λ=0.1; b=0.6; c=2.4´10 -4 ; t=15
  7. ; , with a=0.1; b=88;; c=0.2´10 -6
  8. ; , with a=0.3; b=0.9; c=0.61
  9. ; , with a=38.9; b=-4.7; c=5; z=0.8
  10. ; , with a=15.123; b=9.563; z=0.717
  11. ; , with a=0.5; b=3.1; c=1.4
  12. ; , with a=4.4; b=0.57; c=6; z=0.054
  13. ; , with a=0.5; b=2.7; c=0.4;
  14. ; , at a=4.5´10 -4; b=-2´10 -5 ; c=25
  15. ; , with a=9.6; b=8.2; c=2; k=0.7
  16. ; , with a=1.256; b=-13.5; c=4
  17. ; , with a=1.256; b=3.5; c=0.53; z=7
  18. ; , with a=2.8; b=16.4; c=-5.4
  19. ; , with a=2.953; b=0.254; c=0.5
  20. ; , with a=4.125; b= -1.234; c=0.487
  21. ; , with a= -0.92; b= 0.58
  22. ; , with a=1.725; b=19; c= -2.153
  23. ; , with a=3.457; b= 3.1; c=2
  24. ; , with a=2.389; b= 3.1; c=17
  25. ; , with a=-0.5; b= 1.7; t=0.44
  26. ; , with a=0.816; b= 3.4; c=16.7
  27. ; , with a=1.1; b= 0.2; c=4´10 -3

Control questions

1. Which of the following variable names are allowed or prohibited in Turbo Pascal?

KAFEDRA_SAPR HELP+ME
KAFEDRA SAPR help-me
KAFEDRASAPR ABC...XYZ
OKT16 FOR
160KT SIGMA
ABVGD SIGMA?
abvgd number1
A BVGD number 1
A.B.V.G.D. XXXXXXXXXXX

2. Is it possible to change the values ​​of constants in the program?

3. Which of the following assignment statements are correct if variables

I,J,K:INTEGER;

X,Y:REAL;

A,B:BOOLEAN

A:=(X K);

I:=I+K/I;

X:+I+J-B;

4. Is it acceptable to use quantities of different types in arithmetic expressions?

5. Which of the following entries are correct from the point of view of the Pascal language?

"A"<"В" ; "А"<"В" AND 4<5;

TRUE>FALSE; ("C"<"D") OR (4<5);

"8"<"3" ;

6. Is it possible to assign an expression of a real type to a variable of an integer type, and vice versa, to a variable of a real type - an expression of an integer type?

7. What functions are designed to convert real type values ​​to integer type values?

8. Find errors in the RM1 program (if any) and correct them.

PROGRAM;

(Control program)

VARY X: INTEGERS, Y REAL X + 3: = Y;

Y:=Y + 5.7;

Z:= 4X + 9Y

4.3. Standard features in Turbo Pascal 7

In the Pascal language, there are a number of pre-developed subprograms-functions that can be used as ready-made objects. In Turbo Pascal, their number is increased compared to the language standard, and they are all combined into standard modules (see paragraph 16). This section describes the most commonly used standard functions. Standard I/O and dynamic memory allocation functions are described in paragraphs. 11 and 7 respectively. Other standard functions (working with strings, pointers and addresses, etc.), as well as a more detailed discussion of all the functions mentioned above, are given in paragraph 16.

4.3.1. Arithmetic functions

Arithmetic functions can only be used with integer and integral values. Their list is given in table. 4.

Table 4. Arithmetic functions

Function Purpose Result type
Abs(X) The absolute value of the argument Same as type X
Arctan(X) Arctangent of the argument Real
Cos(X) Cosine of the argument Real
Exp(X) e x Real
Frac(X) Fractional part of a number Real
Lnt(X) The integer part of number Real
Ln(X) Natural logarithm Real
Pi Value of Pi=3.1415926535897932385 Real
Sin(X) Sine of the argument Real
Sqr(X) Argument Square Same as type X
Sqrt(X) Square root of argument Real

Note. If the function is used with the compiler switch ($N+), then instead of a value of type Real, it calculates a value of type Extended.

Example.

($N-)
begin
P:= Pi (3.1415926536E+00)
end.

($N-)
begin
P:= Pi (3.1415926535897932385E+0000)
end.

4.3.2. Type conversion functions

These functions are designed to convert value types, for example (number to integer, real number to integer, etc. These functions include the following functions:

Chr(X)- converting ASCII code to character.

The function argument must be an integer type in the range (0..255). The result is the character corresponding to the given code.

High(X) - obtaining the maximum value of the quantity.

A function argument is a parameter or identifier of an ordinal type, an array type (see section 6.1), a string type (see section 6.2) or an open array (see section 10.3.5).

The result of the function for a value of an ordinal type is the maximum value of this value, an array type is the maximum index value, a string type is the declared size of the string, an open array is the number of array components minus 1 (the maximum index, when numbering starts from zero).

Low(X) - obtaining the minimum value of the quantity.

A function argument is a parameter or identifier of an ordinal type, an array type (see section 6.1), a string type (see section 6.2) or an open array (see section 10.3.5). The result of the function for a value of an ordinal type is the minimum value of this value, an array type is the minimum value of an index, a string type or an open array is 0.

Ord(X) - Convert any ordinal type to an integer type.

The function argument can be a value of any ordinal type (logical, symbolic, enumerated). The result is a Longint value.

Round (X) - Rounding a real number to the nearest integer.

The function's argument is a real value, and the result is a Longint value rounded to the nearest integer. If the result is outside the range of Longint values, an error occurs when the program runs.

Trunc(X) - getting the integer part of a real number.

The function argument is a real value, and the result is the integer part of this number. The result type is Longint. If the result is outside the range of Longint values, an error occurs during program execution.

4.3.3. Functions for ordinal quantities

These functions allow you to perform a number of actions on values ​​of ordinal i type (find the previous or subsequent element, check a number for oddness). These features include the following:

Odd(X) - checking the value of X for oddity.

The function argument is a Longint value, the result is True if the argument is odd, and False if it is even.

Pred(X) - determination of the previous value of X.

The function argument is a value of any ordinal type, the result is a previous value of the same type (for example, Pred(2) is 1). An error occurs when applying a function to the first element of a sequence.

Succ(X) - determination of the subsequent value of X.

The argument of the function is a value of any ordinal type, the result is the subsequent value of the same type (for example, Succ(2) is 3). An error occurs when applying a function to the last element of a sequence.