Материал готовится,

пожалуйста, возвращайтесь позднее

пожалуйста, возвращайтесь позднее

The easiest task for any program is to crunch numbers. The statement WRITELN (or

WRITE) can be used both to display numbers and evaluate numerical expressions. You

can build up arithmetic expressions using the following arithmetic operators:

+ addition

– subtraction

* multiplication

/ division

Take a look at these examples:

WRITELN(123);

WRITELN(1.23 * 4);

The first example displays the number between the parentheses (123). The second

example performs multiplication of two numbers and displays the result. Notice that

for numeric values, unlike text strings, you don’t use quotes.

You may use WRITELN to display text and numbers in the same statement by using

the comma as a separator like this:

WRITELN('The result is=', 125 * 1.75);

The following program is used to evaluate two numeric expressions (multiplication

and division) and display the results preceded by the proper text.

{ — — — — — — — — — — — — — — — Example 1-6 — — — — — — — — — — — — — — — -}

PROGRAM CrunchNumbers(OUTPUT);

BEGIN

WRITELN('I can easily crunch numbers.') ;

WRITELN('Here is multiplication of 50x4:',50*4);

WRITELN('. . and here is division of 2400/8:',2400/8)

END.

The output of this program is:

I can easily crunch numbers.

Here is multiplication of 50x4:200

. . and here is division of 2400/8: 3.0000000000E+02

The multiplication is done as expected. The two operands (50 and 4) were integers

(whole numbers) and the result (200) was an integer too. The division result, however,

came out in a format that needs some explanation.

Integers and Real Numbers

The division performed with the operator / is called real division and always produces

as its result a real number. Real numbers may be written in fixed-point notation (such

as 300.0) or in scientific (exponential) notation (such as 3.0E+02), but in Pascal, real

number output will always be represented in scientific notation by default. A number

written in scientific notation is made up of two parts divided by the letter E (or e). The

left part is called the mantissa and indicates the significant digits, while the right part

is called the exponent. The exponent is a power of ten that determines the position of

the decimal point. So, in this example the number:

3.0000000000E+02

is the same as the number:

3 x 10*2

The same number, when expressed in fixed-point format, becomes:

300.0

If the exponent is preceded by a minus sign as in:

3.124E–02

then the decimal point is shifted two positions to the left. This number, then, is the

same as:

0.03124

If the number is negative, the minus sign should precede the mantissa:

–0.0124E–02

If the number is positive, you may omit the sign for either the mantissa or the

exponent:

1.23E02

The division operator (/) is called the real division operator, because the result always

appears as a real number regardless of the type of the operands.

For integer division use the operator DIV as in the example:

WRITELN(2400 DIV 8);

This will produce the output 300.

With integer division, any fraction in the result will be truncated, as in this example:

WRITELN(9 DIV 4); produces the output 2

Another important operator, MOD, is used to get the remainder of integer division

(modulo), as in these examples:

WRITELN(9 MOD 4); produces the output 1

WRITELN(3 MOD 4); produces the output 3

The operators DIV and MOD take only integer operands and produce integer output.

WRITE) can be used both to display numbers and evaluate numerical expressions. You

can build up arithmetic expressions using the following arithmetic operators:

+ addition

– subtraction

* multiplication

/ division

Take a look at these examples:

WRITELN(123);

WRITELN(1.23 * 4);

The first example displays the number between the parentheses (123). The second

example performs multiplication of two numbers and displays the result. Notice that

for numeric values, unlike text strings, you don’t use quotes.

You may use WRITELN to display text and numbers in the same statement by using

the comma as a separator like this:

WRITELN('The result is=', 125 * 1.75);

The following program is used to evaluate two numeric expressions (multiplication

and division) and display the results preceded by the proper text.

{ — — — — — — — — — — — — — — — Example 1-6 — — — — — — — — — — — — — — — -}

PROGRAM CrunchNumbers(OUTPUT);

BEGIN

WRITELN('I can easily crunch numbers.') ;

WRITELN('Here is multiplication of 50x4:',50*4);

WRITELN('. . and here is division of 2400/8:',2400/8)

END.

The output of this program is:

I can easily crunch numbers.

Here is multiplication of 50x4:200

. . and here is division of 2400/8: 3.0000000000E+02

The multiplication is done as expected. The two operands (50 and 4) were integers

(whole numbers) and the result (200) was an integer too. The division result, however,

came out in a format that needs some explanation.

Integers and Real Numbers

The division performed with the operator / is called real division and always produces

as its result a real number. Real numbers may be written in fixed-point notation (such

as 300.0) or in scientific (exponential) notation (such as 3.0E+02), but in Pascal, real

number output will always be represented in scientific notation by default. A number

written in scientific notation is made up of two parts divided by the letter E (or e). The

left part is called the mantissa and indicates the significant digits, while the right part

is called the exponent. The exponent is a power of ten that determines the position of

the decimal point. So, in this example the number:

3.0000000000E+02

is the same as the number:

3 x 10*2

The same number, when expressed in fixed-point format, becomes:

300.0

If the exponent is preceded by a minus sign as in:

3.124E–02

then the decimal point is shifted two positions to the left. This number, then, is the

same as:

0.03124

If the number is negative, the minus sign should precede the mantissa:

–0.0124E–02

If the number is positive, you may omit the sign for either the mantissa or the

exponent:

1.23E02

The division operator (/) is called the real division operator, because the result always

appears as a real number regardless of the type of the operands.

For integer division use the operator DIV as in the example:

WRITELN(2400 DIV 8);

This will produce the output 300.

With integer division, any fraction in the result will be truncated, as in this example:

WRITELN(9 DIV 4); produces the output 2

Another important operator, MOD, is used to get the remainder of integer division

(modulo), as in these examples:

WRITELN(9 MOD 4); produces the output 1

WRITELN(3 MOD 4); produces the output 3

The operators DIV and MOD take only integer operands and produce integer output.

Загрузка...

Выбрать следующее задание

Ты добавил

Выбрать следующее задание

Ты добавил