Основы языка программирования C++ с применением объектно-ориентированного программирования. Чернышов М.К. - 15 стр.

UptoLike

Составители: 

ɉɨɫɤɨɥɶɤɭ ɩɪɢ ɨɩɢɫɚɧɢɢ ɱɥɟɧɨɜ ɫɬɪɭɤɬɭɪɵ ɞɨɥɠɧɵ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ
ɬɨɥɶɤɨ ɪɚɧɟɟ ɨɩɪɟɞɟɥɟɧɧɵɟ ɢɦɟɧɚ ɬɢɩɨɜ, ɩɪɟɞɭɫɦɨɬɪɟɧ ɜɚɪɢɚɧɬ ɩɪɟɞɜɚɪɢ-
ɬɟɥɶɧɨɝɨ ɨɛɴɹɜɥɟɧɢɹ ɫɬɪɭɤɬɭɪɵ, ɡɚɞɚɸɳɢɣ ɬɨɥɶɤɨ ɢɦɹ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ.
ɇɚɩɪɢɦɟɪ, ɱɬɨɛɵ ɨɩɢɫɚɬɶ ɷɥɟɦɟɧɬ ɞɜɨɢɱɧɨɝɨ ɞɟɪɟɜɚ, ɫɨɞɟɪɠɚɳɢɣ ɭɤɚɡɚɬɟ-
ɥɢ ɧɚ ɥɟɜɭɸ ɢ ɩɪɚɜɭɸ ɜɟɬɜɢ ɞɟɪɟɜɚ, ɢ ɭɤɚɡɚɬɟɥɶ ɧɚ ɧɟɤɨɬɨɪɭɸ ɫɬɪɭɤɬɭɪɭ
ɬɢɩɚ
Value, ɫɨɞɟɪɠɚɳɭɸ ɡɧɚɱɟɧɢɟ ɞɚɧɧɨɝɨ ɜ ɭɡɥɟ, ɦɨɠɧɨ ɩɨɫɬɭɩɢɬɶ ɬɚɤ:
struct Value;
struct Tree_element
{
Value *val;
Tree_element *left, *right;
};
ɑɥɟɧɚɦɢ ɫɬɪɭɤɬɭɪ ɦɨɝɭɬ ɛɵɬɶ ɬɚɤ ɧɚɡɵɜɚɟɦɵɟ ɛɢɬɨɜɵɟ ɩɨɥɹ, ɤɨɝɞɚ ɜ
ɩɨɥɟ ɩɚɦɹɬɢ ɩɟɪɟɦɟɧɧɨɣ ɰɟɥɨɝɨ ɬɢɩɚ (
int ɢɥɢ unsigned int) ɪɚɡɦɟɳɚɟɬɫɹ
ɧɟɫɤɨɥɶɤɨ ɰɟɥɵɯ ɞɚɧɧɵɯ ɦɟɧɶɲɟɣ ɞɥɢɧɵ. ɉɭɫɬɶ, ɧɚɩɪɢɦɟɪ, ɜ ɧɟɤɨɬɨɪɨɣ
ɩɪɨɝɪɚɦɦɟ ɫɢɧɬɚɤɫɢɱɟɫɤɨɝɨ ɪɚɡɛɨɪɚ ɨɩɢɫɚɧɢɟ ɥɟɤɫɟɦɵ ɫɨɞɟɪɠɢɬ ɬɢɩ ɥɟɤ-
ɫɟɦɵ (ɞɨ ɲɟɫɬɢ ɡɧɚɱɟɧɢɣ) ɢ ɩɨɪɹɞɤɨɜɵɣ ɧɨɦɟɪ ɥɟɤɫɟɦɵ ɜ ɬɚɛɥɢɰɟ ɫɨɨɬ-
ɜɟɬɫɬɜɭɸɳɟɝɨ ɬɢɩɚ (ɞɨ 2000 ɡɧɚɱɟɧɢɣ). Ⱦɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɡɧɚɱɟɧɢɹ ɬɢɩɚ
ɥɟɤɫɟɦɵ ɞɨɫɬɚɬɨɱɧɨ ɬɪɟɯ ɞɜɨɢɱɧɵɯ ɪɚɡɪɹɞɨɜ (ɬɪɟɯ ɛɢɬ), ɚ ɞɥɹ ɩɪɟɞɫɬɚɜɥɟ-
ɧɢɹ ɱɢɫɟɥ ɨɬ
0 ɞɨ 2000ɨɞɢɧɧɚɞɰɚɬɢ ɞɜɨɢɱɧɵɯ ɪɚɡɪɹɞɨɜ (11 ɛɢɬ). Ɉɩɢɫɚ-
ɧɢɟ ɫɬɪɭɤɬɭɪɵ, ɫɨɞɟɪɠɚɳɟɣ ɫɜɟɞɟɧɢɹ ɨ ɥɟɤɫɟɦɟ, ɦɨɠɟɬ ɜɵɝɥɹɞɟɬɶ ɬɚɤ:
struct Lexema
{
unsigned int type_lex:3;
unsigned int num_lex:11;
}
Ⱦɜɨɟɬɨɱɢɟ ɫ ɰɟɥɵɦ ɱɢɫɥɨɦ ɩɨɫɥɟ ɢɦɟɧɢ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ ɭɤɚɡɵɜɚɟɬ,
ɱɬɨ ɷɬɨ ɛɢɬɨɜɨɟ ɩɨɥɟ, ɚ ɰɟɥɨɟ ɱɢɫɥɨ ɡɚɞɚɟɬ ɪɚɡɦɟɪ ɩɨɥɹ ɜ ɛɢɬɚɯ.
Ɉɛɴɟɞɢɧɟɧɢɟ ɦɨɠɧɨ ɨɩɪɟɞɟɥɢɬɶ ɤɚɤ ɫɬɪɭɤɬɭɪɭ, ɜɫɟ ɤɨɦɩɨɧɟɧɬɵ ɤɨɬɨ-
ɪɨɣ ɪɚɡɦɟɳɚɸɬɫɹ ɜ ɩɚɦɹɬɢ ɫ ɨɞɧɨɝɨ ɢ ɬɨɝɨ ɠɟ ɚɞɪɟɫɚ. Ɍɚɤɢɦ ɨɛɪɚɡɨɦ, ɨɛɴ-
ɟɞɢɧɟɧɢɟ ɜ ɤɚɠɞɵɣ ɦɨɦɟɧɬ ɜɪɟɦɟɧɢ ɫɨɞɟɪɠɢɬ ɨɞɢɧ ɢɡ ɜɨɡɦɨɠɧɵɯ ɜɚɪɢɚɧ-
ɬɨɜ ɡɧɚɱɟɧɢɣ. Ⱦɥɹ ɪɚɡɦɟɳɟɧɢɹ ɨɛɴɟɞɢɧɟɧɢɹ ɜ ɩɚɦɹɬɢ ɜɵɞɟɥɹɟɬɫɹ ɭɱɚɫɬɨɤ,
ɞɨɫɬɚɬɨɱɧɵɣ ɞɥɹ ɪɚɡɦɟɳɟɧɢɹ ɱɥɟɧɚ ɨɛɴɟɞɢɧɟɧɢɹ ɫɚɦɨɝɨ ɛɨɥɶɲɨɝɨ ɪɚɡɦɟ-
ɪɚ. ɉɪɢɦɟɧɟɧɢɟ ɨɛɴɟɞɢɧɟɧɢɹ ɬɚɤɠɟ ɩɨɡɜɨɥɹɟɬ ɨɛɪɚɳɚɬɶɫɹ ɤ ɨɞɧɨɦɭ ɢ ɬɨɦɭ
ɠɟ ɩɨɥɸ ɩɚɦɹɬɢ ɩɨ ɪɚɡɧɵɦ ɢɦɟɧɚɦ ɢ ɢɧɬɟɪɩɪɟɬɢɪɨɜɚɬɶ ɤɚɤ ɡɧɚɱɟɧɢɹ ɪɚɡ-
ɧɵɯ ɬɢɩɨɜ.
Ɉɩɢɫɚɧɢɟ ɨɛɴɟɞɢɧɟɧɢɹ ɫɬɪɨɢɬɫɹ ɩɨ ɬɨɣ ɠɟ ɫɯɟɦɟ, ɱɬɨ ɢ ɨɩɢɫɚɧɢɟ
ɫɬɪɭɤɬɭɪɵ, ɧɨ ɜɦɟɫɬɨ ɤɥɸɱɟɜɨɝɨ ɫɥɨɜɚ
struct ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɥɨɜɨ union,
ɧɚɩɪɢɦɟɪ, ɨɛɴɟɞɢɧɟɧɢɟ
uword ɩɨɡɜɨɥɹɟɬ ɢɧɬɟɪɩɪɟɬɢɪɨɜɚɬɶ ɩɨɥɟ ɩɚɦɹɬɢ
ɥɢɛɨ ɤɚɤ
unsigned int, ɥɢɛɨ ɤɚɤ ɦɚɫɫɢɜ ɢɡ ɞɜɭɯ ɷɥɟɦɟɧɬɨɜ ɬɢɩɚ
unsigned char.
union uword
{
unsigned int u;
unsigned char b[2];
}
15
    ɉɨɫɤɨɥɶɤɭ ɩɪɢ ɨɩɢɫɚɧɢɢ ɱɥɟɧɨɜ ɫɬɪɭɤɬɭɪɵ ɞɨɥɠɧɵ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ
