ВУЗ:
Составители:
Рубрика:
2.3. ɋɬɪɭɤɬɭɪɵ ɢ ɨɛɴɟɞɢɧɟɧɢɹ
ɇɚɪɹɞɭ ɫ ɦɚɫɫɢɜɚɦɢ ɜ ɋɢ/ɋɢ++ ɢɦɟɸɬɫɹ ɚɝɪɟɝɚɬɵ ɞɚɧɧɵɯ ɬɢɩɚ ɫɬɪɭɤ-
ɬɭɪ ɢ ɨɛɴɟɞɢɧɟɧɢɣ. Ɍɢɩ ɫɬɪɭɤɬɭɪɵ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɭɩɨɪɹɞɨɱɟɧɧɭɸ ɫɨ-
ɜɨɤɭɩɧɨɫɬɶ ɞɚɧɧɵɯ ɪɚɡɥɢɱɧɵɯ ɬɢɩɨɜ, ɤ ɤɨɬɨɪɨɣ ɦɨɠɧɨ ɨɛɪɚɳɚɬɶɫɹ ɤɚɤ ɤ
ɟɞɢɧɨɦɭ ɰɟɥɨɦɭ. Ɉɩɢɫɚɧɢɟ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ:
struct ɢɞɟɧɬɢɮɢɤɚɬɨɪ {ɞɟɤɥɚɪɚɬɨɪɵ ɱɥɟɧɨɜ}
ɞɟɤɥɚɪɚɬɨɪɵ_ɢɧɢɰɢɚɥɢɡɚɬɨɪɵ;
Ɍɚɤɨɟ ɨɛɴɹɜɥɟɧɢɟ ɜɵɩɨɥɧɹɟɬ ɞɜɟ ɮɭɧɤɰɢɢ: ɜɨ-ɩɟɪɜɵɯ, ɨɛɴɹɜɥɹɟɬɫɹ
ɫɬɪɭɤɬɭɪɧɵɣ ɬɢɩ, ɜɨ-ɜɬɨɪɵɯ, ɨɛɴɹɜɥɹɸɬɫɹ ɩɟɪɟɦɟɧɧɵɟ ɷɬɨɝɨ ɬɢɩɚ.
ɂɞɟɧɬɢɮɢɤɚɬɨɪ ɩɨɫɥɟ ɤɥɸɱɟɜɨɝɨ ɫɥɨɜɚ
struct ɹɜɥɹɟɬɫɹ ɢɦɟɧɟɦ
ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ. ɂɦɹ ɬɢɩɚ ɦɨɠɟɬ ɨɬɫɭɬɫɬɜɨɜɚɬɶ, ɬɨɝɞɚ ɬɢɩ ɛɭɞɟɬ ɛɟɡɵ-
ɦɹɧɧɵɦ, ɢ ɜ ɞɪɭɝɢɯ ɱɚɫɬɹɯ ɩɪɨɝɪɚɦɦɵ ɧɟɥɶɡɹ ɛɭɞɟɬ ɨɛɴɹɜɥɹɬɶ ɞɚɧɧɵɟ ɷɬɨ-
ɝɨ ɬɢɩɚ. Ⱦɟɤɥɚɪɚɬɨɪɵ_ɢɧɢɰɢɚɥɢɡɚɬɨɪɵ ɨɛɴɹɜɥɹɸɬ ɤɨɧɤɪɟɬɧɵɟ ɩɟɪɟɦɟɧ-
ɧɵɟ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ, ɬ.ɟ. ɞɚɧɧɵɟ ɨɩɢɫɚɧɧɨɝɨ ɬɢɩɚ, ɭɤɚɡɚɬɟɥɢ ɧɚ ɷɬɨɬ ɬɢɩ
ɢ ɦɚɫɫɢɜɵ ɞɚɧɧɵɯ. Ⱦɟɤɥɚɪɚɬɨɪɵ_ɢɧɢɰɢɚɥɢɡɚɬɨɪɵ ɦɨɝɭɬ ɨɬɫɭɬɫɬɜɨɜɚɬɶ, ɜ
ɷɬɨɦ ɫɥɭɱɚɟ ɨɛɴɹɜɥɟɧɢɟ ɨɩɢɫɵɜɚɟɬ ɬɨɥɶɤɨ ɬɢɩ ɫɬɪɭɤɬɭɪɵ.
ɋɬɪɭɤɬɭɪɚ, ɨɩɢɫɵɜɚɸɳɚɹ ɬɨɱɤɭ ɧɚ ɩɥɨɫɤɨɫɬɢ, ɦɨɠɟɬ ɛɵɬɶ ɨɩɪɟɞɟɥɟɧɚ
ɬɚɤ:
struct Point_struct // ɂɦɹ ɫɬɪɭɤɬɭɪɵ
{int x, y;} // Ⱦɟɤɥɚɪɚɬɨɪɵ ɱɥɟɧɨɜ ɫɬɪɭɤɬɭɪɵ
point1, *ptr_to_point, arpoint[3]; * Ⱦɚɧɧɵɟ
ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ */
ɑɥɟɧɵ (ɤɨɦɩɨɧɟɧɬɵ) ɫɬɪɭɤɬɭɪɵ ɨɩɢɫɵɜɚɸɬɫɹ ɚɧɚɥɨɝɢɱɧɨ ɞɚɧɧɵɦ ɫɨ-
ɨɬɜɟɬɫɬɜɭɸɳɟɝɨ ɬɢɩɚ ɢ ɦɨɝɭɬ ɛɵɬɶ ɫɤɚɥɹɪɧɵɦɢ ɞɚɧɧɵɦɢ, ɭɤɚɡɚɬɟɥɹɦɢ,
ɦɚɫɫɢɜɚɦɢ ɢɥɢ ɞɚɧɧɵɦɢ ɞɪɭɝɨɝɨ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ. ɇɚɩɪɢɦɟɪ, ɞɥɹ ɨɩɢɫɚ-
ɧɢɹ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ «ɩɪɹɦɨɭɝɨɥɶɧɢɤ ɫɨ ɫɬɨɪɨɧɚɦɢ, ɩɚɪɚɥɥɟɥɶɧɵɦɢ
ɨɫɹɦ ɤɨɨɪɞɢɧɚɬ» ɦɨɠɧɨ ɩɪɟɞɥɨɠɢɬɶ ɧɟɫɤɨɥɶɤɨ ɜɚɪɢɚɧɬɨɜ:
struct Rect1
{
Point p1; // Ʉɨɨɪɞɢɧɚɬɵ ɥɟɜɨɝɨ ɜɟɪɯɧɟɝɨ ɭɝɥɚ
Point p2; // Ʉɨɨɪɞɢɧɚɬɵ ɩɪɚɜɨɝɨ ɧɢɠɧɟɝɨ ɭɝɥɚ
};
struct Rect2
{
Point p[2];
};
struct Rect3
{
Point p; // Ʌɟɜɵɣ ɜɟɪɯɧɢɣ ɭɝɨɥ
int width; // ɒɢɪɢɧɚ
int high; // ȼɵɫɨɬɚ ɩɪɹɦɨɭɝɨɥɶɧɢɤɚ
};
14
2.3. ɋɬɪɭɤɬɭɪɵ ɢ ɨɛɴɟɞɢɧɟɧɢɹ ɇɚɪɹɞɭ ɫ ɦɚɫɫɢɜɚɦɢ ɜ ɋɢ/ɋɢ++ ɢɦɟɸɬɫɹ ɚɝɪɟɝɚɬɵ ɞɚɧɧɵɯ ɬɢɩɚ ɫɬɪɭɤ- ɬɭɪ ɢ ɨɛɴɟɞɢɧɟɧɢɣ. Ɍɢɩ ɫɬɪɭɤɬɭɪɵ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɭɩɨɪɹɞɨɱɟɧɧɭɸ ɫɨ- ɜɨɤɭɩɧɨɫɬɶ ɞɚɧɧɵɯ ɪɚɡɥɢɱɧɵɯ ɬɢɩɨɜ, ɤ ɤɨɬɨɪɨɣ ɦɨɠɧɨ ɨɛɪɚɳɚɬɶɫɹ ɤɚɤ ɤ ɟɞɢɧɨɦɭ ɰɟɥɨɦɭ. Ɉɩɢɫɚɧɢɟ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: struct ɢɞɟɧɬɢɮɢɤɚɬɨɪ {ɞɟɤɥɚɪɚɬɨɪɵ ɱɥɟɧɨɜ} ɞɟɤɥɚɪɚɬɨɪɵ_ɢɧɢɰɢɚɥɢɡɚɬɨɪɵ; Ɍɚɤɨɟ ɨɛɴɹɜɥɟɧɢɟ ɜɵɩɨɥɧɹɟɬ ɞɜɟ ɮɭɧɤɰɢɢ: ɜɨ-ɩɟɪɜɵɯ, ɨɛɴɹɜɥɹɟɬɫɹ ɫɬɪɭɤɬɭɪɧɵɣ ɬɢɩ, ɜɨ-ɜɬɨɪɵɯ, ɨɛɴɹɜɥɹɸɬɫɹ ɩɟɪɟɦɟɧɧɵɟ ɷɬɨɝɨ ɬɢɩɚ. ɂɞɟɧɬɢɮɢɤɚɬɨɪ ɩɨɫɥɟ ɤɥɸɱɟɜɨɝɨ ɫɥɨɜɚ struct ɹɜɥɹɟɬɫɹ ɢɦɟɧɟɦ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ. ɂɦɹ ɬɢɩɚ ɦɨɠɟɬ ɨɬɫɭɬɫɬɜɨɜɚɬɶ, ɬɨɝɞɚ ɬɢɩ ɛɭɞɟɬ ɛɟɡɵ- ɦɹɧɧɵɦ, ɢ ɜ ɞɪɭɝɢɯ ɱɚɫɬɹɯ ɩɪɨɝɪɚɦɦɵ ɧɟɥɶɡɹ ɛɭɞɟɬ ɨɛɴɹɜɥɹɬɶ ɞɚɧɧɵɟ ɷɬɨ- ɝɨ ɬɢɩɚ. Ⱦɟɤɥɚɪɚɬɨɪɵ_ɢɧɢɰɢɚɥɢɡɚɬɨɪɵ ɨɛɴɹɜɥɹɸɬ ɤɨɧɤɪɟɬɧɵɟ ɩɟɪɟɦɟɧ- ɧɵɟ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ, ɬ.ɟ. ɞɚɧɧɵɟ ɨɩɢɫɚɧɧɨɝɨ ɬɢɩɚ, ɭɤɚɡɚɬɟɥɢ ɧɚ ɷɬɨɬ ɬɢɩ ɢ ɦɚɫɫɢɜɵ ɞɚɧɧɵɯ. Ⱦɟɤɥɚɪɚɬɨɪɵ_ɢɧɢɰɢɚɥɢɡɚɬɨɪɵ ɦɨɝɭɬ ɨɬɫɭɬɫɬɜɨɜɚɬɶ, ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɨɛɴɹɜɥɟɧɢɟ ɨɩɢɫɵɜɚɟɬ ɬɨɥɶɤɨ ɬɢɩ ɫɬɪɭɤɬɭɪɵ. ɋɬɪɭɤɬɭɪɚ, ɨɩɢɫɵɜɚɸɳɚɹ ɬɨɱɤɭ ɧɚ ɩɥɨɫɤɨɫɬɢ, ɦɨɠɟɬ ɛɵɬɶ ɨɩɪɟɞɟɥɟɧɚ ɬɚɤ: struct Point_struct // ɂɦɹ ɫɬɪɭɤɬɭɪɵ {int x, y;} // Ⱦɟɤɥɚɪɚɬɨɪɵ ɱɥɟɧɨɜ ɫɬɪɭɤɬɭɪɵ point1, *ptr_to_point, arpoint[3]; * Ⱦɚɧɧɵɟ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ */ ɑɥɟɧɵ (ɤɨɦɩɨɧɟɧɬɵ) ɫɬɪɭɤɬɭɪɵ ɨɩɢɫɵɜɚɸɬɫɹ ɚɧɚɥɨɝɢɱɧɨ ɞɚɧɧɵɦ ɫɨ- ɨɬɜɟɬɫɬɜɭɸɳɟɝɨ ɬɢɩɚ ɢ ɦɨɝɭɬ ɛɵɬɶ ɫɤɚɥɹɪɧɵɦɢ ɞɚɧɧɵɦɢ, ɭɤɚɡɚɬɟɥɹɦɢ, ɦɚɫɫɢɜɚɦɢ ɢɥɢ ɞɚɧɧɵɦɢ ɞɪɭɝɨɝɨ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ. ɇɚɩɪɢɦɟɪ, ɞɥɹ ɨɩɢɫɚ- ɧɢɹ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ «ɩɪɹɦɨɭɝɨɥɶɧɢɤ ɫɨ ɫɬɨɪɨɧɚɦɢ, ɩɚɪɚɥɥɟɥɶɧɵɦɢ ɨɫɹɦ ɤɨɨɪɞɢɧɚɬ» ɦɨɠɧɨ ɩɪɟɞɥɨɠɢɬɶ ɧɟɫɤɨɥɶɤɨ ɜɚɪɢɚɧɬɨɜ: struct Rect1 { Point p1; // Ʉɨɨɪɞɢɧɚɬɵ ɥɟɜɨɝɨ ɜɟɪɯɧɟɝɨ ɭɝɥɚ Point p2; // Ʉɨɨɪɞɢɧɚɬɵ ɩɪɚɜɨɝɨ ɧɢɠɧɟɝɨ ɭɝɥɚ }; struct Rect2 { Point p[2]; }; struct Rect3 { Point p; // Ʌɟɜɵɣ ɜɟɪɯɧɢɣ ɭɝɨɥ int width; // ɒɢɪɢɧɚ int high; // ȼɵɫɨɬɚ ɩɪɹɦɨɭɝɨɥɶɧɢɤɚ }; 14
Страницы
- « первая
- ‹ предыдущая
- …
- 12
- 13
- 14
- 15
- 16
- …
- следующая ›
- последняя »