ɬɨɥɶɤɨ ɪɚɧɟɟ ɨɩɪɟɞɟɥɟɧɧɵɟ ɢɦɟɧɚ ɬɢɩɨɜ, ɩɪɟɞɭɫɦɨɬɪɟɧ ɜɚɪɢɚɧɬ ɩɪɟɞɜɚɪɢ-
ɬɟɥɶɧɨɝɨ ɨɛɴɹɜɥɟɧɢɹ ɫɬɪɭɤɬɭɪɵ, ɡɚɞɚɸɳɢɣ ɬɨɥɶɤɨ ɢɦɹ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ.
ɇɚɩɪɢɦɟɪ, ɱɬɨɛɵ ɨɩɢɫɚɬɶ ɷɥɟɦɟɧɬ ɞɜɨɢɱɧɨɝɨ ɞɟɪɟɜɚ, ɫɨɞɟɪɠɚɳɢɣ ɭɤɚɡɚɬɟ-
ɥɢ ɧɚ ɥɟɜɭɸ ɢ ɩɪɚɜɭɸ ɜɟɬɜɢ ɞɟɪɟɜɚ, ɢ ɭɤɚɡɚɬɟɥɶ ɧɚ ɧɟɤɨɬɨɪɭɸ ɫɬɪɭɤɬɭɪɭ
ɬɢɩɚ Value, ɫɨɞɟɪɠɚɳɭɸ ɡɧɚɱɟɧɢɟ ɞɚɧɧɨɝɨ ɜ ɭɡɥɟ, ɦɨɠɧɨ ɩɨɫɬɭɩɢɬɶ ɬɚɤ:
     struct Value;
     struct Tree_element
        {
        Value *val;
        Tree_element *left, *right;
        };
    ɑɥɟɧɚɦɢ ɫɬɪɭɤɬɭɪ ɦɨɝɭɬ ɛɵɬɶ ɬɚɤ ɧɚɡɵɜɚɟɦɵɟ ɛɢɬɨɜɵɟ ɩɨɥɹ, ɤɨɝɞɚ ɜ
ɩɨɥɟ ɩɚɦɹɬɢ ɩɟɪɟɦɟɧɧɨɣ ɰɟɥɨɝɨ ɬɢɩɚ (int ɢɥɢ unsigned int) ɪɚɡɦɟɳɚɟɬɫɹ
ɧɟɫɤɨɥɶɤɨ ɰɟɥɵɯ ɞɚɧɧɵɯ ɦɟɧɶɲɟɣ ɞɥɢɧɵ. ɉɭɫɬɶ, ɧɚɩɪɢɦɟɪ, ɜ ɧɟɤɨɬɨɪɨɣ
ɩɪɨɝɪɚɦɦɟ ɫɢɧɬɚɤɫɢɱɟɫɤɨɝɨ ɪɚɡɛɨɪɚ ɨɩɢɫɚɧɢɟ ɥɟɤɫɟɦɵ ɫɨɞɟɪɠɢɬ ɬɢɩ ɥɟɤ-
ɫɟɦɵ (ɞɨ ɲɟɫɬɢ ɡɧɚɱɟɧɢɣ) ɢ ɩɨɪɹɞɤɨɜɵɣ ɧɨɦɟɪ ɥɟɤɫɟɦɵ ɜ ɬɚɛɥɢɰɟ ɫɨɨɬ-
ɜɟɬɫɬɜɭɸɳɟɝɨ ɬɢɩɚ (ɞɨ 2000 ɡɧɚɱɟɧɢɣ). Ⱦɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɡɧɚɱɟɧɢɹ ɬɢɩɚ
ɥɟɤɫɟɦɵ ɞɨɫɬɚɬɨɱɧɨ ɬɪɟɯ ɞɜɨɢɱɧɵɯ ɪɚɡɪɹɞɨɜ (ɬɪɟɯ ɛɢɬ), ɚ ɞɥɹ ɩɪɟɞɫɬɚɜɥɟ-
ɧɢɹ ɱɢɫɟɥ ɨɬ 0 ɞɨ 2000 – ɨɞɢɧɧɚɞɰɚɬɢ ɞɜɨɢɱɧɵɯ ɪɚɡɪɹɞɨɜ (11 ɛɢɬ). Ɉɩɢɫɚ-
ɧɢɟ ɫɬɪɭɤɬɭɪɵ, ɫɨɞɟɪɠɚɳɟɣ ɫɜɟɞɟɧɢɹ ɨ ɥɟɤɫɟɦɟ, ɦɨɠɟɬ ɜɵɝɥɹɞɟɬɶ ɬɚɤ:
     struct Lexema
        {
        unsigned int type_lex:3;
        unsigned int num_lex:11;
        }
     Ⱦɜɨɟɬɨɱɢɟ ɫ ɰɟɥɵɦ ɱɢɫɥɨɦ ɩɨɫɥɟ ɢɦɟɧɢ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ ɭɤɚɡɵɜɚɟɬ,
ɱɬɨ ɷɬɨ ɛɢɬɨɜɨɟ ɩɨɥɟ, ɚ ɰɟɥɨɟ ɱɢɫɥɨ ɡɚɞɚɟɬ ɪɚɡɦɟɪ ɩɨɥɹ ɜ ɛɢɬɚɯ.
     Ɉɛɴɟɞɢɧɟɧɢɟ ɦɨɠɧɨ ɨɩɪɟɞɟɥɢɬɶ ɤɚɤ ɫɬɪɭɤɬɭɪɭ, ɜɫɟ ɤɨɦɩɨɧɟɧɬɵ ɤɨɬɨ-
ɪɨɣ ɪɚɡɦɟɳɚɸɬɫɹ ɜ ɩɚɦɹɬɢ ɫ ɨɞɧɨɝɨ ɢ ɬɨɝɨ ɠɟ ɚɞɪɟɫɚ. Ɍɚɤɢɦ ɨɛɪɚɡɨɦ, ɨɛɴ-
ɟɞɢɧɟɧɢɟ ɜ ɤɚɠɞɵɣ ɦɨɦɟɧɬ ɜɪɟɦɟɧɢ ɫɨɞɟɪɠɢɬ ɨɞɢɧ ɢɡ ɜɨɡɦɨɠɧɵɯ ɜɚɪɢɚɧ-
ɬɨɜ ɡɧɚɱɟɧɢɣ. Ⱦɥɹ ɪɚɡɦɟɳɟɧɢɹ ɨɛɴɟɞɢɧɟɧɢɹ ɜ ɩɚɦɹɬɢ ɜɵɞɟɥɹɟɬɫɹ ɭɱɚɫɬɨɤ,
ɞɨɫɬɚɬɨɱɧɵɣ ɞɥɹ ɪɚɡɦɟɳɟɧɢɹ ɱɥɟɧɚ ɨɛɴɟɞɢɧɟɧɢɹ ɫɚɦɨɝɨ ɛɨɥɶɲɨɝɨ ɪɚɡɦɟ-
ɪɚ. ɉɪɢɦɟɧɟɧɢɟ ɨɛɴɟɞɢɧɟɧɢɹ ɬɚɤɠɟ ɩɨɡɜɨɥɹɟɬ ɨɛɪɚɳɚɬɶɫɹ ɤ ɨɞɧɨɦɭ ɢ ɬɨɦɭ
ɠɟ ɩɨɥɸ ɩɚɦɹɬɢ ɩɨ ɪɚɡɧɵɦ ɢɦɟɧɚɦ ɢ ɢɧɬɟɪɩɪɟɬɢɪɨɜɚɬɶ ɤɚɤ ɡɧɚɱɟɧɢɹ ɪɚɡ-
ɧɵɯ ɬɢɩɨɜ.
     Ɉɩɢɫɚɧɢɟ ɨɛɴɟɞɢɧɟɧɢɹ ɫɬɪɨɢɬɫɹ ɩɨ ɬɨɣ ɠɟ ɫɯɟɦɟ, ɱɬɨ ɢ ɨɩɢɫɚɧɢɟ
ɫɬɪɭɤɬɭɪɵ, ɧɨ ɜɦɟɫɬɨ ɤɥɸɱɟɜɨɝɨ ɫɥɨɜɚ struct ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɥɨɜɨ union,
ɧɚɩɪɢɦɟɪ, ɨɛɴɟɞɢɧɟɧɢɟ uword ɩɨɡɜɨɥɹɟɬ ɢɧɬɟɪɩɪɟɬɢɪɨɜɚɬɶ ɩɨɥɟ ɩɚɦɹɬɢ
ɥɢɛɨ ɤɚɤ unsigned int, ɥɢɛɨ ɤɚɤ ɦɚɫɫɢɜ ɢɡ ɞɜɭɯ ɷɥɟɦɟɧɬɨɜ ɬɢɩɚ
unsigned char.
     union uword
        {
        unsigned int u;
        unsigned char b[2];
        }
                                  15