ɎȿȾȿɊȺɅɖɇɈȿ ȺȽȿɇɌɋɌȼɈ ɉɈ ɈȻɊȺɁɈȼȺɇɂɘ ȽɈɋɍȾȺɊɋɌȼȿɇɇɈȿ ɈȻɊȺɁɈȼȺɌȿɅɖɇɈȿ ɍɑɊȿɀȾȿɇɂȿ ȼɕɋɒȿȽɈ ɉɊɈɎȿɋɋɂɈɇȺɅɖɇɈȽɈ ɈȻɊȺɁɈȼȺɇɂə «ȼɈɊɈɇȿɀɋɄɂɃ ȽɈɋɍȾȺɊɋɌȼȿɇɇɕɃ ɍɇɂȼȿɊɋɂɌȿɌ»
Ɉɫɧɨɜɵ ɹɡɵɤɚ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ C++ ɫ ɩɪɢɦɟɧɟɧɢɟɦ ɬɟɯɧɨɥɨɝɢɢ ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɍɱɟɛɧɨ-ɦɟɬɨɞɢɱɟɫɤɨɟ ɩɨɫɨɛɢɟ ɞɥɹ ɜɭɡɨɜ
ɋɨɫɬɚɜɢɬɟɥɶ Ɇ.Ʉ. ɑɟɪɧɵɲɨɜ
ɂɡɞɚɬɟɥɶɫɤɨ-ɩɨɥɢɝɪɚɮɢɱɟɫɤɢɣ ɰɟɧɬɪ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ 2007
ɍɬɜɟɪɠɞɟɧɨ ɧɚɭɱɧɨ-ɦɟɬɨɞɢɱɟɫɤɢɦ ɫɨɜɟɬɨɦ ɮɚɤɭɥɶɬɟɬɚ ɉɆɆ 14 ɦɚɹ 2007 ɝ., ɩɪɨɬɨɤɨɥ ʋ 9
ɍɱɟɛɧɨ-ɦɟɬɨɞɢɱɟɫɤɨɟ ɩɨɫɨɛɢɟ ɩɨɞɝɨɬɨɜɥɟɧɨ ɧɚ ɤɚɮɟɞɪɟ ɦɚɬɟɦɚɬɢɱɟɫɤɨɝɨ ɨɛɟɫɩɟɱɟɧɢɹ ɗȼɆ ɮɚɤɭɥɶɬɟɬɚ ɉɆɆ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ/
Ɋɟɤɨɦɟɧɞɭɟɬɫɹ ɞɥɹ ɫɬɭɞɟɧɬɨɜ 2-ɝɨ ɤɭɪɫɚ ɮɚɤɭɥɶɬɟɬɚ ɉɆɆ, ɢɡɭɱɚɸɳɢɯ ɤɭɪɫ «Ɉɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɨɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ», ɚ ɬɚɤɠɟ ɫɬɭɞɟɧɬɨɜ, ɫɩɟɰɢɚɥɢɡɢɪɭɸɳɢɯɫɹ ɧɚ ɤɚɮɟɞɪɟ ɆɈ ɗȼɆ.
Ⱦɥɹ ɫɩɟɰɢɚɥɶɧɨɫɬɢ: 010501 – ɉɪɢɤɥɚɞɧɚɹ ɦɚɬɟɦɚɬɢɤɚ ɢ ɢɧɮɨɪɦɚɬɢɤɚ
2
1. Ɉɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɣ ɩɨɞɯɨɞ ɜ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɢ 1.1. Ɍɟɯɧɨɥɨɝɢɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ Ɍɟɯɧɨɥɨɝɢɹ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ – ɷɬɨ ɫɨɜɨɤɭɩɧɨɫɬɶ ɦɟɬɨɞɨɜ ɢ ɫɪɟɞɫɬɜ ɪɚɡɪɚɛɨɬɤɢ (ɧɚɩɢɫɚɧɢɹ) ɩɪɨɝɪɚɦɦ ɢ ɩɨɪɹɞɨɤ ɩɪɢɦɟɧɟɧɢɹ ɷɬɢɯ ɦɟɬɨɞɨɜ ɢ ɫɪɟɞɫɬɜ. ɇɚ ɪɚɧɧɢɯ ɷɬɚɩɚɯ ɪɚɡɜɢɬɢɹ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɤɨɝɞɚ ɩɪɨɝɪɚɦɦɵ ɩɪɟɞɫɬɚɜɥɹɥɢ ɫɨɛɨɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɦɚɲɢɧɧɵɯ ɤɨɦɚɧɞ, ɤɚɤɚɹ-ɥɢɛɨ ɬɟɯɧɨɥɨɝɢɹ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɨɬɫɭɬɫɬɜɨɜɚɥɚ. ɉɟɪɜɵɟ ɲɚɝɢ ɜ ɪɚɡɪɚɛɨɬɤɟ ɬɟɯɧɨɥɨɝɢɢ ɫɨɫɬɨɹɥɢ ɜ ɩɪɟɞɫɬɚɜɥɟɧɢɢ ɩɪɨɝɪɚɦɦɵ ɜ ɜɢɞɟ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɨɩɟɪɚɬɨɪɨɜ. ɇɚɩɢɫɚɧɢɸ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɦɚɲɢɧɧɵɯ ɤɨɦɚɧɞ ɩɪɟɞɲɟɫɬɜɨɜɚɥɨ ɫɨɫɬɚɜɥɟɧɢɟ ɨɩɟɪɚɬɨɪɧɨɣ ɫɯɟɦɵ, ɨɬɪɚɠɚɸɳɟɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɨɩɟɪɚɬɨɪɨɜ ɢ ɩɟɪɟɯɨɞɵ ɦɟɠɞɭ ɧɢɦɢ. Ɉɩɟɪɚɬɨɪɧɵɣ ɩɨɞɯɨɞ ɩɨɡɜɨɥɢɥ ɪɚɡɪɚɛɨɬɚɬɶ ɩɟɪɜɵɟ ɩɪɨɝɪɚɦɦɵ ɞɥɹ ɚɜɬɨɦɚɬɢɡɚɰɢɢ ɫɨɫɬɚɜɥɟɧɢɹ ɩɪɨɝɪɚɦɦ – ɬɚɤ ɧɚɡɵɜɚɟɦɵɟ ɫɨɫɬɚɜɥɹɸɳɢɟ ɩɪɨɝɪɚɦɦɵ. ɋ ɭɜɟɥɢɱɟɧɢɟɦ ɪɚɡɦɟɪɨɜ ɩɪɨɝɪɚɦɦ ɫɬɚɥɢ ɜɵɞɟɥɹɬɶ ɢɯ ɨɛɨɫɨɛɥɟɧɧɵɟ ɱɚɫɬɢ ɢ ɨɮɨɪɦɥɹɬɶ ɢɯ ɤɚɤ ɩɨɞɩɪɨɝɪɚɦɦɵ. ɑɚɫɬɶ ɬɚɤɢɯ ɩɨɞɩɪɨɝɪɚɦɦ ɨɛɴɟɞɢɧɹɥɚɫɶ ɜ ɛɢɛɥɢɨɬɟɤɢ, ɢɡ ɤɨɬɨɪɵɯ ɩɨɞɩɪɨɝɪɚɦɦɵ ɦɨɠɧɨ ɛɵɥɨ ɜɤɥɸɱɚɬɶ ɜ ɪɚɛɨɱɢɟ ɩɪɨɝɪɚɦɦɵ ɢ ɡɚɬɟɦ ɜɵɡɵɜɚɬɶ ɢɡ ɪɚɛɨɱɢɯ ɩɪɨɝɪɚɦɦ. ɗɬɨ ɩɨɥɨɠɢɥɨ ɧɚɱɚɥɨ ɩɪɨɰɟɞɭɪɧɨɦɭ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɸ – ɛɨɥɶɲɚɹ ɩɪɨɝɪɚɦɦɚ ɩɪɟɞɫɬɚɜɥɹɥɚɫɶ ɫɨɜɨɤɭɩɧɨɫɬɶɸ ɩɪɨɰɟɞɭɪ-ɩɨɞɩɪɨɝɪɚɦɦ. Ɉɞɧɚ ɢɡ ɩɨɞɩɪɨɝɪɚɦɦ ɹɜɥɹɥɚɫɶ ɝɥɚɜɧɨɣ, ɢ ɫ ɧɟɟ ɧɚɱɢɧɚɥɨɫɶ ɜɵɩɨɥɧɟɧɢɟ ɩɪɨɝɪɚɦɦɵ. ȼ 1958 ɝɨɞɭ ɛɵɥɢ ɪɚɡɪɚɛɨɬɚɧɵ ɩɟɪɜɵɟ ɹɡɵɤɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, Ɏɨɪɬɪɚɧ (FORTRAN) ɢ Ⱥɥɝɨɥ-58 (ALGOL). ɉɪɨɝɪɚɦɦɚ ɧɚ Ɏɨɪɬɪɚɧɟ ɫɨɫɬɨɹɥɚ ɢɡ ɝɥɚɜɧɨɣ ɩɪɨɝɪɚɦɦɵ ɢ ɧɟɤɨɬɨɪɨɝɨ ɤɨɥɢɱɟɫɬɜɚ ɩɪɨɰɟɞɭɪ – ɩɨɞɩɪɨɝɪɚɦɦ ɢ ɮɭɧɤɰɢɣ. ɉɪɨɝɪɚɦɦɚ ɧɚ Ⱥɥɝɨɥɟ-58 ɢ ɟɝɨ ɩɨɫɥɟɞɭɸɳɟɣ ɜɟɪɫɢɢ Ⱥɥɝɨɥɟ-60 ɩɪɟɞɫɬɚɜɥɹɥɚ ɫɨɛɨɣ ɟɞɢɧɨɟ ɰɟɥɨɟ, ɧɨ ɢɦɟɥɚ ɛɥɨɱɧɭɸ ɫɬɪɭɤɬɭɪɭ, ɜɤɥɸɱɚɸɳɭɸ ɝɥɚɜɧɵɣ ɛɥɨɤ ɢ ɜɥɨɠɟɧɧɵɟ ɛɥɨɤɢ ɩɨɞɩɪɨɝɪɚɦɦ ɢ ɮɭɧɤɰɢɣ. Ʉɨɦɩɢɥɹɬɨɪɵ ɞɥɹ Ɏɨɪɬɪɚɧɚ ɨɛɟɫɩɟɱɢɜɚɥɢ ɪɚɡɞɟɥɶɧɭɸ ɬɪɚɧɫɥɹɰɢɸ ɩɪɨɰɟɞɭɪ ɢ ɩɨɫɥɟɞɭɸɳɟɟ ɢɯ ɨɛɴɟɞɢɧɟɧɢɟ ɜ ɪɚɛɨɱɭɸ ɩɪɨɝɪɚɦɦɭ, ɩɟɪɜɵɟ ɤɨɦɩɢɥɹɬɨɪɵ ɞɥɹ Ⱥɥɝɨɥɚ ɩɪɟɞɩɨɥɚɝɚɥɢ, ɱɬɨ ɬɪɚɧɫɥɢɪɭɟɬɫɹ ɫɪɚɡɭ ɜɫɹ ɩɪɨɝɪɚɦɦɚ, ɪɚɡɞɟɥɶɧɚɹ ɬɪɚɧɫɥɹɰɢɹ ɩɪɨɰɟɞɭɪ ɧɟ ɨɛɟɫɩɟɱɢɜɚɥɚɫɶ. ɉɪɨɰɟɞɭɪɧɵɣ ɩɨɞɯɨɞ ɩɨɬɪɟɛɨɜɚɥ ɫɬɪɭɤɬɭɪɢɪɨɜɚɧɢɹ ɛɭɞɭɳɟɣ ɩɪɨɝɪɚɦɦɵ, ɪɚɡɞɟɥɟɧɢɹ ɟɟ ɧɚ ɨɬɞɟɥɶɧɵɟ ɩɪɨɰɟɞɭɪɵ. ɉɪɢ ɪɚɡɪɚɛɨɬɤɟ ɨɬɞɟɥɶɧɨɣ ɩɪɨɰɟɞɭɪɵ ɨ ɞɪɭɝɢɯ ɩɪɨɰɟɞɭɪɚɯ ɬɪɟɛɨɜɚɥɨɫɶ ɡɧɚɬɶ ɬɨɥɶɤɨ ɢɯ ɧɚɡɧɚɱɟɧɢɟ ɢ ɫɩɨɫɨɛ ɜɵɡɨɜɚ. ɉɨɹɜɢɥɚɫɶ ɜɨɡɦɨɠɧɨɫɬɶ ɩɟɪɟɪɚɛɚɬɵɜɚɬɶ ɨɬɞɟɥɶɧɵɟ ɩɪɨɰɟɞɭɪɵ, ɧɟ ɡɚɬɪɚɝɢɜɚɹ ɨɫɬɚɥɶɧɨɣ ɱɚɫɬɢ ɩɪɨɝɪɚɦɦɵ, ɫɨɤɪɚɳɚɹ ɩɪɢ ɷɬɨɦ ɡɚɬɪɚɬɵ ɬɪɭɞɚ ɢ ɦɚɲɢɧɧɨɝɨ ɜɪɟɦɟɧɢ ɧɚ ɪɚɡɪɚɛɨɬɤɭ ɢ ɦɨɞɟɪɧɢɡɚɰɢɸ ɩɪɨɝɪɚɦɦ. 3
ɋɥɟɞɭɸɳɢɦ ɲɚɝɨɦ ɜ ɭɝɥɭɛɥɟɧɢɢ ɫɬɪɭɤɬɭɪɢɪɨɜɚɧɢɹ ɩɪɨɝɪɚɦɦ ɫɬɚɥɨ ɬɚɤ ɧɚɡɵɜɚɟɦɨɟ ɫɬɪɭɤɬɭɪɧɨɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ, ɩɪɢ ɤɨɬɨɪɨɦ ɩɪɨɝɪɚɦɦɚ ɜ ɰɟɥɨɦ ɢ ɨɬɞɟɥɶɧɵɟ ɩɪɨɰɟɞɭɪɵ ɪɚɫɫɦɚɬɪɢɜɚɥɢɫɶ ɤɚɤ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɤɚɧɨɧɢɱɟɫɤɢɯ ɫɬɪɭɤɬɭɪ: ɥɢɧɟɣɧɵɯ ɭɱɚɫɬɤɨɜ, ɰɢɤɥɨɜ ɢ ɪɚɡɜɟɬɜɥɟɧɢɣ. ɉɨɹɜɢɥɚɫɶ ɜɨɡɦɨɠɧɨɫɬɶ ɱɢɬɚɬɶ ɢ ɩɪɨɜɟɪɹɬɶ ɩɪɨɝɪɚɦɦɭ ɤɚɤ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɵɣ ɬɟɤɫɬ, ɱɬɨ ɩɨɜɵɫɢɥɨ ɩɪɨɢɡɜɨɞɢɬɟɥɶɧɨɫɬɶ ɬɪɭɞɚ ɩɪɨɝɪɚɦɦɢɫɬɨɜ ɩɪɢ ɪɚɡɪɚɛɨɬɤɟ ɢ ɨɬɥɚɞɤɟ ɩɪɨɝɪɚɦɦ. ɋ ɰɟɥɶɸ ɩɨɜɵɲɟɧɢɹ ɫɬɪɭɤɬɭɪɧɨɫɬɢ ɩɪɨɝɪɚɦɦɵ ɛɵɥɢ ɜɵɞɜɢɧɭɬɵ ɬɪɟɛɨɜɚɧɢɹ ɤ ɛɨɥɶɲɟɣ ɧɟɡɚɜɢɫɢɦɨɫɬɢ ɩɨɞɩɪɨɝɪɚɦɦ, ɩɨɞɩɪɨɝɪɚɦɦɵ ɞɨɥɠɧɵ ɫɜɹɡɵɜɚɬɶɫɹ ɫ ɜɵɡɵɜɚɸɳɢɦɢ ɢɯ ɩɪɨɝɪɚɦɦɚɦɢ ɬɨɥɶɤɨ ɩɭɬɟɦ ɩɟɪɟɞɚɱɢ ɢɦ ɚɪɝɭɦɟɧɬɨɜ. ɂɫɩɨɥɶɡɨɜɚɧɢɟ ɜ ɩɨɞɩɪɨɝɪɚɦɦɚɯ ɩɟɪɟɦɟɧɧɵɯ, ɩɪɢɧɚɞɥɟɠɚɳɢɯ ɞɪɭɝɢɦ ɩɪɨɰɟɞɭɪɚɦ ɢɥɢ ɝɥɚɜɧɨɣ ɩɪɨɝɪɚɦɦɟ, ɫɬɚɥɨ ɫɱɢɬɚɬɶɫɹ ɧɟɠɟɥɚɬɟɥɶɧɵɦ. Ɍɟɯɧɨɥɨɝɢɢ ɩɪɨɰɟɞɭɪɧɨɝɨ ɢ ɫɬɪɭɤɬɭɪɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɡɚɬɪɨɧɭɥɢ ɩɪɟɠɞɟ ɜɫɟɝɨ ɩɪɨɰɟɫɫ ɨɩɢɫɚɧɢɹ ɚɥɝɨɪɢɬɦɚ ɤɚɤ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɲɚɝɨɜ, ɜɟɞɭɳɢɯ ɨɬ ɜɚɪɶɢɪɭɟɦɵɯ ɢɫɯɨɞɧɵɯ ɞɚɧɧɵɯ ɤ ɢɫɤɨɦɨɦɭ ɪɟɡɭɥɶɬɚɬɭ. Ⱦɥɹ ɪɟɲɟɧɢɹ ɫɩɟɰɢɚɥɶɧɵɯ ɡɚɞɚɱ ɫɬɚɥɢ ɪɚɡɪɚɛɚɬɵɜɚɬɶɫɹ ɹɡɵɤɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɟ ɧɚ ɤɨɧɤɪɟɬɧɵɣ ɤɥɚɫɫ ɡɚɞɚɱ: ɧɚ ɫɢɫɬɟɦɵ ɭɩɪɚɜɥɟɧɢɹ ɛɚɡɚɦɢ ɞɚɧɧɵɯ, ɢɦɢɬɚɰɢɨɧɧɨɟ ɦɨɞɟɥɢɪɨɜɚɧɢɟ ɢ ɬɚɤ ɞɚɥɟɟ. ɉɪɢ ɪɚɡɪɚɛɨɬɤɟ ɬɪɚɧɫɥɹɬɨɪɨɜ ɜɫɟ ɛɨɥɶɲɟ ɜɧɢɦɚɧɢɹ ɫɬɚɥɨ ɭɞɟɥɹɬɶɫɹ ɨɛɧɚɪɭɠɟɧɢɸ ɨɲɢɛɨɤ ɜ ɢɫɯɨɞɧɵɯ ɬɟɤɫɬɚɯ ɩɪɨɝɪɚɦɦ, ɨɛɟɫɩɟɱɢɜɚɹ ɷɬɢɦ ɫɨɤɪɚɳɟɧɢɟ ɡɚɬɪɚɬ ɜɪɟɦɟɧɢ ɧɚ ɨɬɥɚɞɤɭ ɩɪɨɝɪɚɦɦ. ɉɪɢɦɟɧɟɧɢɟ ɩɪɨɝɪɚɦɦ ɜ ɫɚɦɵɯ ɪɚɡɧɵɯ ɨɛɥɚɫɬɹɯ ɱɟɥɨɜɟɱɟɫɤɨɣ ɞɟɹɬɟɥɶɧɨɫɬɢ ɩɪɢɜɟɥɨ ɤ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɩɨɜɵɲɟɧɢɹ ɧɚɞɟɠɧɨɫɬɢ ɜɫɟɝɨ ɩɪɨɝɪɚɦɦɧɨɝɨ ɨɛɟɫɩɟɱɟɧɢɹ. Ɉɞɧɢɦ ɢɡ ɧɚɩɪɚɜɥɟɧɢɣ ɫɨɜɟɪɲɟɧɫɬɜɨɜɚɧɢɹ ɹɡɵɤɨɜ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɫɬɚɥɨ ɩɨɜɵɲɟɧɢɟ ɭɪɨɜɧɹ ɬɢɩɢɡɚɰɢɢ ɞɚɧɧɵɯ. Ɍɟɨɪɢɹ ɬɢɩɨɜ ɞɚɧɧɵɯ ɢɫɯɨɞɢɬ ɢɡ ɬɨɝɨ, ɱɬɨ ɤɚɠɞɨɟ ɢɫɩɨɥɶɡɭɟɦɨɟ ɜ ɩɪɨɝɪɚɦɦɟ ɞɚɧɧɨɟ ɩɪɢɧɚɞɥɟɠɢɬ ɨɞɧɨɦɭ ɢ ɬɨɥɶɤɨ ɨɞɧɨɦɭ ɬɢɩɭ ɞɚɧɧɵɯ. Ɍɢɩ ɞɚɧɧɵɯ ɨɩɪɟɞɟɥɹɟɬ ɦɧɨɠɟɫɬɜɨ ɜɨɡɦɨɠɧɵɯ ɡɧɚɱɟɧɢɣ ɞɚɧɧɵɯ ɢ ɧɚɛɨɪ ɨɩɟɪɚɰɢɣ, ɞɨɩɭɫɬɢɦɵɯ ɧɚɞ ɷɬɢɦɢ ɞɚɧɧɵɦɢ. Ⱦɚɧɧɵɟ ɤɨɧɤɪɟɬɧɨɝɨ ɬɢɩɚ ɜ ɪɹɞɟ ɫɥɭɱɚɟɜ ɦɨɝɭɬ ɛɵɬɶ ɩɪɟɨɛɪɚɡɨɜɚɧɵ ɜ ɞɚɧɧɵɟ ɞɪɭɝɨɝɨ ɬɢɩɚ, ɧɨ ɬɚɤɨɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɟ ɞɨɥɠɧɨ ɛɵɬɶ ɹɜɧɨ ɩɪɟɞɫɬɚɜɥɟɧɨ ɜ ɩɪɨɝɪɚɦɦɟ. ȼ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɫɬɟɩɟɧɢ ɜɵɩɨɥɧɟɧɢɹ ɩɟɪɟɱɢɫɥɟɧɧɵɯ ɬɪɟɛɨɜɚɧɢɣ ɦɨɠɧɨ ɝɨɜɨɪɢɬɶ ɨɛ ɭɪɨɜɧɟ ɬɢɩɢɡɚɰɢɢ ɬɨɝɨ ɢɥɢ ɢɧɨɝɨ ɹɡɵɤɚ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ. ɋɬɪɟɦɥɟɧɢɟ ɩɨɜɵɫɢɬɶ ɭɪɨɜɟɧɶ ɬɢɩɢɡɚɰɢɢ ɹɡɵɤɚ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɩɪɢɜɟɥɨ ɤ ɩɨɹɜɥɟɧɢɸ ɹɡɵɤɚ ɉɚɫɤɚɥɶ (PASCAL), ɤɨɬɨɪɵɣ ɫɱɢɬɚɟɬɫɹ ɫɬɪɨɝɨ ɬɢɩɢɡɢɪɨɜɚɧɧɵɦ ɹɡɵɤɨɦ, ɯɨɬɹ ɢ ɜ ɧɟɦ ɪɚɡɪɟɲɟɧɵ ɧɟɤɨɬɨɪɵɟ ɧɟɹɜɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɨɜ, ɧɚɩɪɢɦɟɪ, ɰɟɥɵɯ ɞɚɧɧɵɯ ɜ ɜɟɳɟɫɬɜɟɧɧɵɟ. ɉɪɢɦɟɧɟɧɢɟ ɫɬɪɨɝɨ ɬɢɩɢɡɢɪɨɜɚɧɧɨɝɨ ɹɡɵɤɚ ɩɪɢ ɧɚɩɢɫɚɧɢɢ ɩɪɨɝɪɚɦɦɵ ɩɨɡɜɨɥɹɟɬ ɟɳɟ ɩɪɢ ɬɪɚɧɫɥɹɰɢɢ ɢɫɯɨɞɧɨɝɨ ɬɟɤɫɬɚ ɜɵɹɜɢɬɶ ɦɧɨɝɢɟ ɨɲɢɛɤɢ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɞɚɧɧɵɯ ɢ ɷɬɢɦ ɩɨɜɵɫɢɬɶ ɧɚɞɟɠɧɨɫɬɶ ɩɪɨɝɪɚɦɦɵ. ȼɦɟɫɬɟ ɫ ɬɟɦ ɫɬɪɨɝɚɹ ɬɢɩɢɡɚɰɢɹ ɫɤɨɜɵɜɚɥɚ ɫɜɨɛɨɞɭ ɩɪɨɝɪɚɦɦɢɫɬɚ, ɡɚɬɪɭɞɧɹɥɚ ɩɪɢɦɟɧɟɧɢɟ ɧɟɤɨɬɨɪɵɯ ɩɪɢɟɦɨɜ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɞɚɧɧɵɯ, ɱɚɫɬɨ ɢɫɩɨɥɶɡɭɟɦɵɯ ɜ ɫɢɫɬɟɦɧɨɦ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɢ. ɉɪɚɤɬɢɱɟɫɤɢ ɨɞɧɨɜɪɟɦɟɧɧɨ ɫ ɉɚɫɤɚɥɟɦ ɛɵɥ ɪɚɡɪɚɛɨɬɚɧ ɹɡɵɤ ɋɢ (C), ɜ ɛɨɥɶɲɟɣ 4
ɫɬɟɩɟɧɢ ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɣ ɧɚ ɫɢɫɬɟɦɧɨɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ ɢ ɨɬɧɨɫɹɳɢɣɫɹ ɤ ɫɥɚɛɨ ɬɢɩɢɡɢɪɨɜɚɧɧɵɦ ɹɡɵɤɚɦ. ȼɫɟ ɭɧɢɜɟɪɫɚɥɶɧɵɟ ɹɡɵɤɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɧɟɫɦɨɬɪɹ ɧɚ ɪɚɡɥɢɱɢɹ ɜ ɫɢɧɬɚɤɫɢɫɟ ɢ ɢɫɩɨɥɶɡɭɟɦɵɯ ɤɥɸɱɟɜɵɯ ɫɥɨɜɚɯ, ɪɟɚɥɢɡɭɸɬ ɨɞɧɢ ɢ ɬɟ ɠɟ ɤɚɧɨɧɢɱɟɫɤɢɟ ɫɬɪɭɤɬɭɪɵ: ɨɩɟɪɚɬɨɪɵ ɩɪɢɫɜɚɢɜɚɧɢɹ, ɰɢɤɥɵ ɢ ɪɚɡɜɟɬɜɥɟɧɢɹ. ȼɨ ɜɫɟɯ ɫɨɜɪɟɦɟɧɧɵɯ ɹɡɵɤɚɯ ɩɪɢɫɭɬɫɬɜɭɸɬ ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɵɟ (ɛɚɡɨɜɵɟ) ɬɢɩɵ ɞɚɧɧɵɯ (ɰɟɥɵɟ ɢ ɜɟɳɟɫɬɜɟɧɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɬɢɩɵ, ɫɢɦɜɨɥɶɧɵɣ ɢ, ɜɨɡɦɨɠɧɨ, ɫɬɪɨɤɨɜɵɣ ɬɢɩ), ɢɦɟɟɬɫɹ ɜɨɡɦɨɠɧɨɫɬɶ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɚɝɪɟɝɚɬɨɜ ɞɚɧɧɵɯ, ɜ ɬɨɦ ɱɢɫɥɟ ɦɚɫɫɢɜɨɜ ɢ ɫɬɪɭɤɬɭɪ (ɡɚɩɢɫɟɣ). Ⱦɥɹ ɚɪɢɮɦɟɬɢɱɟɫɤɢɯ ɞɚɧɧɵɯ ɪɚɡɪɟɲɟɧɵ ɨɛɵɱɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɨɩɟɪɚɰɢɢ, ɞɥɹ ɚɝɪɟɝɚɬɨɜ ɞɚɧɧɵɯ ɨɛɵɱɧɨ ɩɪɟɞɭɫɦɨɬɪɟɧɚ ɬɨɥɶɤɨ ɨɩɟɪɚɰɢɹ ɩɪɢɫɜɚɢɜɚɧɢɹ ɢ ɜɨɡɦɨɠɧɨɫɬɶ ɨɛɪɚɳɟɧɢɹ ɤ ɷɥɟɦɟɧɬɚɦ ɚɝɪɟɝɚɬɚ. ȼɦɟɫɬɟ ɫ ɬɟɦ ɩɪɢ ɪɚɡɪɚɛɨɬɤɟ ɩɪɨɝɪɚɦɦɵ ɞɥɹ ɪɟɲɟɧɢɹ ɤɨɧɤɪɟɬɧɨɣ ɩɪɢɤɥɚɞɧɨɣ ɡɚɞɚɱɢ ɠɟɥɚɬɟɥɶɧɚ, ɜɨɡɦɨɠɧɨ, ɛɨɥɶɲɚɹ ɤɨɧɰɟɩɬɭɚɥɶɧɚɹ ɛɥɢɡɨɫɬɶ ɬɟɤɫɬɚ ɩɪɨɝɪɚɦɦɵ ɤ ɨɩɢɫɚɧɢɸ ɡɚɞɚɱɢ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɪɟɲɟɧɢɟ ɡɚɞɚɱɢ ɬɪɟɛɭɟɬ ɜɵɩɨɥɧɟɧɢɹ ɨɩɟɪɚɰɢɣ ɧɚɞ ɤɨɦɩɥɟɤɫɧɵɦɢ ɱɢɫɥɚɦɢ ɢɥɢ ɤɜɚɞɪɚɬɧɵɦɢ ɦɚɬɪɢɰɚɦɢ, ɠɟɥɚɬɟɥɶɧɨ, ɱɬɨɛɵ ɜ ɩɪɨɝɪɚɦɦɟ ɹɜɧɨ ɩɪɢɫɭɬɫɬɜɨɜɚɥɢ ɨɩɟɪɚɬɨɪɵ ɫɥɨɠɟɧɢɹ, ɜɵɱɢɬɚɧɢɹ, ɭɦɧɨɠɟɧɢɹ ɢ ɞɟɥɟɧɢɹ ɞɚɧɧɵɯ ɬɢɩɚ «ɤɨɦɩɥɟɤɫɧɵɟ ɱɢɫɥɚ», ɫɥɨɠɟɧɢɹ, ɜɵɱɢɬɚɧɢɹ, ɭɦɧɨɠɟɧɢɹ ɢ ɨɛɪɚɳɟɧɢɹ ɞɚɧɧɵɯ ɬɢɩɚ «ɤɜɚɞɪɚɬɧɵɟ ɦɚɬɪɢɰɵ». Ɋɟɲɟɧɢɟ ɷɬɨɣ ɩɪɨɛɥɟɦɵ ɞɨɫɬɢɠɢɦɨ ɧɟɫɤɨɥɶɤɢɦɢ ɩɭɬɹɦɢ: x ɩɨɫɬɪɨɟɧɢɟɦ ɹɡɵɤɚ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɫɨɞɟɪɠɚɳɟɝɨ ɤɚɤ ɦɨɠɧɨ ɛɨɥɶɲɟ ɬɢɩɨɜ ɞɚɧɧɵɯ, ɢ ɜɵɛɨɪɨɦ ɞɥɹ ɤɚɠɞɨɝɨ ɤɥɚɫɫɚ ɡɚɞɚɱ ɧɟɤɨɬɨɪɨɝɨ ɩɨɞɦɧɨɠɟɫɬɜɚ ɷɬɨɝɨ ɹɡɵɤɚ. Ɍɚɤɨɣ ɹɡɵɤ ɢɧɨɝɞɚ ɧɚɡɵɜɚɸɬ ɹɡɵɤɨɦ-ɨɛɨɥɨɱɤɨɣ. ɇɚ ɪɨɥɶ ɹɡɵɤɚ-ɨɛɨɥɨɱɤɢ ɩɪɟɬɟɧɞɨɜɚɥ ɹɡɵɤ ɉɅ/1 (PL/1), ɨɤɚɡɚɜɲɢɣɫɹ ɧɚɫɬɨɥɶɤɨ ɫɥɨɠɧɵɦ, ɱɬɨ ɬɚɤ ɢ ɧɟ ɭɞɚɥɨɫɶ ɩɨɫɬɪɨɢɬɶ ɟɝɨ ɮɨɪɦɚɥɢɡɨɜɚɧɧɨɟ ɨɩɢɫɚɧɢɟ. Ɉɬɫɭɬɫɬɜɢɟ ɮɨɪɦɚɥɢɡɨɜɚɧɧɨɝɨ ɨɩɢɫɚɧɢɹ, ɨɞɧɚɤɨ, ɧɟ ɩɨɦɟɲɚɥɨ ɲɢɪɨɤɨɦɭ ɩɪɢɦɟɧɟɧɢɸ ɉɅ/1 ɤɚɤ ɜ Ɂɚɩɚɞɧɨɣ ȿɜɪɨɩɟ, ɬɚɤ ɢ ɜ ɋɋɋɊ; x ɩɨɫɬɪɨɟɧɢɟɦ ɪɚɫɲɢɪɹɟɦɨɝɨ ɹɡɵɤɚ, ɫɨɞɟɪɠɚɳɟɝɨ ɧɟɛɨɥɶɲɨɟ ɹɞɪɨ ɢ ɞɨɩɭɫɤɚɸɳɟɝɨ ɪɚɫɲɢɪɟɧɢɟ, ɞɨɩɨɥɧɹɸɳɟɟ ɹɡɵɤ ɬɢɩɚɦɢ ɞɚɧɧɵɯ ɢ ɨɩɟɪɚɬɨɪɚɦɢ, ɨɬɪɚɠɚɸɳɢɦɢ ɤɨɧɰɟɩɬɭɚɥɶɧɭɸ ɫɭɳɧɨɫɬɶ ɤɨɧɤɪɟɬɧɨɝɨ ɤɥɚɫɫɚ ɡɚɞɚɱ. Ɍɚɤɨɣ ɹɡɵɤ ɧɚɡɵɜɚɸɬ ɹɡɵɤɨɦ-ɹɞɪɨɦ. Ʉɚɤ ɹɡɵɤ-ɹɞɪɨ ɛɵɥɢ ɪɚɡɪɚɛɨɬɚɧɵ ɹɡɵɤɢ ɋɢɦɭɥɚ (SIMULA) ɢ Ⱥɥɝɨɥ-68, ɧɟ ɩɨɥɭɱɢɜɲɢɟ ɲɢɪɨɤɨɝɨ ɪɚɫɩɪɨɫɬɪɚɧɟɧɢɹ, ɧɨ ɨɤɚɡɚɜɲɢɟ ɛɨɥɶɲɨɟ ɜɥɢɹɧɢɟ ɧɚ ɪɚɡɪɚɛɨɬɤɭ ɞɪɭɝɢɯ ɹɡɵɤɨɜ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ. Ⱦɚɥɶɧɟɣɲɢɦ ɪɚɡɜɢɬɢɟɦ ɜɬɨɪɨɝɨ ɩɭɬɢ ɹɜɢɥɫɹ ɨɛɴɟɤɬɧɨɨɪɢɟɧɬɢɪɨɜɚɧɧɵɣ ɩɨɞɯɨɞ ɤ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɸ, ɪɚɫɫɦɚɬɪɢɜɚɟɦɵɣ ɜ ɫɥɟɞɭɸɳɟɦ ɩɚɪɚɝɪɚɮɟ.
5
1.2. ɋɭɳɧɨɫɬɶ ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɨɝɨ ɩɨɞɯɨɞɚ ɤ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɸ Ɉɫɧɨɜɧɵɟ ɢɞɟɢ ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɨɝɨ ɩɨɞɯɨɞɚ ɨɩɢɪɚɸɬɫɹ ɧɚ ɫɥɟɞɭɸɳɢɟ ɩɨɥɨɠɟɧɢɹ: x ɩɪɨɝɪɚɦɦɚ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɦɨɞɟɥɶ ɧɟɤɨɬɨɪɨɝɨ ɪɟɚɥɶɧɨɝɨ ɩɪɨɰɟɫɫɚ, ɱɚɫɬɢ ɪɟɚɥɶɧɨɝɨ ɦɢɪɚ; x ɦɨɞɟɥɶ ɪɟɚɥɶɧɨɝɨ ɦɢɪɚ ɢɥɢ ɟɝɨ ɱɚɫɬɢ ɦɨɠɟɬ ɛɵɬɶ ɨɩɢɫɚɧɚ ɤɚɤ ɫɨɜɨɤɭɩɧɨɫɬɶ ɜɡɚɢɦɨɞɟɣɫɬɜɭɸɳɢɯ ɦɟɠɞɭ ɫɨɛɨɣ ɨɛɴɟɤɬɨɜ; x ɨɛɴɟɤɬ ɨɩɢɫɵɜɚɟɬɫɹ ɧɚɛɨɪɨɦ ɩɚɪɚɦɟɬɪɨɜ, ɡɧɚɱɟɧɢɹ ɤɨɬɨɪɵɯ ɨɩɪɟɞɟɥɹɸɬ ɫɨɫɬɨɹɧɢɟ ɨɛɴɟɤɬɚ, ɢ ɧɚɛɨɪɨɦ ɨɩɟɪɚɰɢɣ (ɞɟɣɫɬɜɢɣ), ɤɨɬɨɪɵɟ ɦɨɠɟɬ ɜɵɩɨɥɧɹɬɶ ɨɛɴɟɤɬ; x ɜɡɚɢɦɨɞɟɣɫɬɜɢɟ ɦɟɠɞɭ ɨɛɴɟɤɬɚɦɢ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɩɨɫɵɥɤɨɣ ɫɩɟɰɢɚɥɶɧɵɯ ɫɨɨɛɳɟɧɢɣ ɨɬ ɨɞɧɨɝɨ ɨɛɴɟɤɬɚ ɤ ɞɪɭɝɨɦɭ. ɋɨɨɛɳɟɧɢɟ, ɩɨɥɭɱɟɧɧɨɟ ɨɛɴɟɤɬɨɦ, ɦɨɠɟɬ ɩɨɬɪɟɛɨɜɚɬɶ ɜɵɩɨɥɧɟɧɢɹ ɨɩɪɟɞɟɥɟɧɧɵɯ ɞɟɣɫɬɜɢɣ, ɧɚɩɪɢɦɟɪ, ɢɡɦɟɧɟɧɢɹ ɫɨɫɬɨɹɧɢɹ ɨɛɴɟɤɬɚ; x ɨɛɴɟɤɬɵ, ɨɩɢɫɚɧɧɵɟ ɨɞɧɢɦ ɢ ɬɟɦ ɠɟ ɧɚɛɨɪɨɦ ɩɚɪɚɦɟɬɪɨɜ ɢ ɫɩɨɫɨɛɧɵɟ ɜɵɩɨɥɧɹɬɶ ɨɞɢɧ ɢ ɬɨɬ ɠɟ ɧɚɛɨɪ ɞɟɣɫɬɜɢɣ, ɩɪɟɞɫɬɚɜɥɹɸɬ ɫɨɛɨɣ ɤɥɚɫɫ ɨɞɧɨɬɢɩɧɵɯ ɨɛɴɟɤɬɨɜ. ɋ ɬɨɱɤɢ ɡɪɟɧɢɹ ɹɡɵɤɚ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɤɥɚɫɫ ɨɛɴɟɤɬɨɜ ɦɨɠɧɨ ɪɚɫɫɦɚɬɪɢɜɚɬɶ ɤɚɤ ɬɢɩ ɞɚɧɧɨɝɨ, ɚ ɨɬɞɟɥɶɧɵɣ ɨɛɴɟɤɬ – ɤɚɤ ɩɟɪɟɦɟɧɧɭɸ ɷɬɨɝɨ ɬɢɩɚ. Ɉɩɪɟɞɟɥɟɧɢɟ ɩɪɨɝɪɚɦɦɢɫɬɨɦ ɫɨɛɫɬɜɟɧɧɵɯ ɤɥɚɫɫɨɜ ɨɛɴɟɤɬɨɜ ɞɥɹ ɤɨɧɤɪɟɬɧɨɝɨ ɧɚɛɨɪɚ ɡɚɞɚɱ ɞɨɥɠɧɨ ɩɨɡɜɨɥɢɬɶ ɨɩɢɫɵɜɚɬɶ ɨɬɞɟɥɶɧɵɟ ɡɚɞɚɱɢ ɜ ɬɟɪɦɢɧɚɯ ɫɚɦɨɝɨ ɤɥɚɫɫɚ ɡɚɞɚɱ (ɩɪɢ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɦ ɜɵɛɨɪɟ ɢɦɟɧ ɬɢɩɨɜ ɢ ɢɦɟɧ ɨɛɴɟɤɬɨɜ, ɢɯ ɩɚɪɚɦɟɬɪɨɜ ɢ ɜɵɩɨɥɧɹɟɦɵɯ ɞɟɣɫɬɜɢɣ). Ɍɚɤɢɦ ɨɛɪɚɡɨɦ, ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɣ ɩɨɞɯɨɞ ɩɪɟɞɩɨɥɚɝɚɟɬ, ɱɬɨ ɩɪɢ ɪɚɡɪɚɛɨɬɤɟ ɩɪɨɝɪɚɦɦɵ ɞɨɥɠɧɵ ɛɵɬɶ ɨɩɪɟɞɟɥɟɧɵ ɤɥɚɫɫɵ ɢɫɩɨɥɶɡɭɟɦɵɯ ɜ ɩɪɨɝɪɚɦɦɟ ɨɛɴɟɤɬɨɜ ɢ ɩɨɫɬɪɨɟɧɵ ɢɯ ɨɩɢɫɚɧɢɹ, ɡɚɬɟɦ ɫɨɡɞɚɧɵ ɷɤɡɟɦɩɥɹɪɵ ɧɟɨɛɯɨɞɢɦɵɯ ɨɛɴɟɤɬɨɜ ɢ ɨɩɪɟɞɟɥɟɧɨ ɜɡɚɢɦɨɞɟɣɫɬɜɢɟ ɦɟɠɞɭ ɧɢɦɢ. Ʉɥɚɫɫɵ ɨɛɴɟɤɬɨɜ ɱɚɫɬɨ ɭɞɨɛɧɨ ɫɬɪɨɢɬɶ ɬɚɤ, ɱɬɨɛɵ ɨɧɢ ɨɛɪɚɡɨɜɵɜɚɥɢ ɢɟɪɚɪɯɢɱɟɫɤɭɸ ɫɬɪɭɤɬɭɪɭ. ɇɚɩɪɢɦɟɪ, ɤɥɚɫɫ «ɋɬɭɞɟɧɬ», ɨɩɢɫɵɜɚɸɳɢɣ ɚɛɫɬɪɚɤɬɧɨɝɨ ɫɬɭɞɟɧɬɚ, ɦɨɠɟɬ ɫɥɭɠɢɬɶ ɨɫɧɨɜɨɣ ɞɥɹ ɩɨɫɬɪɨɟɧɢɹ ɤɥɚɫɫɨɜ «ɋɬɭɞɟɧɬ 1-ɝɨ ɤɭɪɫɚ», «ɋɬɭɞɟɧɬ 2-ɝɨ ɤɭɪɫɚ» ɢ ɬɚɤ ɞɚɥɟɟ, ɤɨɬɨɪɵɟ ɨɛɥɚɞɚɸɬ ɜɫɟɦɢ ɫɜɨɣɫɬɜɚɦɢ ɫɬɭɞɟɧɬɚ ɜɨɨɛɳɟ ɢ ɧɟɤɨɬɨɪɵɦɢ ɞɨɩɨɥɧɢɬɟɥɶɧɵɦɢ ɫɜɨɣɫɬɜɚɦɢ, ɯɚɪɚɤɬɟɪɢɡɭɸɳɢɦɢ ɫɬɭɞɟɧɬɚ ɤɨɧɤɪɟɬɧɨɝɨ ɤɭɪɫɚ. ɉɪɢ ɪɚɡɪɚɛɨɬɤɟ ɢɧɬɟɪɮɟɣɫɚ ɫ ɩɨɥɶɡɨɜɚɬɟɥɟɦ ɩɪɨɝɪɚɦɦɵ ɦɨɝɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɛɴɟɤɬɵ ɨɛɳɟɝɨ ɤɥɚɫɫɚ «Ɉɤɧɨ» ɢ ɨɛɴɟɤɬɵ ɤɥɚɫɫɨɜ ɫɩɟɰɢɚɥɶɧɵɯ ɨɤɨɧ, ɧɚɩɪɢɦɟɪ, ɨɤɨɧ ɢɧɮɨɪɦɚɰɢɨɧɧɵɯ ɫɨɨɛɳɟɧɢɣ, ɨɤɨɧ ɜɜɨɞɚ ɞɚɧɧɵɯ ɢ ɬ.ɩ. ȼ ɬɚɤɢɯ ɢɟɪɚɪɯɢɱɟɫɤɢɯ ɫɬɪɭɤɬɭɪɚɯ ɨɞɢɧ ɤɥɚɫɫ ɦɨɠɟɬ ɪɚɫɫɦɚɬɪɢɜɚɬɶɫɹ ɤɚɤ ɛɚɡɨɜɵɣ ɞɥɹ ɞɪɭɝɢɯ, ɩɪɨɢɡɜɨɞɧɵɯ ɨɬ ɧɟɝɨ ɤɥɚɫɫɨɜ. Ɉɛɴɟɤɬ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ ɨɛɥɚɞɚɟɬ 6
ɜɫɟɦɢ ɫɜɨɣɫɬɜɚɦɢ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ ɢ ɧɟɤɨɬɨɪɵɦɢ ɫɨɛɫɬɜɟɧɧɵɦɢ ɫɜɨɣɫɬɜɚɦɢ, ɨɧ ɦɨɠɟɬ ɪɟɚɝɢɪɨɜɚɬɶ ɧɚ ɬɟ ɠɟ ɬɢɩɵ ɫɨɨɛɳɟɧɢɣ ɨɬ ɞɪɭɝɢɯ ɨɛɴɟɤɬɨɜ, ɱɬɨ ɢ ɨɛɴɟɤɬ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ, ɢ ɧɚ ɫɨɨɛɳɟɧɢɹ, ɢɦɟɸɳɢɟ ɫɦɵɫɥ ɬɨɥɶɤɨ ɞɥɹ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ. Ɉɛɵɱɧɨ ɝɨɜɨɪɹɬ, ɱɬɨ ɨɛɴɟɤɬ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ ɧɚɫɥɟɞɭɟɬ ɜɫɟ ɫɜɨɣɫɬɜɚ ɫɜɨɟɝɨ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ. ɇɟɤɨɬɨɪɵɟ ɩɚɪɚɦɟɬɪɵ ɨɛɴɟɤɬɚ ɦɨɝɭɬ ɛɵɬɶ ɥɨɤɚɥɢɡɨɜɚɧɵ ɜɧɭɬɪɢ ɨɛɴɟɤɬɚ ɢ ɧɟɞɨɫɬɭɩɧɵ ɞɥɹ ɩɪɹɦɨɝɨ ɜɨɡɞɟɣɫɬɜɢɹ ɢɡɜɧɟ ɧɚ ɨɛɴɟɤɬ. ɇɚɩɪɢɦɟɪ, ɜɨ ɜɪɟɦɹ ɞɜɢɠɟɧɢɹ ɨɛɴɟɤɬɚ-ɚɜɬɨɦɨɛɢɥɹ ɨɛɴɟɤɬ-ɜɨɞɢɬɟɥɶ ɦɨɠɟɬ ɜɨɡɞɟɣɫɬɜɨɜɚɬɶ ɬɨɥɶɤɨ ɧɚ ɨɝɪɚɧɢɱɟɧɧɵɣ ɧɚɛɨɪ ɨɪɝɚɧɨɜ ɭɩɪɚɜɥɟɧɢɹ (ɪɭɥɟɜɨɟ ɤɨɥɟɫɨ, ɩɟɞɚɥɢ ɝɚɡɚ, ɫɰɟɩɥɟɧɢɹ ɢ ɬɨɪɦɨɡɚ, ɪɵɱɚɝ ɩɟɪɟɤɥɸɱɟɧɢɹ ɩɟɪɟɞɚɱ) ɢ ɟɦɭ ɧɟɞɨɫɬɭɩɟɧ ɰɟɥɵɣ ɪɹɞ ɩɚɪɚɦɟɬɪɨɜ, ɯɚɪɚɤɬɟɪɢɡɭɸɳɢɯ ɫɨɫɬɨɹɧɢɟ ɞɜɢɝɚɬɟɥɹ ɢ ɚɜɬɨɦɨɛɢɥɹ ɜ ɰɟɥɨɦ. Ɉɱɟɜɢɞɧɨ, ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɩɪɨɞɭɤɬɢɜɧɨ ɩɪɢɦɟɧɹɬɶ ɨɛɴɟɤɬɧɵɣ ɩɨɞɯɨɞ ɞɥɹ ɪɚɡɪɚɛɨɬɤɢ ɩɪɨɝɪɚɦɦ, ɧɟɨɛɯɨɞɢɦɵ ɹɡɵɤɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɩɨɞɞɟɪɠɢɜɚɸɳɢɟ ɷɬɨɬ ɩɨɞɯɨɞ, ɬ. ɟ. ɩɨɡɜɨɥɹɸɳɢɟ ɫɬɪɨɢɬɶ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɨɜ ɨɛɴɟɤɬɨɜ, ɨɛɪɚɡɨɜɵɜɚɬɶ ɞɚɧɧɵɟ ɨɛɴɟɤɬɧɵɯ ɬɢɩɨɜ, ɜɵɩɨɥɧɹɬɶ ɨɩɟɪɚɰɢɢ ɧɚɞ ɨɛɴɟɤɬɚɦɢ. Ɉɞɧɢɦ ɢɡ ɩɟɪɜɵɯ ɬɚɤɢɯ ɹɡɵɤɨɜ ɫɬɚɥ ɹɡɵɤ SmallTalk, ɜ ɤɨɬɨɪɨɦ ɜɫɟ ɞɚɧɧɵɟ ɹɜɥɹɸɬɫɹ ɨɛɴɟɤɬɚɦɢ ɧɟɤɨɬɨɪɵɯ ɤɥɚɫɫɨɜ, ɚ ɨɛɳɚɹ ɫɢɫɬɟɦɚ ɤɥɚɫɫɨɜ ɫɬɪɨɢɬɫɹ ɤɚɤ ɢɟɪɚɪɯɢɱɟɫɤɚɹ ɫɬɪɭɤɬɭɪɚ ɧɚ ɨɫɧɨɜɟ ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɵɯ ɛɚɡɨɜɵɯ ɤɥɚɫɫɨɜ. Ɉɩɵɬ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɩɨɤɚɡɵɜɚɟɬ, ɱɬɨ ɥɸɛɨɣ ɦɟɬɨɞɨɥɨɝɢɱɟɫɤɢɣ ɩɨɞɯɨɞ ɜ ɬɟɯɧɨɥɨɝɢɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɧɟ ɞɨɥɠɟɧ ɩɪɢɦɟɧɹɬɶɫɹ ɫɥɟɩɨ ɫ ɢɝɧɨɪɢɪɨɜɚɧɢɟɦ ɞɪɭɝɢɯ ɩɨɞɯɨɞɨɜ. ɗɬɨ ɨɬɧɨɫɢɬɫɹ ɢ ɤ ɨɛɴɟɤɬɧɨɨɪɢɟɧɬɢɪɨɜɚɧɧɨɦɭ ɩɨɞɯɨɞɭ. ɋɭɳɟɫɬɜɭɟɬ ɪɹɞ ɬɢɩɨɜɵɯ ɩɪɨɛɥɟɦ, ɞɥɹ ɤɨɬɨɪɵɯ ɟɝɨ ɩɨɥɟɡɧɨɫɬɶ ɧɚɢɛɨɥɟɟ ɨɱɟɜɢɞɧɚ, ɤ ɬɚɤɢɦ ɩɪɨɛɥɟɦɚɦ ɨɬɧɨɫɹɬɫɹ, ɜ ɱɚɫɬɧɨɫɬɢ, ɡɚɞɚɱɢ ɢɦɢɬɚɰɢɨɧɧɨɝɨ ɦɨɞɟɥɢɪɨɜɚɧɢɹ, ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ ɞɢɚɥɨɝɨɜ ɫ ɩɨɥɶɡɨɜɚɬɟɥɟɦ. ɋɭɳɟɫɬɜɭɸɬ ɢ ɡɚɞɚɱɢ, ɜ ɤɨɬɨɪɵɯ ɩɪɢɦɟɧɟɧɢɟ ɨɛɴɟɤɬɧɨɝɨ ɩɨɞɯɨɞɚ ɧɢ ɤ ɱɟɦɭ, ɤɪɨɦɟ ɢɡɥɢɲɧɢɯ ɡɚɬɪɚɬ ɬɪɭɞɚ, ɧɟ ɩɪɢɜɟɞɟɬ. ȼ ɫɜɹɡɢ ɫ ɷɬɢɦ ɧɚɢɛɨɥɶɲɟɟ ɪɚɫɩɪɨɫɬɪɚɧɟɧɢɟ ɩɨɥɭɱɢɥɢ ɨɛɴɟɤɬɧɨɨɪɢɟɧɬɢɪɨɜɚɧɧɵɟ ɹɡɵɤɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɩɨɡɜɨɥɹɸɳɢɟ ɫɨɱɟɬɚɬɶ ɨɛɴɟɤɬɧɵɣ ɩɨɞɯɨɞ ɫ ɞɪɭɝɢɦɢ ɦɟɬɨɞɨɥɨɝɢɹɦɢ. ȼ ɧɟɤɨɬɨɪɵɯ ɹɡɵɤɚɯ ɢ ɫɢɫɬɟɦɚɯ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɩɪɢɦɟɧɟɧɢɟ ɨɛɴɟɤɬɧɨɝɨ ɩɨɞɯɨɞɚ ɨɝɪɚɧɢɱɢɜɚɟɬɫɹ ɫɪɟɞɫɬɜɚɦɢ ɢɧɬɟɪɮɟɣɫɚ ɫ ɩɨɥɶɡɨɜɚɬɟɥɟɦ (ɧɚɩɪɢɦɟɪ, Visual FoxPro ɪɚɧɧɢɯ ɜɟɪɫɢɣ). ɇɚɢɛɨɥɟɟ ɢɫɩɨɥɶɡɭɟɦɵɦɢ ɜ ɧɚɫɬɨɹɳɟɟ ɜɪɟɦɹ ɨɛɴɟɤɬɧɨɨɪɢɟɧɬɢɪɨɜɚɧɧɵɦɢ ɹɡɵɤɚɦɢ ɹɜɥɹɸɬɫɹ ɉɚɫɤɚɥɶ ɫ ɨɛɴɟɤɬɚɦɢ ɢ ɋɢ++ (ɋ++), ɩɪɢɱɟɦ ɧɚɢɛɨɥɟɟ ɪɚɡɜɢɬɵɟ ɫɪɟɞɫɬɜɚ ɞɥɹ ɪɚɛɨɬɵ ɫ ɨɛɴɟɤɬɚɦɢ ɫɨɞɟɪɠɚɬɫɹ ɜ ɋɢ++. ɉɪɚɤɬɢɱɟɫɤɢ ɜɫɟ ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɟ ɹɡɵɤɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɹɜɥɹɸɬɫɹ ɪɚɡɜɢɜɚɸɳɢɦɢɫɹ ɹɡɵɤɚɦɢ, ɢɯ ɫɬɚɧɞɚɪɬɵ ɪɟɝɭɥɹɪɧɨ ɭɬɨɱɧɹɸɬɫɹ ɢ ɪɚɫɲɢɪɹɸɬɫɹ. ɋɥɟɞɫɬɜɢɟɦ ɷɬɨɝɨ ɪɚɡɜɢɬɢɹ ɹɜɥɹɸɬɫɹ ɧɟɢɡɛɟɠɧɵɟ ɪɚɡɥɢɱɢɹ ɜɨ ɜɯɨɞɧɵɯ ɹɡɵɤɚɯ ɤɨɦɩɢɥɹɬɨɪɨɜ ɪɚɡɥɢɱɧɵɯ ɫɢɫɬɟɦ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ. ɇɚɢɛɨɥɟɟ ɪɚɫɩɪɨɫɬɪɚɧɟɧɧɵɦɢ ɜ ɧɚɫɬɨɹɳɟɟ ɜɪɟɦɹ ɹɜɥɹɸɬɫɹ ɫɢɫɬɟɦɵ ɩɪɨ7
ɝɪɚɦɦɢɪɨɜɚɧɢɹ Microsoft C++ , Microsoft Visual C++ ɢ ɫɢɫɬɟɦɵ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɮɢɪɦɵ Borland International. Ⱦɚɥɶɧɟɣɲɢɣ ɦɚɬɟɪɢɚɥ ɜ ɞɚɧɧɨɦ ɩɨɫɨɛɢɢ ɢɡɥɚɝɚɟɬɫɹ ɩɪɢɦɟɧɢɬɟɥɶɧɨ ɤ ɫɢɫɬɟɦɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ Borland C++. ɗɬɨ ɫɜɹɡɚɧɨ, ɩɪɟɠɞɟ ɜɫɟɝɨ, ɫ ɧɚɥɢɱɢɟɦ ɜ ɷɬɨɣ ɫɢɫɬɟɦɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɪɚɡɜɢɬɨɣ ɢɧɬɟɝɪɢɪɨɜɚɧɧɨɣ ɫɪɟɞɵ, ɨɛɴɟɞɢɧɹɸɳɟɣ ɬɟɤɫɬɨɜɵɣ ɪɟɞɚɤɬɨɪ, ɤɨɦɩɢɥɹɬɨɪ, ɪɟɞɚɤɬɨɪ ɫɜɹɡɟɣ (ɤɨɦɩɨɧɨɜɳɢɤ) ɢ ɨɬɥɚɞɨɱɧɵɟ ɫɪɟɞɫɬɜɚ.
2. ɇɚɱɚɥɶɧɵɟ ɫɜɟɞɟɧɢɹ ɨ ɹɡɵɤɟ C++ 2.1. ɇɚɡɧɚɱɟɧɢɟ ɹɡɵɤɚ C++, ɢɫɬɨɪɢɱɟɫɤɢɟ ɫɜɟɞɟɧɢɹ əɡɵɤ ɋɢ ɛɵɥ ɪɚɡɪɚɛɨɬɚɧ ɜ 70-ɟ ɝɨɞɵ ɤɚɤ ɹɡɵɤ ɫɢɫɬɟɦɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ. ɉɪɢ ɷɬɨɦ ɫɬɚɜɢɥɚɫɶ ɡɚɞɚɱɚ ɩɨɥɭɱɢɬɶ ɹɡɵɤ, ɨɛɟɫɩɟɱɢɜɚɸɳɢɣ ɪɟɚɥɢɡɚɰɢɸ ɢɞɟɣ ɩɪɨɰɟɞɭɪɧɨɝɨ ɢ ɫɬɪɭɤɬɭɪɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɢ ɜɨɡɦɨɠɧɨɫɬɶ ɪɟɚɥɢɡɚɰɢɢ ɫɩɟɰɢɮɢɱɟɫɤɢɯ ɩɪɢɟɦɨɜ ɫɢɫɬɟɦɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ. Ɍɚɤɨɣ ɹɡɵɤ ɩɨɡɜɨɥɢɥ ɛɵ ɪɚɡɪɚɛɚɬɵɜɚɬɶ ɫɥɨɠɧɵɟ ɩɪɨɝɪɚɦɦɵ ɧɚ ɭɪɨɜɧɟ, ɫɪɚɜɧɢɦɨɦ ɫ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟɦ ɧɚ Ⱥɫɫɟɦɛɥɟɪɟ, ɧɨ ɫɭɳɟɫɬɜɟɧɧɨ ɛɵɫɬɪɟɟ. ɗɬɢ ɰɟɥɢ ɜ ɨɫɧɨɜɧɨɦ ɛɵɥɢ ɞɨɫɬɢɝɧɭɬɵ. Ȼɨɥɶɲɢɧɫɬɜɨ ɤɨɦɩɢɥɹɬɨɪɨɜ ɞɥɹ ɋɢ ɧɚɩɢɫɚɧɵ ɧɚ ɹɡɵɤɟ ɋɢ. Ɉɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ UNIX ɬɚɤɠɟ ɩɨɱɬɢ ɩɨɥɧɨɫɬɶɸ ɧɚɩɢɫɚɧɚ ɧɚ ɋɢ. ɇɟɞɨɫɬɚɬɤɨɦ ɋɢ ɨɤɚɡɚɥɚɫɶ ɧɢɡɤɚɹ ɧɚɞɟɠɧɨɫɬɶ ɪɚɡɪɚɛɚɬɵɜɚɟɦɵɯ ɩɪɨɝɪɚɦɦ ɢɡ-ɡɚ ɨɬɫɭɬɫɬɜɢɹ ɤɨɧɬɪɨɥɹ ɬɢɩɨɜ. ɉɨɩɵɬɤɚ ɩɨɩɪɚɜɢɬɶ ɞɟɥɨ ɜɤɥɸɱɟɧɢɟɦ ɜ ɫɢɫɬɟɦɭ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɋɢ ɨɬɞɟɥɶɧɨɣ ɩɪɨɝɪɚɦɦɵ, ɤɨɧɬɪɨɥɢɪɭɸɳɟɣ ɧɟɹɜɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɨɜ, ɪɟɲɢɥɚ ɷɬɭ ɩɪɨɛɥɟɦɭ ɥɢɲɶ ɱɚɫɬɢɱɧɨ. ɇɚ ɨɫɧɨɜɟ ɋɢ ɜ 80-ɟ ɝɨɞɵ ɛɵɥ ɪɚɡɪɚɛɨɬɚɧ ɹɡɵɤ ɋɢ++, ɜɧɚɱɚɥɟ ɧɚɡɜɚɧɧɵɣ «ɋɢ ɫ ɤɥɚɫɫɚɦɢ». ɋɢ++ ɩɪɚɤɬɢɱɟɫɤɢ ɜɤɥɸɱɚɟɬ ɹɡɵɤ ɋɢ, ɤɨɬɨɪɵɣ ɞɨɩɨɥɧɟɧ ɫɪɟɞɫɬɜɚɦɢ ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ. Ɋɚɛɨɱɚɹ ɜɟɪɫɢɹ ɋɢ++ ɩɨɹɜɢɥɚɫɶ ɜ 1983ɝ. ɋ ɬɟɯ ɩɨɪ ɹɡɵɤ ɩɪɨɞɨɥɠɚɟɬ ɪɚɡɜɢɜɚɬɶɫɹ, ɢ ɨɩɭɛɥɢɤɨɜɚɧɨ ɧɟɫɤɨɥɶɤɨ ɜɟɪɫɢɣ ɩɪɨɟɤɬɚ ɫɬɚɧɞɚɪɬɨɜ ɋɢ ɢ ɋɢ++. Ɋɹɞɨɦ ɮɢɪɦ, ɩɪɨɢɡɜɨɞɹɳɢɯ ɩɪɨɝɪɚɦɦɧɨɟ ɨɛɟɫɩɟɱɟɧɢɟ, ɪɚɡɪɚɛɨɬɚɧɵ ɤɨɦɩɢɥɹɬɨɪɵ ɞɥɹ ɋɢ ɢ ɋɢ++. ɋɢɫɬɟɦɵ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɮɢɪɦɵ Borland International ɜɵɞɟɥɹɸɬɫɹ ɫɪɟɞɢ ɞɪɭɝɢɯ ɮɢɪɦ, ɩɪɟɠɞɟ ɜɫɟɝɨ, ɤɨɦɩɥɟɤɫɧɵɦ ɩɨɞɯɨɞɨɦ ɤ ɪɚɡɪɚɛɨɬɤɟ ɩɪɨɝɪɚɦɦ. Ɉɧɢ ɩɪɟɞɫɬɚɜɥɹɸɬ ɫɨɛɨɣ ɢɧɬɟɝɪɢɪɨɜɚɧɧɭɸ ɫɪɟɞɭ ɪɚɡɪɚɛɨɬɱɢɤɚ, ɨɛɴɟɞɢɧɹɸɳɭɸ ɩɨɞ ɨɛɳɢɦ ɭɩɪɚɜɥɟɧɢɟɦ ɬɟɤɫɬɨɜɵɣ ɪɟɞɚɤɬɨɪ ɞɥɹ ɜɜɨɞɚ ɢɫɯɨɞɧɵɯ ɬɟɤɫɬɨɜ ɩɪɨɝɪɚɦɦ, ɤɨɦɩɢɥɹɬɨɪ, ɪɟɞɚɤɬɨɪ ɫɜɹɡɟɣ ɢ ɧɚɛɨɪ ɨɬɥɚɞɨɱɧɵɯ ɫɪɟɞɫɬɜ. ȼ 1989ɝ. ɷɬɨɣ ɮɢɪɦɨɣ ɛɵɥɚ ɜɵɩɭɳɟɧɚ ɫɢɫɬɟɦɚ Turbo C++, ɜɤɥɸɱɚɸɳɚɹ ɜ ɫɟɛɹ ɤɨɦɩɢɥɹɬɨɪ ɋɢ++, ɪɚɛɨɬɚɸɳɢɣ ɜ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɟ MS DOS. ɋ 1992ɝ. ɜɵɩɭɫɤɚɸɬɫɹ ɫɢɫɬɟɦɵ Borland C++, ɫɨɞɟɪɠɚɳɢɟ ɤɨɦɩɢɥɹɬɨɪɵ ɋɢ++ ɞɥɹ MS DOS ɢ MS WINDOWS, ɫ 1997ɝ. ɩɨɫɬɚɜɥɹɟɬɫɹ ɜɟɪɫɢɹ Borland C 5.0, ɫɨɞɟɪɠɚɳɚɹ ɤɨɦɩɢɥɹɬɨɪɵ ɋɢ++ ɞɥɹ MS WINDOWS, ɩɪɢɱɟɦ ɞɚɧɧɵɣ ɤɨɦɩɢɥɹɬɨɪ ɞɥɹ MS WINDOWS ɩɨ8
ɡɜɨɥɹɟɬ ɪɚɡɪɚɛɚɬɵɜɚɬɶ ɤɚɤ 16-ɪɚɡɪɹɞɧɵɟ, ɬɚɤ ɢ 32-ɪɚɡɪɹɞɧɵɟ ɜɚɪɢɚɧɬɵ ɩɪɨɝɪɚɦɦ ɞɥɹ ɉɗȼɆ ɫ ɩɪɨɰɟɫɫɨɪɚɦɢ i486 ɢ Pentium. ɉɪɨɝɪɚɦɦɚ ɧɚ ɋɢ/ɋɢ++ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɨɞɢɧ ɢɥɢ ɧɟɫɤɨɥɶɤɨ ɢɫɯɨɞɧɵɯ ɮɚɣɥɨɜ, ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɬɪɚɧɫɥɢɪɨɜɚɬɶɫɹ ɪɚɡɞɟɥɶɧɨ. Ɋɟɡɭɥɶɬɚɬɵ ɬɪɚɧɫɥɹɰɢɢ (ɨɛɴɟɤɬɧɵɟ ɮɚɣɥɵ) ɨɛɴɟɞɢɧɹɸɬɫɹ ɜ ɢɫɩɨɥɧɹɟɦɵɣ ɮɚɣɥ ɪɟɞɚɤɬɨɪɨɦ ɫɜɹɡɟɣ (ɤɨɦɩɨɧɨɜɳɢɤɨɦ). Ɉɛɵɱɧɨ ɪɚɡɥɢɱɚɸɬ ɞɜɚ ɬɢɩɚ ɢɫɯɨɞɧɵɯ ɮɚɣɥɨɜ: ɮɚɣɥɵ ɡɚɝɨɥɨɜɤɨɜ ɢ ɩɪɨɝɪɚɦɦɧɵɟ ɮɚɣɥɵ. Ɏɚɣɥɵ ɡɚɝɨɥɨɜɤɨɜ ɫɨɞɟɪɠɚɬ ɨɩɢɫɚɧɢɹ ɬɢɩɨɜ ɞɚɧɧɵɯ ɢ ɩɪɨɬɨɬɢɩɨɜ ɮɭɧɤɰɢɣ ɢ ɩɪɟɞɧɚɡɧɚɱɟɧɵ ɞɥɹ ɜɤɥɸɱɟɧɢɹ ɜ ɩɪɨɝɪɚɦɦɧɵɟ ɮɚɣɥɵ ɩɟɪɟɞ ɢɯ ɤɨɦɩɢɥɹɰɢɟɣ. ɂɯ ɢɦɟɧɚ, ɤɚɤ ɩɪɚɜɢɥɨ, ɢɦɟɸɬ ɪɚɫɲɢɪɟɧɢɟ .h, ɧɚɩɪɢɦɟɪ, stdio.h. ɉɪɨɝɪɚɦɦɧɵɟ ɮɚɣɥɵ ɫɨɞɟɪɠɚɬ ɨɩɢɫɚɧɢɹ ɮɭɧɤɰɢɣ ɢ, ɜɨɡɦɨɠɧɨ, ɝɥɨɛɚɥɶɧɵɯ ɩɟɪɟɦɟɧɧɵɯ ɢ ɤɨɧɫɬɚɧɬ, ɢɯ ɢɦɟɧɚ ɩɪɢɧɹɬɨ ɡɚɩɢɫɵɜɚɬɶ ɫ ɪɚɫɲɢɪɟɧɢɹɦɢ .c ɢɥɢ .cpp, ɧɚɩɪɢɦɟɪ, myprog.cpp. Ɉɞɢɧ ɢ ɬɨɬ ɠɟ ɮɚɣɥ ɡɚɝɨɥɨɜɤɨɜ ɦɨɠɟɬ ɜɤɥɸɱɚɬɶɫɹ ɜ ɧɟɫɤɨɥɶɤɨ ɩɪɨɝɪɚɦɦɧɵɯ ɮɚɣɥɨɜ. Ʉɚɠɞɵɣ ɮɚɣɥ ɫɨɞɟɪɠɢɬ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɬɚɤ ɧɚɡɵɜɚɟɦɵɯ «ɜɧɟɲɧɢɯ ɨɩɪɟɞɟɥɟɧɢɣ», ɨɩɢɫɵɜɚɸɳɢɯ ɬɢɩɵ ɞɚɧɧɵɯ, ɩɟɪɟɦɟɧɧɵɟ, ɤɨɧɫɬɚɧɬɵ ɢ ɮɭɧɤɰɢɢ. ȼ ɩɨɫɥɟɞɭɸɳɢɯ ɩɚɪɚɝɪɚɮɚɯ ɷɬɨɝɨ ɪɚɡɞɟɥɚ ɩɪɢɜɟɞɟɧ ɨɛɡɨɪ ɫɪɟɞɫɬɜ ɋɢ/ɋɢ++, ɧɟ ɫɜɹɡɚɧɧɵɯ ɫ ɨɛɴɟɤɬɧɨɣ ɨɪɢɟɧɬɚɰɢɟɣ ɋɢ++.
2.2. Ⱥɥɮɚɜɢɬ, ɛɚɡɨɜɵɟ ɬɢɩɵ ɢ ɨɩɢɫɚɧɢɟ ɞɚɧɧɵɯ Ⱥɥɮɚɜɢɬ ɹɡɵɤɚ ɜɤɥɸɱɚɟɬ ɩɪɚɤɬɢɱɟɫɤɢ ɜɫɟ ɫɢɦɜɨɥɵ, ɢɦɟɸɳɢɟɫɹ ɧɚ ɫɬɚɧɞɚɪɬɧɨɣ ɤɥɚɜɢɚɬɭɪɟ ɉɗȼɆ: - ɥɚɬɢɧɫɤɢɟ ɛɭɤɜɵ A...Z, a...z; - ɰɢɮɪɵ 0...9; - ɡɧɚɤɢ ɨɩɟɪɚɰɢɣ ɢ ɪɚɡɞɟɥɢɬɟɥɢ: { } [ ] ( ) . , -> & * + - ~ ! / % ? : ; = < > | # ^
ɇɟɤɨɬɨɪɵɟ ɨɩɟɪɚɰɢɢ ɨɛɨɡɧɚɱɚɸɬɫɹ ɤɨɦɛɢɧɚɰɢɹɦɢ ɫɢɦɜɨɥɨɜ, ɡɧɚɱɟɧɢɹ ɫɢɦɜɨɥɨɜ ɨɩɟɪɚɰɢɣ ɜ ɪɹɞɟ ɫɥɭɱɚɟɜ ɡɚɜɢɫɹɬ ɨɬ ɤɨɧɬɟɤɫɬɚ, ɜ ɤɨɬɨɪɨɦ ɨɧɢ ɭɩɨɬɪɟɛɥɟɧɵ. Ȼɚɡɨɜɵɟ (ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɵɟ) ɬɢɩɵ ɞɚɧɧɵɯ ɨɛɴɟɞɢɧɟɧɵ ɜ ɞɜɟ ɝɪɭɩɩɵ: ɞɚɧɧɵɟ ɰɟɥɵɯ ɬɢɩɨɜ ɢ ɞɚɧɧɵɟ ɜɟɳɟɫɬɜɟɧɧɵɯ ɬɢɩɨɜ. Ⱦɚɧɧɵɟ ɰɟɥɵɯ ɬɢɩɨɜ ɦɨɝɭɬ ɛɵɬɶ ɨɛɵɱɧɵɦɢ ɰɟɥɵɦɢ ɫɨ ɡɧɚɤɨɦ (signed) ɢ ɰɟɥɵɦɢ ɛɟɡ ɡɧɚɤɚ (unsigned). ɉɨ ɱɢɫɥɭ ɪɚɡɪɹɞɨɜ, ɢɫɩɨɥɶɡɭɟɦɵɯ ɞɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɞɚɧɧɵɯ (ɞɢɚɩɚɡɨɧɭ ɡɧɚɱɟɧɢɣ), ɪɚɡɥɢɱɚɸɬ ɨɛɵɱɧɵɟ ɰɟɥɵɟ (int), ɤɨɪɨɬɤɢɟ ɰɟɥɵɟ (short int) ɢ ɞɥɢɧɧɵɟ ɰɟɥɵɟ (long int). ɋɢɦɜɨɥɶɧɵɟ ɞɚɧɧɵɟ (char) ɬɚɤɠɟ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɤɚɤ ɰɟɥɵɟ. Ʉɨɧɫɬɚɧɬɵ ɰɟɥɨɝɨ ɬɢɩɚ ɡɚɩɢɫɵɜɚɸɬɫɹ ɤɚɤ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɞɟɫɹɬɢɱɧɵɯ ɰɢɮɪ, ɬɢɩ ɤɨɧɫɬɚɧɬɵ ɡɚɜɢɫɢɬ ɨɬ ɱɢɫɥɚ ɰɢɮɪ ɜ ɡɚɩɢɫɢ ɤɨɧɫɬɚɧɬɵ ɢ ɦɨɠɟɬ ɛɵɬɶ ɭɬɨɱɧɟɧ ɞɨɛɚɜɥɟɧɢɟɦ ɜ ɤɨɧɰɟ ɤɨɧɫɬɚɧɬɵ ɛɭɤɜ L ɢɥɢ l (long), U ɢɥɢ u (unsigned) ɢɥɢ ɢɯ ɫɨɱɟɬɚɧɢɹ: 9
321 – ɤɨɧɫɬɚɧɬɚ ɬɢɩɚ int, 5326u – ɤɨɧɫɬɚɧɬɚ ɬɢɩɚ unsigned int, 45637778 – ɤɨɧɫɬɚɧɬɚ ɬɢɩɚ long int, 2746L – ɤɨɧɫɬɚɧɬɚ ɬɢɩɚ long int.
ɐɟɥɵɟ ɤɨɧɫɬɚɧɬɵ ɦɨɝɭɬ ɡɚɩɢɫɵɜɚɬɶɫɹ ɜ ɜɨɫɶɦɟɪɢɱɧɨɣ ɫɢɫɬɟɦɟ ɫɱɢɫɥɟɧɢɹ, ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɩɟɪɜɨɣ ɰɢɮɪɨɣ ɞɨɥɠɧɚ ɛɵɬɶ ɰɢɮɪɚ 0, ɱɢɫɥɨ ɦɨɠɟɬ ɫɨɞɟɪɠɚɬɶ ɬɨɥɶɤɨ ɰɢɮɪɵ 0 ... 7: 0777 – ɤɨɧɫɬɚɧɬɚ ɬɢɩɚ int, 0453377 – ɤɨɧɫɬɚɧɬɚ ɬɢɩɚ long int. ɐɟɥɵɟ ɤɨɧɫɬɚɧɬɵ ɦɨɠɧɨ ɡɚɩɢɫɵɜɚɬɶ ɢ ɜ ɲɟɫɬɧɚɞɰɚɬɟɪɢɱɧɨɣ ɫɢɫɬɟɦɟ ɫɱɢɫɥɟɧɢɹ, ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɡɚɩɢɫɶ ɤɨɧɫɬɚɧɬɵ ɧɚɱɢɧɚɟɬɫɹ ɫ ɫɢɦɜɨɥɨɜ 0x ɢɥɢ 0X: 0x45F – ɤɨɧɫɬɚɧɬɚ ɬɢɩɚ int, 0xFFFFFFFF – ɤɨɧɫɬɚɧɬɚ ɬɢɩɚ unsigned long int. Ʉɨɧɫɬɚɧɬɵ ɬɢɩɚ char ɜɫɟɝɞɚ ɡɚɤɥɸɱɚɸɬɫɹ ɜ ɨɞɢɧɨɱɧɵɟ ɤɚɜɵɱɤɢ, ɡɧɚ-
ɱɟɧɢɟ ɤɨɧɫɬɚɧɬɵ ɡɚɞɚɟɬɫɹ ɥɢɛɨ ɡɧɚɤɨɦ ɢɡ ɢɫɩɨɥɶɡɭɟɦɨɝɨ ɧɚɛɨɪɚ ɫɢɦɜɨɥɨɜ, ɥɢɛɨ ɰɟɥɨɣ ɤɨɧɫɬɚɧɬɨɣ, ɤɨɬɨɪɨɣ ɩɪɟɞɲɟɫɬɜɭɟɬ ɨɛɪɚɬɧɚɹ ɤɨɫɚɹ ɱɟɪɬɚ: 'A', '\33', '\042', '\x1B'. ɂɦɟɟɬɫɹ ɬɚɤɠɟ ɪɹɞ ɫɩɟɰɢɚɥɶɧɵɯ ɫɢɦɜɨɥɨɜ, ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɭɤɚɡɵɜɚɬɶɫɹ ɜ ɤɚɱɟɫɬɜɟ ɡɧɚɱɟɧɢɣ ɤɨɧɫɬɚɧɬɵ ɬɢɩɚ char: '\n' – ɧɨɜɚɹ ɫɬɪɨɤɚ, '\t' – ɝɨɪɢɡɨɧɬɚɥɶɧɚɹ ɬɚɛɭɥɹɰɢɹ, '\v' – ɜɟɪɬɢɤɚɥɶɧɚɹ ɬɚɛɭɥɹɰɢɹ, '\r' – ɩɟɪɟɜɨɞ ɤɚɪɟɬɤɢ, '\f' – ɩɟɪɟɜɨɞ ɫɬɪɚɧɢɰɵ, '\a' – ɡɜɭɤɨɜɨɣ ɫɢɝɧɚɥ, '\'' – ɨɞɢɧɨɱɧɚɹ ɤɚɜɵɱɤɚ (ɚɩɨɫɬɪɨɮ), '\"' – ɞɜɨɣɧɚɹ ɤɚɜɵɱɤɚ, '\\' – ɨɛɪɚɬɧɚɹ ɤɨɫɚɹ ɱɟɪɬɚ. ȼɟɳɟɫɬɜɟɧɧɵɟ ɱɢɫɥɚ ɦɨɝɭɬ ɛɵɬɶ ɡɧɚɱɟɧɢɹɦɢ ɨɞɧɨɝɨ ɢɡ ɬɪɟɯ ɬɢɩɨɜ: float, double, long double. Ⱦɢɚɩɚɡɨɧ ɡɧɚɱɟɧɢɣ ɤɚɠɞɨɝɨ ɢɡ ɷɬɢɯ ɬɢɩɨɜ ɡɚɜɢɫɢɬ ɨɬ ɢɫɩɨɥɶɡɭɟɦɵɯ ɗȼɆ ɢ ɤɨɦɩɢɥɹɬɨɪɚ. Ʉɨɧɫɬɚɧɬɵ ɜɟɳɟɫɬɜɟɧɧɵɯ ɬɢɩɨɜ ɦɨɝɭɬ ɡɚɩɢɫɵɜɚɬɶɫɹ ɤɚɤ ɜ ɮɨɪɦɟ ɫ ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɬɨɱɤɨɣ, ɬɚɤ ɢ ɜ ɷɤɫɩɨɧɟɧɰɢɚɥɶɧɨɣ ɮɨɪɦɟ. ɉɨ ɭɦɨɥɱɚɧɢɸ ɨɧɢ ɢɦɟɸɬ ɬɢɩ double, ɧɚɩɪɢɦɟɪ, 15.31, 1.43E-3, 2345.1e4. ɉɪɢ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɬɢɩ ɤɨɧɫɬɚɧɬɵ ɦɨɠɧɨ ɭɬɨɱɧɢɬɶ, ɡɚɩɢɫɚɜ ɜ ɤɨɧɰɟ ɫɭɮɮɢɤɫ f ɢɥɢ F ɞɥɹ ɬɢɩɚ float, ɫɭɮɮɢɤɫ l ɢɥɢ L ɞɥɹ ɬɢɩɚ long double. ȼɧɟɲɧɟɟ ɨɩɪɟɞɟɥɟɧɢɟ, ɨɛɴɹɜɥɹɸɳɟɟ ɩɟɪɟɦɟɧɧɵɟ, ɫɨɫɬɨɢɬ ɢɡ ɧɟɨɛɹɡɚɬɟɥɶɧɨɝɨ ɫɩɟɰɢɮɢɤɚɬɨɪɚ ɤɥɚɫɫɚ ɩɚɦɹɬɢ, ɫɩɟɰɢɮɢɤɚɬɨɪɨɜ ɬɢɩɚ ɢ ɫɩɢɫɤɚ ɞɟɤɥɚɪɚɬɨɪɨɜ-ɢɧɢɰɢɚɥɢɡɚɬɨɪɨɜ, ɤɚɠɞɵɣ ɢɡ ɤɨɬɨɪɵɯ ɨɛɴɹɜɥɹɟɬ ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɨɞɧɨɣ ɩɟɪɟɦɟɧɧɨɣ ɢ, ɜɨɡɦɨɠɧɨ, ɡɧɚɱɟɧɢɟ, ɩɪɢɫɜɚɢɜɚɟɦɨɟ ɩɟɪɟɦɟɧɧɨɣ ɩɪɢ ɟɟ ɨɛɴɹɜɥɟɧɢɢ. ȼɧɟɲɧɟɟ ɨɩɪɟɞɟɥɟɧɢɟ ɡɚɤɚɧɱɢɜɚɟɬɫɹ ɬɨɱɤɨɣ ɫ ɡɚɩɹɬɨɣ: 10
/* Ɍɪɢ ɩɟɪɟɦɟɧɧɵɯ ɬɢɩɚ int ɛɟɡ ɹɜɧɨɣ ɢɧɢɰɢɚɥɢɡɚɰɢɢ */ double x=1, y=2; /* Ⱦɜɟ ɩɟɪɟɦɟɧɧɵɯ ɬɢɩɚ double ɫ ɧɚɱɚɥɶɧɵɦɢ ɡɧɚɱɟɧɢɹɦɢ 1 ɢ 2 */ char c1='0'; /* ɉɟɪɟɦɟɧɧɚɹ ɬɢɩɚ char, ɟɟ ɡɧɚɱɟɧɢɟ – ɤɨɞ ɥɢɬɟɪɵ 0 */ Ɍɟɤɫɬ, ɡɚɩɢɫɚɧɧɵɣ ɜ ɷɬɢɯ ɩɪɢɦɟɪɚɯ ɩɨɫɥɟ ɡɧɚɤɨɜ //, ɹɜɥɹɟɬɫɹ ɤɨɦint i, j, k;
ɦɟɧɬɚɪɢɟɦ ɢ ɫɥɭɠɢɬ ɬɨɥɶɤɨ ɞɥɹ ɞɨɤɭɦɟɧɬɢɪɨɜɚɧɢɹ ɩɪɨɝɪɚɦɦɵ. Ɍɚɤɨɣ ɤɨɦɦɟɧɬɚɪɢɣ ɦɨɠɟɬ ɡɚɧɢɦɚɬɶ ɬɨɥɶɤɨ ɨɞɧɭ ɫɬɪɨɤɭ ɬɟɤɫɬɚ ɢ ɞɨɩɭɫɤɚɟɬɫɹ ɜ ɬɟɤɫɬɚɯ ɩɪɨɝɪɚɦɦ ɧɚ ɋɢ++. Ʉɨɦɦɟɧɬɚɪɢɣ, ɡɚɧɢɦɚɸɳɢɣ ɧɟɫɤɨɥɶɤɨ ɫɬɪɨɤ, ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɫɩɟɰɢɚɥɶɧɵɟ ɫɤɨɛɤɢ /* ɢ */. ȼ ɤɚɱɟɫɬɜɟ ɫɩɟɰɢɮɢɤɚɬɨɪɨɜ ɤɥɚɫɫɚ ɩɚɦɹɬɢ ɜɨ ɜɧɟɲɧɟɦ ɨɩɪɟɞɟɥɟɧɢɢ ɦɨɠɟɬ ɭɤɚɡɵɜɚɬɶɫɹ ɨɞɧɨ ɢɡ ɤɥɸɱɟɜɵɯ ɫɥɨɜ extern, static ɢɥɢ typedef. ɋɩɟɰɢɮɢɤɚɬɨɪ extern ɨɡɧɚɱɚɟɬ, ɱɬɨ ɨɛɴɹɜɥɹɟɦɵɣ ɨɛɴɟɤɬ ɩɪɢɧɚɞɥɟɠɢɬ ɞɪɭɝɨɦɭ ɩɪɨɝɪɚɦɦɧɨɦɭ ɮɚɣɥɭ, ɚ ɜ ɞɚɧɧɨɦ ɮɚɣɥɟ ɫɨɞɟɪɠɢɬɫɹ ɬɨɥɶɤɨ ɢɧɮɨɪɦɚɰɢɹ ɨ ɟɝɨ ɬɢɩɟ ɢ ɢɦɟɧɢ ɢ ɧɟ ɞɨɥɠɧɨ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɢɧɢɰɢɚɥɢɡɢɪɭɸɳɟɟ ɜɵɪɚɠɟɧɢɟ. ɋɩɟɰɢɮɢɤɚɬɨɪ static ɨɝɪɚɧɢɱɢɜɚɟɬ ɨɛɥɚɫɬɶ ɞɟɣɫɬɜɢɹ ɨɛɴɹɜɥɹɟɦɨɝɨ ɢɦɟɧɢ ɞɚɧɧɵɦ ɮɚɣɥɨɦ ɢɥɢ ɛɥɨɤɨɦ, ɟɫɥɢ ɨɛɴɹɜɥɟɧɢɟ ɫɨɞɟɪɠɢɬɫɹ ɜ ɛɥɨɤɟ. ȿɫɥɢ ɨɛɴɹɜɥɟɧɢɟ ɞɚɧɧɨɝɨ ɫɨɞɟɪɠɢɬɫɹ ɜɧɭɬɪɢ ɬɟɥɚ ɮɭɧɤɰɢɢ (ɥɨɤɚɥɶɧɨɟ ɨɛɴɹɜɥɟɧɢɟ), ɬɨ ɦɨɠɧɨ ɭɤɚɡɵɜɚɬɶ ɫɩɟɰɢɮɢɤɚɬɨɪɵ ɤɥɚɫɫɚ ɩɚɦɹɬɢ register ɢɥɢ auto. ɋɩɟɰɢɮɢɤɚɬɨɪ register ɧɨɫɢɬ ɪɟɤɨɦɟɧɞɚɬɟɥɶɧɵɣ ɯɚɪɚɤɬɟɪ, ɤɨɦɩɢɥɹɬɨɪ ɩɵɬɚɟɬɫɹ ɪɚɡɦɟɫɬɢɬɶ ɞɚɧɧɵɟ ɷɬɨɝɨ ɤɥɚɫɫɚ ɜ ɪɟɝɢɫɬɪɟ ɩɪɨɰɟɫɫɨɪɚ, ɟɫɥɢ ɜ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɢɦɟɸɬɫɹ ɫɜɨɛɨɞɧɵɟ ɪɟɝɢɫɬɪɵ. ɋɩɟɰɢɮɢɤɚɬɨɪ auto ɩɪɢɧɢɦɚɟɬɫɹ ɩɨ ɭɦɨɥɱɚɧɢɸ ɢ ɩɨɷɬɨɦɭ ɹɜɧɨ ɧɟ ɭɤɚɡɵɜɚɟɬɫɹ, ɨɧ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɞɚɧɧɵɟ ɤɥɚɫɫɚ auto ɞɨɥɠɧɵ ɪɚɡɦɟɳɚɬɶɫɹ ɜ ɩɪɨɝɪɚɦɦɧɨɦ ɫɬɟɤɟ ɩɪɢ ɜɵɡɨɜɟ ɮɭɧɤɰɢɢ. ɋɩɟɰɢɮɢɤɚɬɨɪ typedef ɫɥɭɠɢɬ ɞɥɹ ɩɪɢɫɜɨɟɧɢɹ ɢɦɟɧɢ ɨɩɢɫɵɜɚɟɦɨɦɭ ɬɢɩɭ ɞɚɧɧɵɯ ɢ ɛɭɞɟɬ ɪɚɫɫɦɨɬɪɟɧ ɩɨɞɪɨɛɧɟɟ ɜ ɫɥɟɞɭɸɳɟɦ ɩɚɪɚɝɪɚɮɟ. ɇɚɪɹɞɭ ɫ ɩɨɤɚɡɚɧɧɵɦɢ ɜɵɲɟ ɤɨɧɫɬɚɧɬɚɦɢ-ɥɢɬɟɪɚɥɚɦɢ, ɡɧɚɱɟɧɢɹ ɤɨɬɨɪɵɯ ɨɩɪɟɞɟɥɹɸɬɫɹ ɢɯ ɩɪɟɞɫɬɚɜɥɟɧɢɟɦ ɜ ɩɪɨɝɪɚɦɦɟ, ɜ ɋɢ ɢ ɋɢ++ ɩɪɟɞɭɫɦɨɬɪɟɧɵ ɤɨɧɫɬɚɧɬɵ, ɤɨɬɨɪɵɦ ɩɪɢɫɜɚɢɜɚɸɬɫɹ ɫɨɛɫɬɜɟɧɧɵɟ ɢɦɟɧɚ - ɢɦɟɧɨɜɚɧɧɵɟ ɤɨɧɫɬɚɧɬɵ. ȼ ɨɩɢɫɚɧɢɢ ɢɦɟɧɨɜɚɧɧɨɣ ɤɨɧɫɬɚɧɬɵ ɩɪɢɫɭɬɫɬɜɭɟɬ ɨɩɢɫɚɬɟɥɶ const, ɧɚɩɪɢɦɟɪ, const double Pi=3.141592653;
ɉɟɪɟɦɟɧɧɨɣ, ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɤɨɬɨɪɨɣ ɨɛɴɹɜɥɟɧ ɫ ɨɩɢɫɚɬɟɥɟɦ const, ɧɟɥɶɡɹ ɩɪɢɫɜɨɢɬɶ ɢɧɨɟ ɡɧɚɱɟɧɢɟ, ɱɟɦ ɛɵɥɨ ɭɫɬɚɧɨɜɥɟɧɨ ɩɪɢ ɨɛɴɹɜɥɟɧɢɢ ɢɞɟɧɬɢɮɢɤɚɬɨɪɚ. ɂɧɢɰɢɚɥɢɡɢɪɭɸɳɟɟ ɡɧɚɱɟɧɢɟ ɩɪɢ ɨɛɴɹɜɥɟɧɢɢ ɤɨɧɫɬɚɧɬɵ ɹɜɥɹɟɬɫɹ ɨɛɹɡɚɬɟɥɶɧɵɦ. ɇɚɪɹɞɭ ɫ ɛɚɡɨɜɵɦɢ ɰɟɥɵɦɢ ɢ ɜɟɳɟɫɬɜɟɧɧɵɦɢ ɬɢɩɚɦɢ ɪɚɡɥɢɱɧɵɯ ɪɚɡɦɟɪɨɜ ɜ ɩɪɨɝɪɚɦɦɟ ɦɨɝɭɬ ɨɛɴɹɜɥɹɬɶɫɹ ɢ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɚɧɧɵɟ ɬɢɩɨɜ, ɨɩɪɟɞɟɥɹɟɦɵɯ ɩɪɨɝɪɚɦɦɢɫɬɨɦ: ɭɤɚɡɚɬɟɥɢ, ɫɫɵɥɤɢ, ɚɝɪɟɝɚɬɵ ɞɚɧɧɵɯ ɢ ɞɚɧɧɵɟ ɩɟɪɟɱɢɫɥɢɦɨɝɨ ɬɢɩɚ. 11
ɉɟɪɟɱɢɫɥɟɧɧɵɣ ɬɢɩ ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɞɚɧɧɵɯ ɰɟɥɨɝɨ ɬɢɩɚ, ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɩɪɢɧɢɦɚɬɶ ɨɝɪɚɧɢɱɟɧɧɵɣ ɧɚɛɨɪ ɡɧɚɱɟɧɢɣ. Ʉɚɠɞɨɦɭ ɡɧɚɱɟɧɢɸ ɫɨɨɬɜɟɬɫɬɜɭɟɬ ɫɨɛɫɬɜɟɧɧɨɟ ɢɦɹ-ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɢ ɰɟɥɨɟ ɱɢɫɥɨ, ɡɧɚɱɟɧɢɟ ɷɬɨɝɨ ɢɦɟɧɢ. Ɉɛɴɹɜɥɟɧɢɟ ɩɟɪɟɱɢɫɥɟɧɧɨɝɨ ɬɢɩɚ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: enum ɢɞɟɧɬɢɮɢɤɚɬɨɪ {ɫɩɢɫɨɤ ɩɟɪɟɱɢɫɥɟɧɢɹ} ɞɟɤɥɚɪɚɬɨɪɵ-ɢɧɢɰɢɚɥɢɡɚɬɨɪɵ;
Ɂɞɟɫɶ ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɡɚɞɚɟɬ ɢɦɹ ɩɟɪɟɱɢɫɥɟɧɧɨɝɨ ɬɢɩɚ, ɫɩɢɫɨɤ ɩɟɪɟɱɢɫɥɟɧɢɹ ɫɨɫɬɨɢɬ ɢɡ ɩɟɪɟɱɢɫɥɢɬɟɥɟɣ, ɪɚɡɞɟɥɟɧɧɵɯ ɡɚɩɹɬɵɦɢ. Ʉɚɠɞɵɣ ɩɟɪɟɱɢɫɥɢɬɟɥɶ ɡɚɞɚɟɬɫɹ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɦ ɢ, ɜɨɡɦɨɠɧɨ, ɰɟɥɵɦ ɡɧɚɱɟɧɢɟɦ ɬɢɩɚ char ɢɥɢ int, ɧɚɩɪɢɦɟɪ, enum color {RED, GREEN, BLUE} en_color; enum lex_type {CNST, VAR, OPER=3, FUNC};
ȿɫɥɢ ɡɧɚɱɟɧɢɟ ɩɟɪɟɱɢɫɥɢɬɟɥɹ ɧɟ ɡɚɞɚɧɨ, ɩɟɪɜɵɣ ɢɡ ɧɢɯ ɩɨɥɭɱɚɟɬ ɡɧɚɱɟɧɢɟ 0, ɚ ɤɚɠɞɵɣ ɫɥɟɞɭɸɳɢɣ - ɡɧɚɱɟɧɢɟ, ɛɨɥɶɲɟɟ ɧɚ 1. ȼɨɨɛɳɟ ɥɸɛɨɣ ɩɟɪɟɱɢɫɥɢɬɟɥɶ ɩɨ ɭɦɨɥɱɚɧɢɸ ɢɦɟɟɬ ɡɧɚɱɟɧɢɟ ɧɚ 1 ɛɨɥɶɲɟ ɩɪɟɞɵɞɭɳɟɝɨ. ȼ ɋɢ/ɋɢ++ ɩɪɢɧɹɬɨ ɡɚɩɢɫɵɜɚɬɶ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɩɟɪɟɱɢɫɥɢɬɟɥɟɣ ɩɪɨɩɢɫɧɵɦɢ ɛɭɤɜɚɦɢ. ɂɦɟɧɚ ɩɟɪɟɱɢɫɥɢɬɟɥɟɣ ɢɫɩɨɥɶɡɭɸɬɫɹ ɥɢɛɨ ɤɚɤ ɢɦɟɧɨɜɚɧɧɵɟ ɤɨɧɫɬɚɧɬɵ, ɥɢɛɨ ɞɥɹ ɩɪɢɫɜɚɢɜɚɧɢɹ ɩɟɪɟɦɟɧɧɵɦ ɩɟɪɟɱɢɫɥɢɦɨɝɨ ɬɢɩɚ. ȼ ɋɢ/ɋɢ++ ɞɥɹ ɫɫɵɥɨɤ ɧɚ ɩɟɪɟɦɟɧɧɭɸ ɬɨɝɨ ɢɥɢ ɢɧɨɝɨ ɬɢɩɚ ɫɥɭɠɚɬ ɭɤɚɡɚɬɟɥɢ. ɍɤɚɡɚɬɟɥɶ - ɷɬɨ ɬɢɩ ɞɚɧɧɵɯ, ɡɧɚɱɟɧɢɹɦɢ ɤɨɬɨɪɵɯ ɹɜɥɹɸɬɫɹ ɚɞɪɟɫɚ ɞɪɭɝɢɯ ɞɚɧɧɵɯ. ɉɪɢ ɨɛɴɹɜɥɟɧɢɢ ɭɤɚɡɚɬɟɥɹ ɩɟɪɟɞ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɦ ɡɚɩɢɫɵɜɚɟɬɫɹ ɡɧɚɤ *. ɍɤɚɡɚɬɟɥɶ ɦɨɠɟɬ ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɬɶɫɹ ɚɞɪɟɫɨɦ ɬɨɝɨ ɢɥɢ ɢɧɨɝɨ ɞɚɧɧɨɝɨ, ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɚɞɪɟɫɚ ɫɥɭɠɢɬ ɨɩɟɪɚɰɢɹ & (ɚɦɩɟɪɫɟɧɞ): double y; double *px, *py=&y;
Ⱦɥɹ ɭɤɚɡɚɬɟɥɟɣ ɨɩɪɟɞɟɥɟɧɵ ɨɩɟɪɚɰɢɢ ɫɪɚɜɧɟɧɢɹ, ɫɥɨɠɟɧɢɹ ɭɤɚɡɚɬɟɥɹ ɫ ɰɟɥɵɦ ɱɢɫɥɨɦ, ɜɵɱɢɬɚɧɢɹ ɞɜɭɯ ɭɤɚɡɚɬɟɥɟɣ, ɚ ɬɚɤɠɟ ɨɩɟɪɚɰɢɹ ɢɧɞɟɤɫɢɪɨɜɚɧɢɹ (ɨɩɟɪɚɰɢɹ «[]»). Ⱦɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɩɟɪɟɦɟɧɧɨɣ ɩɨ ɭɤɚɡɚɬɟɥɸ ɜɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɰɢɹ ɪɚɡɵɦɟɧɨɜɚɧɢɹ, ɨɛɨɡɧɚɱɚɟɦɚɹ ɡɧɚɤɨɦ * (ɡɜɟɡɞɨɱɤɚ), ɧɚɩɪɢɦɟɪ, *py=7.5;
ɉɪɢ ɨɛɴɹɜɥɟɧɢɢ ɭɤɚɡɚɬɟɥɹ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɨɩɢɫɚɬɟɥɶ const, ɧɚɩɪɢɦɟɪ, const int cc=20; const int *pc=&cc; double
*const delta=0.001;
// Ɇɨɠɧɨ ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɬɶ // ɚɞɪɟɫɨɦ ɤɨɧɫɬɚɧɬɵ // ɍɤɚɡɚɬɟɥɶ-ɤɨɧɫɬɚɧɬɚ
Ʉɪɨɦɟ ɨɛɵɱɧɵɯ ɩɟɪɟɦɟɧɧɵɯ ɢ ɭɤɚɡɚɬɟɥɟɣ ɜ ɋɢ++ ɢɦɟɟɬɫɹ ɬɢɩ «ɫɫɵɥɤɚ ɧɚ ɩɟɪɟɦɟɧɧɭɸ», ɡɚɞɚɸɳɢɣ ɞɥɹ ɩɟɪɟɦɟɧɧɨɣ ɞɨɩɨɥɧɢɬɟɥɶɧɨɟ ɢɦɹ (ɩɫɟɜɞɨɧɢɦ). ȼɧɭɬɪɟɧɧɟɟ ɩɪɟɞɫɬɚɜɥɟɧɢɟ ɫɫɵɥɤɢ ɹɜɥɹɟɬɫɹ ɬɚɤɢɦ ɠɟ, ɤɚɤ ɞɥɹ ɭɤɚɡɚɬɟɥɹ, ɬɨ ɟɫɬɶ ɜ ɜɢɞɟ ɚɞɪɟɫɚ ɩɟɪɟɦɟɧɧɨɣ, ɧɨ ɨɛɪɚɳɟɧɢɟ ɤ ɩɟɪɟɦɟɧɧɨɣ ɩɨ ɫɫɵɥɤɟ ɡɚɩɢɫɵɜɚɟɬɫɹ ɜ ɬɨɣ ɠɟ ɮɨɪɦɟ, ɱɬɨ ɢ ɨɛɪɚɳɟɧɢɟ ɩɨ ɨɫɧɨɜɧɨɦɭ ɢɦɟɧɢ. ɉɟɪɟɦɟɧɧɚɹ ɬɢɩɚ «ɫɫɵɥɤɚ» ɜɫɟɝɞɚ ɢɧɢɰɢɚɥɢɡɢɪɭɟɬɫɹ ɡɚɞɚɧɢɟɦ ɢɦɟɧɢ
12
ɩɟɪɟɦɟɧɧɨɣ, ɤ ɤɨɬɨɪɨɣ ɨɬɧɨɫɢɬɫɹ ɫɫɵɥɤɚ. ɉɪɢ ɨɛɴɹɜɥɟɧɢɢ ɫɫɵɥɤɢ ɡɚ ɢɦɟɧɟɦ ɬɢɩɚ ɡɚɩɢɫɵɜɚɟɬɫɹ ɡɧɚɤ «&» (ɚɦɩɟɪɫɟɧɞ): int ii; int& aii=ii;
ɉɪɢ ɬɚɤɨɦ ɨɩɢɫɚɧɢɢ ɨɩɟɪɚɬɨɪɵ aii=5; ɢ ii=5; ɷɤɜɢɜɚɥɟɧɬɧɵ. ɂɡ ɩɟɪɟɦɟɧɧɵɯ ɥɸɛɨɝɨ ɬɢɩɚ ɦɨɝɭɬ ɨɛɪɚɡɨɜɵɜɚɬɶɫɹ ɦɚɫɫɢɜɵ. ɉɪɢ ɨɛɴɹɜɥɟɧɢɢ ɦɚɫɫɢɜɚ ɜ ɞɟɤɥɚɪɚɬɨɪɟ-ɢɧɢɰɢɚɥɢɡɚɬɨɪɟ ɡɚ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɦ ɦɚɫɫɢɜɚ ɡɚɞɚɟɬɫɹ ɱɢɫɥɨ ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ ɜ ɤɜɚɞɪɚɬɧɵɯ ɫɤɨɛɤɚɯ: int a[5];
// Ɇɚɫɫɢɜ ɢɡ ɩɹɬɢ ɷɥɟɦɟɧɬɨɜ ɬɢɩɚ int
ɂɧɞɟɤɫɵ ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ ɜɫɟɝɞɚ ɧɚɱɢɧɚɸɬɫɹ ɫ 0, ɢɧɞɟɤɫ ɩɨɫɥɟɞɧɟɝɨ ɷɥɟɦɟɧɬɚ ɧɚ ɟɞɢɧɢɰɭ ɦɟɧɶɲɟ ɱɢɫɥɚ ɷɥɟɦɟɧɬɨɜ ɜ ɦɚɫɫɢɜɟ. Ɇɚɫɫɢɜ ɦɨɠɟɬ ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɬɶɫɹ ɫɩɢɫɤɨɦ ɡɧɚɱɟɧɢɣ ɜ ɮɢɝɭɪɧɵɯ ɫɤɨɛɤɚɯ: int b[4]={1, 2, 3, 4};
ɉɪɢ ɧɚɥɢɱɢɢ ɫɩɢɫɤɚ ɢɧɢɰɢɚɥɢɡɚɰɢɢ, ɨɯɜɚɬɵɜɚɸɳɟɝɨ ɜɫɟ ɷɥɟɦɟɧɬɵ ɦɚɫɫɢɜɚ, ɦɨɠɧɨ ɧɟ ɭɤɚɡɵɜɚɬɶ ɱɢɫɥɨ ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ, ɨɧɨ ɛɭɞɟɬ ɨɩɪɟɞɟɥɟɧɨ ɤɨɦɩɢɥɹɬɨɪɨɦ: int c[]={1, 2, 3};
// Ɇɚɫɫɢɜ ɢɡ ɬɪɟɯ ɷɥɟɦɟɧɬɨɜ ɬɢɩɚ int
Ɇɧɨɝɨɦɟɪɧɵɟ ɦɚɫɫɢɜɵ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɤɚɤ ɦɚɫɫɢɜɵ ɦɚɫɫɢɜɨɜ, ɢ ɞɥɹ ɤɚɠɞɨɝɨ ɢɡɦɟɪɟɧɢɹ ɭɤɚɡɵɜɚɟɬɫɹ ɱɢɫɥɨ ɷɥɟɦɟɧɬɨɜ: double aa[2][2]={1, 2, 3, 4};
// Ɇɚɬɪɢɰɚ 2*2
Ɇɚɫɫɢɜɵ ɬɢɩɚ char ɦɨɝɭɬ ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɬɶɫɹ ɫɬɪɨɤɨɜɵɦ ɥɢɬɟɪɚɥɨɦ. ɋɬɪɨɤɨɜɵɣ ɥɢɬɟɪɚɥ - ɷɬɨ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɥɸɛɵɯ ɫɢɦɜɨɥɨɜ, ɤɪɨɦɟ ɤɚɜɵɱɟɤ ɢ ɨɛɪɚɬɧɨɣ ɤɨɫɨɣ ɱɟɪɬɵ, ɡɚɤɥɸɱɟɧɧɚɹ ɜ ɤɚɜɵɱɤɢ. ȿɫɥɢ ɫɬɪɨɤɨɜɵɣ ɥɢɬɟɪɚɥ ɧɟ ɭɦɟɳɚɟɬɫɹ ɧɚ ɨɞɧɨɣ ɫɬɪɨɤɟ, ɟɝɨ ɦɨɠɧɨ ɩɪɟɪɜɚɬɶ ɫɢɦɜɨɥɨɦ «\» ɢ ɩɪɨɞɨɥɠɢɬɶ ɫ ɧɚɱɚɥɚ ɫɥɟɞɭɸɳɟɣ ɫɬɪɨɤɢ. ȼ ɫɬɚɧɞɚɪɬɟ C++ ɩɪɟɞɭɫɦɨɬɪɟɧɚ ɢ ɞɪɭɝɚɹ ɜɨɡɦɨɠɧɨɫɬɶ ɡɚɩɢɫɢ ɞɥɢɧɧɵɯ ɥɢɬɟɪɚɥɨɜ ɜ ɜɢɞɟ ɧɟɫɤɨɥɶɤɢɯ ɡɚɩɢɫɚɧɧɵɯ ɩɨɞɪɹɞ ɫɬɪɨɤɨɜɵɯ ɥɢɬɟɪɚɥɨɜ. ȿɫɥɢ ɦɟɠɞɭ ɫɬɪɨɤɨɜɵɦɢ ɥɢɬɟɪɚɥɚɦɢ ɧɟɬ ɫɢɦɜɨɥɨɜ, ɨɬɥɢɱɧɵɯ ɨɬ ɩɪɨɛɟɥɨɜ, ɬɚɤɢɟ ɥɢɬɟɪɚɥɵ ɫɥɢɜɚɸɬɫɹ ɤɨɦɩɢɥɹɬɨɪɨɦ ɜ ɨɞɢɧ. ɉɪɢ ɪɚɡɦɟɳɟɧɢɢ ɜ ɩɚɦɹɬɢ ɜ ɤɨɧɰɟ ɫɬɪɨɤɨɜɨɝɨ ɥɢɬɟɪɚɥɚ ɞɨɛɚɜɥɹɟɬɫɹ ɫɢɦɜɨɥ '\0', ɬɨ ɟɫɬɶ ɧɭɥɟɜɨɣ ɛɚɣɬ. ɋɬɪɨɤɨɜɵɣ ɥɢɬɟɪɚɥ ɦɨɠɟɬ ɩɪɢɦɟɧɹɬɶɫɹ ɢ ɞɥɹ ɢɧɢɰɢɚɥɢɡɚɰɢɢ ɭɤɚɡɚɬɟɥɹ ɧɚ ɬɢɩ char: char str1[11]="ɗɬɨ ɫɬɪɨɤɚ", char str2[]="Ɋɚɡɦɟɪ ɷɬɨɝɨ ɦɚɫɫɢɜɚ ɨɩɪɟɞɟɥɹɟɬɫɹ" " ɱɢɫɥɨɦ ɡɧɚɤɨɜ ɜ ɥɢɬɟɪɚɥɟ + 1"; char *pstr="ɍɤɚɡɚɬɟɥɶ ɫ ɢɧɢɰɢɚɥɢɡɚɰɢɟɣ ɫɬɪɨɤɨɣ";
ɂɦɹ ɦɚɫɫɢɜɚ ɜ ɋɢ/ɋɢ++ ɹɜɥɹɟɬɫɹ ɭɤɚɡɚɬɟɥɟɦ-ɤɨɧɫɬɚɧɬɨɣ, ɫɫɵɥɚɸɳɢɦɫɹ ɧɚ ɩɟɪɜɵɣ ɷɥɟɦɟɧɬ ɦɚɫɫɢɜɚ, ɢɦɟɸɳɢɣ ɢɧɞɟɤɫ, ɪɚɜɧɵɣ ɧɭɥɸ. Ⱦɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɷɥɟɦɟɧɬɭ ɦɚɫɫɢɜɚ ɭɤɚɡɵɜɚɟɬɫɹ ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɦɚɫɫɢɜɚ ɢ ɢɧɞɟɤɫ ɷɥɟɦɟɧɬɚ ɜ ɤɪɭɝɥɵɯ ɫɤɨɛɤɚɯ, ɧɚɩɪɢɦɟɪ, c[2], aa[0][1].
13
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
ɉɨɫɤɨɥɶɤɭ ɩɪɢ ɨɩɢɫɚɧɢɢ ɱɥɟɧɨɜ ɫɬɪɭɤɬɭɪɵ ɞɨɥɠɧɵ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɬɨɥɶɤɨ ɪɚɧɟɟ ɨɩɪɟɞɟɥɟɧɧɵɟ ɢɦɟɧɚ ɬɢɩɨɜ, ɩɪɟɞɭɫɦɨɬɪɟɧ ɜɚɪɢɚɧɬ ɩɪɟɞɜɚɪɢɬɟɥɶɧɨɝɨ ɨɛɴɹɜɥɟɧɢɹ ɫɬɪɭɤɬɭɪɵ, ɡɚɞɚɸɳɢɣ ɬɨɥɶɤɨ ɢɦɹ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ. ɇɚɩɪɢɦɟɪ, ɱɬɨɛɵ ɨɩɢɫɚɬɶ ɷɥɟɦɟɧɬ ɞɜɨɢɱɧɨɝɨ ɞɟɪɟɜɚ, ɫɨɞɟɪɠɚɳɢɣ ɭɤɚɡɚɬɟɥɢ ɧɚ ɥɟɜɭɸ ɢ ɩɪɚɜɭɸ ɜɟɬɜɢ ɞɟɪɟɜɚ, ɢ ɭɤɚɡɚɬɟɥɶ ɧɚ ɧɟɤɨɬɨɪɭɸ ɫɬɪɭɤɬɭɪɭ ɬɢɩɚ 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
Ɉɩɢɫɚɧɢɹ ɬɢɩɨɜ, ɨɛɴɹɜɥɹɟɦɵɯ ɩɪɨɝɪɚɦɦɢɫɬɨɦ, ɜ ɬɨɦ ɱɢɫɥɟ ɫɬɪɭɤɬɭɪ ɢ ɨɛɴɟɞɢɧɟɧɢɣ, ɦɨɝɭɬ ɛɵɬɶ ɞɨɫɬɚɬɨɱɧɨ ɛɨɥɶɲɢɦɢ, ɩɨɷɬɨɦɭ ɜ ɋɢ/ɋɢ++ ɩɪɟɞɭɫɦɨɬɪɟɧɚ ɜɨɡɦɨɠɧɨɫɬɶ ɩɪɢɫɜɚɢɜɚɧɢɹ ɬɢɩɚɦ ɫɨɛɫɬɜɟɧɧɵɯ ɢɦɟɧ (ɫɢɧɨɧɢɦɨɜ), ɞɨɫɬɢɝɚɹ ɩɪɢ ɷɬɨɦ ɩɨɜɵɲɟɧɢɹ ɧɚɝɥɹɞɧɨɫɬɢ ɩɪɨɝɪɚɦɦɧɵɯ ɬɟɤɫɬɨɜ. ɋɢɧɨɧɢɦ ɢɦɟɧɢ ɬɢɩɚ ɜɜɨɞɢɬɫɹ ɫ ɤɥɸɱɟɜɵɦ ɫɥɨɜɨɦ typedef ɢ ɫɬɪɨɢɬɫɹ ɤɚɤ ɨɛɵɱɧɨɟ ɨɛɴɹɜɥɟɧɢɟ, ɧɨ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɜ ɞɟɤɥɚɪɚɬɨɪɚɯ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɢɧɬɟɪɩɪɟɬɢɪɭɸɬɫɹ ɤɚɤ ɫɢɧɨɧɢɦɵ ɨɩɢɫɚɧɧɵɯ ɬɢɩɨɜ. ɋɢɧɨɧɢɦɵ ɢɦɟɧ ɬɢɩɨɜ ɩɪɢɧɹɬɨ ɡɚɩɢɫɵɜɚɬɶ ɩɪɨɩɢɫɧɵɦɢ ɛɭɤɜɚɦɢ, ɱɬɨɛɵ ɨɬɥɢɱɚɬɶ ɢɯ ɨɬ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɜ ɩɟɪɟɦɟɧɧɵɯ. ɇɢɠɟ ɩɪɢɜɟɞɟɧɨ ɧɟɫɤɨɥɶɤɨ ɩɪɢɦɟɪɨɜ ɨɛɴɹɜɥɟɧɢɹ ɫɢɧɨɧɢɦɨɜ ɢɦɟɧ ɬɢɩɨɜ. typedef struct {double re, im} COMPLEX; typedef int *PINT;
ɉɨɫɥɟ ɬɚɤɢɯ ɨɛɴɹɜɥɟɧɢɣ ɫɢɧɨɧɢɦ ɢɦɟɧɢ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɤɚɤ ɫɩɟɰɢɮɢɤɚɬɨɪ ɬɢɩɚ: COMPLEX ca, *pca; PINT pi;
/* ɩɟɪɟɦɟɧɧɚɹ ɬɢɩɚ COMPLEX ɢ ɭɤɚɡɚɬɟɥɶ ɧɚ COMPLEX */ // ɭɤɚɡɚɬɟɥɶ ɧɚ int
ɉɪɢɜɟɞɟɧɧɨɟ ɜɵɲɟ ɨɩɢɫɚɧɢɟ ɫɬɪɭɤɬɭɪ ɢ ɨɛɴɟɞɢɧɟɧɢɣ ɜ ɨɫɧɨɜɧɨɦ ɫɨɨɬɜɟɬɫɬɜɭɟɬ ɢɯ ɩɨɫɬɪɨɟɧɢɸ ɜ ɹɡɵɤɟ ɋɢ. ȼ ɋɢ++ ɫɬɪɭɤɬɭɪɵ ɢ ɨɛɴɟɞɢɧɟɧɢɹ ɹɜɥɹɸɬɫɹ ɱɚɫɬɧɵɦɢ ɫɥɭɱɚɹɦɢ ɨɛɴɟɤɬɧɵɯ ɬɢɩɨɜ ɞɚɧɧɵɯ. Ⱦɨɩɨɥɧɢɬɟɥɶɧɵɟ ɫɜɟɞɟɧɢɹ ɨɛ ɷɬɨɦ ɛɭɞɭɬ ɩɪɢɜɟɞɟɧɵ ɩɪɢ ɪɚɫɫɦɨɬɪɟɧɢɢ ɨɛɴɟɤɬɧɨɨɪɢɟɧɬɢɪɨɜɚɧɧɵɯ ɫɪɟɞɫɬɜ ɋɢ++.
2.4. Ɉɩɟɪɚɰɢɢ ɢ ɜɵɪɚɠɟɧɢɹ ɇɟɫɦɨɬɪɹ ɧɚ ɨɝɪɚɧɢɱɟɧɧɵɣ ɧɚɛɨɪ ɛɚɡɨɜɵɯ ɬɢɩɨɜ ɞɚɧɧɵɯ (ɰɟɥɵɟ ɢ ɜɟɳɟɫɬɜɟɧɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɞɚɧɧɵɟ ɢ ɫɬɪɨɤɨɜɵɟ ɥɢɬɟɪɚɥɵ) ɜ ɹɡɵɤɟ ɋɢ++ ɨɩɪɟɞɟɥɟɧ ɨɛɲɢɪɧɵɣ ɧɚɛɨɪ ɨɩɟɪɚɰɢɣ ɧɚɞ ɞɚɧɧɵɦɢ, ɱɚɫɬɶ ɢɡ ɤɨɬɨɪɵɯ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨ ɫɨɨɬɜɟɬɫɬɜɭɟɬ ɦɚɲɢɧɧɵɦ ɤɨɦɚɧɞɚɦ. Ʉɚɤ ɢ ɜɨ ɜɫɟɯ ɹɡɵɤɚɯ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɨɩɟɪɚɰɢɢ ɫɥɭɠɚɬ ɞɥɹ ɩɨɫɬɪɨɟɧɢɹ ɜɵɪɚɠɟɧɢɣ. ȼɵɪɚɠɟɧɢɟ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɨɩɟɪɚɧɞɨɜ ɢ ɡɧɚɤɨɜ ɨɩɟɪɚɰɢɣ ɢ ɫɥɭɠɢɬ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɧɟɤɨɬɨɪɨɝɨ ɡɧɚɱɟɧɢɹ. ȼ ɤɚɱɟɫɬɜɟ ɨɩɟɪɚɧɞɨɜ ɜ ɜɵɪɚɠɟɧɢɢ ɜɵɫɬɭɩɚɸɬ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɩɟɪɟɦɟɧɧɵɯ, ɤɨɧɫɬɚɧɬɵ ɢ ɫɬɪɨɤɨɜɵɟ ɥɢɬɟɪɚɥɵ, ɹɜɥɹɸɳɢɟɫɹ ɩɟɪɜɢɱɧɵɦɢ ɜɵɪɚɠɟɧɢɹɦɢ. ȼɵɪɚɠɟɧɢɟ, ɡɚɤɥɸɱɟɧɧɨɟ ɜ ɤɪɭɝɥɵɟ ɫɤɨɛɤɢ, ɬɚɤɠɟ ɪɚɫɫɦɚɬɪɢɜɚɟɬɫɹ ɤɚɤ ɩɟɪɜɢɱɧɨɟ. Ʉɚɠɞɚɹ ɨɩɟɪɚɰɢɹ ɩɪɟɞɩɨɥɚɝɚɟɬ ɢɫɩɨɥɶɡɨɜɚɧɢɟ ɨɩɪɟɞɟɥɟɧɧɵɯ ɬɢɩɨɜ ɨɩɟɪɚɧɞɨɜ (ɰɟɥɵɯ, ɜɟɳɟɫɬɜɟɧɧɵɯ, ɭɤɚɡɚɬɟɥɟɣ). Ɉɩɟɪɚɰɢɹ ɩɪɢɫɜɚɢɜɚɧɢɹ ɜ ɋɢ++ ɬɚɤɠɟ ɹɜɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟɦ, ɜ ɫɜɹɡɢ ɫ ɷɬɢɦ ɪɚɡɥɢɱɚɸɬɫɹ ɨɩɟɪɚɧɞɵ, ɤɨɬɨɪɵɦ ɦɨɠɧɨ ɩɪɢɫɜɨɢɬɶ ɧɨɜɨɟ ɡɧɚɱɟɧɢɟ, ɢ ɨɩɟɪɚɧɞɵ, ɡɧɚɱɟɧɢɟ ɤɨɬɨɪɵɯ ɧɟ ɦɨɠɟɬ ɦɟɧɹɬɶɫɹ. ɑɬɨɛɵ ɨɩɟɪɚɧɞɭ ɦɨɠɧɨ ɛɵɥɨ ɩɪɢɫɜɨɢɬɶ ɡɧɚɱɟɧɢɟ, ɟɦɭ ɞɨɥɠɧɚ ɫɨɨɬɜɟɬɫɬɜɨɜɚɬɶ ɨɛɥɚɫɬɶ ɩɚɦɹɬɢ ɢ ɤɨɦɩɢɥɹɬɨɪɭ ɞɨɥɠɟɧ ɛɵɬɶ ɢɡɜɟɫɬɟɧ ɚɞɪɟɫ ɷɬɨɣ ɩɚɦɹɬɢ. Ɍɚɤɢɟ ɨɩɟɪɚɧɞɵ ɧɚɡɵɜɚɸɬ 16
1
::
Ⱦɨɫɬɭɩ ɤ ɝɥɨɛɚɥɶɧɨɦɭ ɢɦɟɧɢ ɢɥɢ ɢɦɟɧɢ ɢɡ ɞɪɭɝɨɣ ɨɛɥɚɫɬɢ
ɋɯɟɦɚ
ɇɚɡɧɚɱɟɧɢɟ
ɉɪɢɨɪɢɬɟɬ
Ɂɧɚɤ ɨɩɟɪɚɰɢɢ
L-ɜɵɪɚɠɟɧɢɹɦɢ (ɨɬ ɚɧɝɥɢɣɫɤɨɝɨ left – ɥɟɜɵɣ), ɬɚɤ ɤɚɤ ɨɧɢ ɦɨɝɭɬ ɛɵɬɶ ɡɚɩɢɫɚɧɵ ɜ ɥɟɜɨɣ ɱɚɫɬɢ ɨɩɟɪɚɬɨɪɚ ɩɪɢɫɜɚɢɜɚɧɢɹ. Ɋɟɡɭɥɶɬɚɬ ɜɵɱɢɫɥɟɧɢɹ ɜɵɪɚɠɟɧɢɹ ɡɚɜɢɫɢɬ ɨɬ ɩɪɢɨɪɢɬɟɬɨɜ ɨɩɟɪɚɰɢɣ. ȼ ɋɢ++ ɫɥɨɠɧɚɹ ɫɢɫɬɟɦɚ ɩɪɢɨɪɢɬɟɬɨɜ ɨɩɟɪɚɰɢɣ, ɜɤɥɸɱɚɸɳɚɹ 16 ɭɪɨɜɧɟɣ. ɇɢɠɟ ɜ ɬɚɛɥɢɰɟ ɩɪɢɜɟɞɟɧ ɩɟɪɟɱɟɧɶ ɨɩɟɪɚɰɢɣ ɋɢ++ ɫ ɭɤɚɡɚɧɢɟɦ ɢɯ ɩɪɢɨɪɢɬɟɬɨɜ, ɧɚɡɧɚɱɟɧɢɹ ɢ ɫɯɟɦɵ ɡɚɩɢɫɢ.
::ɝɥɨɛɚɥɶɧɵɣ_ɢɞɟɧɬɢɮɢɤɚɬɨɪ
ɂɅɂ ɢɦɹ_ɨɛɥɚɫɬɢ::ɢɦɹ_ɱɥɟɧɚ_ɫɬɪɭɤɬɭɪɵ
->
Ɉɛɪɚɳɟɧɢɟ ɤ ɱɥɟɧɭ ɫɬɪɭɤɬɭɪɵ ɩɨ ɭɤɚɡɚɬɟɥɸ ɧɚ ɫɬɪɭɤɬɭɪɭ
ɍɤɚɡɚɬɟɥɶ->ɢɦɹ_ɱɥɟɧɚ_ɫɬɪɭɤɬɭɪɵ
1
.
Ɉɛɪɚɳɟɧɢɟ ɤ ɱɥɟɧɭ ɫɬɪɭɤɬɭɪɵ ɩɨ ɢɦɟɧɢ ɫɬɪɭɤɬɭɪɵ
ɢɦɹ_ɫɬɪɭɤɬɭɪɵ.ɢɦɹ_ɱɥɟɧɚ_ɫɬɪɭɤɬɭɪɵ
1
[ ]
Ɉɛɪɚɳɟɧɢɟ ɤ ɷɥɟɦɟɧɬɭ ɦɚɫɫɢɜɚ
ɍɤɚɡɚɬɟɥɶ[ɢɧɞɟɤɫ]
1
( )
ɉɪɟɨɛɪɚɡɨɜɚɧɢɟ ɬɢɩɚ ɞɚɧɧɨɝɨ
1
ɢɦɹ_ɬɢɩɚ(ɜɵɪɚɠɟɧɢɟ)
ɢɥɢ (ɬɢɩ)ɜɵɪɚɠɟɧɢɟ
17
1
( )
ȼɵɡɨɜ ɮɭɧɤɰɢɢ
++
Ⱥɜɬɨɭɜɟɥɢɱɟɧɢɟ
++L-ɡɧɚɱɟɧɢɟ
2
Ⱥɜɬɨɭɦɟɧɶɲɟɧɢɟ
--L-ɡɧɚɱɟɧɢɟ
2
--
ɮɭɧɤɰɢɹ (ɚɪɝɭɦɟɧɬɵ)
ɢɥɢ L-ɡɧɚɱɟɧɢɟ++
ɢɥɢ L-ɡɧɚɱɟɧɢɟ--
2
~
Ȼɢɬɨɜɨɟ ɢɧɜɟɪɬɢɪɨɜɚɧɢɟ
~ɰɟɥɨɟ_ɜɵɪɚɠɟɧɢɟ
2
!
Ʌɨɝɢɱɟɫɤɨɟ ɨɬɪɢɰɚɧɢɟ
!ɜɵɪɚɠɟɧɢɟ
2
-
Ɉɞɧɨɦɟɫɬɧɵɣ ɦɢɧɭɫ
-ɜɵɪɚɠɟɧɢɟ
2
+
Ɉɞɧɨɦɟɫɬɧɵɣ ɩɥɸɫ
+ɜɵɪɚɠɟɧɢɟ
2
&
ɉɨɥɭɱɟɧɢɟ ɚɞɪɟɫɚ
&L-ɡɧɚɱɟɧɢɟ
2
*
Ɋɚɡɵɦɟɧɨɜɚɧɢɟ ɭɤɚɡɚɬɟɥɹ
*ɭɤɚɡɚɬɟɥɶ
2
new
ȼɵɞɟɥɟɧɢɟ ɞɢɧɚɦɢɱɟɫɤɨɣ ɩɚɦɹɬɢ
new ɬɢɩ_ɞɚɧɧɨɝɨ
2
delete
Ɉɫɜɨɛɨɠɞɟɧɢɟ ɩɚɦɹɬɢ
delete ɭɤɚɡɚɬɟɥɶ
2
delete []
Ɉɫɜɨɛɨɠɞɟɧɢɟ ɩɚɦɹɬɢ ɞɥɹ ɦɚɫɫɢɜɚ
delete [] ɭɤɚɡɚɬɟɥɶ
18
2
sizeof
2
Ɋɚɡɦɟɪ ɞɚɧɧɨɝɨ
sizeof ɜɵɪɚɠɟɧɢɟ
Ɋɚɡɦɟɪ ɬɢɩɚ ɞɚɧɧɨɝɨ
sizeof(ɢɦɹ ɬɢɩɚ )
3
*
ɍɦɧɨɠɟɧɢɟ
ɜɵɪɚɠɟɧɢɟ*ɜɵɪɚɠɟɧɢɟ
3
/
Ⱦɟɥɟɧɢɟ
ɜɵɪɚɠɟɧɢɟ/ɜɵɪɚɠɟɧɢɟ
3
%
Ɉɫɬɚɬɨɤ ɨɬ ɞɟɥɟɧɢɹ ɧɚɰɟɥɨ
ɜɵɪɚɠɟɧɢɟ%ɜɵɪɚɠɟɧɢɟ
->*
Ɉɛɪɚɳɟɧɢɟ ɤ ɱɥɟɧɭ ɫɬɪɭɤɬɭɪɵ ɩɨ ɭɤɚɡɚɬɟɥɸ
ɭɤɚɡɚɬɟɥɶ_ɧɚ_ɫɬɪɭɤɬɭɪɭ->* ɢɦɹ_ɱɥɟɧɚ_ɫɬɪɭɤɬɭɪɵ-ɭɤɚɡɚɬɟɥɹ
4
.*
Ɉɛɪɚɳɟɧɢɟ ɤ ɱɥɟɧɭ ɫɬɪɭɤɬɭɪɵ ɩɨ ɢɦɟɧɢ ɫɬɪɭɤɬɭɪɵ
ɢɦɹ_ɫɬɪɭɤɬɭɪɵ.* ɢɦɹ_ɱɥɟɧɚ_ɫɬɪɭɤɬɭɪɵ-ɭɤɚɡɚɬɟɥɹ
5
+
ɋɥɨɠɟɧɢɟ
ɜɵɪɚɠɟɧɢɟ+ɜɵɪɚɠɟɧɢɟ
5
-
ȼɵɱɢɬɚɧɢɟ
ɜɵɪɚɠɟɧɢɟ-ɜɵɪɚɠɟɧɢɟ
6
<<
ɋɞɜɢɝ ɜɥɟɜɨ
ɰɟɥɨɟ_ɜɵɪɚɠɟɧɢɟ<<ɰɟɥɨɟ_ɜɵɪɚɠɟɧɢɟ
6
>>
ɋɞɜɢɝ ɜɩɪɚɜɨ
ɰɟɥɨɟ_ɜɵɪɚɠɟɧɢɟ>>ɰɟɥɨɟ_ɜɵɪɚɠɟɧɢɟ
7
<
Ɇɟɧɶɲɟ
ɜɵɪɚɠɟɧɢɟ<ɜɵɪɚɠɟɧɢɟ
7
<=
Ɇɟɧɶɲɟ ɢɥɢ ɪɚɜɧɨ
ɜɵɪɚɠɟɧɢɟ<=ɜɵɪɚɠɟɧɢɟ
7
>
Ȼɨɥɶɲɟ
ɜɵɪɚɠɟɧɢɟ>ɜɵɪɚɠɟɧɢɟ
7
>=
Ȼɨɥɶɲɟ ɢɥɢ
ɜɵɪɚɠɟɧɢɟ>=ɜɵɪɚɠɟɧɢɟ
4
19
ɪɚɜɧɨ 8
==
Ɋɚɜɧɨ
ɜɵɪɚɠɟɧɢɟ==ɜɵɪɚɠɟɧɢɟ
8
!=
ɇɟ ɪɚɜɧɨ
ɜɵɪɚɠɟɧɢɟ!=ɜɵɪɚɠɟɧɢɟ
9
&
ɉɨɪɚɡɪɹɞɧɚɹ ɤɨɧɴɸɧɤɰɢɹ
ɜɵɪɚɠɟɧɢɟ&ɜɵɪɚɠɟɧɢɟ
10
^
Ɉɬɪɢɰɚɧɢɟ ɪɚɜɧɨɡɧɚɱɧɨɫɬɢ
ɜɵɪɚɠɟɧɢɟ^ɜɵɪɚɠɟɧɢɟ
11
|
ɉɨɪɚɡɪɹɞɧɚɹ ɞɢɡɴɸɧɤɰɢɹ
ɜɵɪɚɠɟɧɢɟ|ɜɵɪɚɠɟɧɢɟ
12
&&
Ʌɨɝɢɱɟɫɤɨɟ "ɂ"
ɜɵɪɚɠɟɧɢɟ&&ɜɵɪɚɠɟɧɢɟ
13
| |
Ʌɨɝɢɱɟɫɤɨɟ "ɂɅɂ"
ɜɵɪɚɠɟɧɢɟ||ɜɵɪɚɠɟɧɢɟ
14
? :
ɍɫɥɨɜɧɨɟ ɜɵɪɚɠɟɧɢɟ
ɜɵɪɚɠɟɧɢɟ?ɜɵɪɚɠɟɧɢɟ1:ɜɵɪɚɠɟɧɢɟ2
15
=
ɉɪɨɫɬɨɟ ɩɪɢɫɜɚɢɜɚɧɢɟ
ɜɵɪɚɠɟɧɢɟ=ɜɵɪɚɠɟɧɢɟ
15
@=
ɋɨɫɬɚɜɧɨɟ ɩɪɢɫɜɚɢɜɚɧɢɟ, ɡɧɚɤ @ ɨɞɢɧ ɢɡ ɡɧɚɤɨɜ ɨɩɟɪɚɰɢɣ
ɜɵɪɚɠɟɧɢɟ@=ɜɵɪɚɠɟɧɢɟ
* / % + << >> & ^ | 16
,
Ɉɩɟɪɚɰɢɹ ɫɥɟɞɨɜɚɧɢɹ
ɜɵɪɚɠɟɧɢɟ,ɜɵɪɚɠɟɧɢɟ
Ɋɚɫɫɦɨɬɪɢɦ ɨɫɨɛɟɧɧɨɫɬɢ ɩɪɢɦɟɧɟɧɢɹ ɧɟɤɨɬɨɪɵɯ ɢɡ ɨɩɟɪɚɰɢɣ, ɩɟɪɟɱɢɫɥɟɧɧɵɯ ɜɵɲɟ. 20
Ɉɩɟɪɚɰɢɹ «::» (ɞɜɚ ɞɜɨɟɬɨɱɢɹ) ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɭɬɨɱɧɟɧɢɹ ɢɦɟɧɢ ɨɛɴɟɤɬɚ ɩɪɨɝɪɚɦɦɵ ɜ ɫɥɭɱɚɟ, ɤɨɝɞɚ ɜ ɷɬɨɦ ɦɟɫɬɟ ɩɪɨɝɪɚɦɦɵ ɢɡɜɟɫɬɧɵ ɞɜɚ ɨɞɢɧɚɤɨɜɵɯ ɢɦɟɧɢ, ɧɚɩɪɢɦɟɪ, ɤɨɝɞɚ ɨɞɧɨ ɢɦɹ ɨɛɴɹɜɥɟɧɨ ɝɥɨɛɚɥɶɧɨ, ɚ ɞɪɭɝɨɟ ɡɚɞɚɧɨ ɜ ɬɟɥɟ ɮɭɧɤɰɢɢ. ȿɫɥɢ ɢɦɟɧɢ ɩɪɟɞɲɟɫɬɜɭɸɬ ɞɜɚ ɞɜɨɟɬɨɱɢɹ, ɬɨ ɷɬɨ ɝɥɨɛɚɥɶɧɨɟ ɢɦɹ. Ⱦɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɱɥɟɧɚɦ ɫɬɪɭɤɬɭɪɵ ɢɥɢ ɨɛɴɟɞɢɧɟɧɢɹ ɦɨɠɧɨ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɥɢɛɨ ɢɦɟɧɟɦ ɫɬɪɭɤɬɭɪɧɨɝɨ ɞɚɧɧɨɝɨ, ɥɢɛɨ ɭɤɚɡɚɬɟɥɟɦ ɧɚ ɫɬɪɭɤɬɭɪɧɨɟ ɞɚɧɧɨɟ. ȼ ɩɟɪɜɨɦ ɫɥɭɱɚɟ ɩɨɥɧɨɟ ɢɦɹ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ ɫɨɫɬɨɢɬ ɢɡ ɢɦɟɧɢ ɫɚɦɨɣ ɫɬɪɭɤɬɭɪɵ ɢ ɢɦɟɧɢ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ, ɪɚɡɞɟɥɟɧɧɵɯ ɬɨɱɤɨɣ. ȼɨ ɜɬɨɪɨɦ ɫɥɭɱɚɟ ɡɚ ɢɦɟɧɟɦ ɭɤɚɡɚɬɟɥɹ ɧɚ ɫɬɪɭɤɬɭɪɭ ɫɬɚɜɢɬɫɹ ɡɧɚɤ «->» (ɫɬɪɟɥɤɚ), ɚ ɡɚ ɧɢɦ ɢɦɹ ɱɥɟɧɚ ɫɬɪɭɤɬɭɪɵ. ɉɭɫɬɶ ɜ ɩɪɨɝɪɚɦɦɟ ɨɛɴɹɜɥɟɧ ɫɬɪɭɤɬɭɪɧɵɣ ɬɢɩ AnyStruct, ɫɨɞɟɪɠɚɳɢɣ ɤɨɦɩɨɧɟɧɬɭ ɫ ɢɦɟɧɟɦ member ɬɢɩɚ int, ɢ ɨɛɴɹɜɥɟɧɵ ɩɟɪɟɦɟɧɧɵɟ // Ⱦɚɧɧɨɟ s1 ɬɢɩɚ AnyStruct /* ɍɤɚɡɚɬɟɥɶ ɧɚ ɞɚɧɧɨɟ ɬɢɩɚ AnyStruct */ Ɍɨɝɞɚ ɤ ɱɥɟɧɭ ɫɬɪɭɤɬɭɪɵ member ɢɡ s1 ɦɨɠɧɨ ɨɛɪɚɬɢɬɶɫɹ ɤɚɤ ɤ s1.member ɢɥɢ ɤɚɤ ɤ ps1->member. AnyStruct s1; AnyStruct *ps1 = &s1;
ɉɨɫɤɨɥɶɤɭ ɱɥɟɧɨɦ ɫɬɪɭɤɬɭɪɵ ɦɨɠɟɬ ɛɵɬɶ ɭɤɚɡɚɬɟɥɶ, ɜ ɋɢ++ ɢɦɟɸɬɫɹ ɫɩɟɰɢɚɥɶɧɵɟ ɨɩɟɪɚɰɢɢ ɪɚɡɵɦɟɧɨɜɚɧɢɹ ɬɚɤɨɝɨ ɭɤɚɡɚɬɟɥɹ, ɨɩɟɪɚɰɢɢ «.*» ɢ «->*». ɉɭɫɬɶ ɨɞɧɢɦ ɢɡ ɱɥɟɧɨɜ ɫɬɪɭɤɬɭɪɵ AnyStruct ɹɜɥɹɟɬɫɹ ɭɤɚɡɚɬɟɥɶ pp1 ɧɚ ɞɚɧɧɨɟ ɬɢɩɚ int. Ɍɨɝɞɚ ɜɵɪɚɠɟɧɢɹ s1.*pp1 ɢ ps1->*pp1 ɨɛɟɫɩɟɱɚɬ ɞɨɫɬɭɩ ɤ ɡɧɚɱɟɧɢɸ ɞɚɧɧɨɝɨ, ɧɚ ɤɨɬɨɪɨɟ ɭɤɚɡɵɜɚɟɬ pp1 ɢɡ s1. ȼɵɲɟ ɨɬɦɟɱɚɥɨɫɶ, ɱɬɨ ɢɦɹ ɦɚɫɫɢɜɚ ɜ ɋɢ/ɋɢ++ ɢɧɬɟɪɩɪɟɬɢɪɭɟɬɫɹ ɤɚɤ ɭɤɚɡɚɬɟɥɶ-ɤɨɧɫɬɚɧɬɚ ɧɚ ɩɟɪɜɵɣ ɷɥɟɦɟɧɬ ɦɚɫɫɢɜɚ. Ⱦɥɹ ɪɚɡɵɦɟɧɨɜɚɧɢɹ ɭɤɚɡɚɬɟɥɹ, ɬ. ɟ. ɞɥɹ ɞɨɫɬɭɩɚ ɤ ɞɚɧɧɨɦɭ ɩɨ ɭɤɚɡɚɬɟɥɸ ɧɚ ɷɬɨ ɞɚɧɧɨɟ, ɫɥɭɠɢɬ ɨɩɟɪɚɰɢɹ «*» (ɡɜɟɡɞɨɱɤɚ). ɋɥɟɞɨɜɚɬɟɥɶɧɨ, ɟɫɥɢ ɜ ɩɪɨɝɪɚɦɦɟ ɨɛɴɹɜɥɟɧ ɦɚɫɫɢɜ int Array1[10];
ɬɨ ɜɵɪɚɠɟɧɢɟ *Array1=0 ɫɥɭɠɢɬ ɞɥɹ ɩɪɢɫɜɨɟɧɢɹ ɧɭɥɟɜɨɝɨ ɡɧɚɱɟɧɢɹ ɩɟɪɜɨɦɭ ɷɥɟɦɟɧɬɭ ɦɚɫɫɢɜɚ. ɑɬɨɛɵ ɨɛɪɚɬɢɬɶɫɹ ɤ ɩɪɨɢɡɜɨɥɶɧɨɦɭ ɷɥɟɦɟɧɬɭ ɦɚɫɫɢɜɚ, ɧɭɠɧɨ ɭɤɚɡɚɬɶ ɢɧɞɟɤɫ ɷɥɟɦɟɧɬɚ, ɧɚɩɪɢɦɟɪ, Array1[3]. ɗɬɨ ɜɵɪɚɠɟɧɢɟ ɷɤɜɢɜɚɥɟɧɬɧɨ ɜɵɪɚɠɟɧɢɸ *(Array1 + 3), ɬɨ ɟɫɬɶ ɬɪɟɛɭɟɬɫɹ ɫɧɚɱɚɥɚ ɭɜɟɥɢɱɢɬɶ ɭɤɚɡɚɬɟɥɶ Array1 ɧɚ 3 ɟɞɢɧɢɰɵ, ɚ ɡɚɬɟɦ ɪɚɡɵɦɟɧɨɜɚɬɶ ɩɨɥɭɱɟɧɧɵɣ ɭɤɚɡɚɬɟɥɶ. ɉɪɢ ɫɥɨɠɟɧɢɢ ɭɤɚɡɚɬɟɥɹ ɧɚ ɨɛɴɟɤɬ ɧɟɤɨɬɨɪɨɝɨ ɬɢɩɚ T ɫ ɰɟɥɵɦ ɱɢɫɥɨɦ N ɡɧɚɱɟɧɢɟ ɭɤɚɡɚɬɟɥɹ ɭɜɟɥɢɱɢɜɚɟɬɫɹ ɧɚ N, ɭɦɧɨɠɟɧɧɨɟ ɧɚ ɞɥɢɧɭ ɞɚɧɧɨɝɨ ɬɢɩɚ T. Ɉɬɦɟɬɢɦ, ɱɬɨ ɢɧɞɟɤɫ ɦɨɠɧɨ ɡɚɞɚɜɚɬɶ ɧɟ ɬɨɥɶɤɨ ɞɥɹ ɢɦɟɧ ɦɚɫɫɢɜɨɜ, ɧɨ ɢ ɞɥɹ ɥɸɛɨɝɨ ɬɢɩɚ ɭɤɚɡɚɬɟɥɹ, ɤɪɨɦɟ ɭɤɚɡɚɬɟɥɹ ɧɚ ɬɢɩ void: int *pint=&Array[4]; pint[2]=1;
21
ȼ ɷɬɨɦ ɩɪɢɦɟɪɟ ɭɤɚɡɚɬɟɥɶ pint ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɧ ɚɞɪɟɫɨɦ ɩɹɬɨɝɨ ɷɥɟɦɟɧɬɚ ɦɚɫɫɢɜɚ Array, ɚ ɡɚɬɟɦ ɫɟɞɶɦɨɦɭ ɷɥɟɦɟɧɬɭ ɷɬɨɝɨ ɦɚɫɫɢɜɚ ɩɪɢɫɜɨɟɧɨ ɡɧɚɱɟɧɢɟ 1. ȼ ɤɚɱɟɫɬɜɟ ɢɧɞɟɤɫɚ ɦɨɠɟɬ ɡɚɞɚɜɚɬɶɫɹ ɥɸɛɨɟ ɜɵɪɚɠɟɧɢɟ ɫɨ ɡɧɚɱɟɧɢɟɦ ɰɟɥɨɝɨ ɬɢɩɚ. ɉɨɫɤɨɥɶɤɭ ɋɢ++ ɹɜɥɹɟɬɫɹ ɬɢɩɢɡɢɪɨɜɚɧɧɵɦ ɹɡɵɤɨɦ, ɜ ɧɟɦ ɨɩɪɟɞɟɥɟɧɵ ɹɜɧɵɟ ɢ ɧɟɹɜɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɨɜ ɞɚɧɧɵɯ. ɇɟɹɜɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɜɵɩɨɥɧɹɸɬɫɹ ɩɪɢ ɛɢɧɚɪɧɵɯ ɚɪɢɮɦɟɬɢɱɟɫɤɢɯ ɨɩɟɪɚɰɢɹɯ ɢ ɨɩɟɪɚɰɢɹɯ ɩɪɢɫɜɚɢɜɚɧɢɹ. ɗɬɢ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ, ɧɚɡɵɜɚɟɦɵɟ ɫɬɚɧɞɚɪɬɧɵɦɢ ɚɪɢɮɦɟɬɢɱɟɫɤɢɦɢ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹɦɢ, ɜɵɩɨɥɧɹɸɬɫɹ ɜ ɫɥɟɞɭɸɳɟɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ: x ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ long double, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛɪɚɡɭɟɬɫɹ ɜ ɬɢɩ long double; x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ double, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛɪɚɡɭɟɬɫɹ ɜ ɬɢɩ double; x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ float, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛɪɚɡɭɟɬɫɹ ɜ ɬɢɩ float; x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ unsigned long int, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛɪɚɡɭɟɬɫɹ ɜ ɬɢɩ unsigned long int; x ɢɧɚɱɟ, ɟɫɥɢ ɨɞɢɧ ɨɩɟɪɚɧɞ ɢɦɟɟɬ ɬɢɩ long int, ɞɪɭɝɨɣ ɨɩɟɪɚɧɞ ɩɪɟɨɛɪɚɡɭɟɬɫɹ ɜ ɬɢɩ long int; x ɢɧɚɱɟ ɜɵɩɨɥɧɹɸɬɫɹ ɫɬɚɧɞɚɪɬɧɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɞɥɹ ɰɟɥɵɯ, ɩɪɢ ɷɬɨɦ ɬɢɩɵ char, short int ɢ ɛɢɬɨɜɵɟ ɩɨɥɹ ɬɢɩɚ int ɩɪɟɨɛɪɚɡɭɸɬɫɹ ɜ ɬɢɩ int; x ɜ ɨɫɬɚɥɶɧɵɯ ɫɥɭɱɚɹɯ ɨɩɟɪɚɧɞɵ ɢɦɟɸɬ ɬɢɩ int. əɜɧɨɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɟ ɬɢɩɨɜ ɦɨɠɟɬ ɛɵɬɶ ɡɚɞɚɧɨ ɜ ɞɜɭɯ ɮɨɪɦɚɯ. ɉɟɪɜɚɹ ɮɨɪɦɚ ɫɨɜɦɟɫɬɢɦɚ ɫ ɋɢ, ɜ ɧɟɣ ɡɚ ɢɦɟɧɟɦ ɬɢɩɚ ɜ ɤɪɭɝɥɵɯ ɫɤɨɛɤɚɯ ɡɚɩɢɫɵɜɚɟɬɫɹ ɩɪɟɨɛɪɚɡɭɟɦɨɟ ɡɧɚɱɟɧɢɟ, ɤɨɬɨɪɨɟ ɦɨɠɟɬ ɛɵɬɶ ɩɟɪɜɢɱɧɵɦ ɜɵɪɚɠɟɧɢɟɦ ɢɥɢ ɜɵɪɚɠɟɧɢɟɦ ɫ ɭɧɚɪɧɨɣ ɨɩɟɪɚɰɢɟɣ. ɂɦɹ ɬɢɩɚ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɦɨɠɟɬ ɛɵɬɶ ɩɪɟɞɫɬɚɜɥɟɧɨ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶɸ ɨɩɢɫɚɬɟɥɟɣ, ɧɚɩɪɢɦɟɪ, (long int*)pp
ɢ ɨɩɪɟɞɟɥɹɟɬ ɩɪɟɨɛɪɚɡɨɜɚɧɢɟ ɧɟɤɨɬɨɪɨɝɨ ɞɚɧɧɨɝɨ pp ɜ ɬɢɩ ɭɤɚɡɚɬɟɥɹ ɧɚ long int. ȼɬɨɪɚɹ ɮɨɪɦɚ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɚ ɡɚɩɢɫɵɜɚɟɬɫɹ ɤɚɤ ɜɵɡɨɜ ɮɭɧɤɰɢɢ, ɩɪɢ ɷɬɨɦ ɢɦɹ ɬɢɩɚ ɞɨɥɠɧɨ ɡɚɞɚɜɚɬɶɫɹ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɦ, ɧɚɩɪɢɦɟɪ, int(x);
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɪɟɡɭɥɶɬɚɬ ɹɜɧɨɝɨ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɧɟ ɹɜɥɹɟɬɫɹ L-ɡɧɚɱɟɧɢɟɦ.
Ɉɩɟɪɚɰɢɢ ɚɜɬɨɭɜɟɥɢɱɟɧɢɹ ɢ ɚɜɬɨɭɦɟɧɶɲɟɧɢɹ («++» ɢ «--») ɦɨɝɭɬ ɛɵɬɶ ɩɪɟɮɢɤɫɧɵɦɢ ɢ ɩɨɫɬɮɢɤɫɧɵɦɢ ɢ ɜɵɡɵɜɚɸɬ ɭɜɟɥɢɱɟɧɢɟ (ɭɦɟɧɶɲɟɧɢɟ) ɫɜɨɟɝɨ ɨɩɟɪɚɧɞɚ ɧɚ ɟɞɢɧɢɰɭ, ɬɨ ɟɫɬɶ ɜɵɪɚɠɟɧɢɟ ++x ɷɤɜɢɜɚɥɟɧɬɧɨ x=x+1, ɚ --x ɷɤɜɢɜɚɥɟɧɬɧɨ x=x-1. ɉɪɟɮɢɤɫɧɚɹ ɨɩɟɪɚɰɢɹ ɜɵɩɨɥɧɹɟɬɫɹ ɞɨ ɬɨɝɨ, ɤɚɤ ɟɟ ɨɩɟɪɚɧɞ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɧ ɜ ɜɵɱɢɫɥɟɧɢɢ ɜɵɪɚɠɟɧɢɹ, ɚ ɩɨɫɬ22
ɮɢɤɫɧɚɹ ɨɩɟɪɚɰɢɹ ɜɵɩɨɥɧɹɟɬɫɹ ɩɨɫɥɟ ɬɨɝɨ, ɤɚɤ ɟɟ ɨɩɟɪɚɧɞ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɧ ɜ ɜɵɪɚɠɟɧɢɢ, ɧɚɩɪɢɦɟɪ ɜ ɪɟɡɭɥɶɬɚɬɟ ɜɵɱɢɫɥɟɧɢɹ ɜɵɪɚɠɟɧɢɹ ++x*2+y--*3;
ɩɟɪɟɦɟɧɧɚɹ x ɫɧɚɱɚɥɚ ɭɜɟɥɢɱɢɜɚɟɬɫɹ ɧɚ 1, ɚ ɡɚɬɟɦ ɭɦɧɨɠɚɟɬɫɹ ɧɚ 2, ɚ ɩɟɪɟɦɟɧɧɚɹ y ɫɧɚɱɚɥɚ ɭɦɧɨɠɚɟɬɫɹ ɧɚ 3, ɡɚɬɟɦ ɭɦɟɧɶɲɚɟɬɫɹ ɧɚ 1. ȿɫɥɢ ɩɟɪɟɞ ɜɵɱɢɫɥɟɧɢɟɦ ɷɬɨɝɨ ɜɵɪɚɠɟɧɢɹ x ɢ y ɛɵɥɢ ɪɚɜɧɵ 1, ɬɨ ɪɟɡɭɥɶɬɚɬ ɜɵɪɚɠɟɧɢɹ ɛɭɞɟɬ ɪɚɜɟɧ 5. Ʉɪɨɦɟ ɬɨɝɨ, ɩɟɪɟɦɟɧɧɚɹ x ɩɨɥɭɱɢɬ ɡɧɚɱɟɧɢɟ 2, ɚ ɩɟɪɟɦɟɧɧɚɹ y - ɡɧɚɱɟɧɢɟ 0. Ɍɚɤɢɦ ɨɛɪɚɡɨɦ, ɨɩɟɪɚɰɢɢ ɚɜɬɨɭɜɟɥɢɱɟɧɢɹ ɢ ɚɜɬɨɭɦɟɧɶɲɟɧɢɹ ɜɫɟɝɞɚ ɞɚɸɬ ɩɨɛɨɱɧɵɣ ɷɮɮɟɤɬ, ɢɡɦɟɧɹɹ ɡɧɚɱɟɧɢɹ ɫɜɨɢɯ ɨɩɟɪɚɧɞɨɜ. Ɉɩɟɪɚɧɞɵ ɷɬɢɯ ɨɩɟɪɚɰɢɣ ɞɨɥɠɧɵ ɛɵɬɶ L-ɡɧɚɱɟɧɢɹɦɢ. Ɉɩɟɪɚɰɢɹ «~» (ɬɢɥɶɞɚ) ɩɪɢɦɟɧɹɟɬɫɹ ɬɨɥɶɤɨ ɤ ɰɟɥɨɦɭ ɡɧɚɱɟɧɢɸ ɢ ɡɚɦɟɧɹɟɬ ɜɫɟ ɛɢɬɵ ɫɜɨɟɝɨ ɨɩɟɪɚɧɞɚ ɫɨ ɡɧɚɱɟɧɢɟɦ 0 ɧɚ 1, ɚ ɛɢɬɵ ɫɨ ɡɧɚɱɟɧɢɟɦ 1 ɧɚ 0. Ʌɨɝɢɱɟɫɤɨɟ ɨɬɪɢɰɚɧɢɟ (ɨɩɟɪɚɰɢɹ «!») ɜɨɡɜɪɚɳɚɟɬ ɡɧɚɱɟɧɢɟ 0 ɰɟɥɨɝɨ ɬɢɩɚ, ɟɫɥɢ ɨɩɟɪɚɧɞ ɧɟ ɪɚɜɟɧ ɧɭɥɸ, ɢɥɢ ɡɧɚɱɟɧɢɟ 1, ɟɫɥɢ ɨɩɟɪɚɧɞ ɪɚɜɟɧ ɧɭɥɸ. Ɉɩɟɪɚɰɢɢ «ɭɧɚɪɧɵɣ +» ɢ «ɭɧɚɪɧɵɣ –» ɢɦɟɸɬ ɨɛɵɱɧɵɣ ɦɚɬɟɦɚɬɢɱɟɫɤɢɣ ɫɦɵɫɥ, ɡɧɚɤ «+» ɧɟ ɢɡɦɟɧɹɟɬ ɡɧɚɱɟɧɢɹ ɨɩɟɪɚɧɞɚ, ɡɧɚɤ «-» ɦɟɧɹɟɬ ɡɧɚɤ ɨɩɟɪɚɧɞɚ ɧɚ ɩɪɨɬɢɜɨɩɨɥɨɠɧɵɣ. Ⱦɥɹ ɩɨɥɭɱɟɧɢɹ ɚɞɪɟɫɚ ɨɩɟɪɚɧɞɚ, ɹɜɥɹɸɳɟɝɨɫɹ L-ɡɧɚɱɟɧɢɟɦ, ɩɪɢɦɟɧɹɟɬɫɹ ɨɩɟɪɚɰɢɹ «&» (ɚɦɩɟɪɫɚɧɞ). Ɋɟɡɭɥɶɬɚɬɨɦ ɞɟɣɫɬɜɢɹ ɷɬɨɣ ɨɩɟɪɚɰɢɢ ɹɜɥɹɟɬɫɹ ɭɤɚɡɚɬɟɥɶ ɧɚ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɣ ɬɢɩ ɞɚɧɧɵɯ. Ɋɚɡɵɦɟɧɨɜɚɧɢɟ ɭɤɚɡɚɬɟɥɹ, ɬɨ ɟɫɬɶ ɩɨɥɭɱɟɧɢɟ ɡɧɚɱɟɧɢɹ ɞɚɧɧɨɝɨ ɩɨ ɭɤɚɡɚɬɟɥɸ ɧɚ ɧɟɝɨ, ɨɛɟɫɩɟɱɢɜɚɟɬɫɹ ɨɩɟɪɚɰɢɟɣ «*» (ɡɜɟɡɞɨɱɤɚ). Ɋɟɡɭɥɶɬɚɬ ɨɩɟɪɚɰɢɢ ɪɚɡɵɦɟɧɨɜɚɧɢɹ ɹɜɥɹɟɬɫɹ L-ɡɧɚɱɟɧɢɟɦ. ȼ ɋɢ++ ɨɩɪɟɞɟɥɟɧɵ ɨɩɟɪɚɰɢɢ ɪɚɡɦɟɳɟɧɢɹ ɞɚɧɧɵɯ ɜ ɞɢɧɚɦɢɱɟɫɤɨɣ ɩɚɦɹɬɢ ɢ ɭɞɚɥɟɧɢɹ ɞɢɧɚɦɢɱɟɫɤɢɯ ɞɚɧɧɵɯ ɢɡ ɩɚɦɹɬɢ. Ɉɩɟɪɚɰɢɹ new ɬɪɟɛɭɟɬ ɜ ɤɚɱɟɫɬɜɟ ɨɩɟɪɚɧɞɚ ɢɦɹ ɬɢɩɚ ɢ ɩɪɟɞɧɚɡɧɚɱɟɧɚ ɞɥɹ ɪɚɡɦɟɳɟɧɢɹ ɞɚɧɧɵɯ ɭɤɚɡɚɧɧɨɝɨ ɬɢɩɚ ɜ ɞɢɧɚɦɢɱɟɫɤɨɣ ɩɚɦɹɬɢ. Ɋɟɡɭɥɶɬɚɬɨɦ ɨɩɟɪɚɰɢɢ ɹɜɥɹɟɬɫɹ ɭɤɚɡɚɬɟɥɶ ɧɚ ɞɚɧɧɵɟ. ɉɪɢ ɧɟɜɨɡɦɨɠɧɨɫɬɢ ɜɵɞɟɥɢɬɶ ɩɚɦɹɬɶ ɨɩɟɪɚɰɢɹ new ɜɨɡɜɪɚɳɚɟɬ ɡɧɚɱɟɧɢɟ NULL – ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɭɸ ɤɨɧɫɬɚɧɬɭ, ɢɦɟɸɳɭɸ ɧɭɥɟɜɨɟ ɡɧɚɱɟɧɢɟ ɩɪɚɤɬɢɱɟɫɤɢ ɜɨ ɜɫɟɯ ɤɨɦɩɢɥɹɬɨɪɚɯ ɋɢ ɢ ɋɢ++. ɉɚɦɹɬɶ, ɜɵɞɟɥɹɟɦɭɸ ɨɩɟɪɚɰɢɟɣ new, ɦɨɠɧɨ ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɬɶ, ɭɤɚɡɚɜ ɡɚ ɢɦɟɧɟɦ ɬɢɩɚ ɫɤɚɥɹɪɧɵɯ ɞɚɧɧɵɯ ɧɚɱɚɥɶɧɨɟ ɡɧɚɱɟɧɢɟ ɜ ɤɪɭɝɥɵɯ ɫɤɨɛɤɚɯ, ɡɚɞɚɧɢɟ ɧɚɱɚɥɶɧɵɯ ɡɧɚɱɟɧɢɣ ɞɥɹ ɚɝɪɟɝɚɬɨɜ ɞɚɧɧɵɯ ɛɭɞɟɬ ɪɚɫɫɦɨɬɪɟɧɨ ɩɨɡɠɟ. ɉɪɢɦɟɪɵ ɩɪɢɦɟɧɟɧɢɹ ɨɩɟɪɚɰɢɢ new: // ɫɨɡɞɚɧɢɟ ɨɛɴɟɤɬɚ ɬɢɩɚ int ɢ // ɩɨɥɭɱɟɧɢɟ ɭɤɚɡɚɬɟɥɹ ɧɚ ɧɟɝɨ int *ip2 = new int(2); /* ɬɨ ɠɟ ɫ ɭɫɬɚɧɨɜɤɨɣ ɧɚɱɚɥɶɧɨɝɨ ɡɧɚɱɟɧɢɹ, ɪɚɜɧɨɝɨ 2 */ inr *intArray = new int [10]; /* ɦɚɫɫɢɜ ɢɡ 10 ɷɥɟɦɟɧɬɨɜ ɬɢɩɚ int */ double **matr = new double [m][n]; /* ɦɚɬɪɢɰɚ ɢɡ m ɫɬɪɨɤ ɢ n ɫɬɨɥɛɰɨɜ */ int *ip = new int;
23
Ⱦɚɧɧɵɟ, ɪɚɡɦɟɳɟɧɧɵɟ ɜ ɞɢɧɚɦɢɱɟɫɤɨɣ ɩɚɦɹɬɢ ɨɩɟɪɚɰɢɟɣ new, ɭɞɚɥɹɸɬɫɹ ɢɡ ɩɚɦɹɬɢ ɨɩɟɪɚɰɢɟɣ delete ɫ ɨɩɟɪɚɧɞɨɦ-ɭɤɚɡɚɬɟɥɟɦ, ɡɧɚɱɟɧɢɟ ɤɨɬɨɪɨɝɨ ɩɨɥɭɱɟɧɨ ɨɩɟɪɚɰɢɟɣ new, ɧɚɩɪɢɦɟɪ, delete intArray; delete ip2; Ɉɩɟɪɚɰɢɹ delete ɬɨɥɶɤɨ ɨɫɜɨɛɨɠɞɚɟɬ ɞɢɧɚɦɢɱɟɫɤɭɸ ɩɚɦɹɬɶ, ɧɨ ɧɟ
ɢɡɦɟɧɹɟɬ ɡɧɚɱɟɧɢɟ ɭɤɚɡɚɬɟɥɹ-ɨɩɟɪɚɧɞɚ. ɉɪɨɝɪɚɦɦɢɫɬ ɞɨɥɠɟɧ ɩɨɦɧɢɬɶ, ɱɬɨ ɩɨɫɥɟ ɨɫɜɨɛɨɠɞɟɧɢɹ ɩɚɦɹɬɢ ɢɫɩɨɥɶɡɨɜɚɬɶ ɷɬɨɬ ɭɤɚɡɚɬɟɥɶ ɞɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɞɚɧɧɵɦ ɧɟɥɶɡɹ. Ɋɚɡɦɟɪ ɞɚɧɧɨɝɨ ɢɥɢ ɬɢɩɚ ɞɚɧɧɨɝɨ ɜ ɛɚɣɬɚɯ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɩɨ ɨɩɟɪɚɰɢɢ sizeof. Ɉɩɟɪɚɧɞ ɦɨɠɟɬ ɛɵɬɶ ɥɸɛɨɝɨ ɬɢɩɚ, ɤɪɨɦɟ ɬɢɩɚ ɮɭɧɤɰɢɢ ɢ ɛɢɬɨɜɨɝɨ ɩɨɥɹ. ȿɫɥɢ ɨɩɟɪɚɧɞɨɦ ɹɜɥɹɟɬɫɹ ɢɦɹ ɬɢɩɚ, ɨɧɨ ɞɨɥɠɧɨ ɡɚɤɥɸɱɚɬɶɫɹ ɜ ɫɤɨɛɤɢ. ȼɨɡɜɪɚɳɚɟɦɨɟ ɡɧɚɱɟɧɢɟ ɢɦɟɟɬ ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɵɣ ɬɢɩ size_t ɰɟɥɵɣ ɬɢɩ, ɪɚɡɦɟɪ ɤɨɬɨɪɨɝɨ ɨɩɪɟɞɟɥɹɟɬɫɹ ɪɟɚɥɢɡɚɰɢɟɣ ɤɨɦɩɢɥɹɬɨɪɚ. Ɉɛɵɱɧɨ ɬɢɩɭ size_t ɫɨɨɬɜɟɬɫɬɜɭɟɬ unsigned int. Ɋɚɡɦɟɪ ɦɚɫɫɢɜɚ ɪɚɜɟɧ ɱɢɫɥɭ ɛɚɣɬ, ɡɚɧɢɦɚɟɦɵɯ ɦɚɫɫɢɜɨɦ ɜ ɩɚɦɹɬɢ, ɪɚɡɦɟɪ ɫɬɪɨɤɨɜɨɝɨ ɥɢɬɟɪɚɥɚ ɷɬɨ «ɱɢɫɥɨ ɡɧɚɤɨɜ ɜ ɥɢɬɟɪɚɥɟ + 1», ɬɨ ɟɫɬɶ ɡɚɜɟɪɲɚɸɳɢɣ ɧɭɥɟɜɨɣ ɛɚɣɬ ɭɱɢɬɵɜɚɟɬɫɹ ɩɪɢ ɨɩɪɟɞɟɥɟɧɢɢ ɞɥɢɧɵ ɥɢɬɟɪɚɥɚ. Ɂɧɚɱɟɧɢɟ, ɜɨɡɜɪɚɳɚɟɦɨɟ sizeof, ɹɜɥɹɟɬɫɹ ɤɨɧɫɬɚɧɬɨɣ. Ȼɢɧɚɪɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɨɩɟɪɚɰɢɢ ɭɦɧɨɠɟɧɢɹ («*»), ɞɟɥɟɧɢɹ («/»), ɩɨɥɭɱɟɧɢɹ ɨɫɬɚɬɤɚ ɨɬ ɞɟɥɟɧɢɹ ɧɚɰɟɥɨ («%»), ɫɥɨɠɟɧɢɹ («+») ɢ ɜɵɱɢɬɚɧɢɹ («-») ɢɦɟɸɬ ɨɛɵɱɧɵɣ ɫɦɵɫɥ ɢ ɨɛɵɱɧɵɣ ɨɬɧɨɫɢɬɟɥɶɧɵɣ ɩɪɢɨɪɢɬɟɬ. ȿɫɥɢ ɨɩɟɪɚɧɞɵ ɚɪɢɮɦɟɬɢɱɟɫɤɨɣ ɨɩɟɪɚɰɢɢ ɢɦɟɸɬ ɪɚɡɧɵɟ ɬɢɩɵ, ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɜɵɩɨɥɧɹɸɬɫɹ ɫɬɚɧɞɚɪɬɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ, ɢ ɬɢɩ ɪɟɡɭɥɶɬɚɬɚ ɨɩɟɪɚɰɢɢ ɨɩɪɟɞɟɥɹɟɬɫɹ ɨɛɳɢɦ ɬɢɩɨɦ ɨɩɟɪɚɧɞɨɜ ɩɨɫɥɟ ɫɬɚɧɞɚɪɬɧɵɯ ɩɪɟɨɛɪɚɡɨɜɚɧɢɣ. ɋɥɟɞɨɜɚɬɟɥɶɧɨ, ɜɵɪɚɠɟɧɢɟ 7/2 ɛɭɞɟɬ ɢɦɟɬɶ ɡɧɚɱɟɧɢɟ 3 ɬɢɩɚ int, ɬɚɤ ɤɚɤ ɨɛɚ ɨɩɟɪɚɧɞɚ ɢɦɟɸɬ ɬɢɩ int, ɚ ɜɵɪɚɠɟɧɢɟ 7.0/2 ɞɚɫɬ ɪɟɡɭɥɶɬɚɬ 3.5 ɬɢɩɚ double, ɩɨɫɤɨɥɶɤɭ ɷɬɨɬ ɬɢɩ ɢɦɟɟɬ ɩɟɪɜɵɣ ɨɩɟɪɚɧɞ. Ɉɩɟɪɚɰɢɢ ɨɬɧɨɲɟɧɢɹ ɞɜɭɯ ɜɵɪɚɠɟɧɢɣ («<», «<=», «>», «>=») ɬɪɟɛɭɸɬ ɨɩɟɪɚɧɞɨɜ ɚɪɢɮɦɟɬɢɱɟɫɤɨɝɨ ɬɢɩɚ ɢɥɢ ɠɟ ɨɛɚ ɨɩɟɪɚɧɞɚ ɞɨɥɠɧɵ ɛɵɬɶ ɭɤɚɡɚɬɟɥɹɦɢ ɧɚ ɨɞɢɧɚɤɨɜɵɣ ɬɢɩ. ȼ ɫɥɭɱɚɟ ɨɩɟɪɚɧɞɨɜ ɚɪɢɮɦɟɬɢɱɟɫɤɨɝɨ ɬɢɩɚ ɜɵɱɢɫɥɹɸɬɫɹ ɡɧɚɱɟɧɢɹ ɨɩɟɪɚɧɞɨɜ, ɜɵɩɨɥɧɹɸɬɫɹ ɫɬɚɧɞɚɪɬɧɵɟ ɚɪɢɮɦɟɬɢɱɟɫɤɢɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɢ ɜɨɡɜɪɚɳɚɟɬɫɹ 1 ɬɢɩɚ int, ɟɫɥɢ ɨɬɧɨɲɟɧɢɟ ɜɵɩɨɥɧɹɟɬɫɹ (ɢɫɬɢɧɧɨ), ɢɥɢ 0, ɟɫɥɢ ɨɬɧɨɲɟɧɢɟ ɧɟ ɜɵɩɨɥɧɹɟɬɫɹ (ɥɨɠɧɨ). Ʉɨɝɞɚ ɫɪɚɜɧɢɜɚɸɬɫɹ ɞɜɚ ɭɤɚɡɚɬɟɥɹ, ɪɟɡɭɥɶɬɚɬ ɡɚɜɢɫɢɬ ɨɬ ɨɬɧɨɫɢɬɟɥɶɧɨɝɨ ɪɚɡɦɟɳɟɧɢɹ ɜ ɩɚɦɹɬɢ ɨɛɴɟɤɬɨɜ, ɧɚ ɤɨɬɨɪɵɟ ɫɫɵɥɚɸɬɫɹ ɭɤɚɡɚɬɟɥɢ. Ɉɩɟɪɚɰɢɢ ɫɪɚɜɧɟɧɢɹ «==» (ɪɚɜɧɨ) ɢ «!=» (ɧɟ ɪɚɜɧɨ) ɜɵɩɨɥɧɹɸɬɫɹ ɚɧɚɥɨɝɢɱɧɵɦ ɨɛɪɚɡɨɦ, ɧɨ ɢɦɟɸɬ ɦɟɧɶɲɢɣ ɩɪɢɨɪɢɬɟɬ. ȼɵɪɚɠɟɧɢɹ ɨɬɧɨɲɟɧɢɣ ɦɨɝɭɬ ɫɨɟɞɢɧɹɬɶɫɹ ɥɨɝɢɱɟɫɤɢɦɢ ɫɜɹɡɤɚɦɢ «&&» (ɤɨɧɴɸɧɤɰɢɹ, ɥɨɝɢɱɟɫɤɨɟ ɭɦɧɨɠɟɧɢɟ) ɢ «||» (ɞɢɡɴɸɧɤɰɢɹ, ɥɨɝɢɱɟɫɤɨɟ ɫɥɨɠɟɧɢɟ). ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ ɨɩɟɪɚɧɞɚɦɢ ɥɨɝɢɱɟɫɤɢɯ ɫɜɹɡɨɤ ɦɨɝɭɬ ɛɵɬɶ ɥɸɛɵɟ ɫɤɚɥɹɪɧɵɟ ɡɧɚɱɟɧɢɹ ɢ ɨɩɟɪɚɰɢɹ «&&» ɞɚɟɬ ɪɟɡɭɥɶɬɚɬ, ɪɚɜɧɵɣ 1 ɬɢɩɚ 24
int, ɟɫɥɢ ɨɛɚ ɨɩɟɪɚɧɞɚ ɢɦɟɸɬ ɧɟɧɭɥɟɜɵɟ ɡɧɚɱɟɧɢɹ, ɚ ɨɩɟɪɚɰɢɹ «||» ɞɚɟɬ ɪɟɡɭɥɶɬɚɬ, ɪɚɜɧɵɣ 0, ɟɫɥɢ ɡɧɚɱɟɧɢɹ ɨɛɨɢɯ ɨɩɟɪɚɧɞɨɜ ɧɭɥɟɜɵɟ. ɉɪɢɦɟɧɹ-
ɟɬɫɹ ɫɨɤɪɚɳɟɧɧɚɹ ɮɨɪɦɚ ɜɵɱɢɫɥɟɧɢɹ ɡɧɚɱɟɧɢɹ ɥɨɝɢɱɟɫɤɢɯ ɫɜɹɡɨɤ: ɟɫɥɢ ɜ ɨɩɟɪɚɰɢɢ «&&» ɩɟɪɜɵɣ ɨɩɟɪɚɧɞ ɪɚɜɟɧ ɧɭɥɸ, ɬɨ ɜɬɨɪɨɣ ɨɩɟɪɚɧɞ ɧɟ ɜɵɱɢɫɥɹɟɬɫɹ ɢ ɜɨɡɜɪɚɳɚɟɬɫɹ 0, ɟɫɥɢ ɜ ɨɩɟɪɚɰɢɢ «||» ɩɟɪɜɵɣ ɨɩɟɪɚɧɞ ɧɟ ɪɚɜɟɧ ɧɭɥɸ, ɬɨ ɜɬɨɪɨɣ ɨɩɟɪɚɧɞ ɧɟ ɜɵɱɢɫɥɹɟɬɫɹ ɢ ɜɨɡɜɪɚɳɚɟɬɫɹ ɡɧɚɱɟɧɢɟ 1. Ʉɚɤ ɭɠɟ ɨɬɦɟɱɚɥɨɫɶ, ɩɪɢɫɜɚɢɜɚɧɢɟ, ɨɛɨɡɧɚɱɚɟɦɨɟ ɡɧɚɤɨɦ «=» ɜ ɋɢ/ɋɢ++ ɪɚɫɫɦɚɬɪɢɜɚɟɬɫɹ ɤɚɤ ɨɩɟɪɚɰɢɹ ɢ ɜɨɡɜɪɚɳɚɟɬ ɡɧɚɱɟɧɢɟ, ɤɨɬɨɪɨɟ ɛɵɥɨ ɩɪɢɫɜɨɟɧɨ ɥɟɜɨɦɭ ɨɩɟɪɚɧɞɭ. Ɉɩɟɪɚɰɢɹ ɩɪɢɫɜɚɢɜɚɧɢɹ ɜɵɱɢɫɥɹɟɬɫɹ ɫɩɪɚɜɚ ɧɚɥɟɜɨ, ɬɨ ɟɫɬɶ ɫɧɚɱɚɥɚ ɜɵɱɢɫɥɹɟɬɫɹ ɩɪɢɫɜɚɢɜɚɟɦɨɟ ɡɧɚɱɟɧɢɟ, ɡɚɬɟɦ ɜɵɩɨɥɧɹɟɬɫɹ ɩɪɢɫɜɚɢɜɚɧɢɟ. ɗɬɨ ɩɨɡɜɨɥɹɟɬ ɡɚɩɢɫɵɜɚɬɶ ɜɵɪɚɠɟɧɢɹ ɜɢɞɚ x=y=z=1;
ɞɥɹ ɭɫɬɚɧɨɜɤɢ ɨɞɢɧɚɤɨɜɵɯ ɡɧɚɱɟɧɢɣ ɧɟɫɤɨɥɶɤɢɦ ɩɟɪɟɦɟɧɧɵɦ. Ɇɨɠɧɨ, ɯɨɬɹ ɷɬɨ ɢ ɫɧɢɠɚɟɬ ɧɚɝɥɹɞɧɨɫɬɶ ɩɪɨɝɪɚɦɦɵ, ɫɬɪɨɢɬɶ ɢ ɜɵɪɚɠɟɧɢɹ ɫ ɩɨɛɨɱɧɵɦ ɷɮɮɟɤɬɨɦ ɜɢɞɚ (x=2)*(y=3)+(z=4);
Ɋɟɡɭɥɶɬɚɬɨɦ ɷɬɨɝɨ ɜɵɪɚɠɟɧɢɹ ɛɭɞɟɬ 24, ɧɨ ɨɞɧɨɜɪɟɦɟɧɧɨ ɩɟɪɟɦɟɧɧɵɟ x, y ɢ z ɩɨɥɭɱɚɬ ɧɨɜɵɟ ɡɧɚɱɟɧɢɹ. Ʉɪɨɦɟ ɩɪɨɫɬɨɝɨ ɩɪɢɫɜɚɢɜɚɧɢɹ ɢɦɟɟɬɫɹ ɧɚɛɨɪ ɫɨɫɬɚɜɧɵɯ ɨɩɟɪɚɰɢɣ ɩɪɢɫɜɚɢɜɚɧɢɹ, ɜ ɤɨɬɨɪɵɯ ɩɪɢɫɜɚɢɜɚɧɢɟ ɫɨɜɦɟɳɚɟɬɫɹ ɫ ɭɤɚɡɚɧɧɨɣ ɛɢɧɚɪɧɨɣ ɨɩɟɪɚɰɢɟɣ. Ɂɚɩɢɫɶ x+=y ɷɤɜɢɜɚɥɟɧɬɧɚ ɜɵɪɚɠɟɧɢɸ x=x+y. Ⱦɥɹ ɰɟɥɵɯ ɨɩɟɪɚɧɞɨɜ ɨɩɪɟɞɟɥɟɧɵ ɨɩɟɪɚɰɢɢ ɫɞɜɢɝɚ ɜɥɟɜɨ ɢ ɜɩɪɚɜɨ. ɉɪɢ ɜɵɩɨɥɧɟɧɢɢ ɨɩɟɪɚɰɢɢ e1<<e2 ɛɢɬɵ ɩɟɪɜɨɝɨ ɨɩɟɪɚɧɞɚ ɫɞɜɢɝɚɸɬɫɹ ɜɥɟɜɨ ɧɚ e1 ɪɚɡɪɹɞɨɜ ɢ ɪɟɡɭɥɶɬɚɬ ɢɦɟɟɬ ɬɢɩ ɩɟɪɜɨɝɨ ɨɩɟɪɚɧɞɚ. Ɉɫɜɨɛɨɠɞɚɸɳɢɟɫɹ ɩɪɚɜɵɟ ɪɚɡɪɹɞɵ ɡɚɩɨɥɧɹɸɬɫɹ ɧɭɥɹɦɢ. ɉɪɢ ɫɞɜɢɝɟ ɜɩɪɚɜɨ (e1>>e2) ɟɫɥɢ e1 ɢɦɟɟɬ ɬɢɩ unsigned, ɨɫɜɨɛɨɠɞɚɸɳɢɟɫɹ ɥɟɜɵɟ ɪɚɡɪɹɞɵ ɡɚɩɨɥɧɹɸɬɫɹ ɧɭɥɹɦɢ, ɚ ɩɪɢ e1 ɬɢɩɚ signed ɜ ɨɫɜɨɛɨɠɞɚɸɳɢɯɫɹ ɥɟɜɵɯ ɪɚɡɪɹɞɚɯ ɩɨɜɬɨɪɹɟɬɫɹ ɡɧɚɤɨɜɵɣ ɪɚɡɪɹɞ. ɇɚɞ ɰɟɥɵɦɢ ɨɩɟɪɚɧɞɚɦɢ ɞɨɩɭɫɬɢɦɵ ɨɩɟɪɚɰɢɢ ɩɨɪɚɡɪɹɞɧɨɝɨ ɥɨɝɢɱɟɫɤɨɝɨ ɭɦɧɨɠɟɧɢɹ, ɥɨɝɢɱɟɫɤɨɝɨ ɫɥɨɠɟɧɢɹ ɢ ɢɫɤɥɸɱɚɸɳɟɝɨ «ɢɥɢ» (ɨɬɪɢɰɚɧɢɹ ɪɚɜɧɨɡɧɚɱɧɨɫɬɢ). ȼ ɷɬɢɯ ɨɩɟɪɚɰɢɹɯ ɨɩɟɪɚɧɞɵ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɤɚɤ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɛɢɬɨɜ, ɢ ɨɩɟɪɚɰɢɹ ɜɵɩɨɥɧɹɟɬɫɹ ɧɚɞ ɤɚɠɞɨɣ ɩɚɪɨɣ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɯ ɪɚɡɪɹɞɨɜ ɢɡ ɨɛɨɢɯ ɨɩɟɪɚɧɞɨɜ. ɇɚɩɪɢɦɟɪ, ɪɟɡɭɥɶɬɚɬɨɦ ɜɵɪɚɠɟɧɢɹ x>>(p-n+1))&(~(~0<
ɛɭɞɟɬ ɜɵɞɟɥɟɧɢɟ ɢɡ ɰɟɥɨɝɨ ɛɟɡɡɧɚɤɨɜɨɝɨ x n ɛɢɬɨɜ, ɧɚɱɢɧɚɹ ɫ ɛɢɬɚ ɫ ɧɨɦɟɪɨɦ p, ɢ ɫɞɜɢɝ ɜɵɞɟɥɟɧɧɵɯ ɛɢɬɨɜ ɜɩɪɚɜɨ, ɬɨ ɟɫɬɶ ɜɵɞɟɥɟɧɢɟ n-ɪɚɡɪɹɞɧɨɝɨ ɰɟɥɨɝɨ, ɯɪɚɧɹɳɟɝɨɫɹ ɜ ɦɚɲɢɧɧɨɦ ɫɥɨɜɟ x ɧɚɱɢɧɚɹ ɫ p-ɝɨ ɪɚɡɪɹɞɚ. ȼ ɋɢ/ɋɢ++ ɢɦɟɟɬɫɹ ɤɨɧɫɬɪɭɤɰɢɹ, ɤɨɬɨɪɚɹ ɧɚɡɵɜɚɟɬɫɹ ɭɫɥɨɜɧɵɦ ɜɵɪɚɠɟɧɢɟɦ. ɍɫɥɨɜɧɨɟ ɜɵɪɚɠɟɧɢɟ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: ɭɫɥɨɜɢɟ?ɜɵɪɚɠɟɧɢɟ1:ɜɵɪɚɠɟɧɢɟ2;
ȼ ɤɚɱɟɫɬɜɟ ɭɫɥɨɜɢɹ ɦɨɠɟɬ ɜɵɫɬɭɩɚɬɶ ɥɸɛɨɟ ɫɤɚɥɹɪɧɨɟ ɜɵɪɚɠɟɧɢɟ. ȿɫɥɢ ɪɟɡɭɥɶɬɚɬ ɜɵɱɢɫɥɟɧɢɹ ɭɫɥɨɜɢɹ ɧɟɧɭɥɟɜɨɣ, ɬɨ ɡɧɚɱɟɧɢɟɦ ɜɫɟɝɨ ɜɵɪɚɠɟ25
ɧɢɹ ɛɭɞɟɬ ɜɵɪɚɠɟɧɢɟ1, ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ - ɜɵɪɚɠɟɧɢɟ2. ȼɬɨɪɨɣ ɢ ɬɪɟɬɢɣ ɨɩɟɪɚɧɞɵ ɭɫɥɨɜɧɨɝɨ ɜɵɪɚɠɟɧɢɹ ɥɢɛɨ ɨɛɚ ɞɨɥɠɧɵ ɛɵɬɶ ɜɵɪɚɠɟɧɢɹɦɢ ɚɪɢɮɦɟɬɢɱɟɫɤɨɝɨ ɬɢɩɚ, ɥɢɛɨ ɨɞɧɨɬɢɩɧɵɦɢ ɫɬɪɭɤɬɭɪɚɦɢ ɢɥɢ ɨɛɴɟɞɢɧɟɧɢɹɦɢ, ɥɢɛɨ ɭɤɚɡɚɬɟɥɹɦɢ ɨɞɢɧɚɤɨɜɨɝɨ ɬɢɩɚ, ɥɢɛɨ ɨɞɢɧ ɢɡ ɧɢɯ ɞɨɥɠɟɧ ɛɵɬɶ ɭɤɚɡɚɬɟɥɟɦ ɧɚ ɤɚɤɨɣ-ɥɢɛɨ ɬɢɩ, ɚ ɞɪɭɝɨɣ ɢɦɟɬɶ ɡɚɧɱɟɧɢɟ NULL ɢɥɢ ɛɵɬɶ ɭɤɚɡɚɬɟɥɟɦ ɬɢɩɚ void*. ȼɵɪɚɠɟɧɢɟ x>0?1:0;
ɜɨɡɜɪɚɳɚɟɬ 1, ɟɫɥɢ x ɛɨɥɶɲɟ 0, ɢ 0 ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ. ȼɵɪɚɠɟɧɢɟ ɦɨɠɟɬ ɛɵɬɶ ɩɪɟɞɫɬɚɜɥɟɧɨ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶɸ ɜɵɪɚɠɟɧɢɣ, ɪɚɡɞɟɥɟɧɧɵɯ ɡɚɩɹɬɵɦɢ, ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɜɵɱɢɫɥɹɸɬɫɹ ɜɫɟ ɜɵɪɚɠɟɧɢɹ ɫɥɟɜɚ ɧɚɩɪɚɜɨ ɢ ɜɨɡɜɪɚɳɚɟɬɫɹ ɡɧɚɱɟɧɢɟ ɩɨɫɥɟɞɧɟɝɨ ɜɵɪɚɠɟɧɢɹ ɜ ɫɩɢɫɤɟ. ɇɚɩɪɢɦɟɪ, ɜ ɪɟɡɭɥɶɬɚɬɟ ɜɵɱɢɫɥɟɧɢɹ ɜɵɪɚɠɟɧɢɹ x=2, e*3, x+1;
ɛɭɞɟɬ ɩɨɥɭɱɟɧɨ ɡɧɚɱɟɧɢɟ 3, ɚ ɩɟɪɟɦɟɧɧɚɹ x ɩɨɥɭɱɢɬ ɡɧɚɱɟɧɢɟ 2. Ɋɟɡɭɥɶɬɚɬ ɭɦɧɨɠɟɧɢɹ e*3 ɧɢɤɚɤ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧ.
2.5. Ɉɩɟɪɚɬɨɪɵ ɋɢ++ Ɉɩɟɪɚɬɨɪɵ - ɷɬɨ ɫɢɧɬɚɤɫɢɱɟɫɤɢɟ ɤɨɧɫɬɪɭɤɰɢɢ, ɨɩɪɟɞɟɥɹɸɳɢɟ ɞɟɣɫɬɜɢɹ, ɜɵɩɨɥɧɹɟɦɵɟ ɩɪɨɝɪɚɦɦɨɣ. ȼ ɋɢ/ɋɢ++ ɢɦɟɸɬɫɹ ɫɥɟɞɭɸɳɢɟ ɬɢɩɵ ɨɩɟɪɚɬɨɪɨɜ: ɨɩɟɪɚɬɨɪɵ-ɜɵɪɚɠɟɧɢɹ, ɨɩɟɪɚɬɨɪɵ ɜɵɛɨɪɚ, ɨɩɟɪɚɬɨɪɵ ɰɢɤɥɚ ɢ ɨɩɟɪɚɬɨɪ ɩɟɪɟɯɨɞɚ. ɋɢɧɬɚɤɫɢɫ ɧɟɤɨɬɨɪɵɯ ɨɩɟɪɚɬɨɪɨɜ ɫɨɞɟɪɠɢɬ ɜɵɪɚɠɟɧɢɹ, ɢɝɪɚɸɳɢɟ ɪɨɥɶ ɭɫɥɨɜɢɣ, ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɜɵɩɨɥɧɟɧɢɹ ɢɥɢ ɧɟɜɵɩɨɥɧɟɧɢɹ ɤɨɬɨɪɵɯ ɜɵɛɢɪɚɟɬɫɹ ɬɚ ɢɥɢ ɢɧɚɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɞɟɣɫɬɜɢɣ. ɉɨɫɤɨɥɶɤɭ ɜ ɋɢ ɧɟɬ ɛɭɥɟɜɵɯ ɜɵɪɚɠɟɧɢɣ, ɜ ɤɚɱɟɫɬɜɟ ɭɫɥɨɜɢɣ ɢɫɩɨɥɶɡɭɸɬɫɹ ɥɸɛɵɟ ɜɵɪɚɠɟɧɢɹ, ɞɚɸɳɢɟ ɫɤɚɥɹɪɧɵɟ ɡɧɚɱɟɧɢɹ, ɢ ɭɫɥɨɜɢɟ ɫɱɢɬɚɟɬɫɹ ɜɵɩɨɥɧɟɧɵɦ, ɟɫɥɢ ɷɬɨ ɡɧɚɱɟɧɢɟ ɨɬɥɢɱɧɨ ɨɬ ɧɭɥɹ, ɢ ɧɟɜɵɩɨɥɧɟɧɧɵɦ, ɟɫɥɢ ɨɧɨ ɪɚɜɧɨ ɧɭɥɸ. ɇɟɫɤɨɥɶɤɨ ɨɩɟɪɚɬɨɪɨɜ ɦɨɝɭɬ ɛɵɬɶ ɨɛɴɟɞɢɧɟɧɵ ɜ ɫɨɫɬɚɜɧɨɣ ɨɩɟɪɚɬɨɪ ɩɭɬɟɦ ɡɚɤɥɸɱɟɧɢɹ ɢɯ ɜ ɮɢɝɭɪɧɵɟ (ɨɩɟɪɚɬɨɪɧɵɟ) ɫɤɨɛɤɢ. ɉɪɢɡɧɚɤɨɦ ɤɨɧɰɚ ɨɩɟɪɚɬɨɪɚ (ɤɪɨɦɟ ɫɨɫɬɚɜɧɨɝɨ ɨɩɟɪɚɬɨɪɚ) ɫɥɭɠɢɬ ɬɨɱɤɚ ɫ ɡɚɩɹɬɨɣ, ɹɜɥɹɸɳɚɹɫɹ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɱɚɫɬɶɸ ɨɩɟɪɚɬɨɪɚ. ɉɟɪɟɞ ɥɸɛɵɦ ɨɩɟɪɚɬɨɪɨɦ ɦɨɠɟɬ ɛɵɬɶ ɡɚɩɢɫɚɧɚ ɦɟɬɤɚ ɜ ɜɢɞɟ ɢɞɟɧɬɢɮɢɤɚɬɨɪɚ, ɨɬɞɟɥɟɧɧɨɝɨ ɨɬ ɩɨɦɟɱɚɟɦɨɝɨ ɨɩɟɪɚɬɨɪɚ ɞɜɨɟɬɨɱɢɟɦ. Ɇɟɬɤɚ ɫɥɭɠɢɬ ɬɨɥɶɤɨ ɞɥɹ ɭɤɚɡɚɧɢɹ ɟɟ ɜ ɨɩɟɪɚɬɨɪɟ ɩɟɪɟɯɨɞɚ. ɇɚɢɛɨɥɟɟ ɩɪɨɫɬɵɦ ɹɜɥɹɟɬɫɹ ɨɩɟɪɚɬɨɪ-ɜɵɪɚɠɟɧɢɟ, ɩɪɟɞɫɬɚɜɥɹɸɳɢɣ ɫɨɛɨɣ ɩɨɥɧɨɟ ɜɵɪɚɠɟɧɢɟ, ɡɚɤɧɱɢɜɚɸɳɟɟɫɹ ɬɨɱɤɨɣ ɫ ɡɚɩɹɬɨɣ, ɧɚɩɪɢɦɟɪ, x=3; y=(x+1)*t; i++;
ȼɵɪɚɠɟɧɢɟ, ɨɮɨɪɦɥɟɧɧɨɟ ɤɚɤ ɨɩɟɪɚɬɨɪ, ɜɵɱɢɫɥɹɟɬɫɹ, ɧɨ ɟɝɨ ɡɧɚɱɟɧɢɟ ɬɟɪɹɟɬɫɹ, ɢ ɞɟɣɫɬɜɢɟ ɨɩɟɪɚɬɨɪɚ-ɜɵɪɚɠɟɧɢɹ ɫɨɫɬɨɢɬ ɜ ɩɨɛɨɱɧɵɯ ɷɮɮɟɤɬɚɯ, ɫɨɩɪɨɜɨɠɞɚɸɳɢɯ ɜɵɱɢɫɥɟɧɢɟ, ɧɚɩɪɢɦɟɪ, ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ ɨɩɟɪɚɰɢɣ ɩɪɢɫɜɚɢɜɚɧɢɹ, ɚɜɬɨɭɜɟɥɢɱɟɧɢɹ ɢ ɚɜɬɨɭɦɟɧɶɲɟɧɢɹ. Ɉɩɟɪɚɬɨɪɵ ɜɵɛɨɪɚ ɜ ɋɢ/ɋɢ++ ɩɪɟɞɫɬɚɜɥɟɧɵ ɭɫɥɨɜɧɵɦ ɨɩɟɪɚɬɨɪɨɦ ɢ ɩɟɪɟɤɥɸɱɚɬɟɥɟɦ. ɍɫɥɨɜɧɵɣ ɨɩɟɪɚɬɨɪ ɪɚɛɨɬɚɟɬ ɚɧɚɥɨɝɢɱɧɨ ɭɫɥɨɜɧɵɦ ɨɩɟ26
ɪɚɬɨɪɚɦ ɞɪɭɝɢɯ ɹɡɵɤɨɜ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɢ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɜ ɫɨɤɪɚɳɟɧɧɨɣ ɢ ɩɨɥɧɨɣ ɮɨɪɦɚɯ, ɤɨɬɨɪɵɦ ɫɨɨɬɜɟɬɫɬɜɭɸɬ ɫɯɟɦɵ: if (ɜɵɪɚɠɟɧɢɟ-ɭɫɥɨɜɢɟ) ɨɩɟɪɚɬɨɪ; if (ɜɵɪɚɠɟɧɢɟ-ɭɫɥɨɜɢɟ) ɨɩɟɪɚɬɨɪ1 else ɨɩɟɪɚɬɨɪ2;
ȼ ɫɨɤɪɚɳɟɧɧɨɣ ɮɨɪɦɟ ɭɫɥɨɜɧɨɝɨ ɨɩɟɪɚɬɨɪɚ ɜɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟɭɫɥɨɜɢɟ, ɢ, ɟɫɥɢ ɟɝɨ ɡɧɚɱɟɧɢɟ ɨɬɥɢɱɧɨ ɨɬ ɧɭɥɹ, ɜɵɩɨɥɧɹɟɬɫɹ ɫɥɟɞɭɸɳɢɣ ɡɚ ɭɫɥɨɜɢɟɦ ɨɩɟɪɚɬɨɪ, ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ ɧɟ ɩɪɨɢɡɜɨɞɢɬɫɹ ɧɢɤɚɤɢɯ ɞɟɣɫɬɜɢɣ. ȼ ɩɨɥɧɨɣ ɮɨɪɦɟ ɭɫɥɨɜɧɨɝɨ ɨɩɟɪɚɬɨɪɚ ɩɪɢ ɧɟɧɭɥɟɜɨɦ ɡɧɚɱɟɧɢɢ ɜɵɪɚɠɟɧɢɹ-ɭɫɥɨɜɢɹ ɜɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɬɨɪ1 ɫ ɩɨɫɥɟɞɭɸɳɢɦ ɩɟɪɟɯɨɞɨɦ ɤ ɫɥɟɞɭɸɳɟɦɭ ɨɩɟɪɚɬɨɪɭ ɩɪɨɝɪɚɦɦɵ, ɚ ɩɪɢ ɧɭɥɟɜɨɦ ɡɧɚɱɟɧɢɢ ɜɵɪɚɠɟɧɢɹ ɭɫɥɨɜɢɹ ɜɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɬɨɪ2 ɫ ɩɟɪɟɯɨɞɨɦ ɤ ɫɥɟɞɭɸɳɟɦɭ ɨɩɟɪɚɬɨɪɭ ɩɪɨɝɪɚɦɦɵ. ɉɟɪɟɤɥɸɱɚɬɟɥɶ ɩɨɡɜɨɥɹɟɬ ɜɵɛɪɚɬɶ ɨɞɧɭ ɢɡ ɧɟɫɤɨɥɶɤɢɯ ɜɨɡɦɨɠɧɵɯ ɜɟɬɜɟɣ ɜɵɱɢɫɥɟɧɢɣ ɢ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: switch (ɰɟɥɨɟ_ɭɩɪɚɜɥɹɸɳɟɟ_ɜɵɪɚɠɟɧɢɟ) ɨɩɟɪɚɬɨɪ
Ɉɩɟɪɚɬɨɪ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɬɟɥɨ ɩɟɪɟɤɥɸɱɚɬɟɥɹ, ɩɪɚɤɬɢɱɟɫɤɢ ɜɫɟɝɞɚ ɹɜɥɹɟɬɫɹ ɫɨɫɬɚɜɧɵɦ ɢ ɢɦɟɟɬ ɬɚɤɨɣ ɜɢɞ: { case ɤɨɧɫɬɚɧɬɚ1: ɨɩɟɪɚɬɨɪɵ case ɤɨɧɫɬɚɧɬɚ2: ɨɩɟɪɚɬɨɪɵ .......................... default: ɨɩɟɪɚɬɨɪɵ }
ȼɵɩɨɥɧɟɧɢɟ ɩɟɪɟɤɥɸɱɚɬɟɥɹ ɫɨɫɬɨɢɬ ɜ ɜɵɱɢɫɥɟɧɢɢ ɭɩɪɚɜɥɹɸɳɟɝɨ_ɜɵɪɚɠɟɧɢɹ ɢ ɩɟɪɟɯɨɞɟ ɤ ɝɪɭɩɩɟ ɨɩɟɪɚɬɨɪɨɜ, ɩɨɦɟɱɟɧɧɵɯ case-ɦɟɬɤɨɣ, ɪɚɜɧɨɣ ɭɩɪɚɜɥɹɸɳɟɦɭ_ɜɵɪɚɠɟɧɢɸ. ȿɫɥɢ ɬɚɤɨɣ case-ɦɟɬɤɢ ɧɟɬ, ɜɵɩɨɥɧɹɸɬɫɹ ɨɩɟɪɚɬɨɪɵ ɩɨ ɦɟɬɤɟ default. ɉɭɧɤɬ default ɦɨɠɟɬ ɨɬɫɭɬɫɬɜɨɜɚɬɶ, ɢ ɬɨɝɞɚ, ɟɫɥɢ ɭɩɪɚɜɥɹɸɳɟɦɭ_ɜɵɪɚɠɟɧɢɸ ɧɟ ɫɨɨɬɜɟɬɫɬɜɭɟɬ ɧɢ ɨɞɧɚ caseɦɟɬɤɚ, ɜɟɫɶ ɩɟɪɟɤɥɸɱɚɬɟɥɶ ɷɤɜɢɜɚɥɟɧɬɟɧ ɩɭɫɬɨɦɭ ɨɩɟɪɚɬɨɪɭ. ɋɥɟɞɭɟɬ ɭɱɢɬɵɜɚɬɶ, ɱɬɨ ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ ɩɟɪɟɤɥɸɱɚɬɟɥɹ ɩɪɨɢɫɯɨɞɢɬ ɩɟɪɟɯɨɞ ɤ ɨɩɟɪɚɬɨɪɭ ɫ ɜɵɛɪɚɧɧɨɣ case-ɦɟɬɤɨɣ, ɢ ɞɚɥɶɲɟ ɨɩɟɪɚɬɨɪɵ ɜɵɩɨɥɧɹɸɬɫɹ ɜ ɟɫɬɟɫɬɜɟɧɧɨɦ ɩɨɪɹɞɤɟ. ɇɚɩɪɢɦɟɪ, ɜ ɩɟɪɟɤɥɸɱɚɬɟɥɟ switch (count) { case 1: x=1; case 2: x=2; case 3: x=3; default: x=4; } ɟɫɥɢ ɡɧɚɱɟɧɢɟ count ɪɚɜɧɨ 1, ɬɨ ɩɨɫɥɟ ɩɟɪɟɯɨɞɚ ɧɚ case 1 ɛɭɞɭɬ ɜɵɩɨɥɧɟɧɵ ɜɫɟ ɨɩɟɪɚɬɨɪɵ, ɜ ɪɟɡɭɥɶɬɚɬɟ x ɫɬɚɧɟɬ ɪɚɜɧɵɦ 4. ɑɬɨɛɵ ɪɚɡɞɟɥɢɬɶ
ɜɟɬɜɢ ɩɟɪɟɤɥɸɱɚɬɟɥɹ, ɜ ɤɨɧɰɟ ɤɚɠɞɨɣ ɜɟɬɜɢ ɧɭɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɩɟɪɚɬɨɪ break, ɧɟ ɢɦɟɸɳɢɣ ɨɩɟɪɚɧɞɨɜ. ɉɨ ɷɬɨɦɭ ɨɩɟɪɚɬɨɪɭ ɩɪɨɢɫɯɨɞɢɬ ɜɵɯɨɞ ɢɡ ɩɟɪɟɤɥɸɱɚɬɟɥɹ ɤ ɫɥɟɞɭɸɳɟɦɭ ɨɩɟɪɚɬɨɪɭ ɩɪɨɝɪɚɦɦɵ: 27
switch (count) { case 1: x=1; break; case 2: x=2; break; case 3: x=3; break; default: x=4; }
Ɍɟɩɟɪɶ, ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɡɧɚɱɟɧɢɹ count, ɛɭɞɟɬ ɜɵɩɨɥɧɹɬɶɫɹ ɬɨɥɶɤɨ ɨɞɧɚ ɜɟɬɜɶ ɩɟɪɟɤɥɸɱɚɬɟɥɹ ɢ x ɛɭɞɟɬ ɩɪɢɧɢɦɚɬɶ ɨɞɧɨ ɢɡ ɱɟɬɵɪɟɯ ɩɪɟɞɭɫɦɨɬɪɟɧɧɵɯ ɡɧɚɱɟɧɢɣ. ȼ ɋɢ/ɋɢ++ ɢɦɟɟɬɫɹ ɬɪɢ ɜɚɪɢɚɧɬɚ ɨɩɟɪɚɬɨɪɚ ɰɢɤɥɚ: ɰɢɤɥ ɫ ɩɪɟɞɭɫɥɨɜɢɟɦ, ɰɢɤɥ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɢ ɰɢɤɥ ɫ ɩɚɪɚɦɟɬɪɨɦ. ɐɢɤɥ ɫ ɩɪɟɞɭɫɥɨɜɢɟɦ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: while (ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ) ɨɩɟɪɚɬɨɪ
ɉɪɢ ɤɚɠɞɨɦ ɩɨɜɬɨɪɟɧɢɢ ɰɢɤɥɚ ɜɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ, ɢ, ɟɫɥɢ ɡɧɚɱɟɧɢɟ ɷɬɨɝɨ ɜɵɪɚɠɟɧɢɹ ɧɟ ɪɚɜɧɨ ɧɭɥɸ, ɜɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɬɨɪ - ɬɟɥɨ ɰɢɤɥɚ. ɐɢɤɥ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: do ɨɩɟɪɚɬɨɪ while (ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ); ȼɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ ɜɵɱɢɫɥɹɟɬɫɹ ɢ ɩɪɨɜɟɪɹɟɬɫɹ ɩɨɫɥɟ ɤɚɠɞɨɝɨ ɩɨɜɬɨɪɟɧɢɹ ɨɩɟɪɚɬɨɪɚ – ɬɟɥɚ ɰɢɤɥɚ, ɰɢɤɥ ɩɨɜɬɨɪɹɟɬɫɹ, ɩɨɤɚ ɜɵɪɚɠɟɧɢɟ_ɭɫɥɨɜɢɟ ɜɵ-
ɩɨɥɧɹɟɬɫɹ. Ɍɟɥɨ ɰɢɤɥɚ ɜ ɰɢɤɥɟ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɜɵɩɨɥɧɹɟɬɫɹ ɯɨɬɹ ɛɵ ɨɞɢɧ ɪɚɡ. ɐɢɤɥ ɫ ɩɚɪɚɦɟɬɪɨɦ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: for (E1; E2; E3) ɨɩɟɪɚɬɨɪ;
ɝɞɟ E1, E2 ɢ E3 – ɜɵɪɚɠɟɧɢɹ ɫɤɚɥɹɪɧɨɝɨ ɬɢɩɚ. ɐɢɤɥ ɫ ɩɚɪɚɦɟɬɪɨɦ ɪɟɚɥɢɡɭɟɬɫɹ ɩɨ ɫɥɟɞɭɸɳɟɦɭ ɚɥɝɨɪɢɬɦɭ: 1. ȼɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ E1. Ɉɛɵɱɧɨ ɷɬɨ ɜɵɪɚɠɟɧɢɟ ɜɵɩɨɥɧɹɟɬ ɩɨɞɝɨɬɨɜɤɭ ɤ ɧɚɱɚɥɭ ɰɢɤɥɚ. 2. ȼɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ E2, ɢ, ɟɫɥɢ ɨɧɨ ɪɚɜɧɨ ɧɭɥɸ, ɜɵɩɨɥɧɹɟɬɫɹ ɩɟɪɟɯɨɞ ɤ ɫɥɟɞɭɸɳɟɦɭ ɨɩɟɪɚɬɨɪɭ ɩɪɨɝɪɚɦɦɵ (ɜɵɯɨɞ ɢɡ ɰɢɤɥɚ). ȿɫɥɢ E2 ɧɟ ɪɚɜɧɨ ɧɭɥɸ, ɜɵɩɨɥɧɹɟɬɫɹ ɲɚɝ 3. 3. ȼɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɬɨɪ – ɬɟɥɨ ɰɢɤɥɚ. 4. ȼɵɱɢɫɥɹɟɬɫɹ ɜɵɪɚɠɟɧɢɟ E3 (ɜɵɩɨɥɧɹɟɬɫɹ ɩɨɞɝɨɬɨɜɤɚ ɤ ɩɨɜɬɨɪɟɧɢɸ ɰɢɤɥɚ), ɩɨɫɥɟ ɱɟɝɨ ɫɧɨɜɚ ɜɵɩɨɥɧɹɟɬɫɹ ɲɚɝ 2. ɉɭɫɬɶ ɬɪɟɛɭɟɬɫɹ ɩɨɞɫɱɢɬɚɬɶ ɫɭɦɦɭ ɷɥɟɦɟɧɬɨɜ ɧɟɤɨɬɨɪɨɝɨ ɦɚɫɫɢɜɚ ɢɡ n ɷɥɟɦɟɧɬɨɜ. ɋ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɰɢɤɥɚ ɫ ɩɪɟɞɭɫɥɨɜɢɟɦ ɷɬɨ ɦɨɠɧɨ ɫɞɟɥɚɬɶ ɬɚɤ: int s=0; int i=0; while (i
ɗɬɚ ɠɟ ɡɚɞɚɱɚ ɫ ɩɪɢɦɟɧɟɧɢɟɦ ɰɢɤɥɚ ɫ ɩɨɫɬɭɫɥɨɜɢɟɦ ɪɟɲɚɟɬɫɹ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ: int s=0; int i=0; 28
do {s+=a[i++];} while (i
ɉɨɫɤɨɥɶɤɭ ɜ ɞɚɧɧɨɦ ɫɥɭɱɚɟ ɩɨɜɬɨɪɟɧɢɹɦɢ ɰɢɤɥɚ ɭɩɪɚɜɥɹɟɬ ɩɚɪɚɦɟɬɪ i, ɷɬɭ ɡɚɞɚɱɭ ɦɨɠɧɨ ɪɟɲɢɬɶ ɢ ɫ ɩɨɦɨɳɶɸ ɰɢɤɥɚ ɬɪɟɬɶɟɝɨ ɬɢɩɚ: int i, s; for (s=0, i=0; i
Ɉɛɴɹɜɥɟɧɢɹ ɩɟɪɟɦɟɧɧɵɯ ɦɨɠɧɨ ɜɧɟɫɬɢ ɜ ɜɵɪɚɠɟɧɢɟ E1 ɨɩɟɪɚɬɨɪɚ for ɢ ɜɫɟ ɡɚɩɢɫɚɬɶ ɜ ɜɢɞɟ ɨɞɧɨɝɨ ɨɩɟɪɚɬɨɪɚ for ɫ ɩɭɫɬɵɦ ɬɟɥɨɦ ɰɢɤɥɚ: for (int i=0, s=0; i
Ⱦɥɹ ɩɪɟɪɵɜɚɧɢɹ ɩɨɜɬɨɪɟɧɢɣ ɨɩɟɪɚɬɨɪɚ ɰɢɤɥɚ ɥɸɛɨɝɨ ɬɢɩɚ ɜ ɬɟɥɟ ɰɢɤɥɚ ɦɨɠɟɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧ ɨɩɟɪɚɬɨɪ break. Ⱦɥɹ ɩɟɪɟɯɨɞɚ ɤ ɫɥɟɞɭɸɳɟɦɭ ɩɨɜɬɨɪɟɧɢɸ ɰɢɤɥɚ ɢɡ ɥɸɛɨɝɨ ɦɟɫɬɚ ɬɟɥɚ ɰɢɤɥɚ ɦɨɠɟɬ ɛɵɬɶ ɩɪɢɦɟɧɟɧ ɨɩɟɪɚɬɨɪ continue. ɗɬɢ ɨɩɟɪɚɬɨɪɵ ɩɨ ɫɜɨɟɦɭ ɧɚɡɧɚɱɟɧɢɸ ɚɧɚɥɨɝɢɱɧɵ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɦ ɨɩɟɪɚɬɨɪɚɦ ɹɡɵɤɚ ɉɚɫɤɚɥɶ. ɇɟɫɦɨɬɪɹ ɧɚ ɬɨ, ɱɬɨ ɋɢ++ ɫɨɞɟɪɠɢɬ ɩɨɥɧɵɣ ɧɚɛɨɪ ɨɩɟɪɚɬɨɪɨɜ ɞɥɹ ɫɬɪɭɤɬɭɪɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ, ɜ ɧɟɝɨ ɜɫɟ ɠɟ ɜɤɥɸɱɟɧ ɨɩɟɪɚɬɨɪ ɩɟɪɟɯɨɞɚ: goto ɦɟɬɤɚ; Ɇɟɬɤɚ ɡɚɞɚɟɬ ɚɞɪɟɫ ɩɟɪɟɯɨɞɚ ɢ ɞɨɥɠɧɚ ɩɨɦɟɱɚɬɶ ɨɩɟɪɚɬɨɪ ɜ ɬɨɦ ɠɟ ɫɨɫɬɚɜɧɨɦ ɨɩɟɪɚɬɨɪɟ, ɤɨɬɨɪɨɦɭ ɩɪɢɧɚɞɥɟɠɢɬ ɨɩɟɪɚɬɨɪ goto. ȼɯɨɞ ɜ ɫɨɫɬɚɜɧɨɣ
ɨɩɟɪɚɬɨɪ, ɫɨɞɟɪɠɚɳɢɣ ɨɛɴɹɜɥɟɧɢɹ ɞɚɧɧɵɯ ɧɟ ɱɟɪɟɡ ɟɝɨ ɧɚɱɚɥɨ, ɡɚɩɪɟɳɟɧ.
2.6. Ɏɭɧɤɰɢɢ Ʌɸɛɚɹ ɩɪɨɝɪɚɦɦɚ ɧɚ ɋɢ/ɋɢ++ ɫɨɞɟɪɠɢɬ ɯɨɬɹ ɛɵ ɨɞɧɭ ɮɭɧɤɰɢɸ. Ⱥɥɝɨɪɢɬɦ ɪɟɲɟɧɢɹ ɥɸɛɨɣ ɡɚɞɚɱɢ ɪɟɚɥɢɡɭɟɬɫɹ ɩɭɬɟɦ ɜɵɡɨɜɨɜ ɮɭɧɤɰɢɣ. Ɉɞɧɚ ɢɡ ɮɭɧɤɰɢɣ ɫɱɢɬɚɟɬɫɹ ɝɥɚɜɧɨɣ ɢ ɢɦɟɟɬ ɮɢɤɫɢɪɨɜɚɧɧɨɟ ɢɦɹ. ɗɬɚ ɮɭɧɤɰɢɹ ɜɵɡɵɜɚɟɬɫɹ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɨɣ ɩɪɢ ɡɚɩɭɫɤɟ ɩɪɨɝɪɚɦɦɵ, ɚ ɢɡ ɧɟɟ ɦɨɝɭɬ ɜɵɡɵɜɚɬɶɫɹ ɞɪɭɝɢɟ ɮɭɧɤɰɢɢ. ɉɪɢ ɪɚɛɨɬɟ ɜ MS DOS ɝɥɚɜɧɚɹ ɮɭɧɤɰɢɹ ɢɦɟɟɬ ɢɦɹ main. Ɉɩɢɫɚɧɢɟ ɮɭɧɤɰɢɢ ɢɦɟɟɬ ɨɛɳɢɣ ɫɢɧɬɚɤɫɢɫ ɜɧɟɲɧɟɝɨ ɨɩɪɟɞɟɥɟɧɢɹ ɢ ɨɬɥɢɱɚɟɬɫɹ ɨɬ ɨɩɢɫɚɧɢɹ ɩɟɪɟɦɟɧɧɨɣ ɫɢɧɬɚɤɫɢɫɨɦ ɞɟɤɥɚɪɚɬɨɪɚɢɧɢɰɢɚɥɢɡɚɬɨɪɚ, ɤɨɬɨɪɵɣ ɫɨɞɟɪɠɢɬ ɢɞɟɧɬɢɮɢɤɚɬɨɪ (ɢɦɹ) ɮɭɧɤɰɢɢ ɢ ɫɩɢɫɨɤ ɩɚɪɚɦɟɬɪɨɜ ɜ ɤɪɭɝɥɵɯ ɫɤɨɛɤɚɯ. Ɍɢɩ ɮɭɧɤɰɢɢ – ɷɬɨ ɬɢɩ ɡɧɚɱɟɧɢɹ, ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɮɭɧɤɰɢɟɣ. Ɏɭɧɤɰɢɹ ɦɨɠɟɬ ɜɨɡɜɪɚɳɚɬɶ ɡɧɚɱɟɧɢɟ ɥɸɛɨɝɨ ɬɢɩɚ, ɤɪɨɦɟ ɦɚɫɫɢɜɚ ɢ ɮɭɧɤɰɢɢ, ɧɨ ɞɨɩɭɫɤɚɟɬɫɹ ɜɨɡɜɪɚɳɚɬɶ ɭɤɚɡɚɬɟɥɶ ɧɚ ɦɚɫɫɢɜ ɢɥɢ ɭɤɚɡɚɬɟɥɶ ɧɚ ɮɭɧɤɰɢɸ. ȿɫɥɢ ɮɭɧɤɰɢɹ ɧɟ ɜɨɡɜɪɚɳɚɟɬ ɧɢɤɚɤɨɝɨ ɡɧɚɱɟɧɢɹ, ɬɢɩ ɮɭɧɤɰɢɢ ɨɛɨɡɧɚɱɚɟɬɫɹ ɤɥɸɱɟɜɵɦ ɫɥɨɜɨɦ void. ȼ ɫɩɢɫɤɟ ɩɚɪɚɦɟɬɪɨɜ ɭɤɚɡɵɜɚɸɬɫɹ ɬɢɩɵ ɢ ɢɦɟɧɚ ɩɚɪɚɦɟɬɪɨɜ, ɩɟɪɟɞɚɜɚɟɦɵɯ ɜ ɮɭɧɤɰɢɸ ɩɪɢ ɟɟ ɜɵɡɨɜɟ. ȿɫɥɢ ɮɭɧɤɰɢɹ ɧɟ ɬɪɟɛɭɟɬ ɩɟɪɟɞɚɱɢ ɟɣ ɚɪɝɭɦɟɧɬɨɜ, ɫɩɢɫɨɤ ɩɚɪɚɦɟɬɪɨɜ ɦɨɠɟɬ ɛɵɬɶ ɩɭɫɬɵɦ. Ⱦɥɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɫ ɩɪɨɝɪɚɦɦɚɦɢ, ɧɚɩɢɫɚɧɧɵɦɢ ɧɚ ɋɢ, ɪɟɤɨɦɟɧɞɭɟɬɫɹ ɜ ɤɚɱɟɫɬɜɟ ɩɭɫɬɨɝɨ ɫɩɢɫɤɚ ɩɚɪɚɦɟɬɪɨɜ ɭɤɚɡɵɜɚɬɶ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ void.
29
ɉɨɥɧɨɟ ɨɩɢɫɚɧɢɟ ɮɭɧɤɰɢɢ ɫɨɞɟɪɠɢɬ ɬɚɤɠɟ ɬɟɥɨ ɮɭɧɤɰɢɢ, ɩɪɟɞɫɬɚɜɥɹɸɳɟɟ ɫɨɛɨɣ ɫɨɫɬɚɜɧɨɣ ɨɩɟɪɚɬɨɪ. ɋɨɫɬɚɜɧɨɣ ɨɩɟɪɚɬɨɪ - ɷɬɨ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɨɩɢɫɚɧɢɣ ɜɧɭɬɪɟɧɧɢɯ ɞɚɧɧɵɯ ɢ ɨɩɟɪɚɬɨɪɨɜ, ɡɚɤɥɸɱɟɧɧɚɹ ɜ ɮɢɝɭɪɧɵɟ ɫɤɨɛɤɢ. ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɜ ɋɢ/ɋɢ++ ɨɩɢɫɚɧɢɟ ɮɭɧɤɰɢɣ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɜɥɨɠɟɧɧɵɦ, ɜ ɬɟɥɟ ɮɭɧɤɰɢɢ ɧɟɥɶɡɹ ɨɛɴɹɜɢɬɶ ɞɪɭɝɭɸ ɮɭɧɤɰɢɸ. Ɏɭɧɤɰɢɹ, ɜɨɡɜɪɚɳɚɸɳɚɹ ɫɪɟɞɧɟɟ ɚɪɢɮɦɟɬɢɱɟɫɤɨɟ ɬɪɟɯ ɜɟɳɟɫɬɜɟɧɧɵɯ ɞɚɧɧɵɯ, ɦɨɠɟɬ ɛɵɬɶ ɨɩɢɫɚɧɚ ɬɚɤ: double sred(double x, double y, double z) { double s; s=x+y+z; return s/3; }
Ⱦɥɹ ɜɵɡɨɜɚ ɬɚɤɨɣ ɮɭɧɤɰɢɢ ɩɪɢ ɭɫɥɨɜɢɢ, ɱɬɨ ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɨɛɴɹɜɥɟɧɵ ɩɟɪɟɦɟɧɧɵɟ p, a, b, ɢ c, ɦɨɠɧɨ ɡɚɩɢɫɚɬɶ ɨɩɟɪɚɬɨɪ: p=sred(a, b, c);
Ɉɱɟɜɢɞɧɨ, ɱɬɨ ɮɭɧɤɰɢɹ ɞɨɥɠɧɚ ɛɵɬɶ ɨɩɢɫɚɧɚ ɞɨ ɬɨɝɨ, ɤɚɤ ɜɫɬɪɟɬɢɬɫɹ ɟɟ ɩɟɪɜɵɣ ɜɵɡɨɜ. ɗɬɨ ɧɟ ɜɫɟɝɞɚ ɜɨɡɦɨɠɧɨ, ɧɚɩɪɢɦɟɪ, ɤɨɝɞɚ ɞɜɟ ɮɭɧɤɰɢɢ ɜɵɡɵɜɚɸɬ ɞɪɭɝ ɞɪɭɝɚ. Ʉɨɝɞɚ ɩɪɨɝɪɚɦɦɚ ɫɨɫɬɨɢɬ ɢɡ ɧɟɫɤɨɥɶɤɢɯ ɮɚɣɥɨɜ, ɩɨɥɧɨɟ ɨɩɢɫɚɧɢɟ ɮɭɧɤɰɢɢ ɞɨɥɠɧɨ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɬɨɥɶɤɨ ɜ ɨɞɧɨɦ ɮɚɣɥɟ, ɧɨ ɜɵɡɨɜɵ ɮɭɧɤɰɢɢ ɜɨɡɦɨɠɧɵ ɢɡ ɪɚɡɧɵɯ ɮɚɣɥɨɜ ɩɪɨɝɪɚɦɦɵ. ɑɬɨɛɵ ɪɚɡɪɟɲɢɬɶ ɩɨɞɨɛɧɵɟ ɩɪɨɬɢɜɨɪɟɱɢɹ, ɩɪɟɞɭɫɦɨɬɪɟɧɵ ɞɜɟ ɮɨɪɦɵ ɨɩɢɫɚɧɢɹ ɮɭɧɤɰɢɣ: ɩɨɥɧɚɹ ɮɨɪɦɚ, ɧɚɡɵɜɚɟɦɚɹ ɬɚɤɠɟ ɨɩɪɟɞɟɥɟɧɢɟɦ ɮɭɧɤɰɢɢ, ɢ ɫɨɤɪɚɳɟɧɧɚɹ, ɧɚɡɵɜɚɟɦɚɹ ɨɩɢɫɚɧɢɟɦ ɩɪɨɬɨɬɢɩɚ ɮɭɧɤɰɢɢ, ɢɥɢ ɩɪɨɫɬɨ ɩɪɨɬɨɬɢɩɨɦ. ɉɪɨɬɨɬɢɩ ɮɭɧɤɰɢɢ ɫɨɞɟɪɠɢɬ ɬɨɥɶɤɨ ɡɚɝɨɥɨɜɨɤ ɮɭɧɤɰɢɢ ɢ ɡɚɞɚɟɬ ɢɦɹ ɮɭɧɤɰɢɢ, ɬɢɩ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ ɢ ɬɢɩɵ ɩɚɪɚɦɟɬɪɨɜ. ɉɨ ɷɬɨɣ ɢɧɮɨɪɦɚɰɢɢ ɩɪɢ ɤɨɦɩɢɥɹɰɢɢ ɩɪɨɝɪɚɦɦɧɨɝɨ ɮɚɣɥɚ ɦɨɠɧɨ ɩɪɨɜɟɪɢɬɶ ɩɪɚɜɢɥɶɧɨɫɬɶ ɡɚɩɢɫɢ ɜɵɡɨɜɚ ɮɭɧɤɰɢɢ ɢ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ. ȼ ɋɢ++ ɬɪɟɛɭɟɬɫɹ, ɱɬɨɛɵ ɜɵɡɨɜɭ ɥɸɛɨɣ ɮɭɧɤɰɢɢ ɩɪɟɞɲɟɫɬɜɨɜɚɥɨ ɜ ɬɨɦ ɠɟ ɮɚɣɥɟ ɥɢɛɨ ɩɨɥɧɨɟ ɨɩɪɟɞɟɥɟɧɢɟ ɮɭɧɤɰɢɢ, ɥɢɛɨ ɨɩɢɫɚɧɢɟ ɟɟ ɩɪɨɬɨɬɢɩɚ. ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ ɪɹɞ ɞɨɩɨɥɧɢɬɟɥɶɧɵɯ ɜɨɡɦɨɠɧɨɫɬɟɣ ɨɩɢɫɚɧɢɹ ɮɭɧɤɰɢɣ ɜ ɋɢ++: 1. ɉɪɢ ɨɩɢɫɚɧɢɢ ɩɪɨɬɨɬɢɩɚ ɮɭɧɤɰɢɢ ɦɨɠɧɨ ɧɟ ɭɤɚɡɵɜɚɬɶ ɢɦɟɧɚ ɩɚɪɚɦɟɬɪɨɜ, ɞɨɫɬɚɬɨɱɧɨ ɭɤɚɡɚɬɶ ɢɯ ɬɢɩɵ. 2. Ⱦɥɹ ɱɚɫɬɢ ɩɚɪɚɦɟɬɪɨɜ ɮɭɧɤɰɢɢ ɦɨɠɧɨ ɡɚɞɚɜɚɬɶ ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɱɬɨ ɩɨɡɜɨɥɹɟɬ ɜɵɡɵɜɚɬɶ ɮɭɧɤɰɢɸ ɫ ɦɟɧɶɲɢɦ ɱɢɫɥɨɦ ɚɪɝɭɦɟɧɬɨɜ, ɱɟɦ ɩɪɟɞɭɫɦɨɬɪɟɧɨ ɨɩɢɫɚɧɢɟɦ ɮɭɧɤɰɢɢ. Ɂɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɦɨɠɧɨ ɭɤɚɡɵɜɚɬɶ ɬɨɥɶɤɨ ɞɥɹ ɩɨɫɥɟɞɧɢɯ ɩɚɪɚɦɟɬɪɨɜ ɜ ɫɩɢɫɤɟ. ɇɚɩɪɢɦɟɪ, ɮɭɧɤɰɢɹ sred ɦɨɝɥɚ ɛɵ ɛɵɬɶ ɨɩɢɫɚɧɚ ɬɚɤ: double sred(double x, double y, double z=0) { double s; s = x+y+z; 30
return }
s/3;
Ʉ ɬɚɤɨɣ ɮɭɧɤɰɢɢ ɦɨɠɧɨ ɨɛɪɚɳɚɬɶɫɹ ɫ ɞɜɭɦɹ ɢ ɫ ɬɪɟɦɹ ɚɪɝɭɦɟɧɬɚɦɢ. 3. Ɏɭɧɤɰɢɹ ɦɨɠɟɬ ɢɦɟɬɶ ɩɟɪɟɦɟɧɧɨɟ ɱɢɫɥɨ ɩɚɪɚɦɟɬɪɨɜ, ɞɥɹ ɱɚɫɬɢ ɩɚɪɚɦɟɬɪɨɜ ɦɨɝɭɬ ɛɵɬɶ ɧɟɢɡɜɟɫɬɧɵ ɢɯ ɬɢɩɵ. ɇɟɢɡɜɟɫɬɧɚɹ ɱɚɫɬɶ ɫɩɢɫɤɚ ɩɚɪɚɦɟɬɪɨɜ ɨɛɨɡɧɚɱɚɟɬɫɹ ɦɧɨɝɨɬɨɱɢɟɦ. ɇɚɩɪɢɦɟɪ, ɮɭɧɤɰɢɹ ɫ ɩɪɨɬɨɬɢɩɨɦ int varfunc(int n, ...);
ɢɦɟɟɬ ɨɞɢɧ ɨɛɹɡɚɬɟɥɶɧɵɣ ɩɚɪɚɦɟɬɪ ɬɢɩɚ int ɢ ɧɟɨɩɪɟɞɟɥɟɧɧɨɟ ɱɢɫɥɨ ɩɚɪɚɦɟɬɪɨɜ ɧɟɢɡɜɟɫɬɧɵɯ ɬɢɩɨɜ. ɉɪɚɜɢɥɶɧɚɹ ɢɧɬɟɪɩɪɟɬɚɰɢɹ ɬɚɤɨɝɨ ɫɩɢɫɤɚ ɩɚɪɚɦɟɬɪɨɜ ɜ ɬɟɥɟ ɮɭɧɤɰɢɢ ɬɪɟɛɭɟɬ ɨɬ ɩɪɨɝɪɚɦɦɢɫɬɚ ɞɨɩɨɥɧɢɬɟɥɶɧɵɯ ɭɫɢɥɢɣ. ɉɪɢ ɜɵɡɨɜɟ ɮɭɧɤɰɢɢ ɚɪɝɭɦɟɧɬɵ ɩɟɪɟɞɚɸɬɫɹ ɜ ɮɭɧɤɰɢɸ ɩɨ ɡɧɚɱɟɧɢɹɦ. ɗɬɨ ɡɧɚɱɢɬ, ɱɬɨ ɞɥɹ ɤɚɠɞɨɝɨ ɚɪɝɭɦɟɧɬɚ ɜ ɩɚɦɹɬɢ ɫɨɡɞɚɟɬɫɹ ɟɝɨ ɤɨɩɢɹ, ɤɨɬɨɪɚɹ ɢ ɢɫɩɨɥɶɡɭɟɬɫɹ ɩɪɢ ɜɵɱɢɫɥɟɧɢɢ ɮɭɧɤɰɢɢ. ɋɥɟɞɨɜɚɬɟɥɶɧɨ, ɥɸɛɵɟ ɢɡɦɟɧɟɧɢɹ ɡɧɚɱɟɧɢɣ ɚɪɝɭɦɟɧɬɨɜ ɜ ɬɟɥɟ ɮɭɧɤɰɢɢ ɬɟɪɹɸɬɫɹ ɩɪɢ ɜɵɯɨɞɟ ɢɡ ɮɭɧɤɰɢɢ. ȿɫɥɢ ɚɪɝɭɦɟɧɬ ɹɜɥɹɟɬɫɹ ɦɚɫɫɢɜɨɦ, ɬɨ ɜ ɮɭɧɤɰɢɸ ɩɟɪɟɞɚɟɬɫɹ ɭɤɚɡɚɬɟɥɶ ɧɚ ɧɚɱɚɥɶɧɵɣ ɷɥɟɦɟɧɬ ɦɚɫɫɢɜɚ ɢ ɩɪɢɫɜɚɢɜɚɧɢɹ ɷɥɟɦɟɧɬɚɦ ɦɚɫɫɢɜɚ ɜ ɬɟɥɟ ɮɭɧɤɰɢɢ ɢɡɦɟɧɹɸɬ ɡɧɚɱɟɧɢɹ ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ ɚɪɝɭɦɟɧɬɚ. ȼ ɋ++ ɫ ɨɛɴɹɜɥɟɧɢɟɦ ɮɭɧɤɰɢɢ ɫɜɹɡɵɜɚɟɬɫɹ ɬɚɤ ɧɚɡɵɜɚɟɦɚɹ ɫɢɝɧɚɬɭɪɚ ɮɭɧɤɰɢɢ, ɨɩɪɟɞɟɥɹɟɦɚɹ ɬɢɩɨɦ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ ɢ ɬɢɩɚɦɢ ɩɚɪɚɦɟɬɪɨɜ. ɗɬɨ ɩɨɡɜɨɥɹɟɬ ɧɚɡɧɚɱɚɬɶ ɮɭɧɤɰɢɹɦ, ɢɦɟɸɳɢɦ ɚɧɚɥɨɝɢɱɧɨɟ ɧɚɡɧɚɱɟɧɢɟ, ɧɨ ɢɫɩɨɥɶɡɭɸɳɢɦ ɩɚɪɚɦɟɬɪɵ ɪɚɡɧɵɯ ɬɢɩɨɜ, ɨɞɢɧɚɤɨɜɵɟ ɢɦɟɧɚ. ɇɚɩɪɢɦɟɪ, ɧɚɪɹɞɭ ɫ ɩɪɢɜɟɞɟɧɧɨɣ ɜɵɲɟ ɮɭɧɤɰɢɟɣ sred ɞɥɹ ɜɟɳɟɫɬɜɟɧɧɵɯ ɚɪɝɭɦɟɧɬɨɜ ɜ ɬɨɣ ɠɟ ɩɪɨɝɪɚɦɦɟ ɦɨɠɟɬ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɮɭɧɤɰɢɹ double sred(int x, int y, int z=0) { int s; s = x+y+z; return s/3; }
Ɏɭɧɤɰɢɹ, ɜ ɬɟɥɟ ɤɨɬɨɪɨɣ ɨɬɫɭɬɫɬɜɭɸɬ ɨɩɟɪɚɬɨɪɵ ɰɢɤɥɚ ɢ ɩɟɪɟɤɥɸɱɚɬɟɥɢ, ɦɨɠɟɬ ɛɵɬɶ ɨɛɴɹɜɥɟɧɚ ɫ ɞɨɩɨɥɧɢɬɟɥɶɧɵɦ ɨɩɢɫɚɬɟɥɟɦ inline. ȼ ɬɨɱɤɟ ɜɵɡɨɜɚ ɬɚɤɨɣ ɮɭɧɤɰɢɢ ɩɪɢ ɤɨɦɩɢɥɹɰɢɢ ɩɪɨɫɬɨ ɜɫɬɚɜɥɹɟɬɫɹ ɬɟɥɨ ɮɭɧɤɰɢɢ ɫ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɣ ɡɚɦɟɧɨɣ ɩɚɪɚɦɟɬɪɨɦ ɧɚ ɚɪɝɭɦɟɧɬɵ ɜɵɡɨɜɚ. ȼ ɪɟɡɭɥɶɬɚɬɟ ɷɤɨɧɨɦɢɬɫɹ ɜɪɟɦɹ ɧɚ ɩɟɪɟɞɚɱɭ ɩɚɪɚɦɟɬɪɨɜ, ɩɟɪɟɯɨɞ ɧɚ ɩɨɞɩɪɨɝɪɚɦɦɭ ɢ ɨɪɝɚɧɢɡɚɰɢɸ ɜɨɡɜɪɚɬɚ ɜ ɜɵɡɵɜɚɸɳɭɸ ɩɪɨɝɪɚɦɦɭ. Ɏɭɧɤɰɢɢ ɫ ɨɩɢɫɚɬɟɥɟɦ inline ɧɚɡɵɜɚɸɬ ɜɫɬɪɨɟɧɧɵɦɢ, ɨɧɢ ɪɟɚɥɢɡɭɸɬɫɹ ɤɚɤ ɨɬɤɪɵɬɵɟ ɩɨɞɩɪɨɝɪɚɦɦɵ. Ɍɢɩɢɱɧɵɣ ɩɪɢɦɟɪ ɬɚɤɨɣ ɮɭɧɤɰɢɢ – ɨɩɪɟɞɟɥɟɧɢɟ ɧɚɢɛɨɥɶɲɟɝɨ (ɧɚɢɦɟɧɶɲɟɝɨ) ɢɡ ɞɜɭɯ ɱɢɫɟɥ: inline int max(int x1, int x2) { return x1>x2?x1:x2 }
ȼ ɫɢɫɬɟɦɚɯ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ Turbo C++ ɢ Borland C++ ɝɥɚɜɧɚɹ ɮɭɧɤɰɢɹ (ɮɭɧɤɰɢɹ main) ɦɨɠɟɬ ɩɪɢɧɢɦɚɬɶ ɬɪɢ ɩɚɪɚɦɟɬɪɚ, ɱɬɨ ɩɨɡɜɨɥɹɟɬ 31
ɜɵɡɵɜɚɬɶ ɋɢ-ɩɪɨɝɪɚɦɦɵ ɢɡ ɤɨɦɚɧɞɧɨɣ ɫɬɪɨɤɢ MS DOS ɫ ɩɟɪɟɞɚɱɟɣ ɜ ɩɪɨɝɪɚɦɦɭ ɧɟɨɛɯɨɞɢɦɵɯ ɚɪɝɭɦɟɧɬɨɜ. ɋɬɚɧɞɚɪɬɧɵɣ ɩɪɨɬɨɬɢɩ ɮɭɧɤɰɢɢ main ɢɦɟɟɬ ɜɢɞ. int main (int argc, char *argv[], char *envp[])
ȼ ɤɨɧɤɪɟɬɧɨɣ ɩɪɨɝɪɚɦɦɟ ɦɨɠɧɨ ɨɛɴɹɜɥɹɬɶ ɮɭɧɤɰɢɸ main ɛɟɡ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ (ɜɨɡɜɪɚɳɚɸɳɭɸ ɬɢɩ void) ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɬɨɥɶɤɨ ɞɜɭɯ ɩɟɪɜɵɯ ɩɚɪɚɦɟɬɪɨɜ ɢɥɢ ɜɨɨɛɳɟ ɛɟɡ ɩɚɪɚɦɟɬɪɨɜ. ɉɚɪɚɦɟɬɪɵ argc ɢ argv ɫɥɭɠɚɬ ɞɥɹ ɩɟɪɟɞɚɱɢ ɜ ɩɪɨɝɪɚɦɦɭ ɚɪɝɭɦɟɧɬɨɜ ɜ ɜɢɞɟ ɦɚɫɫɢɜɚ ɫɬɪɨɤ. ɉɚɪɚɦɟɬɪ argc ɫɨɞɟɪɠɢɬ ɱɢɫɥɨ ɷɥɟɦɟɧɬɨɜ ɷɬɨɝɨ ɦɚɫɫɢɜɚ, argv - ɷɬɨ ɦɚɫɫɢɜ ɭɤɚɡɚɬɟɥɟɣ ɧɚ ɩɟɪɟɞɚɜɚɟɦɵɟ ɫɬɪɨɤɢ, ɩɪɢɱɟɦ ɩɟɪɜɵɣ ɷɥɟɦɟɧɬ ɦɚɫɫɢɜɚ, ɧɚ ɤɨɬɨɪɵɣ ɭɤɚɡɵɜɚɟɬ argv[0], ɫɨɞɟɪɠɢɬ ɢɦɹ ɩɪɨɝɪɚɦɦɵ (ɢɦɹ exe-ɮɚɣɥɚ ɩɪɨɝɪɚɦɦɵ), ɨɫɬɚɥɶɧɵɟ ɷɥɟɦɟɧɬɵ ɩɪɟɞɫɬɚɜɥɹɸɬ ɫɨɛɨɣ ɚɪɝɭɦɟɧɬɵ ɢɡ ɤɨɦɚɧɞɧɨɣ ɫɬɪɨɤɢ MS DOS. ɉɚɪɚɦɟɬɪ envp ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɞɨɫɬɭɩɚ ɤ ɷɥɟɦɟɧɬɚɦ ɬɟɤɭɳɟɣ ɫɪɟɞɵ MS DOS.
2.7. Ȼɢɛɥɢɨɬɟɤɚ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ ȼ ɨɩɪɟɞɟɥɟɧɢɢ ɹɡɵɤɨɜ ɋɢ ɢ ɋɢ++ ɨɬɫɭɬɫɬɜɭɸɬ ɨɩɟɪɚɬɨɪɵ ɜɜɨɞɚɜɵɜɨɞɚ, ɨɩɟɪɚɰɢɢ ɧɚɞ ɫɬɪɨɤɨɜɵɦɢ ɞɚɧɧɵɦɢ ɢ ɦɧɨɝɢɟ ɞɪɭɝɢɟ ɫɪɟɞɫɬɜɚ, ɢɦɟɸɳɢɟɫɹ ɜ ɞɪɭɝɢɯ ɹɡɵɤɚɯ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ. ɗɬɨɬ ɧɟɞɨɫɬɚɬɨɤ ɤɨɦɩɟɧɫɢɪɭɟɬɫɹ ɞɨɛɚɜɥɟɧɢɟɦ ɜ ɫɢɫɬɟɦɵ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɋɢ ɢ ɋɢ++ ɛɢɛɥɢɨɬɟɤ ɮɭɧɤɰɢɣ, ɩɨɞɤɥɸɱɚɟɦɵɯ ɤ ɪɚɛɨɱɢɦ ɩɪɨɝɪɚɦɦɚɦ ɩɪɢ ɪɟɞɚɤɬɢɪɨɜɚɧɢɢ ɫɜɹɡɟɣ ɢ ɧɚɡɵɜɚɟɦɵɯ ɛɢɛɥɢɨɬɟɤɚɦɢ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ. Ɉɬɞɟɥɟɧɢɟ ɷɬɢɯ ɛɢɛɥɢɨɬɟɤ ɨɬ ɤɨɦɩɢɥɹɬɨɪɚ ɩɨɡɜɨɥɹɟɬ ɜ ɧɟɨɛɯɨɞɢɦɵɯ ɫɥɭɱɚɹɯ ɢɫɩɨɥɶɡɨɜɚɬɶ ɪɚɡɥɢɱɧɵɟ ɜɚɪɢɚɧɬɵ ɷɬɢɯ ɛɢɛɥɢɨɬɟɤ, ɧɚɩɪɢɦɟɪ, ɞɥɹ ɪɚɡɥɢɱɧɵɯ ɦɨɞɟɥɟɣ ɗȼɆ ɢɥɢ ɨɩɟɪɚɰɢɨɧɧɵɯ ɫɢɫɬɟɦ. ɑɚɫɬɶ ɮɭɧɤɰɢɣ ɢɡ ɛɢɛɥɢɨɬɟɤ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ ɫɬɚɧɞɚɪɬɢɡɨɜɚɧɚ, ɜ ɫɬɚɧɞɚɪɬɟ ɡɚɮɢɤɫɢɪɨɜɚɧɵ ɢɦɹ ɮɭɧɤɰɢɢ, ɟɟ ɧɚɡɧɚɱɟɧɢɟ, ɩɟɪɟɱɟɧɶ ɢ ɬɢɩɵ ɩɚɪɚɦɟɬɪɨɜ, ɚ ɬɚɤɠɟ ɬɢɩ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ. Ⱦɪɭɝɢɟ ɮɭɧɤɰɢɢ ɨɪɢɟɧɬɢɪɨɜɚɧɵ ɧɚ ɤɨɧɤɪɟɬɧɵɟ ɦɨɞɟɥɢ ɗȼɆ ɢ ɨɩɟɪɚɰɢɨɧɧɵɟ ɫɢɫɬɟɦɵ, ɫɩɨɫɨɛɵ ɢɯ ɜɵɡɨɜɚ ɢ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɦɨɝɭɬ ɛɵɬɶ ɪɚɡɥɢɱɧɵɦɢ ɜ ɫɢɫɬɟɦɚɯ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɪɚɡɧɵɯ ɮɢɪɦ. ɑɬɨɛɵ ɨɛɟɫɩɟɱɢɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɤɨɧɬɪɨɥɹ ɩɪɚɜɢɥɶɧɨɫɬɢ ɜɵɡɨɜɚ ɮɭɧɤɰɢɣ ɩɪɢ ɤɨɦɩɢɥɹɰɢɢ ɩɪɨɝɪɚɦɦɵ, ɜ ɫɢɫɬɟɦɭ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɜɯɨɞɹɬ ɮɚɣɥɵ ɡɚɝɨɥɨɜɤɨɜ ɮɭɧɤɰɢɣ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ. ȼ ɮɚɣɥɚɯ ɡɚɝɨɥɨɜɤɨɜ ɨɩɪɟɞɟɥɹɸɬɫɹ ɩɪɨɬɨɬɢɩɵ ɛɢɛɥɢɨɬɟɱɧɵɯ ɮɭɧɤɰɢɣ, ɚ ɬɚɤɠɟ ɤɨɧɫɬɚɧɬɵ ɢ ɬɢɩɵ ɞɚɧɧɵɯ, ɢɫɩɨɥɶɡɭɟɦɵɟ ɷɬɢɦɢ ɮɭɧɤɰɢɹɦɢ. ɇɢɠɟ ɩɪɢɜɟɞɟɧɵ ɢɦɟɧɚ ɧɟɤɨɬɨɪɵɯ ɮɚɣɥɨɜ ɡɚɝɨɥɨɜɤɨɜ ɢ ɧɚɡɧɚɱɟɧɢɟ ɨɩɢɫɚɧɧɵɯ ɜ ɧɢɯ ɩɪɨɬɨɬɢɩɨɜ ɝɪɭɩɩ ɮɭɧɤɰɢɣ. ɋɬɚɧɞɚɪɬɨɦ ɋɢ ɨɩɪɟɞɟɥɟɧɵ ɫɥɟɞɭɸɳɢɟ ɮɚɣɥɵ ɡɚɝɨɥɨɜɤɨɜ: ASSERT.H: ɫɨɞɟɪɠɢɬ ɦɚɤɪɨɫɵ ɞɥɹ ɫɨɨɛɳɟɧɢɣ ɨɛ ɨɲɢɛɤɚɯ ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ ɭɫɥɨɜɢɹ, ɡɚɞɚɜɚɟɦɨɝɨ ɩɪɨɝɪɚɦɦɢɫɬɨɦ. CTYPE.H: ɮɭɧɤɰɢɢ ɞɥɹ ɩɪɨɜɟɪɤɢ ɢ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɞɚɧɧɵɯ ɬɢɩɚ char. 32
FLOAT.H: ɦɚɤɪɨɫɵ ɞɥɹ ɨɩɟɪɚɰɢɣ ɧɚɞ ɱɢɫɥɚɦɢ ɫ ɩɥɚɜɚɸɳɟɣ ɬɨɱɤɨɣ. LIMITS.H: ɦɚɤɪɨɫɵ, ɡɚɞɚɸɳɢɟ ɞɢɚɩɚɡɨɧɵ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɰɟɥɵɯ. LOCALE.H: ɩɪɟɞɫɬɚɜɥɟɧɢɟ ɞɚɬɵ, ɜɪɟɦɟɧɢ, ɞɟɧɟɠɧɵɯ ɟɞɢɧɢɰ. MATH.H: ɩɚɤɟɬ ɫɬɚɧɞɚɪɬɧɵɯ ɦɚɬɟɦɚɬɢɱɟɫɤɢɯ ɮɭɧɤɰɢɣ. SETJUMP.H: ɢɦɟɧɚ ɬɢɩɨɜ ɢ ɮɭɧɤɰɢɢ ɞɥɹ ɪɟɚɥɢɡɚɰɢɢ ɨɩɟɪɚɬɨɪɨɜ ɩɟɪɟɯɨɞɚ
ɢɫɩɨɥɶɡɭɟɬɫɹ ɪɟɞɤɨ. SIGNAL.H: ɦɚɤɪɨɫɵ ɞɥɹ ɫɢɝɧɚɥɢɡɚɰɢɢ ɨɛ ɨɲɢɛɤɚɯ ɫɨɝɥɚɫɧɨ ɫɬɚɧɞɚɪɬɭ
ANSI. STDARG.H: ɦɚɤɪɨɫɵ ɞɥɹ ɜɵɡɨɜɚ ɮɭɧɤɰɢɣ ɫ ɩɟɪɟɦɟɧɧɵɦ ɱɢɫɥɨɦ ɚɪɝɭɦɟɧ-
ɬɨɜ. STDDEF.H: ɨɩɪɟɞɟɥɟɧɢɟ ɨɛɳɢɯ ɬɢɩɨɜ ɞɥɹ ɭɤɚɡɚɬɟɥɟɣ, ɬɢɩɨɜ size_t ɢ NULL. STDIO.H: ɫɬɚɧɞɚɪɬɧɵɟ ɮɭɧɤɰɢɢ ɜɜɨɞɚ-ɜɵɜɨɞɚ. STDLIB.H: ɨɩɪɟɞɟɥɟɧɢɟ ɨɛɳɢɯ ɬɢɩɨɜ, ɩɟɪɟɦɟɧɧɵɯ ɢ ɮɭɧɤɰɢɣ. STRING.H: ɮɭɧɤɰɢɢ ɞɥɹ ɨɩɟɪɚɰɢɣ ɧɚɞ ɫɬɪɨɤɨɜɵɦɢ ɞɚɧɧɵɦɢ. TIME.H: ɫɬɪɭɤɬɭɪɵ ɢ ɮɭɧɤɰɢɢ ɞɥɹ ɨɩɟɪɚɰɢɣ ɫ ɞɚɬɚɦɢ ɢ ɜɪɟɦɟɧɟɦ.
ȼ ɋɢ++ ɞɨɛɚɜɥɟɧɵ ɨɩɟɪɚɰɢɢ ɫ ɤɨɦɩɥɟɤɫɧɵɦɢ ɱɢɫɥɚɦɢ ɢ ɞɟɫɹɬɢɱɧɵɦɢ ɞɚɧɧɵɦɢ: BCD.H: ɞɚɧɧɵɟ, ɩɪɟɞɫɬɚɜɥɟɧɧɵɟ ɜ ɞɟɫɹɬɢɱɧɨɣ ɫɢɫɬɟɦɟ ɫɱɢɫɥɟɧɢɹ. COMPLEX.H: ɮɭɧɤɰɢɢ ɢ ɨɩɟɪɚɰɢɢ ɧɚɞ ɤɨɦɩɥɟɤɫɧɵɦɢ ɱɢɫɥɚɦɢ. ɂɦɟɸɬɫɹ ɬɚɤɠɟ ɮɚɣɥɵ ɩɪɨɬɨɬɢɩɨɜ ɮɭɧɤɰɢɣ ɞɥɹ ɪɚɫɩɪɟɞɟɥɟɧɢɹ ɢ ɨɫɜɨɛɨɠɞɟɧɢɹ ɞɢɧɚɦɢɱɟɫɤɨɣ ɩɚɦɹɬɢ, ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɫɪɟɞɫɬɜ MS DOS ɢ BIOS. ɑɬɨɛɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɚɤɢɟ-ɥɢɛɨ ɮɭɧɤɰɢɢ ɢɡ ɛɢɛɥɢɨɬɟɤ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ ɜ ɩɪɨɝɪɚɦɦɭ ɞɨɥɠɟɧ ɛɵɬɶ ɜɤɥɸɱɟɧ ɮɚɣɥ-ɡɚɝɨɥɨɜɨɤ ɫ ɩɪɨɬɨɬɢɩɚɦɢ ɬɪɟɛɭɟɦɵɯ ɮɭɧɤɰɢɣ. ȼɤɥɸɱɟɧɢɟ ɜ ɩɪɨɝɪɚɦɦɭ ɮɚɣɥɚ-ɡɚɝɨɥɨɜɤɚ ɨɛɟɫɩɟɱɢɜɚɟɬɫɹ ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɣ ɞɢɪɟɤɬɢɜɨɣ #include <ɢɦɹ_ɮɚɣɥɚ>
ɇɚɩɪɢɦɟɪ, ɞɥɹ ɜɤɥɸɱɟɧɢɹ ɡɚɝɨɥɨɜɤɚ ɫ ɮɭɧɤɰɢɹɦɢ ɜɜɨɞ-ɜɵɜɨɞɚ ɜ ɫɬɢɥɟ ɋɢ ɜ ɧɚɱɚɥɟ ɩɪɨɝɪɚɦɦɵ ɡɚɩɢɫɵɜɚɟɬɫɹ ɫɬɪɨɤɚ #include <stdio.h>
Ɉɱɟɜɢɞɧɨ, ɥɸɛɚɹ ɩɪɨɝɪɚɦɦɚ ɢɫɩɨɥɶɡɭɟɬ ɤɚɤɢɟ-ɥɢɛɨ ɜɯɨɞɧɵɟ ɞɚɧɧɵɟ ɢ ɤɭɞɚ-ɥɢɛɨ ɜɵɜɨɞɢɬ ɩɨɥɭɱɟɧɧɵɟ ɪɟɡɭɥɶɬɚɬɵ, ɩɨɷɬɨɦɭ ɮɚɣɥ ɡɚɝɨɥɨɜɤɨɜ stdio.h ɩɪɢɫɭɬɫɬɜɭɟɬ ɩɨɱɬɢ ɜɨ ɜɫɟɯ ɩɪɨɝɪɚɦɦɚɯ. Ɉɬɦɟɬɢɦ ɧɟɤɨɬɨɪɵɟ ɮɭɧɤɰɢɢ ɢɡ ɷɬɨɝɨ ɮɚɣɥɚ. Ɏɭɧɤɰɢɢ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɢɫɩɨɥɶɡɭɸɬ ɩɨɧɹɬɢɟ ɩɨɬɨɤɚ, ɪɚɫɫɦɚɬɪɢɜɚɟɦɨɝɨ ɤɚɤ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɛɚɣɬɨɜ. ɉɨɬɨɤ ɦɨɠɟɬ ɛɵɬɶ ɫɜɹɡɚɧ ɫ ɞɢɫɤɨɜɵɦ ɮɚɣɥɨɦ ɢɥɢ ɞɪɭɝɢɦ ɭɫɬɪɨɣɫɬɜɨɦ ɜɜɨɞɚ-ɜɵɜɨɞɚ, ɜ ɬɨɦ ɱɢɫɥɟ ɫ ɤɨɧɫɨɥɶɸ, ɤɨɝɞɚ ɜɜɨɞ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɫ ɤɥɚɜɢɚɬɭɪɵ, ɚ ɜɵɜɨɞ - ɧɚ ɷɤɪɚɧ ɦɨɧɢɬɨɪɚ. ɉɪɟɞɭɫɦɨɬɪɟɧɨ ɧɟɫɤɨɥɶɤɨ ɫɬɚɧɞɚɪɬɧɵɯ ɩɨɬɨɤɨɜ: stdin – ɫɬɚɧɞɚɪɬɧɵɣ ɜɜɨɞ, stdout – ɫɬɚɧɞɚɪɬɧɵɣ ɜɵɜɨɞ, stderr – ɞɥɹ ɜɵɜɨɞɚ ɫɨɨɛɳɟɧɢɣ ɨɛ ɨɲɢɛɤɚɯ, stdprn – ɫɬɚɧɞɚɪɬɧɨɟ ɭɫɬɪɨɣɫɬɜɨ ɩɟɱɚɬɢ, 33
stdaux – ɫɬɚɧɞɚɪɬɧɵɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɵɣ ɩɨɪɬ. ɉɨɬɨɤɢ stdin, stdout ɢ stderr ɨɛɵɱɧɨ ɫɜɹɡɵɜɚɸɬɫɹ ɫ ɤɨɧɫɨɥɶɸ, ɧɨ
ɦɨɝɭɬ ɛɵɬɶ ɩɟɪɟɧɚɡɧɚɱɟɧɵ ɧɚ ɞɪɭɝɢɟ ɭɫɬɪɨɣɫɬɜɚ. ɇɚɡɧɚɱɟɧɢɟ ɞɜɭɯ ɩɨɫɥɟɞɧɢɯ ɩɨɬɨɤɨɜ ɡɚɜɢɫɢɬ ɨɬ ɢɫɩɨɥɶɡɭɟɦɨɣ ɚɩɩɚɪɚɬɭɪɵ. ɋɬɚɧɞɚɪɬɧɵɟ ɩɨɬɨɤɢ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɨɬɤɪɵɜɚɸɬɫɹ ɩɪɢ ɡɚɩɭɫɤɟ ɋɢ-ɩɪɨɝɪɚɦɦɵ ɢ ɡɚɤɪɵɜɚɸɬɫɹ ɩɪɢ ɟɟ ɡɚɜɟɪɲɟɧɢɢ. ɉɨɬɨɤɢ, ɫɨɡɞɚɜɚɟɦɵɟ ɩɪɨɝɪɚɦɦɢɫɬɨɦ, ɨɬɤɪɵɜɚɸɬɫɹ ɮɭɧɤɰɢɟɣ fopen ɢ ɡɚɤɪɵɜɚɸɬɫɹ ɮɭɧɤɰɢɟɣ fclose. Ɏɭɧɤɰɢɢ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɢɡ stdio.h ɭɫɥɨɜɧɨ ɦɨɠɧɨ ɪɚɡɛɢɬɶ ɧɚ ɱɟɬɵɪɟ ɝɪɭɩɩɵ: ɜɜɨɞ-ɜɵɜɨɞ ɛɚɣɬɨɜ, ɜɜɨɞ-ɜɵɜɨɞ ɫɬɪɨɤ, ɮɨɪɦɚɬɧɵɣ ɜɜɨɞ-ɜɵɜɨɞ ɢ ɬɚɤ ɧɚɡɵɜɚɟɦɵɣ ɩɪɹɦɨɣ (ɛɟɫɮɨɪɦɚɬɧɵɣ) ɜɜɨɞ-ɜɵɜɨɞ. Ɂɞɟɫɶ ɨɬɦɟɬɢɦ ɬɨɥɶɤɨ ɨɬɞɟɥɶɧɵɯ ɩɪɟɞɫɬɚɜɢɬɟɥɟɣ ɩɟɪɜɵɯ ɬɪɟɯ ɝɪɭɩɩ, ɩɪɟɞɧɚɡɧɚɱɟɧɧɵɯ ɞɥɹ ɜɜɨɞɚ ɢɡ ɩɨɬɨɤɚ stdin ɢ ɜɵɜɨɞɚ ɜ ɩɨɬɨɤ stdout. Ɏɭɧɤɰɢɹ int getchar() ɫɥɭɠɢɬ ɞɥɹ ɜɜɨɞɚ ɨɞɧɨɝɨ ɫɢɦɜɨɥɚ ɫ ɤɥɚɜɢɚɬɭɪɵ ɢ ɜɨɡɜɪɚɳɚɟɬ ɤɨɞ ɫɢɦɜɨɥɚ, ɩɪɟɨɛɪɚɡɨɜɚɧɧɵɣ ɤ ɬɢɩɭ int. Ɏɭɧɤɰɢɹ int putchar(int c) ɜɵɜɨɞɢɬ ɫɢɦɜɨɥ c ɜ ɨɱɟɪɟɞɧɭɸ ɩɨɡɢɰɢɸ ɧɚ ɷɤɪɚɧɟ ɦɨɧɢɬɨɪɚ. Ⱦɥɹ ɜɜɨɞɚ ɫɬɪɨɤɢ ɫ ɤɥɚɜɢɚɬɭɪɵ ɫɥɭɠɢɬ ɮɭɧɤɰɢɹ char* gets(char* buf ), ɤɨɬɨɪɚɹ ɱɢɬɚɟɬ ɜɜɨɞ ɫ ɤɥɚɜɢɚɬɭɪɵ (ɞɨ ɫɢɦɜɨɥɚ ɧɨɜɨɣ ɫɬɪɨɤɢ ɢɥɢ ɧɚɠɚɬɢɹ ɤɥɚɜɢɲɢ Enter) ɢ ɩɨɦɟɳɚɟɬ ɤɨɞɵ ɩɪɨɱɢɬɚɧɧɵɯ ɫɢɦɜɨɥɨɜ ɜ ɛɭɮɟɪ, ɚɞɪɟɫ ɤɨɬɨɪɨɝɨ ɡɚɞɚɟɬɫɹ ɩɚɪɚɦɟɬɪɨɦ buf, ɜ ɤɨɧɰɟ ɫɬɪɨɤɢ ɞɨɛɚɜɥɹɟɬɫɹ ɧɭɥɟɜɨɣ ɛɚɣɬ. ȼɵɜɨɞ ɫɬɪɨɤɢ ɜɵɩɨɥɧɹɟɬ ɮɭɧɤɰɢɸ int puts(char* string), ɤɨɬɨɪɚɹ ɜɵɜɨɞɢɬ ɫɬɪɨɤɭ ɩɨ ɚɞɪɟɫɭ string ɧɚ ɷɤɪɚɧ, ɩɨɤɚ ɜ ɫɬɪɨɤɟ ɧɟ ɜɫɬɪɟɬɢɬɫɹ ɧɭɥɟɜɨɣ ɛɚɣɬ, ɢ ɜɨɡɜɪɚɳɚɟɬ ɤɨɞ ɩɨɫɥɟɞɧɟɝɨ ɜɵɜɟɞɟɧɧɨɝɨ ɫɢɦɜɨɥɚ. Ɏɭɧɤɰɢɢ ɮɨɪɦɚɬɧɨɝɨ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɩɪɢɧɢɦɚɸɬ ɜ ɤɚɱɟɫɬɜɟ ɩɚɪɚɦɟɬɪɨɜ ɫɬɪɨɤɭ ɫ ɨɩɢɫɚɧɢɟɦ ɮɨɪɦɚɬɚ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɞɚɧɧɵɯ ɧɚ ɜɧɟɲɧɟɦ ɭɫɬɪɨɣɫɬɜɟ ɢ ɫɩɢɫɨɤ ɜɜɨɞɢɦɵɯ ɢɥɢ ɜɵɜɨɞɢɦɵɯ ɞɚɧɧɵɯ. ɋɬɪɨɤɚ ɨɩɢɫɚɧɢɹ ɮɨɪɦɚɬɚ ɫɨɫɬɨɢɬ ɢɡ ɨɛɵɱɧɵɯ ɫɢɦɜɨɥɨɜ, ɭɩɪɚɜɥɹɸɳɢɯ ɫɢɦɜɨɥɨɜ ɬɢɩɚ ɧɨɜɨɣ ɫɬɪɨɤɢ, ɜɨɡɜɪɚɬɚ ɤɚɪɟɬɤɢ ɢ ɬ.ɩ. ɢ ɫɩɟɰɢɮɢɤɚɰɢɣ ɩɨɥɟɣ ɜɜɨɞɚ ɢɥɢ ɜɵɜɨɞɚ. Ʉɚɠɞɚɹ ɬɚɤɚɹ ɫɩɟɰɢɮɢɤɚɰɢɹ ɧɚɱɢɧɚɟɬɫɹ ɫɢɦɜɨɥɨɦ «%» (ɩɪɨɰɟɧɬ), ɡɚ ɤɨɬɨɪɵɦ ɫɥɟɞɭɸɬ ɤɨɞɵ ɮɥɚɝɨɜ, ɪɚɡɦɟɪ ɩɨɥɹ ɜɜɨɞɚ ɢɥɢ ɜɵɜɨɞɚ, ɱɢɫɥɨ ɰɢɮɪ ɜ ɞɪɨɛɧɨɣ ɱɚɫɬɢ ɱɢɫɥɚ, ɩɪɟɮɢɤɫɚ ɪɚɡɦɟɪɚ ɞɚɧɧɨɝɨ ɢ ɤɨɞɚ ɬɢɩɚ ɮɨɪɦɚɬɚ. Ɉɛɹɡɚɬɟɥɶɧɨ ɭɤɚɡɵɜɚɬɶ ɬɨɥɶɤɨ ɬɢɩ ɮɨɪɦɚɬɚ, ɨɫɬɚɥɶɧɵɟ ɤɨɦɩɨɧɟɧɬɵ ɫɩɟɰɢɮɢɤɚɰɢɢ ɮɨɪɦɚɬɚ ɡɚɞɚɸɬɫɹ ɩɨ ɧɟɨɛɯɨɞɢɦɨɫɬɢ. Ɉɬɦɟɬɢɦ ɧɟɤɨɬɨɪɵɟ ɤɨɞɵ ɬɢɩɚ ɮɨɪɦɚɬɚ: d – ɞɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɰɟɥɨɝɨ ɫɨ ɡɧɚɤɨɦ ɜ ɞɟɫɹɬɢɱɧɨɣ ɫɢɫɬɟɦɟ ɫɱɢɫɥɟɧɢɹ, i – ɞɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɰɟɥɨɝɨ ɛɟɡ ɡɧɚɤɚ ɜ ɞɟɫɹɬɢɱɧɨɣ ɫɢɫɬɟɦɟ ɫɱɢɫɥɟɧɢɹ, f – ɞɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɜɟɳɟɫɬɜɟɧɧɨɝɨ ɱɢɫɥɚ ɫ ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɬɨɱɤɨɣ, e ɢɥɢ E – ɞɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɱɢɫɥɚ ɫ ɩɥɚɜɚɸɳɟɣ ɬɨɱɤɨɣ, s – ɜɜɨɞ-ɜɵɜɨɞ ɫɬɪɨɤɨɜɵɯ ɞɚɧɧɵɯ, c – ɜɜɨɞ-ɜɵɜɨɞ ɫɢɦɜɨɥɨɜ. Ⱦɥɹ ɮɨɪɦɚɬɧɨɝɨ ɜɵɜɨɞɚ ɫɥɭɠɢɬ ɮɭɧɤɰɢɹ 34
int printf(char* format, ...)
ɢɦɟɸɳɚɹ ɫɩɢɫɨɤ ɩɚɪɚɦɟɬɪɨɜ ɩɟɪɟɦɟɧɧɨɣ ɞɥɢɧɵ. Ʉɨɥɢɱɟɫɬɜɨ ɞɨɩɨɥɧɢɬɟɥɶɧɵɯ ɩɚɪɚɦɟɬɪɨɜ ɞɨɥɠɧɨ ɫɨɨɬɜɟɬɫɬɜɨɜɚɬɶ ɱɢɫɥɭ ɫɩɟɰɢɮɢɤɚɰɢɣ ɮɨɪɦɚɬɚ ɜ ɮɨɪɦɚɬɧɨɣ ɫɬɪɨɤɟ, ɞɚɧɧɵɟ ɞɥɹ ɜɵɜɨɞɚ ɦɨɝɭɬ ɡɚɞɚɜɚɬɶɫɹ ɜɵɪɚɠɟɧɢɹɦɢ, ɨɬɜɟɬɫɬɜɟɧɧɨɫɬɶ ɡɚ ɩɪɚɜɢɥɶɧɨɟ ɡɚɞɚɧɢɟ ɫɩɟɰɢɮɢɤɚɰɢɢ ɮɨɪɦɚɬɚ ɞɥɹ ɤɚɠɞɨɝɨ ɜɵɜɨɞɢɦɨɝɨ ɞɚɧɧɨɝɨ ɩɨɥɧɨɫɬɶɸ ɥɟɠɢɬ ɧɚ ɩɪɨɝɪɚɦɦɢɫɬɟ. ɉɪɢɦɟɪ ɩɪɢɦɟɧɟɧɢɹ ɮɭɧɤɰɢɢ printf(): printf("\n x=%d, y=%f %s",x,y,st);
ɉɪɢ ɜɵɩɨɥɧɟɧɢɢ ɷɬɨɣ ɮɭɧɤɰɢɢ ɩɪɨɫɦɚɬɪɢɜɚɟɬɫɹ ɫɬɪɨɤɚ ɮɨɪɦɚɬɚ ɫɥɟɜɚ ɧɚɩɪɚɜɨ ɢ ɫɢɦɜɨɥɵ, ɧɟ ɹɜɥɹɸɳɢɟɫɹ ɫɩɟɰɢɮɢɤɚɰɢɹɦɢ ɮɨɪɦɚɬɚ, ɤɨɩɢɪɭɸɬɫɹ ɧɚ ɜɵɜɨɞɧɨɟ ɭɫɬɪɨɣɫɬɜɨ. ɍɩɪɚɜɥɹɸɳɢɣ ɫɢɦɜɨɥ «\n» ɩɟɪɟɜɨɞɢɬ ɤɭɪɫɨɪ ɧɚ ɷɤɪɚɧɟ ɤ ɧɚɱɚɥɭ ɫɥɟɞɭɸɳɟɣ ɫɬɪɨɤɢ. Ʉɨɝɞɚ ɜɫɬɪɟɱɚɟɬɫɹ ɫɩɟɰɢɮɢɤɚɰɢɹ ɮɨɪɦɚɬɚ, ɜɵɛɢɪɚɟɬɫɹ ɨɱɟɪɟɞɧɨɣ ɚɪɝɭɦɟɧɬ ɢɡ ɫɩɢɫɤɚ ɜɵɜɨɞɚ, ɩɪɟɨɛɪɚɡɭɟɬɫɹ ɜ ɫɨɨɬɜɟɬɫɬɜɢɢ ɫ ɮɨɪɦɚɬɨɦ ɢ ɜɵɜɨɞɢɬɫɹ ɜ ɨɱɟɪɟɞɧɭɸ ɩɨɡɢɰɢɸ ɷɤɪɚɧɚ. Ⱦɥɹ ɩɪɚɜɢɥɶɧɨɝɨ ɜɵɜɨɞɚ ɬɪɟɛɭɟɬɫɹ, ɱɬɨɛɵ ɩɟɪɟɦɟɧɧɚɹ x ɛɵɥɚ ɬɢɩɚ int, y ɬɢɩɚ float, ɚ ɩɟɪɟɦɟɧɧɚɹ st - ɬɢɩɚ char*. Ɏɨɪɦɚɬɧɵɣ ɜɜɨɞ ɜɵɩɨɥɧɹɟɬ ɮɭɧɤɰɢɹ int scanf(char* format, ...)
ɜ ɤɨɬɨɪɨɣ ɫɩɢɫɨɤ ɜɜɨɞɚ ɞɨɥɠɟɧ ɡɚɞɚɜɚɬɶ ɭɤɚɡɚɬɟɥɢ ɧɚ ɜɜɨɞɢɦɵɟ ɩɟɪɟɦɟɧɧɵɟ. ȿɫɥɢ ɜ ɫɬɪɨɤɟ ɮɨɪɦɚɬɚ ɩɪɢɫɭɬɫɬɜɭɸɬ ɫɢɦɜɨɥɵ, ɧɟ ɜɯɨɞɹɳɢɟ ɜ ɫɩɟɰɢɮɢɤɚɰɢɢ ɮɨɪɦɚɬɨɜ ɢ ɧɟ ɹɜɥɹɸɳɢɟɫɹ ɩɪɨɛɟɥɶɧɵɦɢ, ɬɨ ɜɨ ɜɯɨɞɧɨɦ ɩɨɬɨɤɟ ɞɨɥɠɧɵ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɬɟ ɠɟ ɫɢɦɜɨɥɵ. ɉɪɨɛɟɥɶɧɵɦɢ ɫɢɦɜɨɥɚɦɢ ɫɱɢɬɚɸɬɫɹ ɡɧɚɤɢ ɩɪɨɛɟɥɚ, ɬɚɛɭɥɹɰɢɢ, ɧɨɜɨɣ ɫɬɪɨɤɢ, ɨɧɢ ɫɱɢɬɵɜɚɸɬɫɹ ɢɡ ɩɨɬɨɤɚ ɜɜɨɞɚ, ɧɨ ɧɟ ɭɱɚɫɬɜɭɸɬ ɜ ɮɨɪɦɢɪɨɜɚɧɢɢ ɜɯɨɞɧɵɯ ɞɚɧɧɵɯ. Ʉɨɝɞɚ ɜ ɮɨɪɦɚɬɧɨɣ ɫɬɪɨɤɟ ɜɫɬɪɟɱɚɟɬɫɹ ɫɩɟɰɢɮɢɤɚɰɢɹ ɮɨɪɦɚɬɚ, ɜɨ ɜɯɨɞɧɨɦ ɩɨɬɨɤɟ ɩɪɨɩɭɫɤɚɸɬɫɹ ɩɪɨɛɟɥɶɧɵɟ ɫɢɦɜɨɥɵ, ɚ ɩɨɫɥɟɞɭɸɳɢɟ ɫɢɦɜɨɥɵ ɢɧɬɟɪɩɪɟɬɢɪɭɸɬɫɹ ɜ ɫɨɨɬɜɟɬɫɬɜɢɢ ɫ ɬɢɩɨɦ ɮɨɪɦɚɬɚ ɢ ɩɪɟɨɛɪɚɡɭɸɬɫɹ ɜɨ ɜɧɭɬɪɟɧɧɟɟ ɩɪɟɞɫɬɚɜɥɟɧɢɟ. ɉɨɥɭɱɟɧɧɨɟ ɡɧɚɱɟɧɢɟ ɡɚɩɢɫɵɜɚɟɬɫɹ ɜ ɩɚɦɹɬɶ ɩɨ ɚɞɪɟɫɭ ɨɱɟɪɟɞɧɨɝɨ ɷɥɟɦɟɧɬɚ ɫɩɢɫɤɚ ɜɜɨɞɚ. ɇɚɩɪɢɦɟɪ, ɞɥɹ ɜɜɨɞɚ ɞɜɭɯ ɩɟɪɟɦɟɧɧɵɯ ɰɟɥɨɝɨ ɬɢɩɚ ɢ ɨɞɧɨɣ ɜɟɳɟɫɬɜɟɧɧɨɝɨ ɬɢɩɚ ɦɨɠɧɨ ɩɪɢɦɟɧɢɬɶ ɨɩɟɪɚɬɨɪɚ ɜɵɡɨɜɚ ɮɭɧɤɰɢɢ scanf("%d %d %f", &x1, &x2, &y);
Ɂɞɟɫɶ x1 ɢ x2 ɞɨɥɠɧɵ ɛɵɬɶ ɬɢɩɚ int, ɚ y - ɬɢɩɚ float. ȼɨ ɜɯɨɞɧɨɦ ɩɨɬɨɤɟ ɜɜɨɞɢɦɵɟ ɡɧɚɱɟɧɢɹ ɞɨɥɠɧɵ ɪɚɡɞɟɥɹɬɶɫɹ ɯɨɬɹ ɛɵ ɨɞɧɢɦ ɩɪɨɛɟɥɨɦ. Ȼɨɥɟɟ ɩɨɥɧɭɸ ɢɧɮɨɪɦɚɰɢɸ ɨ ɮɭɧɤɰɢɹɯ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɜ ɫɬɢɥɟ ɋɢ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɜ ɫɩɪɚɜɨɱɧɨɣ ɫɢɫɬɟɦɟ ɢɧɬɟɝɪɢɪɨɜɚɧɧɨɣ ɫɪɟɞɵ Borland C++. ɇɟɞɨɫɬɚɬɤɨɦ ɪɚɫɫɦɨɬɪɟɧɧɵɯ ɜɵɲɟ ɮɭɧɤɰɢɣ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɹɜɥɹɟɬɫɹ ɨɬɫɭɬɫɬɜɢɟ ɤɨɧɬɪɨɥɹ ɫɨɨɬɜɟɬɫɬɜɢɹ ɬɢɩɨɜ ɮɨɪɦɚɬɨɜ ɢ ɬɢɩɨɜ ɜɜɨɞɢɦɵɯ (ɜɵɜɨɞɢɦɵɯ) ɞɚɧɧɵɯ, ɱɚɫɬɨ ɩɪɢɜɨɞɹɳɟɟ ɤ ɨɲɢɛɤɚɦ ɜ ɩɪɨɝɪɚɦɦɚɯ. ȼ ɋɢ++ ɜɤɥɸɱɟɧɵ ɫɨɛɫɬɜɟɧɧɵɟ ɫɪɟɞɫɬɜɚ ɩɨɬɨɤɨɜɨɝɨ ɜɜɨɞɚ-ɜɵɜɨɞɚ, ɨɛɟɫɩɟɱɢɜɚɸɳɢɟ ɠɟɫɬɤɢɣ ɤɨɧɬɪɨɥɶ ɬɢɩɨɜ ɜ ɨɩɟɪɚɰɢɹɯ ɜɜɨɞɚ-ɜɵɜɨɞɚ. Ⱦɥɹ ɷɬɨɝɨ ɨɩɪɟɞɟɥɟɧɵ ɱɟɬɵɪɟ ɧɨɜɵɯ ɫɬɚɧɞɚɪɬɧɵɯ ɩɨɬɨɤɚ: 35
cin – ɞɥɹ ɜɜɨɞɚ ɞɚɧɧɵɯ, cout – ɞɥɹ ɜɵɜɨɞɚ ɞɚɧɧɵɯ, cerr – ɜɵɜɨɞ ɫɨɨɛɳɟɧɢɣ ɨɛ ɨɲɢɛɤɚɯ ɛɟɡ ɛɭɮɟɪɢɡɚɰɢɢ ɜɵɜɨɞɚ, clog – ɜɵɜɨɞ ɫɨɨɛɳɟɧɢɣ ɨɛ ɨɲɢɛɤɚɯ ɫ ɛɭɮɟɪɢɡɚɰɢɟɣ ɜɵɜɨɞɚ. ȼ ɤɚɱɟɫɬɜɟ ɡɧɚɤɚ ɨɩɟɪɚɰɢɢ ɜɵɜɨɞɚ ɨɩɪɟɞɟɥɟɧɵ ɡɧɚɤɢ <<, ɚ ɡɧɚɤɨɦ ɨɩɟɪɚɰɢɢ ɜɜɨɞɚ - ɡɧɚɤɢ >>, ɬɟ ɠɟ, ɱɬɨ ɢ ɞɥɹ ɨɩɟɪɚɰɢɣ ɫɞɜɢɝɚ. Ʉɨɦɩɢɥɹɬɨɪ
ɩɨ ɤɨɧɬɟɤɫɬɭ ɨɩɪɟɞɟɥɹɟɬ, ɤɚɤɭɸ ɨɩɟɪɚɰɢɸ ɡɚɞɚɸɬ ɷɬɢ ɫɢɦɜɨɥɵ, ɜɜɨɞɜɵɜɨɞ ɢɥɢ ɫɞɜɢɝ. ɑɬɨɛɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɪɟɞɫɬɜɚ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɋɢ++ ɜ ɩɪɨɝɪɚɦɦɭ ɞɨɥɠɟɧ ɛɵɬɶ ɜɤɥɸɱɟɧ ɮɚɣɥ-ɡɚɝɨɥɨɜɨɤ iostream.h: #include
ȼ ɨɩɟɪɚɰɢɹɯ ɜɵɜɨɞɚ ɥɟɜɵɦ ɨɩɟɪɚɧɞɨɦ ɞɨɥɠɟɧ ɛɵɬɶ ɩɨɬɨɤ ɜɵɜɨɞɚ, ɩɪɚɜɵɦ ɨɩɟɪɚɧɞɨɦ – ɜɵɜɨɞɢɦɵɟ ɞɚɧɧɵɟ. Ɋɟɡɭɥɶɬɚɬɨɦ ɨɩɟɪɚɰɢɢ ɜɵɜɨɞɚ ɹɜɥɹɟɬɫɹ ɩɨɬɨɤ ɜɵɜɨɞɚ, ɱɬɨ ɩɨɡɜɨɥɹɟɬ ɡɚɩɢɫɵɜɚɬɶ ɜɵɜɨɞ ɜ ɜɢɞɟ ɰɟɩɨɱɤɢ ɨɩɟɪɚɰɢɣ <<, ɧɚɩɪɢɦɟɪ, cout<<"x1="<<x1<<" x2 =" <<x2<<"\n";
Ⱦɥɹ ɛɚɡɨɜɵɯ ɬɢɩɨɜ ɞɚɧɧɵɯ ɨɩɪɟɞɟɥɟɧɵ ɢɯ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɩɪɢ ɜɵɜɨɞɟ ɢ ɬɨɱɧɨɫɬɶ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɧɚ ɭɫɬɪɨɣɫɬɜɟ ɜɵɜɨɞɚ. ɗɬɢ ɯɚɪɚɤɬɟɪɢɫɬɢɤɢ ɦɨɠɧɨ ɦɟɧɹɬɶ, ɩɪɢɦɟɧɹɹ ɫɩɟɰɢɚɥɶɧɵɟ ɮɭɧɤɰɢɢ, ɧɚɡɵɜɚɟɦɵɟ ɦɚɧɢɩɭɥɹɬɨɪɚɦɢ. ȼ ɨɩɟɪɚɰɢɢ ɜɜɨɞɚ ɥɟɜɵɦ ɨɩɟɪɚɧɞɨɦ ɞɨɥɠɟɧ ɛɵɬɶ ɩɨɬɨɤ ɜɜɨɞɚ, ɚ ɩɪɚɜɵɦ ɨɩɟɪɚɧɞɨɦ - ɢɦɹ ɜɜɨɞɢɦɨɝɨ ɞɚɧɧɨɝɨ ɞɥɹ ɚɪɢɮɦɟɬɢɱɟɫɤɢɯ ɞɚɧɧɵɯ ɢɥɢ ɭɤɚɡɚɬɟɥɶ ɬɢɩɚ char* ɞɥɹ ɜɜɨɞɚ ɫɬɪɨɤ, ɧɚɩɪɢɦɟɪ, cin>>x1>>x2>>st;
Ɉɩɟɪɚɰɢɢ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɜɵɩɨɥɧɹɸɬɫɹ ɫɥɟɜɚ ɧɚɩɪɚɜɨ, ɢ ɩɨɫɥɟɞɧɢɣ ɨɩɟɪɚɬɨɪ ɷɤɜɢɜɚɥɟɧɬɟɧ ɨɩɟɪɚɬɨɪɭ ((cin>>x1)>>x2)>>st; ɢɥɢ ɬɪɟɦ ɨɩɟɪɚɬɨɪɚɦ cin >> x1; cin >> x2; cin >> st;
ȼ ɡɚɤɥɸɱɟɧɢɟ ɩɪɢɜɟɞɟɦ ɩɪɢɦɟɪ ɩɪɨɫɬɨɣ ɩɪɨɝɪɚɦɦɵ, ɡɚɩɪɚɲɢɜɚɸɳɟɣ ɭ ɩɨɥɶɡɨɜɚɬɟɥɹ ɞɜɚ ɰɟɥɵɯ ɱɢɫɥɚ ɢ ɜɵɜɨɞɹɳɟɣ ɧɚ ɷɤɪɚɧ ɢɯ ɫɭɦɦɭ: #include int x, y; int main() { cout<<"x="; cin>>x; cout<<"\n y="; cin>>y; cout<<"\n" << "x+y="<<x+y; return 0; }
36
// Ɂɚɩɪɨɫ ɢ ɜɜɨɞ ɡɧɚɱɟɧɢɹ x // Ɂɚɩɪɨɫ ɢ ɜɜɨɞ ɡɧɚɱɟɧɢɹ y
3. ɉɪɟɩɪɨɰɟɫɫɨɪɧɵɟ ɫɪɟɞɫɬɜɚ ɜ C ɢ ɋ++ 3.1. Ɉɫɧɨɜɧɵɟ ɩɨɧɹɬɢɹ ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɣ ɨɛɪɚɛɨɬɤɢ ɉɪɟɩɪɨɰɟɫɫɨɪɧɚɹ ɨɛɪɚɛɨɬɤɚ (ɦɚɤɪɨɨɛɪɚɛɨɬɤɚ) – ɷɬɨ ɩɪɟɨɛɪɚɡɨɜɚɧɢɟ ɬɟɤɫɬɚ ɩɭɬɟɦ ɡɚɦɟɧɵ ɩɪɟɩɪɨɰɟɫɫɨɪɧɵɯ ɩɟɪɟɦɟɧɧɵɯ ɢɯ ɡɧɚɱɟɧɢɹɦɢ ɢ ɜɵɩɨɥɧɟɧɢɹ ɩɪɟɩɪɨɰɟɫɫɨɪɧɵɯ ɨɩɟɪɚɬɨɪɨɜ (ɞɢɪɟɤɬɢɜ ɩɪɟɩɪɨɰɟɫɫɨɪɚ). ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ ɩɪɟɩɪɨɰɟɫɫɨɪɧɵɟ ɫɪɟɞɫɬɜɚ ɜɤɥɸɱɚɸɬ: – ɨɩɪɟɞɟɥɟɧɢɟ ɩɪɟɩɪɨɰɟɫɫɨɪɧɵɯ ɩɟɪɟɦɟɧɧɵɯ ɢ ɩɪɢɫɜɨɟɧɧɵɯ ɢɦ ɡɧɚɱɟɧɢɣ; – ɫɪɟɞɫɬɜɚ ɭɩɪɚɜɥɟɧɢɹ ɩɪɨɫɦɨɬɪɨɦ ɩɪɟɨɛɪɚɡɭɟɦɨɝɨ ɬɟɤɫɬɚ; – ɩɪɚɜɢɥɚ ɩɨɞɫɬɚɧɨɜɤɢ ɡɧɚɱɟɧɢɣ ɦɚɤɪɨɩɟɪɟɦɟɧɧɵɯ. Ɉɩɪɟɞɟɥɟɧɢɟ ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɣ ɩɟɪɟɦɟɧɧɨɣ ɱɚɫɬɨ ɧɚɡɵɜɚɸɬ ɦɚɤɪɨɨɩɪɟɞɟɥɟɧɢɟɦ ɢɥɢ ɦɚɤɪɨɫɨɦ, ɚ ɩɨɞɫɬɚɧɨɜɤɭ ɟɟ ɡɧɚɱɟɧɢɹ ɜ ɨɛɪɚɛɚɬɵɜɚɟɦɵɣ ɬɟɤɫɬ — ɦɚɤɪɨɪɚɫɲɢɪɟɧɢɟɦ. Ɇɚɤɪɨɨɛɪɚɛɨɬɤɚ ɫɨɫɬɨɢɬ ɜ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɦ ɩɪɨɫɦɨɬɪɟ ɢɫɯɨɞɧɨɝɨ ɬɟɤɫɬɚ ɢ ɜɵɞɟɥɟɧɢɢ ɜ ɧɟɦ ɥɟɤɫɟɦ — ɫɤɚɧɢɪɨɜɚɧɢɢ ɬɟɤɫɬɚ. ȿɫɥɢ ɜɵɞɟɥɟɧɧɚɹ ɥɟɤɫɟɦɚ ɹɜɥɹɟɬɫɹ ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɣ ɩɟɪɟɦɟɧɧɨɣ, ɨɧɚ ɡɚɦɟɧɹɟɬɫɹ ɫɜɨɢɦ ɡɧɚɱɟɧɢɟɦ, ɬɨ ɟɫɬɶ ɫɬɪɨɢɬɫɹ ɦɚɤɪɨɪɚɫɲɢɪɟɧɢɟ. ȿɫɥɢ ɜɫɬɪɟɱɚɟɬɫɹ ɩɪɟɩɪɨɰɟɫɫɨɪɧɚɹ ɞɢɪɟɤɬɢɜɚ, ɬɨ ɨɧɚ ɜɵɩɨɥɧɹɟɬɫɹ. Ʌɟɤɫɟɦɵ, ɧɟ ɹɜɥɹɸɳɢɟɫɹ ɩɪɟɩɪɨɰɟɫɫɨɪɧɵɦɢ ɩɟɪɟɦɟɧɧɵɦɢ ɢɥɢ ɞɢɪɟɤɬɢɜɚɦɢ, ɩɟɪɟɧɨɫɹɬɫɹ ɜ ɜɵɯɨɞɧɨɣ ɬɟɤɫɬ ɛɟɡ ɢɡɦɟɧɟɧɢɹ. Ɋɟɡɭɥɶɬɚɬɨɦ ɬɚɤɨɣ ɨɛɪɚɛɨɬɤɢ ɹɜɥɹɟɬɫɹ ɬɟɤɫɬ, ɧɟ ɫɨɞɟɪɠɚɳɢɣ ɩɪɟɩɪɨɰɟɫɫɨɪɧɵɯ ɞɢɪɟɤɬɢɜ ɢ ɩɪɟɩɪɨɰɟɫɫɨɪɧɵɯ ɩɟɪɟɦɟɧɧɵɯ. ȿɫɥɢ ɢɫɯɨɞɧɵɣ ɬɟɤɫɬ ɛɵɥ ɩɪɨɝɪɚɦɦɨɣ ɧɚ C ɢɥɢ C++, ɬɨ ɩɨɫɥɟ ɦɚɤɪɨɨɛɪɚɛɨɬɤɢ ɞɨɥɠɟɧ ɛɵɬɶ ɩɨɥɭɱɟɧ ɫɢɧɬɚɤɫɢɱɟɫɤɢ ɩɪɚɜɢɥɶɧɵɣ ɬɟɤɫɬ ɧɚ C ɢɥɢ C++. Ʉɚɤ ɩɪɚɜɢɥɨ, ɫɬɪɨɤɨɜɵɟ ɥɢɬɟɪɚɥɵ (ɫɬɪɨɤɢ ɜ ɤɚɜɵɱɤɚɯ) ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɩɪɟɩɪɨɰɟɫɫɨɪɨɦ ɤɚɤ ɨɬɞɟɥɶɧɵɟ ɥɟɤɫɟɦɵ ɢ ɩɟɪɟɧɨɫɹɬɫɹ ɜ ɜɵɯɨɞɧɨɣ ɬɟɤɫɬ ɛɟɡ ɢɡɦɟɧɟɧɢɹ. ɉɪɟɩɪɨɰɟɫɫɨɪ ɨɛɵɱɧɨ ɨɛɟɫɩɟɱɢɜɚɟɬ ɜɨɡɦɨɠɧɨɫɬɶ ɜɤɥɸɱɟɧɢɹ ɜ ɩɪɨɝɪɚɦɦɭ ɢɫɯɨɞɧɵɯ ɬɟɤɫɬɨɜ ɢɡ ɞɪɭɝɢɯ ɮɚɣɥɨɜ, ɜ ɋɢ/ɋɢ++ ɷɬɨ ɜɵɩɨɥɧɹɟɬɫɹ ɩɨ ɞɢɪɟɤɬɢɜɟ #include ɢɦɹ_ɮɚɣɥɚ
ȿɫɥɢ ɜɤɥɸɱɚɟɦɵɣ ɮɚɣɥ ɧɚɯɨɞɢɬɫɹ ɜ ɨɞɧɨɦ ɢɡ ɨɝɥɚɜɥɟɧɢɣ, ɭɤɚɡɵɜɚɟɦɵɯ ɜ ɭɫɬɚɧɨɜɤɚɯ ɢɧɬɟɝɪɢɪɨɜɚɧɧɨɣ ɫɪɟɞɵ ɜ ɩɭɧɤɬɟ options–directoryinclude, ɝɞɟ ɦɨɠɧɨ ɭɤɚɡɚɬɶ ɧɟɫɤɨɥɶɤɨ ɩɭɬɟɣ, ɪɚɡɞɟɥɹɹ ɢɯ ɬɨɱɤɨɣ ɫ ɡɚɩɹɬɨɣ, ɢɦɹ ɮɚɣɥɚ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɭɝɨɥɤɨɜɵɟ ɤɚɜɵɱɤɢ, ɧɚɩɪɢɦɟɪ: #include
ɜ ɨɫɬɚɥɶɧɵɯ ɫɥɭɱɚɹɯ ɭɤɚɡɵɜɚɟɬɫɹ ɩɨɥɧɵɣ ɩɭɬɶ ɤ ɜɤɥɸɱɚɟɦɨɦɭ ɮɚɣɥɭ ɜ ɤɚɜɵɱɤɚɯ: #include “c:\myinclud\includ1.h”
ɉɪɢ ɜɤɥɸɱɟɧɢɢ ɮɚɣɥɚ ɧɚ ɦɟɫɬɨ ɞɢɪɟɤɬɢɜɵ #include ɜɫɬɚɜɥɹɟɬɫɹ ɬɟɤɫɬ ɢɡ ɷɬɨɝɨ ɮɚɣɥɚ, ɚ ɩɨɫɥɟɞɭɸɳɢɟ ɫɬɪɨɤɢ ɢɫɯɨɞɧɨɝɨ ɮɚɣɥɚ ɫɞɜɢɝɚɸɬɫɹ ɜɧɢɡ. ɉɨɫɥɟ ɷɬɨɝɨ ɜɫɬɚɜɥɟɧɧɵɣ ɬɟɤɫɬ ɫɤɚɧɢɪɭɟɬɫɹ ɩɪɟɩɪɨɰɟɫɫɨɪɨɦ. 37
Ɉɬɦɟɬɢɦ, ɱɬɨ ɞɢɪɟɤɬɢɜɵ ɩɪɟɩɪɨɰɟɫɫɨɪɚ ɜɫɟɝɞɚ ɡɚɩɢɫɵɜɚɸɬɫɹ ɫ ɧɨɜɨɣ ɫɬɪɨɤɢ ɢ ɩɟɪɜɵɦ ɫɢɦɜɨɥɨɦ ɞɢɪɟɤɬɢɜɵ ɞɨɥɠɟɧ ɛɵɬɶ ɡɧɚɤ «#», ɤɨɬɨɪɨɦɭ ɦɨɝɭɬ ɩɪɟɞɲɟɫɬɜɨɜɚɬɶ ɬɨɥɶɤɨ ɩɪɨɛɟɥɵ ɢ ɡɧɚɤɢ ɬɚɛɭɥɹɰɢɢ. Ʉɨɧɰɨɦ ɬɟɤɫɬɚ ɞɢɪɟɤɬɢɜɵ ɫɥɭɠɢɬ ɤɨɧɟɰ ɫɬɪɨɤɢ. ȿɫɥɢ ɞɢɪɟɤɬɢɜɚ ɧɟ ɩɨɦɟɳɚɟɬɫɹ ɜ ɨɞɧɨɣ ɫɬɪɨɤɟ, ɜ ɤɨɧɰɟ ɫɬɪɨɤɢ ɫɬɚɜɢɬɫɹ ɡɧɚɤ «\», ɢ ɞɢɪɟɤɬɢɜɚ ɩɪɨɞɨɥɠɚɟɬɫɹ ɧɚ ɫɥɟɞɭɸɳɟɣ ɫɬɪɨɤɟ. Ʉɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ ɩɪɨɞɨɥɠɟɧɢɹ ɧɟ ɨɝɪɚɧɢɱɢɜɚɟɬɫɹ.
3.2. ɉɪɟɩɪɨɰɟɫɫɨɪɧɵɟ ɩɟɪɟɦɟɧɧɵɟ ɉɪɟɩɪɨɰɟɫɫɨɪɧɚɹ ɩɟɪɟɦɟɧɧɚɹ (ɦɚɤɪɨɢɦɹ) ɨɛɴɹɜɥɹɟɬɫɹ ɞɢɪɟɤɬɢɜɨɣ #define ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɡɧɚɱɟɧɢɟ
ɇɚɩɪɢɦɟɪ: #define L_NAME 6 #define END_FORMULA ‘;’ #define DEBUG
ȿɫɥɢ ɨɛɴɹɜɥɟɧɧɨɟ ɬɚɤɢɦ ɫɩɨɫɨɛɨɦ ɦɚɤɪɨɢɦɹ ɜɫɬɪɟɬɢɬɫɹ ɜ ɩɨɫɥɟɞɭɸɳɟɦ ɬɟɤɫɬɟ ɩɪɨɝɪɚɦɦɵ, ɨɧɨ ɛɭɞɟɬ ɡɚɦɟɧɟɧɨ ɧɚ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɟ ɡɧɚɱɟɧɢɟ: char namevar [L_NAME]; // ɷɤɜɢɜɚɥɟɧɬɧɨ char namevar[6]; if (c!=END_FORMULA) ... // if ( c != ‘;’) ... ɉɟɪɟɦɟɧɧɚɹ DEBUG ɨɛɴɹɜɥɟɧɚ, ɧɨ ɧɟ ɢɦɟɟɬ ɡɧɚɱɟɧɢɹ. ȼ ɩɨɫɥɟɞɭɸɳɟɦ
ɬɟɤɫɬɟ ɦɨɠɧɨ ɩɪɨɜɟɪɹɬɶ, ɨɛɴɹɜɥɟɧɨ ɢɥɢ ɧɟɬ ɷɬɨ ɢɦɹ ɢ ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɪɟɡɭɥɶɬɚɬɚ ɩɪɨɜɟɪɤɢ ɜɤɥɸɱɚɬɶ ɢɥɢ ɧɟ ɜɤɥɸɱɚɬɶ ɜ ɩɪɨɝɪɚɦɦɭ ɧɟɤɨɬɨɪɵɟ ɨɩɟɪɚɬɨɪɵ. Ɉɛɴɹɜɥɟɧɧɨɟ ɜ define ɦɚɤɪɨɢɦɹ ɢɡɜɟɫɬɧɨ ɩɪɟɩɪɨɰɟɫɫɨɪɭ ɨɬ ɬɨɱɤɢ ɟɝɨ ɨɛɴɹɜɥɟɧɢɹ ɞɨ ɤɨɧɰɚ ɮɚɣɥɚ ɢɥɢ ɩɨɤɚ ɧɟ ɜɫɬɪɟɬɢɬɫɹ ɞɢɪɟɤɬɢɜɚ #undef ɢɦɹ
ɇɚɩɪɢɦɟɪ, #undef DEBUG. ȿɫɥɢ ɜ ɩɨɫɥɟɞɭɸɳɟɦ ɬɟɤɫɬɟ ɜɫɬɪɟɬɢɬɫɹ ɢɦɹ DEBUG, ɨɧɨ ɛɭɞɟɬ ɪɚɫɫɦɚɬɪɢɜɚɬɶɫɹ ɤɚɤ ɨɛɵɱɧɨɟ, ɚ ɧɟ ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɟ ɢɦɹ. ɂɦɟɟɬɫɹ ɪɹɞ ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɵɯ ɦɚɤɪɨɢɦɟɧ, ɩɪɟɞɭɫɦɨɬɪɟɧɧɵɯ ɫɬɚɧɞɚɪɬɚɦɢ ɹɡɵɤɨɜ C ɢ C++, ɜ ɬɨɦ ɱɢɫɥɟ: __LINE__: ɧɨɦɟɪ ɫɬɪɨɤɢ ɜ ɢɫɯɨɞɧɨɦ ɮɚɣɥɟ, __FILE__: ɢɦɹ ɨɛɪɚɛɚɬɵɜɚɟɦɨɝɨ ɮɚɣɥɚ, __DATE__: ɞɚɬɚ ɧɚɱɚɥɚ ɨɛɪɚɛɨɬɤɢ ɩɪɟɩɪɨɰɟɫɫɨɪɨɦ, __TIME__: ɜɪɟɦɹ ɧɚɱɚɥɚ ɨɛɪɚɛɨɬɤɢ, __STDC__: ɩɪɨɝɪɚɦɦɚ ɞɨɥɠɧɚ ɫɨɨɬɜɟɬɫɬɜɨɜɚɬɶ ɫɬɚɧɞɚɪɬɭ ANSI, __cplusplus: ɤɨɦɩɢɥɢɪɨɜɚɬɶ ɩɪɨɝɪɚɦɦɭ ɜ ɫɨɨɬɜɟɬɫɬɜɢɢ ɫ ɫɢɧɬɚɤɫɢɫɨɦ ɋɢ++, __PASCAL__: ɩɨɫɥɟɞɭɸɳɢɟ ɢɦɟɧɚ ɩɨ ɭɦɨɥɱɚɧɢɸ ɢɦɟɸɬ ɬɢɩ «ɢɦɹ ɹɡɵɤɚ Pascal» ɉɪɟɞɨɩɪɟɞɟɥɟɧɧɵɟ ɢɦɟɧɚ ɧɟɥɶɡɹ ɨɛɴɹɜɥɹɬɶ ɜ #define ɢɥɢ ɨɬɦɟɧɹɬɶ ɜ #undef.
38
Ɇɚɤɪɨɫɵ FILE, DATE ɢ TIME ɦɨɝɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɜ ɫɨɨɛɳɟɧɢɹɯ, ɜɵɞɚɜɚɟɦɵɯ ɜ ɧɚɱɚɥɟ ɩɪɨɝɪɚɦɦɵ, ɞɥɹ ɭɤɚɡɚɧɢɹ, ɤɚɤɚɹ ɜɟɪɫɢɹ ɩɪɨɝɪɚɦɦɵ ɢɫɩɨɥɶɡɭɟɬɫɹ, ɧɚɩɪɢɦɟɪ cout<<“\n Ɏɚɣɥ “<<__FILE__<<“Ɉɬ “<<__DATE__<<“\n”;
Ɇɚɤɪɨɫ PASCAL ɩɪɢɦɟɧɹɟɬɫɹ ɩɪɢ ɨɩɢɫɚɧɢɢ ɮɭɧɤɰɢɣ, ɩɪɟɞɧɚɡɧɚɱɟɧɧɵɯ ɞɥɹ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɜ ɩɪɨɝɪɚɦɦɚɯ, ɧɚɩɢɫɚɧɧɵɯ ɧɚ ɹɡɵɤɟ Pascal, ɚ ɬɚɤɠɟ ɮɭɧɤɰɢɣ, ɜɵɡɵɜɚɟɦɵɯ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɨɣ MS Windows. ɉɪɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɢ ɧɚ ɋɢ ɞɢɪɟɤɬɢɜɵ ɬɢɩɚ #define MAX_LEN 80
ɨɛɵɱɧɨ ɩɪɢɦɟɧɹɸɬɫɹ ɞɥɹ ɡɚɞɚɧɢɹ ɢɦɟɧɨɜɚɧɧɵɯ ɤɨɧɫɬɚɧɬ. ȼɜɟɞɟɧɢɟ ɨɩɢɫɚɬɟɥɹ const ɜ ɩɨɫɥɟɞɧɢɟ ɜɟɪɫɢɢ ɋɢ ɢ ɜ ɋɢ++ ɩɨɡɜɨɥɹɟɬ ɨɩɪɟɞɟɥɹɬɶ ɢɦɟɧɨɜɚɧɧɵɟ ɤɨɧɫɬɚɧɬɵ ɬɚɤ ɠɟ, ɤɚɤ ɢ ɨɛɵɱɧɵɟ ɩɟɪɟɦɟɧɧɵɟ.
3.3. Ɇɚɤɪɨɨɩɪɟɞɟɥɟɧɢɹ (ɦɚɤɪɨɫɵ) Ɋɚɫɫɦɨɬɪɟɧɧɵɣ ɜɵɲɟ ɜɚɪɢɚɧɬ ɞɢɪɟɤɬɢɜɵ #define ɹɜɥɹɟɬɫɹ ɱɚɫɬɧɵɦ ɫɥɭɱɚɟɦ. ɉɨɥɧɵɣ ɫɢɧɬɚɤɫɢɫ ɷɬɨɣ ɞɢɪɟɤɬɢɜɵ ɢɦɟɟɬ ɜɢɞ: #define ɢɞɟɧɬɢɮɢɤɚɬɨɪ(ɩɚɪɚɦɟɬɪɵ) ɫɩɢɫɨɤ_ɡɚɦɟɧɵ
ɉɚɪɚɦɟɬɪɵ ɡɚɞɚɸɬɫɹ ɢɯ ɢɦɟɧɚɦɢ, ɫɩɢɫɨɤ ɡɚɦɟɧɵ - ɷɬɨ ɬɟɤɫɬ ɧɚ C, C++, ɫɨɞɟɪɠɚɳɢɣ ɢɦɟɧɚ ɩɚɪɚɦɟɬɪɨɜ, ɧɚɩɪɢɦɟɪ: #define MAX(a,b) ((a)>(b))?(a):(b) #define PRINT(a) cout<<#a<<“=“<<(a)<<endl;
ȿɫɥɢ ɜ ɨɛɥɚɫɬɢ ɞɟɣɫɬɜɢɹ ɷɬɢɯ ɦɚɤɪɨɨɩɪɟɞɟɥɟɧɢɣ ɜɫɬɪɟɬɢɬɫɹ ɬɟɤɫɬ x=MAX(y+p, s);
ɬɨ ɨɧ ɛɭɞɟɬ ɡɚɦɟɧɟɧ ɧɚ ɜɵɪɚɠɟɧɢɟ x=((y+p)>(s))?(y+p):(s);
Ɉɩɟɪɚɬɨɪ PRINT(x) ɛɭɞɟɬ ɡɚɦɟɧɟɧ ɧɚ cout<<“x”<<“=“<<(x)<<endl;
Ɂɧɚɤ «#» ɩɟɪɟɞ ɢɦɟɧɟɦ ɩɚɪɚɦɟɬɪɚ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɡɧɚɱɟɧɢɟ ɚɪɝɭɦɟɧɬɚ ɪɚɫɫɦɚɬɪɢɜɚɟɬɫɹ ɤɚɤ ɫɬɪɨɤɨɜɵɣ ɥɢɬɟɪɚɥ. ȿɫɥɢ ɦɟɠɞɭ ɞɜɭɦɹ ɩɚɪɚɦɟɬɪɚɦɢ ɜ ɦɚɤɪɨɪɚɫɲɢɪɟɧɢɢ ɫɬɨɹɬ ɡɧɚɤɢ «##», ɬɨ ɡɧɚɱɟɧɢɹ ɚɪɝɭɦɟɧɬɨɜ ɫɰɟɩɥɹɸɬɫɹ ɩɨ ɩɪɚɜɢɥɭ ɫɰɟɩɥɟɧɢɹ ɫɬɪɨɤ, ɧɚɩɪɢɦɟɪ, #define VAR(a,b) (a##b) x=d[VAR(i,j)];
// x=d[(ij)];
ɂɫɩɨɥɶɡɨɜɚɧɢɟ ɦɚɤɪɨɫɨɜ ɜ ɪɹɞɟ ɫɥɭɱɚɟɜ ɩɨɡɜɨɥɹɟɬ ɫɨɤɪɚɬɢɬɶ ɢɫɯɨɞɧɵɣ ɬɟɤɫɬ ɩɪɨɝɪɚɦɦɵ ɢ ɫɞɟɥɚɬɶ ɟɝɨ ɛɨɥɟɟ ɧɚɝɥɹɞɧɵɦ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɩɨɦɟɫɬɢɬɶ ɜ ɮɚɣɥ-ɡɚɝɨɥɨɜɨɤ ɦɚɤɪɨɫɵ #if defined(__cplusplus) #define _PTRDEF(name) typedef name *P##name; #define _REFDEF(name) typedef name &R##name; #define _STRUCTDEF(name) struct name; #endif
ɬɨ ɦɵ ɩɨɥɭɱɢɦ ɜɨɡɦɨɠɧɨɫɬɶ ɨɞɧɨɣ ɫɬɪɨɤɨɣ ɩɪɨɝɪɚɦɦɵ _STRUCTDEF(MyStruct)
39
ɨɛɴɹɜɢɬɶ ɢɦɹ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ MyStruct, ɭɤɚɡɚɬɟɥɶ ɧɚ ɷɬɨɬ ɬɢɩ PMyStruct ɢ ɬɢɩ ɫɫɵɥɤɢ ɧɚ ɧɟɝɨ RMyStruct, ɬɨ ɟɫɬɶ ɩɨɥɭɱɢɬɶ ɜ ɜɵɯɨɞɧɨɦ ɬɟɤɫɬɟ ɫɬɪɨɱɤɢ struct MyStruct; typedef MyStruct *PMyStruct; typedef MyStruct &RMystruct;
3.4. ɍɫɥɨɜɧɚɹ ɤɨɦɩɢɥɹɰɢɹ Ⱦɢɪɟɤɬɢɜɵ ɩɪɟɩɪɨɰɟɫɫɨɪɚ #if, #else, #endif ɢ #elif ɩɨɡɜɨɥɹɸɬ, ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɪɟɡɭɥɶɬɚɬɨɜ ɩɪɨɜɟɪɤɢ ɧɟɤɨɬɨɪɵɯ ɭɫɥɨɜɢɣ, ɜɤɥɸɱɚɬɶ ɜ ɩɪɨɝɪɚɦɦɭ ɨɞɢɧ ɢɡ ɧɟɫɤɨɥɶɤɢɯ ɜɚɪɢɚɧɬɨɜ ɬɟɤɫɬɚ: #if ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɟ_ɭɫɥɨɜɢɟ ɬɟɤɫɬ 1 # else ɬɟɤɫɬ 2 #endif ɞɚɥɶɧɟɣɲɢɣ ɬɟɤɫɬ. ɍɫɥɨɜɢɟ — ɷɬɨ ɤɨɧɫɬɚɧɬɧɨɟ ɜɵɪɚɠɟɧɢɟ, ɤɨɬɨɪɨɟ ɫɬɪɨɢɬɫɹ ɢɡ ɦɚɤɪɨɢɦɟɧ, ɤɨɧɫɬɚɧɬ ɢ ɡɧɚɤɨɜ ɨɩɟɪɚɰɢɣ, ɜɤɥɸɱɚɹ ɥɨɝɢɱɟɫɤɢɟ ɫɜɹɡɤɢ «&&» ɢ «||». Ⱦɨɩɭɫɤɚɟɬɫɹ ɬɚɤɠɟ ɢɫɩɨɥɶɡɨɜɚɧɢɟ ɜɵɪɚɠɟɧɢɹ sizeof(ɢɦɹ_ɬɢɩɚ) ɢ ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɣ ɮɭɧɤɰɢɢ defined(ɦɚɤɪɨɢɦɹ), ɜɨɡɜɪɚɳɚɸɳɟɣ 1, ɟɫɥɢ
ɷɬɨ ɦɚɤɪɨɢɦɹ ɨɩɪɟɞɟɥɟɧɨ, ɢ 0, ɟɫɥɢ ɨɧɨ ɧɟ ɨɩɪɟɞɟɥɟɧɨ. ȼɦɟɫɬɨ ɞɢɪɟɤɬɢɜɵ #if defined(DEBUG)
ɦɨɠɧɨ ɧɚɩɢɫɚɬɶ #ifdef DEBUG
ȼɦɟɫɬɨ #if !defined(DEBUG)
ɦɨɠɧɨ ɧɚɩɢɫɚɬɶ #ifndef DEBUG
Ʉɨɦɛɢɧɚɰɢɢ #if - #else ɦɨɝɭɬ ɛɵɬɶ ɜɥɨɠɟɧɧɵɦɢ, ɩɪɢɱɟɦ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ #else - #if ɡɚɦɟɧɹɟɬɫɹ ɨɞɧɨɣ ɞɢɪɟɤɬɢɜɨɣ #elif ɫ ɭɫɥɨɜɢɟɦ: #if ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɟ_ɭɫɥɨɜɢɟ_1 ɬɟɤɫɬ 1 #elif ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɟ_ɭɫɥɨɜɢɟ_2 ɬɟɤɫɬ_2 #else
40
ɬɟɤɫɬ_3 #endif
ȼ ɮɚɣɥɚɯ ɡɚɝɨɥɨɜɤɨɜ ɞɥɹ ɩɪɟɞɨɬɜɪɚɳɟɧɢɹ ɦɧɨɝɨɤɪɚɬɧɨɝɨ ɜɤɥɸɱɟɧɢɹ ɨɞɧɨɝɨ ɢ ɬɨɝɨ ɠɟ ɡɚɝɨɥɨɜɤɚ ɜ ɩɪɨɝɪɚɦɦɭ ɨɛɵɱɧɨ ɩɪɢɫɭɬɫɬɜɭɟɬ ɬɟɤɫɬ ɜɢɞɚ: #if !defined(_DEFS_H) #define _DEFS_H /* Ɍɟɤɫɬ ɨɛɴɹɜɥɹɟɦɵɯ ɡɚɝɨɥɨɜɤɨɜ */ ... #endif /* Ʉɨɧɟɰ _DEFS_H */
4. Ɉɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɟ ɫɪɟɞɫɬɜɚ C++ 4.1. Ɉɛɴɟɤɬɧɵɟ ɬɢɩɵ ɞɚɧɧɵɯ Ɉɛɴɟɤɬɧɵɟ ɬɢɩɵ ɞɚɧɧɵɯ – ɷɬɨ ɚɝɪɟɝɚɬɧɵɟ ɬɢɩɵ, ɩɨɥɧɨɫɬɶɸ ɨɩɪɟɞɟɥɹɟɦɵɟ ɩɪɨɝɪɚɦɦɢɫɬɨɦ, ɨɩɢɫɚɧɢɟ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ ɞɨɥɠɧɨ ɫɨɞɟɪɠɚɬɶ ɤɨɦɩɨɧɟɧɬɵ-ɞɚɧɧɵɟ, ɨɩɪɟɞɟɥɹɸɳɢɟ ɨɛɥɚɫɬɶ ɜɨɡɦɨɠɧɵɯ ɡɧɚɱɟɧɢɣ ɩɟɪɟɦɟɧɧɵɯ ɷɬɨɝɨ ɬɢɩɚ, ɢ ɨɩɢɫɚɧɢɟ ɨɩɟɪɚɰɢɣ, ɞɨɩɭɫɬɢɦɵɯ ɧɚɞ ɩɟɪɟɦɟɧɧɵɦɢ ɷɬɨɝɨ ɬɢɩɚ ɢ ɤɨɦɩɨɧɟɧɬɚɦɢ-ɞɚɧɧɵɦɢ, ɫɨɫɬɚɜɥɹɸɳɢɦɢ ɩɟɪɟɦɟɧɧɭɸ. Ⱦɥɹ ɫɨɯɪɚɧɟɧɢɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɫ ɩɪɨɝɪɚɦɦɚɦɢ ɧɚ ɋɢ ɫɢɧɬɚɤɫɢɫ ɨɩɢɫɚɧɢɹ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ ɜ ɋɢ++ ɜɵɛɪɚɧ ɩɨɞɨɛɧɵɦ ɨɩɢɫɚɧɢɸ ɫɬɪɭɤɬɭɪɧɨɝɨ ɬɢɩɚ ɢɥɢ ɬɢɩɚ ɨɛɴɟɞɢɧɟɧɢɹ ɜ ɋɢ. ȼ ɫɭɳɧɨɫɬɢ ɫɬɪɭɤɬɭɪɵ ɢ ɨɛɴɟɞɢɧɟɧɢɹ ɜ ɋɢ++ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɤɚɤ ɜɚɪɢɚɧɬɵ ɨɛɴɟɤɬɧɵɯ ɬɢɩɨɜ. ɂɦɟɸɬɫɹ ɬɪɢ ɜɚɪɢɚɧɬɚ ɨɛɴɟɤɬɧɵɯ ɬɢɩɨɜ: ɫɬɪɭɤɬɭɪɚ (struct), ɨɛɴɟɞɢɧɟɧɢɟ (union) ɢ ɤɥɚɫɫ (class), ɪɚɡɥɢɱɚɸɳɢɟɫɹ ɜɨɡɦɨɠɧɨɫɬɹɦɢ ɞɨɫɬɭɩɚ ɤ ɤɨɦɩɨɧɟɧɬɚɦ ɬɢɩɚ. ȼ ɞɚɥɶɧɟɣɲɟɦ ɞɥɹ ɤɪɚɬɤɨɫɬɢ ɜɫɟ ɜɚɪɢɚɧɬɵ ɨɛɴɟɤɬɧɵɯ ɬɢɩɨɜ ɛɭɞɟɦ ɧɚɡɵɜɚɬɶ ɤɥɚɫɫɚɦɢ. Ɉɩɢɫɚɧɢɟ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: ɜɚɪɢɚɧɬ_ɬɢɩɚ ɢɦɹ_ɬɢɩɚ: ɫɩɢɫɨɤ_ɛɚɡɨɜɵɯ_ɤɥɚɫɫɨɜ {ɤɨɦɩɨɧɟɧɬɵ (ɱɥɟɧɵ) ɤɥɚɫɫɚ}
Ʉɨɦɩɨɧɟɧɬɚɦɢ ɤɥɚɫɫɚ ɦɨɝɭɬ ɛɵɬɶ ɤɨɦɩɨɧɟɧɬɵ-ɞɚɧɧɵɟ ɢ ɤɨɦɩɨɧɟɧɬɵɮɭɧɤɰɢɢ. Ʉɨɦɩɨɧɟɧɬɵ-ɮɭɧɤɰɢɢ ɩɪɟɞɧɚɡɧɚɱɟɧɵ ɞɥɹ ɜɵɩɨɥɧɟɧɢɹ ɨɩɟɪɚɰɢɣ ɧɚɞ ɨɛɴɟɤɬɧɵɦɢ ɞɚɧɧɵɦɢ, ɢɯ ɱɚɫɬɨ ɧɚɡɵɜɚɸɬ ɦɟɬɨɞɚɦɢ ɤɥɚɫɫɚ. Ʉɨɦɩɨɧɟɧɬɵ-ɞɚɧɧɵɟ ɧɚɡɵɜɚɸɬ ɫɜɨɣɫɬɜɚɦɢ ɤɥɚɫɫɚ. Ⱦɥɹ ɤɚɠɞɨɝɨ ɤɨɦɩɨɧɟɧɬɚ ɤɥɚɫɫɚ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɭɪɨɜɟɧɶ ɞɨɫɬɭɩɚ ɥɢɛɨ ɹɜɧɨ, ɭɤɚɡɚɧɢɟɦ ɭɪɨɜɧɹ ɞɨɫɬɭɩɚ ɨɞɧɢɦ ɢɡ ɤɥɸɱɟɜɵɯ ɫɥɨɜ public, protected ɢɥɢ private ɫ ɞɜɨɟɬɨɱɢɟɦ, ɥɢɛɨ ɧɟɹɜɧɨ, ɩɨ ɭɦɨɥɱɚɧɢɸ. ɍɤɚɡɚɧɢɟ ɭɪɨɜɧɹ ɞɨɫɬɭɩɚ ɨɬɧɨɫɢɬɫɹ ɤɨ ɜɫɟɦ ɩɨɫɥɟɞɭɸɳɢɦ ɤɨɦɩɨɧɟɧɬɚɦ ɤɥɚɫɫɚ, ɩɨɤɚ ɧɟ ɜɫɬɪɟɬɢɬɫɹ ɭɤɚɡɚɧɢɟ ɞɪɭɝɨɝɨ ɭɪɨɜɧɹ ɞɨɫɬɭɩɚ. ɍɪɨɜɟɧɶ ɞɨɫɬɭɩɚ public ɪɚɡɪɟɲɚɟɬ ɞɨɫɬɭɩ ɤ ɤɨɦɩɨɧɟɧɬɚɦ ɤɥɚɫɫɚ ɢɡ ɥɸɛɨɝɨ ɦɟɫɬɚ ɩɪɨɝɪɚɦɦɵ, ɜ ɤɨɬɨɪɨɦ ɢɡɜɟɫɬɧɚ ɩɟɪɟɦɟɧɧɚɹ ɷɬɨɝɨ ɤɥɚɫɫɚ. ɍɪɨɜɟɧɶ ɞɨɫɬɭɩɚ private ɪɚɡɪɟɲɚɟɬ ɞɨɫɬɭɩ ɤ ɤɨɦɩɨɧɟɧɬɚɦ ɤɥɚɫɫɚ ɬɨɥɶɤɨ ɢɡ ɦɟɬɨɞɨɜ ɷɬɨɝɨ ɤɥɚɫɫɚ. ɍɪɨɜɟɧɶ ɞɨɫɬɭɩɚ protected ɢɦɟɟɬ ɫɦɵɫɥ ɬɨɥɶɤɨ ɜ ɢɟɪɚɪɯɢɱɟɫɤɨɣ ɫɢɫɬɟɦɟ 41
ɤɥɚɫɫɨɜ ɢ ɪɚɡɪɟɲɚɟɬ ɞɨɫɬɭɩ ɤ ɤɨɦɩɨɧɟɧɬɚɦ ɷɬɨɝɨ ɭɪɨɜɧɹ ɢɡ ɦɟɬɨɞɨɜ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ. ɉɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɜɫɟɯ ɤɨɦɩɨɧɟɧɬ ɤɥɚɫɫɚ ɬɢɩɚ struct ɩɪɢɧɢɦɚɟɬɫɹ ɭɪɨɜɟɧɶ ɞɨɫɬɭɩɚ public, ɧɨ ɦɨɠɧɨ ɹɜɧɨ ɡɚɞɚɜɚɬɶ ɢ ɞɪɭɝɢɟ ɭɪɨɜɧɢ ɞɨɫɬɭɩɚ, ɭɪɨɜɟɧɶ ɞɨɫɬɭɩɚ ɤ ɤɨɦɩɨɧɟɧɬɚɦ ɤɥɚɫɫɚ ɬɢɩɚ class ɩɨ ɭɦɨɥɱɚɧɢɸ private, ɞɥɹ ɤɥɚɫɫɚ ɬɢɩɚ union ɭɪɨɜɟɧɶ ɞɨɫɬɭɩɚ public, ɤɨɬɨɪɵɣ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɢɡɦɟɧɟɧ. ɇɚɩɪɢɦɟɪ, ɩɭɫɬɶ ɩɪɨɝɪɚɦɦɢɫɬ ɪɟɲɢɥ ɜ ɤɥɚɫɫɟ TPoint (ɬɨɱɤɚ) ɡɚɩɪɟɬɢɬɶ ɜɧɟɲɧɢɣ ɞɨɫɬɭɩ ɤ ɤɨɨɪɞɢɧɚɬɚɦ ɬɨɱɤɢ ɢ ɪɚɡɪɟɲɢɬɶ ɜɧɟɲɧɢɣ ɞɨɫɬɭɩ ɤ ɦɟɬɨɞɚɦ ɩɟɪɟɦɟɳɟɧɢɹ ɬɨɱɤɢ ɧɚ ɩɥɨɫɤɨɫɬɢ. Ɉɩɢɫɚɧɢɟ ɤɥɚɫɫɚ TPoint ɦɨɠɧɨ ɩɨɫɬɪɨɢɬɶ ɬɚɤ: class TPoint { private: int x,y; public: void movePoint(int newx, int newy); void relmove (int dx, int dy); int getx() {return x;}; int gety() {return y;}; };
// ɩɟɪɟɧɨɫ ɬɨɱɤɢ // ɫɞɜɢɝ ɬɨɱɤɢ
Ɉɩɢɫɚɧɢɟ ɬɟɥɚ ɤɨɦɩɨɧɟɧɬɵ-ɮɭɧɤɰɢɢ ɦɨɠɟɬ ɛɵɬɶ ɜɤɥɸɱɟɧɨ ɜ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ, ɤɚɤ ɷɬɨ ɫɞɟɥɚɧɨ ɜ ɩɪɢɦɟɪɟ ɞɥɹ ɮɭɧɤɰɢɣ getx ɢ gety, ɢɥɢ ɩɨɦɟɳɟɧɨ ɜɧɟ ɨɩɢɫɚɧɢɹ ɤɥɚɫɫɚ. Ʉɨɦɩɨɧɟɧɬɵ-ɮɭɧɤɰɢɢ ɩɪɢ ɢɯ ɜɵɡɨɜɟ ɧɟɹɜɧɨ ɩɨɥɭɱɚɸɬ ɞɨɩɨɥɧɢɬɟɥɶɧɵɣ ɚɪɝɭɦɟɧɬ - ɭɤɚɡɚɬɟɥɶ ɧɚ ɩɟɪɟɦɟɧɧɭɸ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ, ɞɥɹ ɤɨɬɨɪɨɣ ɜɵɡɜɚɧɚ ɮɭɧɤɰɢɹ, ɢ ɜ ɬɟɥɟ ɮɭɧɤɰɢɢ ɦɨɠɧɨ ɨɛɪɚɳɚɬɶɫɹ ɤɨ ɜɫɟɦ ɤɨɦɩɨɧɟɧɬɚɦ ɤɥɚɫɫɚ. ȼ ɫɜɹɡɢ ɫ ɷɬɢɦ ɩɪɢ ɨɩɢɫɚɧɢɢ ɬɟɥɚ ɤɨɦɩɨɧɟɧɬɵ-ɮɭɧɤɰɢɢ ɜɧɟ ɨɩɢɫɚɧɢɹ ɤɥɚɫɫɚ ɧɭɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɩɟɪɚɰɢɸ ɪɚɡɪɟɲɟɧɢɹ ɤɨɧɬɟɤɫɬɚ, ɱɬɨɛɵ ɢɧɮɨɪɦɢɪɨɜɚɬɶ ɤɨɦɩɢɥɹɬɨɪ ɨ ɩɪɢɧɚɞɥɟɠɧɨɫɬɢ ɮɭɧɤɰɢɢ ɤ ɤɥɚɫɫɭ. Ɇɟɬɨɞɵ ɤɥɚɫɫɚ TPoint ɦɨɠɧɨ ɨɩɢɫɚɬɶ ɬɚɤ: void TPoint::movePoint(int newx, int newy) {x=newx; y=newy;} void TPoint::relmove(int dx, int dy) {x+=dx; y+=dy;}
ɑɬɨɛɵ ɜɵɩɨɥɧɢɬɶ ɧɚɱɚɥɶɧɭɸ ɢɧɢɰɢɚɥɢɡɚɰɢɸ ɤɨɦɩɨɧɟɧɬ-ɞɚɧɧɵɯ, ɩɪɢ ɫɨɡɞɚɧɢɢ ɩɟɪɟɦɟɧɧɵɯ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ ɜ ɨɩɢɫɚɧɢɟ ɬɢɩɚ ɜɤɥɸɱɚɸɬɫɹ ɫɩɟɰɢɚɥɶɧɵɟ ɦɟɬɨɞɵ-ɤɨɧɫɬɪɭɤɬɨɪɵ. ɂɦɹ ɤɨɧɫɬɪɭɤɬɨɪɚ ɫɨɜɩɚɞɚɟɬ ɫ ɢɦɟɧɟɦ ɬɢɩɚ, ɤɨɧɫɬɪɭɤɬɨɪ ɧɟ ɜɨɡɜɪɚɳɚɟɬ ɧɢɤɚɤɨɝɨ ɡɧɚɱɟɧɢɹ ɢ ɞɥɹ ɧɟɝɨ ɧɟ ɭɤɚɡɵɜɚɟɬɫɹ ɬɢɩ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ. Ⱦɥɹ ɪɚɫɫɦɨɬɪɟɧɧɨɝɨ ɜɵɲɟ ɤɥɚɫɫɚ TPoint ɦɨɠɧɨ ɛɵɥɨ ɨɛɨɣɬɢɫɶ ɛɟɡ ɤɨɧɫɬɪɭɤɬɨɪɚ ɢ ɢɫɩɨɥɶɡɨɜɚɬɶ ɞɥɹ ɢɧɢɰɢɚɥɢɡɚɰɢɢ ɦɟɬɨɞ movePoint. Ɋɚɫɫɦɨɬɪɢɦ ɜ ɤɚɱɟɫɬɜɟ ɩɪɢɦɟɪɚ ɤɥɚɫɫ TRect, ɨɩɢɫɵɜɚɸɳɢɣ ɩɪɹɦɨɭɝɨɥɶɧɢɤ ɫɨ ɫɬɨɪɨɧɚɦɢ, ɩɚɪɚɥɥɟɥɶɧɵɦɢ ɨɫɹɦ ɤɨɨɪɞɢɧɚɬ: enum Boolean {FALSE, TRUE}; class TRect { 42
public: TPoint a,b;
/* ɥɟɜɵɣ ɜɟɪɯɧɢɣ ɢ ɩɪɚɜɵɣ ɧɢɠɧɢɣ ɭɝɥɵ ɩɪɹɦɨɭɝɨɥɶɧɢɤɚ ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ */ void move(int dx, int dy) /* ɩɟɪɟɦɟɳɟɧɢɟ ɩɪɹɦɨɭɝɨɥɶɧɢɤɚ */ {a.relmove(dx, dy); b.relmove(dx, dy);} void grow(int dx, int dy) // ɢɡɦɟɧɟɧɢɟ ɪɚɡɦɟɪɨɜ {a.x+=dx; a.y+=dy; b.x+=dx; b.y+=dy;} void intersect(const TRect& r); // ɨɛɳɚɹ ɱɚɫɬɶ ɞɜɭɯ ɩɪɹɦɨɭɝɨɥɶɧɢɤɨɜ void Union(const TRect& r); // ɩɪɹɦɨɭɝɨɥɶɧɢɤ, ɨɯɜɚɬɵɜɚɸɳɢɣ ɞɜɚ ɩɪɹɦɨɭɝɨɥɶɧɢɤɚ Boolean contains(const TPoint& p); // TRUE, ɟɫɥɢ ɬɨɱɤɚ p ɩɪɢɧɚɞɥɟɠɢɬ ɩɪɹɦɨɭɝɨɥɶɧɢɤɭ Boolean isEmpty(); //TRUE, ɟɫɥɢ ɲɢɪɢɧɚ ɢɥɢ ɜɵɫɨɬɚ ɩɪɹɦɨɭɝɨɥɶɧɢɤɚ ɪɚɜɧɵ ɧɭɥɸ TRect(int ax, int ay, int bx, int by) // ɤɨɧɫɬɪɭɤɬɨɪ {a.x=ax; a.y=ay; b.x=bx; b.y=by;}; TRect (TPoint p1, TPoint p2) // ɤɨɧɫɬɪɭɤɬɨɪ {a=p1; b=p2;}; TRect() // ɤɨɧɫɬɪɭɤɬɨɪ ɛɟɡ ɩɚɪɚɦɟɬɪɨɜ {a.x=a.y=b.x=b.y=0;}; }; /* Ɇɟɬɨɞɵ ɤɥɚɫɫɚ TRect */ void TRect::intersect(const TRect& r) { a.x=max(a.x, r.a.x); b.x=min(b.x, r.b.x); a.y=max(a.y, r.a.y); b.y=min(b.y, r.b.y); }; void TRect::Union(const TRect& r) {a.x=(a.x<=r.a.x)?a.x:r.a.x; a.y=(a.y<=r.a.y)?a.y:r.a.y; b.x=(b.x>=r.b.x)?b.x:r.b.x; b.y=(b.y>=r.b.y)?b.y:r.b.y; }; Boolean TRect::contains(const TPoint& p) { return Boolean(p.x>=p.x&&p.x=a.y&&p.y=b.x||a.y>=b.y);};
Ȼɨɥɟɟ ɩɨɥɧɚɹ ɢɧɮɨɪɦɚɰɢɹ ɨ ɤɨɧɫɬɪɭɤɬɨɪɚɯ ɨɛɴɟɤɬɧɵɯ ɬɢɩɨɜ ɩɪɢɜɟɞɟɧɚ ɜ ɫɥɟɞɭɸɳɟɦ ɪɚɡɞɟɥɟ. Ɉɛɴɹɜɥɟɧɢɟ ɩɟɪɟɦɟɧɧɨɣ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ ɫɬɪɨɢɬɫɹ ɩɨ ɨɛɳɢɦ ɩɪɚɜɢɥɚɦ, ɧɨ ɡɚ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɦ ɩɟɪɟɦɟɧɧɨɣ ɦɨɠɧɨ ɭɤɚɡɚɬɶ ɜ ɫɤɨɛɤɚɯ ɚɪɝɭɦɟɧɬɵ ɨɩɪɟɞɟɥɟɧɧɨɝɨ ɜ ɤɥɚɫɫɟ ɤɨɧɫɬɪɭɤɬɨɪɚ, ɧɚɩɪɢɦɟɪ: TRect r1(2,4,20,50); TRect *pr=&r1;
/* ɢɧɢɰɢɚɥɢɡɚɰɢɹ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɩɟɪɜɨɝɨ ɤɨɧɫɬɪɭɤɬɨɪɚ */ // ɭɤɚɡɚɬɟɥɶ ɧɚ TRect 43
TRect r2;
// ɢɫɩɨɥɶɡɭɟɬɫɹ ɤɨɧɫɬɪɭɤɬɨɪ ɛɟɡ ɩɚɪɚɦɟɬɪɨɜ
ȼ ɨɩɟɪɚɰɢɢ new ɞɥɹ ɪɚɡɦɟɳɟɧɢɹ ɜ ɞɢɧɚɦɢɱɟɫɤɨɣ ɩɚɦɹɬɢ ɨɛɴɟɤɬɧɨɣ ɩɟɪɟɦɟɧɧɨɣ ɡɚ ɢɦɟɧɟɦ ɬɢɩɚ ɬɚɤɠɟ ɭɤɚɡɵɜɚɸɬɫɹ ɚɪɝɭɦɟɧɬɵ ɤɨɧɫɬɪɭɤɬɨɪɚ ɷɬɨɝɨ ɬɢɩɚ: ptr=new TRect(7,3,18,40);
Ⱦɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɤɨɦɩɨɧɟɧɬɚɦ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ ɢɦɹ ɤɨɦɩɨɧɟɧɬɵ ɞɨɥɠɧɨ ɭɬɨɱɧɹɬɶɫɹ ɢɦɟɧɟɦ ɨɛɴɟɤɬɧɨɣ ɩɟɪɟɦɟɧɧɨɣ ɢɥɢ ɭɤɚɡɚɬɟɥɟɦ ɧɚ ɧɟɟ: r1.grow(2, -3); pr->move(1, 1); Boolean bb=r1.isEmpty();
4.2. Ʉɨɧɫɬɪɭɤɬɨɪɵ ɢ ɞɟɫɬɪɭɤɬɨɪɵ Ɉɩɢɫɚɧɢɟ ɤɥɚɫɫɚ ɨɛɵɱɧɨ ɫɨɞɟɪɠɢɬ ɫɩɟɰɢɚɥɶɧɵɟ ɦɟɬɨɞɵ, ɜɵɡɵɜɚɟɦɵɟ ɩɪɢ ɫɨɡɞɚɧɢɢ ɩɟɪɟɦɟɧɧɨɣ ɷɬɨɝɨ ɤɥɚɫɫɚ ɢ ɭɞɚɥɟɧɢɢ ɩɟɪɟɦɟɧɧɨɣ ɢɡ ɞɢɧɚɦɢɱɟɫɤɨɣ ɩɚɦɹɬɢ – ɤɨɧɫɬɪɭɤɬɨɪɵ ɢ ɞɟɫɬɪɭɤɬɨɪɵ. Ʉɨɧɫɬɪɭɤɬɨɪ ɜɵɡɵɜɚɟɬɫɹ ɩɨɫɥɟ ɜɵɞɟɥɟɧɢɹ ɩɚɦɹɬɢ ɞɥɹ ɩɟɪɟɦɟɧɧɨɣ ɢ ɨɛɟɫɩɟɱɢɜɚɟɬ ɢɧɢɰɢɚɥɢɡɚɰɢɸ ɤɨɦɩɨɧɟɧɬ-ɞɚɧɧɵɯ, ɞɟɫɬɪɭɤɬɨɪ ɜɵɡɵɜɚɟɬɫɹ ɩɟɪɟɞ ɨɫɜɨɛɨɠɞɟɧɢɟɦ ɩɚɦɹɬɢ, ɡɚɧɢɦɚɟɦɨɣ ɨɛɴɟɤɬɧɨɣ ɩɟɪɟɦɟɧɧɨɣ, ɢ ɩɪɟɞɧɚɡɧɚɱɟɧ ɞɥɹ ɜɵɩɨɥɧɟɧɢɹ ɞɨɩɨɥɧɢɬɟɥɶɧɵɯ ɞɟɣɫɬɜɢɣ, ɫɜɹɡɚɧɧɵɯ ɫ ɭɧɢɱɬɨɠɟɧɢɟɦ ɨɛɴɟɤɬɧɨɣ ɩɟɪɟɦɟɧɧɨɣ, ɧɚɩɪɢɦɟɪ ɞɥɹ ɨɫɜɨɛɨɠɞɟɧɢɹ ɩɚɦɹɬɢ, ɜɵɞɟɥɟɧɧɨɣ ɞɥɹ ɨɛɴɟɤɬɚ ɜɧɟ ɭɱɚɫɬɤɚ, ɨɬɜɟɞɟɧɧɨɝɨ ɞɥɹ ɤɨɦɩɨɧɟɧɬ-ɞɚɧɧɵɯ. Ʉɚɤ ɭɠɟ ɨɬɦɟɱɚɥɨɫɶ, ɤɨɧɫɬɪɭɤɬɨɪ ɜɫɟɝɞɚ ɢɦɟɟɬ ɢɦɹ, ɫɨɜɩɚɞɚɸɳɟɟ ɫ ɢɦɟɧɟɦ ɤɥɚɫɫɚ, ɞɥɹ ɧɟɝɨ ɧɟ ɭɤɚɡɵɜɚɟɬɫɹ ɬɢɩ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ ɢ ɨɧ ɧɟ ɜɨɡɜɪɚɳɚɟɬ ɧɢɤɚɤɨɝɨ ɡɧɚɱɟɧɢɹ. Ʉɨɧɫɬɪɭɤɬɨɪ ɞɨɥɠɟɧ ɨɛɟɫɩɟɱɢɜɚɬɶ ɢɧɢɰɢɚɥɢɡɚɰɢɸ ɜɫɟɯ ɤɨɦɩɨɧɟɧɬ-ɞɚɧɧɵɯ. Ⱦɥɹ ɤɥɚɫɫɚ ɦɨɠɟɬ ɛɵɬɶ ɨɛɴɹɜɥɟɧɨ ɧɟɫɤɨɥɶɤɨ ɤɨɧɫɬɪɭɤɬɨɪɨɜ, ɪɚɡɥɢɱɚɸɳɢɯɫɹ ɱɢɫɥɨɦ ɢ ɬɢɩɚɦɢ ɩɚɪɚɦɟɬɪɨɜ. ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ ɪɚɡɥɢɱɚɸɬ ɫɥɟɞɭɸɳɢɟ ɜɢɞɵ ɤɨɧɫɬɪɭɤɬɨɪɨɜ: ɤɨɧɫɬɪɭɤɬɨɪ ɫ ɩɚɪɚɦɟɬɪɚɦɢ, ɤɨɧɫɬɪɭɤɬɨɪ ɛɟɡ ɩɚɪɚɦɟɬɪɨɜ ɢ ɤɨɧɫɬɪɭɤɬɨɪ ɤɨɩɢɪɨɜɚɧɢɹ ɫ ɨɞɧɢɦ ɩɚɪɚɦɟɬɪɨɦ- ɫɫɵɥɤɨɣ ɧɚ ɩɟɪɟɦɟɧɧɭɸ ɬɨɝɨ ɠɟ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ. ȿɫɥɢ ɞɥɹ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ ɧɟ ɨɩɪɟɞɟɥɟɧɨ ɧɢ ɨɞɧɨɝɨ ɤɨɧɫɬɪɭɤɬɨɪɚ, ɤɨɦɩɢɥɹɬɨɪ ɫɨɡɞɚɟɬ ɞɥɹ ɧɟɝɨ ɤɨɧɫɬɪɭɤɬɨɪ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɧɟ ɢɫɩɨɥɶɡɭɸɳɢɣ ɩɚɪɚɦɟɬɪɨɜ. Ʉɨɧɫɬɪɭɤɬɨɪ ɤɨɩɢɪɨɜɚɧɢɹ ɧɟɨɛɯɨɞɢɦ, ɟɫɥɢ ɩɟɪɟɦɟɧɧɚɹ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ ɩɟɪɟɞɚɟɬɫɹ ɜ ɤɚɤɭɸ-ɧɢɛɭɞɶ ɮɭɧɤɰɢɸ ɤɚɤ ɚɪɝɭɦɟɧɬ, ɩɨɫɤɨɥɶɤɭ ɜɫɟ ɚɪɝɭɦɟɧɬɵ ɩɟɪɟɞɚɸɬɫɹ ɜ ɮɭɧɤɰɢɸ ɩɨ ɡɧɚɱɟɧɢɸ. Ⱦɟɫɬɪɭɤɬɨɪ ɧɟɨɛɯɨɞɢɦ, ɟɫɥɢ ɨɛɴɟɤɬɧɵɣ ɬɢɩ ɫɨɞɟɪɠɢɬ ɤɨɦɩɨɧɟɧɬɵ, ɹɜɥɹɸɳɢɟɫɹ ɭɤɚɡɚɬɟɥɹɦɢ ɧɚ ɞɢɧɚɦɢɱɟɫɤɢɟ ɞɚɧɧɵɟ, ɤɨɬɨɪɵɟ ɞɨɥɠɧɵ ɭɧɢɱɬɨɠɚɬɶɫɹ ɩɪɢ ɭɧɢɱɬɨɠɟɧɢɢ ɨɛɴɟɤɬɧɨɣ ɩɟɪɟɦɟɧɧɨɣ. Ⱦɟɫɬɪɭɤɬɨɪ ɜɫɟɝɞɚ ɢɦɟɟɬ ɬɨ ɠɟ ɢɦɹ, ɱɬɨ ɢ ɢɦɹ ɤɥɚɫɫɚ, ɧɨ ɩɟɪɟɞ ɢɦɟɧɟɦ ɡɚɩɢɫɵɜɚɟɬɫɹ ɡɧɚɤ «~» (ɬɢɥɶɞɚ). Ⱦɟɫɬɪɭɤɬɨɪ ɧɟ ɢɦɟɟɬ ɩɚɪɚɦɟɬɪɨɜ ɢ ɩɨɞɨɛɧɨ ɤɨɧɫɬɪɭɤɬɨɪɭ ɧɟ ɜɨɡɜɪɚɳɚɟɬ ɧɢɤɚɤɨɝɨ ɡɧɚɱɟɧɢɹ. ȼ ɤɚɱɟɫɬɜɟ ɩɪɢɦɟɪɚ ɪɚɫɫɦɨɬɪɢɦ ɨɛɴɟɤɬɧɵɣ ɬɢɩ TString ɞɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɫɬɪɨɤɨɜɵɯ ɞɚɧɧɵɯ ɫ ɛɨɥɟɟ ɜɵɫɨɤɨɣ ɫɬɟɩɟɧɶɸ ɡɚɳɢɬɵ ɨɬ ɨɲɢ44
ɛɨɤ, ɱɟɦ ɷɬɨ ɨɛɟɫɩɟɱɟɧɨ ɫɬɚɧɞɚɪɬɧɵɦɢ ɮɭɧɤɰɢɹɦɢ ɨɛɪɚɛɨɬɤɢ ɫɬɪɨɤ ɢɡ ɮɚɣɥɚ-ɡɚɝɨɥɨɜɤɚ string.h. #include #include <string.h> class TString { public: TString(); // ɤɨɧɫɬɪɭɤɬɨɪ ɛɟɡ ɩɚɪɚɦɟɪɨɜ TString(int n, char* s=0); /* ɤɨɧɫɬɪɭɤɬɨɪ, ɫɨɡɞɚɸɳɢɣ ɩɭɫɬɭɸ ɫɬɪɨɤɭ */ TString(char* s); /* ɤɨɧɫɬɪɭɤɬɨɪ, ɩɪɟɨɛɪɚɡɭɸɳɢɣ ɦɚɫɫɢɜ ɢɡ char ɫ ɡɚɜɟɪɲɚɸɳɢɦ ɧɭɥɟɦ ɜ ɬɢɩ TString */ TString(TString& st); // ɤɨɧɫɬɪɭɤɬɨɪ ɤɨɩɢɪɨɜɚɧɢɹ ~TString(); // ɞɟɫɬɪɭɤɬɨɪ void print(); // ɜɵɜɨɞ ɫɬɪɨɤɢ ɧɚ ɷɤɪɚɧ int sz; // ɞɥɢɧɚ ɫɬɪɨɤɢ char* ps; // ɭɤɚɡɚɬɟɥɶ ɧɚ ɩɚɦɹɬɶ ɞɥɹ ɯɪɚɧɟɧɢɹ ɫɬɪɨɤɢ }; /* Ɇɟɬɨɞɵ ɤɥɚɫɫɚ TString */ TString::TString(){sz=0; ps=0;} TString::TString(int n, char* s) { sz=n; ps=new char[n+1]; strncpy(ps,s,n); ps[sz]='\0'; } TString::TString(char* s) { sz=strlen(s)+1; ps=new char[sz]; strcpy(ps,s); } TString::TString(TString& str) { sz = str.sz; ps=new char[sz+1]; strcpy(ps,str.ps); } TString::~TString() {if (ps != 0) delete [] ps; } void TString::print() { if (sz==0) {cout << " ɋɬɪɨɤɚ ɩɭɫɬɚɹ "<< endl; return;} cout<<" ɋɬɪɨɤɚ = "<< ps <<endl; }
ɇɢɠɟ ɩɪɢɜɟɞɟɧ ɩɪɢɦɟɪ ɩɪɨɝɪɚɦɦɵ, ɢɥɥɸɫɬɪɢɪɭɸɳɟɣ ɢɫɩɨɥɶɡɨɜɚɧɢɟ ɞɚɧɧɵɯ ɬɢɩɚ TString. int main() { char rabstr[60]="yes"; 45
while (*rabstr!='n') { cin >> rabstr; if (*rabstr=='n') break; TString s1(); TString s2(6); TString s3(6, rabstr); TString* ps1=new TString("ɗɬɨ ɫɬɪɨɤɚ ɩɨ ɭɤɚɡɚɬɟɥɸ"); cout <<"s1="; ps1->print(); cout <<"s2="; s2.print(); cout <<"s3="; s3.print(); } return 0; }
Ɉɩɢɫɚɧɢɟ ɤɨɧɫɬɪɭɤɬɨɪɚ ɦɨɠɧɨ ɭɩɪɨɫɬɢɬɶ, ɟɫɥɢ ɤɨɦɩɨɧɟɧɬɵ-ɞɚɧɧɵɟ ɩɪɢɧɚɞɥɟɠɚɬ ɤ ɛɚɡɨɜɵɦ ɬɢɩɚɦ ɢɥɢ ɹɜɥɹɸɬɫɹ ɨɛɴɟɤɬɧɵɦɢ ɩɟɪɟɦɟɧɧɵɦɢ, ɢɦɟɸɳɢɦɢ ɤɨɧɫɬɪɭɤɬɨɪ. ɉɪɢ ɨɩɢɫɚɧɢɢ ɤɨɧɫɬɪɭɤɬɨɪɚ ɩɨɫɥɟ ɡɚɝɨɥɨɜɤɚ ɮɭɧɤɰɢɢ ɦɨɠɧɨ ɩɨɫɬɚɜɢɬɶ ɞɜɨɟɬɨɱɢɟ ɢ ɡɚ ɧɢɦ ɫɩɢɫɨɤ ɢɧɢɰɢɚɥɢɡɚɬɨɪɨɜ ɜɢɞɚ ɢɞɟɧɬɢɮɢɤɚɬɨɪ (ɚɪɝɭɦɟɧɬɵ). ɇɚɩɪɢɦɟɪ, ɞɥɹ ɤɥɚɫɫɚ TPoint ɢɡ ɩɪɟɞɵɞɭɳɟɝɨ ɩɚɪɚɝɪɚɮɚ ɦɨɠɧɨ ɛɵɥɨ ɨɩɪɟɞɟɥɢɬɶ ɤɨɧɫɬɪɭɤɬɨɪ ɬɚɤ: class TPoint { ..... public: TPoint(int x0, int y0):x(x0), y(y0){}; }
ȼ ɷɬɨɦ ɤɨɧɫɬɪɭɤɬɨɪɟ ɜɫɟ ɤɨɦɩɨɧɟɧɬɵ ɩɨɥɭɱɚɸɬ ɡɧɚɱɟɧɢɹ ɢɡ ɫɩɢɫɤɚ ɢɧɢɰɢɚɥɢɡɚɰɢɢ, ɚ ɬɟɥɨ ɤɨɧɫɬɪɭɤɬɨɪɚ ɩɪɟɞɫɬɚɜɥɟɧɨ ɩɭɫɬɵɦ ɫɨɫɬɚɜɧɵɦ ɨɩɟɪɚɬɨɪɨɦ.
4.3. ɉɪɨɢɡɜɨɞɧɵɟ ɤɥɚɫɫɵ Ʉɥɚɫɫɵ ɨɛɪɚɡɭɸɬ ɢɟɪɚɪɯɢɱɟɫɤɭɸ ɫɬɪɭɤɬɭɪɭ, ɤɨɝɞɚ ɜɵɞɟɥɹɟɬɫɹ ɧɟɤɨɬɨɪɵɣ ɛɚɡɨɜɵɣ ɤɥɚɫɫ, ɫɨɞɟɪɠɚɳɢɣ ɨɛɳɢɟ ɞɚɧɧɵɟ ɢ ɦɟɬɨɞɵ ɝɪɭɩɩɵ ɫɯɨɞɧɵɯ ɤɥɚɫɫɨɜ, ɢ ɫɬɪɨɢɬɫɹ ɧɟɫɤɨɥɶɤɨ ɩɪɨɢɡɜɨɞɧɵɯ ɤɥɚɫɫɨɜ, ɜ ɤɨɬɨɪɵɯ ɤ ɞɚɧɧɵɦ ɢ ɦɟɬɨɞɚɦ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ ɞɨɛɚɜɥɹɸɬɫɹ ɞɚɧɧɵɟ ɢ ɦɟɬɨɞɵ, ɧɟɨɛɯɨɞɢɦɵɟ ɞɥɹ ɪɟɚɥɢɡɚɰɢɢ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ. Ɉɩɢɫɚɧɢɟ ɫɢɫɬɟɦɵ ɤɥɚɫɫɨɜ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɜɵɝɥɹɞɢɬ ɬɚɤ: // ɛɚɡɨɜɵɣ ɤɥɚɫɫ
class TA { ɉɟɪɟɦɟɧɧɵɟ ɢ ɦɟɬɨɞɵ TA }
class TAA:public TA // ɤɥɚɫɫ, ɩɪɨɢɡɜɨɞɧɵɣ ɨɬ ɤɥɚɫɫɚ TA { ɉɟɪɟɦɟɧɧɵɟ ɢ ɦɟɬɨɞɵ TAA } 46
class TAAB:public TAA // ɤɥɚɫɫ, ɩɪɨɢɡɜɨɞɧɵɣ ɨɬ ɤɥɚɫɫɚ TAA { ɉɟɪɟɦɟɧɧɵɟ ɢ ɦɟɬɨɞɵ TAAB } ȼ ɞɚɧɧɨɦ ɩɪɢɦɟɪɟ public ɹɜɥɹɟɬɫɹ ɨɞɧɢɦ ɢɡ ɦɨɞɢɮɢɤɬɚɬɨɪɨɜ ɞɨɫɬɭ-
ɩɚ, ɭɩɪɚɜɥɹɸɳɢɯ ɞɨɫɬɭɩɨɦ ɤ ɤɨɦɩɨɧɟɧɬɚɦ ɩɪɨɢɡɜɨɞɧɵɯ ɤɥɚɫɫɨɜ. ȿɫɥɢ ɛɚɡɨɜɵɣ ɤɥɚɫɫ ɨɛɴɹɜɥɟɧ ɤɚɤ public, ɬɨ ɜ ɩɪɨɢɡɜɨɞɧɨɦ ɤɥɚɫɫɟ publicɤɨɦɩɨɧɟɧɬɵ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ ɨɫɬɚɧɭɬɫɹ public, protected-ɤɨɦɩɨɧɟɧɬɵ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ ɨɫɬɚɧɭɬɫɹ protected, private-ɤɨɦɩɨɧɟɧɬɵ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ ɞɥɹ ɮɭɧɤɰɢɣ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ ɛɭɞɭɬ ɧɟɞɨɫɬɭɩɧɵ. ȿɫɥɢ ɛɚɡɨɜɵɣ ɤɥɚɫɫ ɨɛɴɹɜɥɟɧ ɤɚɤ private, ɬɨ ɜ ɩɪɨɢɡɜɨɞɧɨɦ ɤɥɚɫɫɟ public ɢ protected-ɤɨɦɩɨɧɟɧɬɵ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ ɞɨɫɬɭɩɧɵ ɞɥɹ ɮɭɧɤɰɢɣ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ, ɧɨ ɞɥɹ ɫɥɟɞɭɸɳɟɝɨ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ ɨɧɢ ɛɭɞɭɬ ɫɱɢɬɚɬɶɫɹ private, ɬɨ ɟɫɬɶ ɛɭɞɭɬ ɧɟɞɨɫɬɭɩɧɵ, private-ɤɨɦɩɨɧɟɧɬɵ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ ɧɟɞɨɫɬɭɩɧɵ ɜ ɩɪɨɢɡɜɨɞɧɵɯ ɤɥɚɫɫɚɯ. ȿɫɥɢ ɛɚɡɨɜɵɣ ɤɥɚɫɫ ɨɛɴɹɜɥɟɧ ɤɚɤ protected, ɬɨ ɜ ɩɪɨɢɡɜɨɞɧɨɦ ɤɥɚɫɫɟ public ɢ protected-ɤɨɦɩɨɧɟɧɬɵ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ ɛɭɞɭɬ ɢɦɟɬɶ ɭɪɨɜɟɧɶ ɞɨɫɬɭɩɚ protected, private-ɤɨɦɩɨɧɟɧɬɵ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ ɛɭɞɭɬ ɧɟɞɨɫɬɭɩɧɵ ɜ ɩɪɨɢɡɜɨɞɧɵɯ ɤɥɚɫɫɚɯ. Ʉɨɧɫɬɪɭɤɬɨɪ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ ɞɨɥɠɟɧ ɜɵɡɵɜɚɬɶ ɤɨɧɫɬɪɭɤɬɨɪ ɫɜɨɟɝɨ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ: class TBase { public: TBase(int s, int m, int d); /* Ⱦɪɭɝɢɟ ɤɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ TBase */ } class TVect:public TBase { public: TVect(int k, int s,int m, int d):TBase(s, m, d) {/* ɢɧɢɰɢɚɥɢɡɚɰɢɹ ɨɫɬɚɥɶɧɵɯ ɤɨɦɩɨɧɟɧɬ TVect */}; }
Ɉɛɪɚɬɢɦ ɜɧɢɦɚɧɢɟ ɧɚ ɬɨɬ ɮɚɤɬ, ɱɬɨ ɜ ɤɨɧɫɬɪɭɤɬɨɪɟ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ ɩɚɪɚɦɟɬɪɵ, ɩɟɪɟɞɚɜɚɟɦɵɟ ɤɨɧɫɬɪɭɤɬɨɪɭ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ (ɜ ɧɚɲɟɦ ɩɪɢɦɟɪɟ ɩɚɪɚɦɟɬɪɵ s, m ɢ d), ɹɜɥɹɸɬɫɹ ɮɚɤɬɢɱɟɫɤɢɦɢ, ɚ ɧɟ ɮɨɪɦɚɥɶɧɵɦɢ.
4.4. ɉɪɢɦɟɪ ɩɨɫɬɪɨɟɧɢɹ ɫɢɫɬɟɦɵ ɤɥɚɫɫɨɜ ɂɡɜɟɫɬɧɨ, ɱɬɨ ɩɪɢ ɨɛɴɹɜɥɟɧɢɢ ɦɚɫɫɢɜɨɜ ɜ ɋɢ/ɋɢ++ ɤɨɥɢɱɟɫɬɜɨ ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ ɡɚɞɚɟɬɫɹ ɤɨɧɫɬɚɧɬɨɣ ɢ ɜ ɞɚɥɶɧɟɣɲɟɦ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɢɡɦɟɧɟɧɨ. ɉɪɢ ɨɛɪɚɳɟɧɢɢ ɤ ɷɥɟɦɟɧɬɚɦ ɦɚɫɫɢɜ ɨɬɫɭɬɫɬɜɭɟɬ ɤɨɧɬɪɨɥɶ ɜɵɯɨɞɚ ɡɚ ɩɪɟɞɟɥɵ ɢɧɞɟɤɫɨɜ ɦɚɫɫɢɜɚ, ɱɬɨ ɩɪɢɜɨɞɢɬ ɤ ɬɪɭɞɧɨ ɨɛɧɚɪɭɠɢɜɚɦɵɦ ɨɲɢɛɤɚɦ ɜ ɩɪɨɝɪɚɦɦɚɯ. ɉɨɫɬɪɨɢɦ ɫɢɫɬɟɦɭ ɤɥɚɫɫɨɜ ɞɥɹ ɨɛɪɚɛɨɬɤɢ ɞɢɧɚɦɢɱɟɫɤɢɯ 47
ɦɚɫɫɢɜɨɜ, ɜ ɤɨɬɨɪɵɟ ɦɨɠɧɨ ɞɨɛɚɜɥɹɬɶ ɧɨɜɵɟ ɷɥɟɦɟɧɬɵ ɢ ɢɫɤɥɸɱɢɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɜɵɯɨɞɚ ɡɚ ɩɪɟɞɟɥɵ ɬɟɤɭɳɟɝɨ ɪɚɡɦɟɪɚ ɦɚɫɫɢɜɚ. Ɉɛɳɢɟ ɫɜɨɣɫɬɜɚ ɦɚɫɫɢɜɨɜ ɫ ɬɚɤɢɦɢ ɫɜɨɣɫɬɜɚɦɢ, ɧɟ ɡɚɜɢɫɹɳɢɟ ɨɬ ɬɢɩɚ ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ, ɨɛɴɟɞɢɧɢɦ ɜ ɤɥɚɫɫɟ TBase, ɚ ɞɥɹ ɦɚɫɫɢɜɨɜ ɫ ɪɚɡɥɢɱɧɵɦɢ ɬɢɩɚɦɢ ɷɥɟɦɟɧɬɨɜ ɨɛɪɚɡɭɟɦ ɫɜɨɢ ɤɥɚɫɫɵ. Ɉɩɢɫɚɧɢɹ ɤɥɚɫɫɨɜ ɨɛɴɟɞɢɧɢɦ ɜ ɮɚɣɥɟ ɡɚɝɨɥɨɜɤɨɜ TBASEARR.H, ɚ ɨɩɪɟɞɟɥɟɧɢɹ ɦɟɬɨɞɨɜ ɩɪɢɜɟɞɟɦ ɜ ɮɚɣɥɟ TBASEARR.CPP. // ɮɚɣɥ TBASEARR.H #include <string.h> #include class TBase //ɛɚɡɨɜɵɣ ɤɥɚɫɫ ɞɥɹ ɦɚɫɫɢɜɨɜ ɜɫɟɯ ɬɢɩɨɜ { int size, //ɪɚɡɦɟɪ ɷɥɟɦɟɧɬɚ count, //ɬɟɤɭɳɟɟ ɱɢɫɥɨ ɷɥɟɦɟɧɬɨɜ maxCount, //ɪɚɡɦɟɪ ɜɵɞɟɥɟɧɧɨɣ ɩɚɦɹɬɢ ɜ ɛɚɣɬɚɯ delta; //ɩɪɢɪɚɳɟɧɢɟ ɩɚɦɹɬɢ ɜ ɛɚɣɬɚɯ char *pmem; //ɭɤɚɡɚɬɟɥɶ ɧɚ ɜɵɞɟɥɟɧɧɭɸ ɩɚɦɹɬɶ int changeSize(); //ɩɟɪɟɪɚɫɩɪɟɞɟɥɟɧɢɟ ɩɚɦɹɬɢ protected: void* getAddr(){return (void*)pmem;}; void addNewItem(void*); //ɞɨɛɚɜɥɟɧɢɟ ɜ ɤɨɧɟɰ ɦɚɫɫɢɜɚ void error(const char* msg){cout<<msg<<endl;}; public: int getCount(){return count;}; TBase(int s,int m,int d); TBase(); TBase(TBase&); ~TBase(); }; /* Ɇɚɫɫɢɜ ɫ ɷɥɟɦɟɧɬɚɦɢ ɬɢɩɚ int */ class TIntArray:public TBase { public: int getElem(int index); /* Ɂɧɚɱɟɧɢɟ ɷɥɟɦɟɧɬɚ ɩɨ ɢɧɞɟɤɫɭ */ void putElem(int index,int &pe); /* Ɂɚɦɟɧɚ ɡɧɚɱɟɧɢɹ ɷɥɟɦɟɧɬɚ ɩɨ ɢɧɞɟɤɫɭ */ void addElem(int& el); /* Ⱦɨɛɚɜɥɟɧɢɟ ɷɥɟɦɟɧɬɚ ɜ ɤɨɧɟɰ ɦɚɫɫɢɜɚ */ TIntArray& add(TIntArray&); /* ɋɥɨɠɟɧɢɟ ɞɜɭɯ ɦɚɫɫɢɜɨɜ ɉɨɷɥɟɦɟɧɬɧɨ */ TIntArray& subtract(TIntArray&); // ȼɵɱɢɬɚɧɢɟ ɦɚɫɫɢɜɨɜ void printElem(int index); /* ȼɵɜɨɞ ɡɧɚɱɟɧɢɹ ɷɥɟɦɟɧɬɚ ɧɚ ɷɤɪɚɧ */ void print(); // ȼɵɜɨɞ ɧɚ ɷɤɪɚɧ ɜɫɟɝɨ ɦɚɫɫɢɜɚ TIntArray(int m,int d):TBase((int)sizeof(int),m,d){}; // Ʉɨɧɫɬɪɭɤɬɨɪ TIntArray(TBase& a):TBase(a){}; // Ʉɨɧɫɬɪɭɤɬɨɪ ~TIntArray(); // Ⱦɟɫɬɪɭɤɬɨɪ }; 48
Ɉɩɪɟɞɟɥɟɧɢɹ ɦɟɬɨɞɨɜ ɩɪɢɜɟɞɟɧɵ ɜ ɮɚɣɥɟ TBASEARR.CPP: #include #include <stdlib.h> #include #include /* Ɇɟɬɨɞɵ ɤɥɚɫɫɚ TBase */ TBase::TBase(int s, int m, int d):size(s), maxCount(m), delta(d) { char* p; int k; count = 0; p=pmem=new char[size*maxCount]; for(k=0; k < maxCount; k++) {*p='\0'; p++;} } TBase::TBase():size(1),maxCount(10),delta(1) { char* p; int k; count=0; p=pmem=new char[size*maxCount]; for(k=0; k<maxCount; k++) {*p ='\0'; p++;} } TBase::TBase(TBase &b):size(b.size), maxCount(b.maxCount), delta(b.delta) { int k; count=b.count; pmem=new char[size*maxCount]; for(k=0; k<maxCount*size; k++) { pmem[k]=b.pmem[k]; } } TBase::~TBase() { delete [] pmem; }
4.5. ȼɢɪɬɭɚɥɶɧɵɟ ɮɭɧɤɰɢɢ 4.5.1. ɉɨɧɹɬɢɟ ɨ “ɩɨɡɞɧɟɦ” ɫɜɹɡɵɜɚɧɢɢ
ɉɪɢ ɨɩɢɫɚɧɢɢ ɨɛɴɟɤɬɧɵɯ ɬɢɩɨɜ ɮɭɧɤɰɢɢ ɢɦɟɸɳɢɟ ɫɯɨɞɧɨɟ ɧɚɡɧɚɱɟɧɢɟ ɜ ɪɚɡɧɵɯ ɤɥɚɫɫɚɯ ɦɨɝɭɬ ɢɦɟɬɶ ɨɞɢɧɚɤɨɜɵɟ ɢɦɟɧɚ, ɬɢɩɵ ɩɚɪɚɦɟɬɪɨɜ ɢ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ. ɉɪɢ ɨɛɪɚɳɟɧɢɢ ɤ ɬɚɤɨɣ ɮɭɧɤɰɢɢ ɫ ɭɤɚɡɚɧɢɟɦ ɢɦɟɧɢ ɨɛɴɟɤɬɚ ɤɨɦɩɢɥɹɬɨɪɭ ɢɡɜɟɫɬɧɨ, ɤɚɤɚɹ ɢɡ ɨɞɧɨɢɦɟɧɧɵɯ ɮɭɧɤɰɢɣ ɬɪɟɛɭɟɬɫɹ. ȼ ɬɨ ɠɟ ɜɪɟɦɹ ɤ ɨɛɴɟɤɬɚɦ ɩɪɨɢɡɜɨɞɧɨɝɨ ɬɢɩɚ ɦɨɠɧɨ ɨɛɪɚɳɚɬɶɫɹ ɩɨ ɭɤɚɡɚɬɟɥɸ ɧɚ ɛɚɡɨɜɵɣ ɬɢɩ ɢ ɬɨɝɞɚ ɧɚ ɷɬɚɩɟ ɤɨɦɩɢɥɹɰɢɢ ɧɟɥɶɡɹ ɭɫɬɚɧɨɜɢɬɶ, ɮɭɧɤɰɢɹ ɤɚɤɨɝɨ ɢɡ ɩɪɨɢɡɜɨɞɧɵɯ ɬɢɩɨɜ ɞɨɥɠɧɚ ɛɵɬɶ ɜɵɡɜɚɧɚ. ȼ ɯɨɞɟ 49
ɜɵɩɨɥɧɟɧɢɹ ɩɪɨɝɪɚɦɦɵ ɬɪɟɛɭɟɬɫɹ ɩɪɨɜɟɪɹɬɶ, ɧɚ ɨɛɴɟɤɬ ɤɚɤɨɝɨ ɬɢɩɚ ɫɫɵɥɚɟɬɫɹ ɭɤɚɡɚɬɟɥɶ, ɢ ɩɨɫɥɟ ɬɚɤɨɣ ɩɪɨɜɟɪɤɢ ɜɵɡɵɜɚɬɶ ɬɪɟɛɭɟɦɭɸ ɮɭɧɤɰɢɸ. ɗɬɢ ɞɟɣɫɬɜɢɹ ɧɚɡɵɜɚɸɬ «ɩɨɡɞɧɢɦ» ɫɜɹɡɵɜɚɧɢɟɦ, ɜ ɨɬɥɢɱɢɟ ɨɬ «ɪɚɧɧɟɝɨ» ɫɜɹɡɵɜɚɧɢɹ, ɩɪɢ ɤɨɬɨɪɨɦ ɭɠɟ ɧɚ ɷɬɚɩɚɯ ɤɨɦɩɢɥɹɰɢɢ ɢɥɢ ɪɟɞɚɤɬɢɪɨɜɚɧɢɹ ɫɜɹɡɟɣ ɦɨɠɧɨ ɭɫɬɚɧɨɜɢɬɶ ɚɞɪɟɫ ɬɨɱɤɢ ɜɯɨɞɚ ɜɵɡɵɜɚɟɦɨɣ ɮɭɧɤɰɢɢ. ȼ ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɯ ɹɡɵɤɚɯ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɞɥɹ ɪɟɲɟɧɢɹ ɷɬɨɣ ɩɪɨɛɥɟɦɵ ɩɪɢɦɟɧɹɸɬɫɹ ɜɢɪɬɭɚɥɶɧɵɟ ɦɟɬɨɞɵ. 4.5.2. Ɉɩɢɫɚɧɢɟ ɜɢɪɬɭɚɥɶɧɵɯ ɮɭɧɤɰɢɣ
Ɏɭɧɤɰɢɹ-ɤɨɦɩɨɧɟɧɬɚ ɤɥɚɫɫɚ ɨɛɴɹɜɥɹɟɬɫɹ ɤɚɤ ɜɢɪɬɭɚɥɶɧɚɹ ɭɤɚɡɚɧɢɟɦ ɤɥɸɱɟɜɨɝɨ ɫɥɨɜɚ virtual. Ɏɭɧɤɰɢɢ-ɤɨɦɩɨɧɟɧɬɵ ɜ ɩɪɨɢɡɜɨɞɧɵɯ ɤɥɚɫɫɚɯ, ɡɚɦɟɧɹɸɳɢɟ ɜɢɪɬɭɚɥɶɧɭɸ ɮɭɧɤɰɢɸ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ, ɞɨɥɠɧɵ ɨɛɴɹɜɥɹɬɶɫɹ ɫ ɬɟɦ ɠɟ ɢɦɟɧɟɦ, ɬɟɦ ɠɟ ɫɩɢɫɤɨɦ ɩɚɪɚɦɟɬɪɨɜ ɢ ɬɢɩɨɦ ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɡɧɚɱɟɧɢɹ, ɱɬɨ ɢ ɫɨɨɬɜɟɬɫɬɜɭɸɳɚɹ ɮɭɧɤɰɢɹ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ. ȿɫɥɢ ɢɡ ɩɪɨɢɡɜɨɞɧɨɝɨ ɤɥɚɫɫɚ ɧɟ ɨɛɪɚɡɭɟɬɫɹ ɧɨɜɵɯ ɩɪɨɢɡɜɨɞɧɵɯ ɤɥɚɫɫɨɜ, ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ virtual ɜ ɨɩɢɫɚɧɢɢ ɮɭɧɤɰɢɢ ɦɨɠɧɨ ɨɩɭɫɬɢɬɶ. ȿɫɥɢ ɜ ɩɪɨɢɡɜɨɞɧɨɦ ɤɥɚɫɫɟ ɧɟɬ ɨɛɴɹɜɥɟɧɢɹ ɮɭɧɤɰɢɢ ɫ ɬɟɦ ɠɟ ɢɦɟɧɟɦ, ɱɬɨ ɢ ɜɢɪɬɭɚɥɶɧɚɹ ɮɭɧɤɰɢɹ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ, ɛɭɞɟɬ ɜɵɡɵɜɚɬɶɫɹ ɮɭɧɤɰɢɹ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ. ȼɢɪɬɭɚɥɶɧɚɹ ɮɭɧɤɰɢɹ ɦɨɠɟɬ ɛɵɬɶ ɨɛɴɹɜɥɟɧɚ ɜ ɮɨɪɦɟ: virtual void print()=0;
Ɍɚɤɚɹ ɮɭɧɤɰɢɹ ɧɚɡɵɜɚɟɬɫɹ «ɱɢɫɬɨɣ» (pure) ɜɢɪɬɭɚɥɶɧɨɣ ɮɭɧɤɰɢɟɣ, ɚ ɨɛɴɟɤɬɧɵɣ ɬɢɩ, ɫɨɞɟɪɠɚɳɢɣ ɟɟ ɨɛɴɹɜɥɟɧɢɟ, ɧɚɡɵɜɚɟɬɫɹ ɚɛɫɬɪɚɤɬɧɵɦ ɨɛɴɟɤɬɧɵɦ ɬɢɩɨɦ. ȼ ɩɪɨɝɪɚɦɦɟ ɧɟ ɦɨɝɭɬ ɫɨɡɞɚɜɚɬɶɫɹ ɷɤɡɟɦɩɥɹɪɵ ɚɛɫɬɪɚɤɬɧɵɯ ɬɢɩɨɜ, ɬɚɤɨɣ ɬɢɩ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɬɨɥɶɤɨ ɞɥɹ ɨɛɪɚɡɨɜɚɧɢɹ ɩɪɨɢɡɜɨɞɧɵɯ ɬɢɩɨɜ, ɩɪɢɱɟɦ ɜ ɩɪɨɢɡɜɨɞɧɨɦ ɬɢɩɟ ɫɥɟɞɭɟɬ ɥɢɛɨ ɫɧɨɜɚ ɨɩɪɟɞɟɥɢɬɶ ɷɬɭ ɜɢɪɬɭɚɥɶɧɭɸ ɮɭɧɤɰɢɸ ɤɚɤ ɱɢɫɬɭɸ, ɥɢɛɨ ɨɛɴɹɜɢɬɶ ɟɟ ɤɚɤ ɨɛɵɱɧɭɸ ɜɢɪɬɭɚɥɶɧɭɸ ɮɭɧɤɰɢɸ, ɜɵɩɨɥɧɹɸɳɭɸ ɤɨɧɤɪɟɬɧɵɟ ɞɟɣɫɬɜɢɹ. ȼɢɪɬɭɚɥɶɧɵɟ ɮɭɧɤɰɢɢ ɨɫɨɛɟɧɧɨ ɩɨɥɟɡɧɵ, ɤɨɝɞɚ ɤ ɦɟɬɨɞɨɦ ɤɥɚɫɫɚ ɬɪɟɛɭɟɬɫɹ ɨɛɪɚɳɚɬɶɫɹ ɱɟɪɟɡ ɭɤɚɡɚɬɟɥɶ ɧɚ ɷɤɡɟɦɩɥɹɪ ɤɥɚɫɫɚ, ɚ ɫɚɦ ɷɬɨɬ ɭɤɚɡɚɬɟɥɶ ɢɦɟɟɬ ɬɢɩ ɭɤɚɡɚɬɟɥɹ ɧɚ ɛɚɡɨɜɵɣ ɤɥɚɫɫ. ɉɭɫɬɶ, ɧɚɩɪɢɦɟɪ, ɜ ɤɥɚɫɫɟ TBase ɨɛɴɹɜɥɟɧɚ ɱɢɫɬɚɹ ɜɢɪɬɭɚɥɶɧɚɹ ɮɭɧɤɰɢɹ print: class TBase // ɛɚɡɨɜɵɣ ɤɥɚɫɫ ɞɥɹ ɦɚɫɫɢɜɨɜ ɜɫɟɯ ɬɢɩɨɜ { int size, //ɪɚɡɦɟɪ ɷɥɟɦɟɧɬɚ count, //ɬɟɤɭɳɟɟ ɱɢɫɥɨ ɷɥɟɦɟɧɬɨɜ maxCount, //ɪɚɡɦɟɪ ɜɵɞɟɥɟɧɧɨɣ ɩɚɦɹɬɢ ɜ ɛɚɣɬɚɯ delta; //ɩɪɢɪɚɳɟɧɢɟ ɩɚɦɹɬɢ ɜ ɛɚɣɬɚɯ char *pmem; //ɭɤɚɡɚɬɟɥɶ ɧɚ ɜɵɞɟɥɟɧɧɭɸ ɩɚɦɹɬɶ int changeSize(); //ɩɟɪɟɪɚɫɩɪɟɞɟɥɟɧɢɟ ɩɚɦɹɬɢ protected: void* getAddr(){return (void*)pmem;}; void addNewItem(void*); //ɞɨɛɚɜɥɟɧɢɟ ɜ ɤɨɧɟɰ ɦɚɫɫɢɜɚ void error(const char* msg){cout<<msg<<endl;}; 50
public: int getCount(){return count;}; TBase(int s,int m,int d); TBase(); TBase(TBase&); ~TBase(); virtual void print()=0; // ɑɢɫɬɚɹ ɜɢɪɬɭɚɥɶɧɚɹ ɮɭɧɤɰɢɹ };
Ɍɨɝɞɚ ɜ ɩɪɨɢɡɜɨɞɧɵɯ ɤɥɚɫɫɚɯ ɞɨɥɠɧɚ ɛɵɬɶ ɨɛɴɹɜɥɟɧɚ ɡɚɦɟɳɚɸɳɚɹ ɟɟ ɮɭɧɤɰɢɹ print, ɜɵɩɨɥɧɹɸɳɚɹ ɪɟɚɥɶɧɵɟ ɞɟɣɫɬɜɢɹ: class TIntArray:public TBase { /* Ⱦɪɭɝɢɟ ɦɟɬɨɞɵ */ virtual void print(); } class TRealArray:public TBase { /* Ⱦɪɭɝɢɟ ɦɟɬɨɞɵ */ virtual void print(); }
ȼ ɩɪɨɝɪɚɦɦɟ, ɢɫɩɨɥɶɡɭɸɳɟɣ ɨɛɴɟɤɬɵ ɤɥɚɫɫɨɜ TIntArray ɢ TRealArray, ɦɨɝɭɬ ɫɨɡɞɚɜɚɬɶɫɹ ɷɤɡɟɦɩɥɹɪɵ ɷɬɢɯ ɤɥɚɫɫɨɜ ɫ ɜɨɡɦɨɠɧɨɫɬɶɸ ɨɛɪɚɳɟɧɢɹ ɤ ɧɢɦ ɱɟɪɟɡ ɭɤɚɡɚɬɟɥɶ ɧɚ ɛɚɡɨɜɵɣ ɤɥɚɫɫ: TBase *pb; TIntArray aint(5,3); TRealArray areal(4,2);
Ɍɨɝɞɚ ɞɥɹ ɩɟɱɚɬɢ ɦɚɫɫɢɜɨɜ ɦɨɝɭɬ ɩɪɢɦɟɧɹɬɶɫɹ ɨɩɟɪɚɬɨɪɵ //ɉɟɱɚɬɶ ɦɚɫɫɢɜɚ aint // ɉɟɱɚɬɶ ɦɚɫɫɢɜɚ areal
pb=&aint; pb->print(); pb=&areal; pb->print();
ɉɪɢɜɟɞɟɦ ɟɳɟ ɨɞɢɧ ɩɪɢɦɟɪ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɜɢɪɬɭɚɥɶɧɵɯ ɮɭɧɤɰɢɣ. ɉɭɫɬɶ ɧɟɤɨɬɨɪɵɣ ɥɸɛɢɬɟɥɶ ɞɨɦɚɲɧɢɯ ɠɢɜɨɬɧɵɯ ɪɟɲɢɥ ɡɚɜɟɫɬɢ ɤɚɬɚɥɨɝ ɫɜɨɢɯ ɥɸɛɢɦɰɟɜ ɢ ɞɥɹ ɤɚɠɞɨɝɨ ɜɢɞɚ ɠɢɜɨɬɧɵɯ ɨɩɪɟɞɟɥɢɥ ɫɜɨɣ ɤɥɚɫɫ ɫ ɨɛɳɢɦ ɛɚɡɨɜɵɦ ɤɥɚɫɫɨɦ Pet. Ⱦɥɹ ɤɪɚɬɤɨɫɬɢ ɨɝɪɚɧɢɱɢɦɫɹ ɜ ɨɩɢɫɚɧɢɢ ɤɚɠɞɨɝɨ ɠɢɜɨɬɧɨɝɨ ɟɝɨ ɤɥɢɱɤɨɣ ɢ ɬɢɩɨɜɵɦ ɢɡɞɚɜɚɟɦɵɦ ɠɢɜɨɬɧɵɦ ɡɜɭɤɨɦ ɫ ɜɨɡɦɨɠɧɨɫɬɶɸ ɜɵɜɨɞɚ ɧɚ ɷɤɪɚɧ ɫɩɢɫɤɚ ɤɥɢɱɟɤ ɢ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɢɡɞɚɜɚɟɦɵɯ ɢɦɢ ɡɜɭɤɨɜ. ɉɪɨɝɪɚɦɦɚ: #include struct Pet // Ȼɚɡɨɜɵɣ ɤɥɚɫɫ { char *name; virtual void speak()=0; Pet( char *nm){name=nm;} } struct Dog:public Pet { virtual void speak() {cout<
Dog(char *nm):Pet(nm){}; }; struct Cat:public Pet { virtual void speak() {cout<speak(); return 0; }
4.6. «Ⱦɪɭɠɟɫɬɜɟɧɧɵɟ» (friend) ɮɭɧɤɰɢɢ Ɏɭɧɤɰɢɹ, ɨɛɴɹɜɥɟɧɧɚɹ ɜ ɩɪɨɢɡɜɨɞɧɨɦ ɤɥɚɫɫɟ, ɦɨɠɟɬ ɢɦɟɬɶ ɞɨɫɬɭɩ ɬɨɥɶɤɨ ɤ ɡɚɳɢɳɟɧɧɵɦ (protected) ɢɥɢ ɨɛɳɢɦ (public) ɤɨɦɩɨɧɟɧɬɚɦ ɛɚɡɨɜɨɝɨ ɤɥɚɫɫɚ. Ɏɭɧɤɰɢɹ, ɨɛɴɹɜɥɟɧɧɚɹ ɜɧɟ ɤɥɚɫɫɚ, ɦɨɠɟɬ ɢɦɟɬɶ ɞɨɫɬɭɩ ɬɨɥɶɤɨ ɤ ɨɛɳɢɦ (public) ɤɨɦɩɨɧɟɧɬɚɦ ɤɥɚɫɫɚ ɢ ɨɛɪɚɳɚɬɶɫɹ ɤ ɧɢɦ ɩɨ ɢɦɟɧɢ, ɭɬɨɱɧɟɧɧɨɦɭ ɢɦɟɧɟɦ ɨɛɴɟɤɬɚ ɢɥɢ ɭɤɚɡɚɬɟɥɹ ɧɚ ɨɛɴɟɤɬ. ɑɬɨɛɵ ɩɨɥɭɱɢɬɶ ɞɨɫɬɭɩ ɤ ɥɢɱɧɵɦ ɤɨɦɩɨɧɟɧɬɚɦ ɨɛɴɟɤɬɨɜ ɧɟɤɨɬɨɪɨɝɨ ɤɥɚɫɫɚ ɏ ɜ ɮɭɧɤɰɢɢ, ɧɟ ɢɦɟɸɳɟɣ ɤ ɧɢɦ ɞɨɫɬɭɩɚ, ɷɬɚ ɮɭɧɤɰɢɹ ɞɨɥɠɧɚ ɛɵɬɶ ɨɛɴɹɜɥɟɧɚ ɞɪɭɠɟɫɬɜɟɧɧɨɣ (friend) ɜ ɤɥɚɫɫɟ X: class X { friend void Y:: fprv( int, char*); /* Ⱦɪɭɝɢɟ ɤɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ X */ } Ɇɨɠɧɨ ɨɛɴɹɜɢɬɶ ɜɫɟ ɮɭɧɤɰɢɢ ɤɥɚɫɫɚ Y ɞɪɭɠɟɫɬɜɟɧɧɵɦɢ ɜ ɤɥɚɫɫɟ X: class Y; class X { friend Y; /* Ⱦɪɭɝɢɟ ɤɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ X */ } class Y { void fy1(int, int); int fy2(char*, int); /* Ⱦɪɭɝɢɟ ɤɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ Y */ }
52
Ⱦɪɭɠɟɫɬɜɟɧɧɨɣ ɦɨɠɟɬ ɛɵɬɶ ɢ ɮɭɧɤɰɢɹ, ɧɟ ɹɜɥɹɸɳɚɹɫɹ ɤɨɦɩɨɧɟɧɬɨɣ ɤɚɤɨɝɨ-ɥɢɛɨ ɤɥɚɫɫɚ, ɧɚɩɪɢɦɟɪ: class XX { friend int printXX(); /* Ⱦɪɭɝɢɟ ɤɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ ɏɏ */ } Ɂɞɟɫɶ ɮɭɧɤɰɢɹ printXX ɢɦɟɟɬ ɞɨɫɬɭɩ ɤɨ ɜɫɟɦ ɤɨɦɩɨɧɟɧɬɚɦ ɤɥɚɫɫɚ XX,
ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɡɚɤɪɟɩɥɟɧɧɨɝɨ ɡɚ ɧɢɦɢ ɭɪɨɜɧɹ ɞɨɫɬɭɩɚ. ȼ ɬɟɨɪɢɢ ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɨɝɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɫɱɢɬɚɟɬɫɹ, ɱɬɨ ɩɪɢ ɯɨɪɨɲɨ ɫɩɪɨɟɤɬɢɪɨɜɚɧɧɨɣ ɫɢɫɬɟɦɟ ɤɥɚɫɫɨɜ ɧɟ ɞɨɥɠɧɨ ɛɵɬɶ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɜ ɞɪɭɠɟɫɬɜɟɧɧɵɯ ɮɭɧɤɰɢɹɯ, ɨɞɧɚɤɨ ɜ ɪɹɞɟ ɫɥɭɱɚɟɜ ɢɯ ɢɫɩɨɥɶɡɨɜɚɧɢɟ ɭɩɪɨɳɚɟɬ ɩɨɧɢɦɚɧɢɟ ɢ ɩɨɫɥɟɞɭɸɳɢɟ ɦɨɞɢɮɢɤɚɰɢɢ ɩɪɨɝɪɚɦɦɵ.
4.7. ɋɬɚɬɢɱɟɫɤɢɟ ɤɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ Ɉɩɢɫɚɬɟɥɶ static ɜ ɋ++ ɢɦɟɟɬ ɪɚɡɥɢɱɧɨɟ ɧɚɡɧɚɱɟɧɢɟ ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɤɨɧɬɟɤɫɬɚ, ɜ ɤɨɬɨɪɨɦ ɨɧ ɩɪɢɦɟɧɟɧ. ɉɟɪɟɦɟɧɧɵɟ ɢ ɮɭɧɤɰɢɢ, ɨɛɴɹɜɥɟɧɧɵɟ ɜɧɟ ɤɥɚɫɫɚ ɢ ɜɧɟ ɬɟɥɚ ɮɭɧɤɰɢɢ ɫ ɨɩɢɫɚɬɟɥɟɦ static, ɢɦɟɸɬ ɨɛɥɚɫɬɶ ɞɟɣɫɬɜɢɹ, ɨɝɪɚɧɢɱɟɧɧɭɸ ɮɚɣɥɨɦ, ɜ ɤɨɬɨɪɨɦ ɨɧɢ ɨɛɴɹɜɥɟɧɵ. ɉɟɪɟɦɟɧɧɵɟ, ɨɛɴɹɜɥɟɧɧɵɟ ɤɚɤ static ɜɧɭɬɪɢ ɮɭɧɤɰɢɢ, ɜɢɞɢɦɵ ɬɨɥɶɤɨ ɜɧɭɬɪɢ ɷɬɨɣ ɮɭɧɤɰɢɢ, ɧɨ ɫɨɯɪɚɧɹɸɬ ɫɜɨɢ ɡɧɚɱɟɧɢɹ ɩɨɫɥɟ ɜɵɯɨɞɚ ɢɡ ɮɭɧɤɰɢɢ ɢ ɢɧɢɰɢɚɥɢɡɢɪɭɸɬɫɹ ɬɨɥɶɤɨ ɩɪɢ ɩɟɪɜɨɦ ɨɛɪɚɳɟɧɢɢ ɤ ɮɭɧɤɰɢɢ. Ʉɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ ɬɚɤɠɟ ɦɨɝɭɬ ɨɛɴɹɜɥɹɬɶɫɹ ɫ ɨɩɢɫɚɬɟɥɟɦ static, ɬɚɤɢɟ ɤɨɦɩɨɧɟɧɬɵ - ɞɚɧɧɵɟ ɹɜɥɹɸɬɫɹ ɨɛɳɢɦɢ ɞɥɹ ɜɫɟɯ ɷɤɡɟɦɩɥɹɪɨɜ ɨɛɴɟɤɬɨɜ ɷɬɨɝɨ ɤɥɚɫɫɚ ɢ ɪɚɡɦɟɳɚɸɬɫɹ ɜ ɩɚɦɹɬɢ ɨɬɞɟɥɶɧɨ ɨɬ ɞɚɧɧɵɯ ɨɛɴɟɤɬɨɜ ɤɥɚɫɫɚ. Ⱦɨɫɬɭɩ ɤ static-ɨɦɩɨɧɟɧɬɚɦ ɤɥɚɫɫɚ ɜɨɡɦɨɠɟɧ ɩɨ ɢɦɟɧɢ, ɭɬɨɱɧɟɧɧɨɦɭ ɢɦɟɧɟɦ ɤɥɚɫɫɚ (ɢɦɟɧɟɦ ɬɢɩɚ) ɢɥɢ ɢɦɟɧɟɦ ɨɛɴɟɤɬɚ ɷɬɨɝɨ ɤɥɚɫɫɚ, ɩɪɢɱɟɦ ɤ static-ɤɨɦɩɨɧɟɧɬɚɦ ɤɥɚɫɫɚ ɦɨɠɧɨ ɨɛɪɚɳɚɬɶɫɹ ɞɨ ɫɨɡɞɚɧɢɹ ɷɤɡɟɦɩɥɹɪɨɜ ɨɛɴɟɤɬɨɜ ɷɬɨɝɨ ɤɥɚɫɫɚ. ɋɬɚɬɢɱɟɫɤɨɟ ɞɚɧɧɨɟ-ɱɥɟɧ ɤɥɚɫɫɚ ɞɨɥɠɧɨ ɛɵɬɶ ɨɛɹɡɚɬɟɥɶɧɨ ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɧɨ ɜɧɟ ɨɩɢɫɚɧɢɹ ɤɥɚɫɫɚ: class TBase //ɛɚɡɨɜɵɣ ɤɥɚɫɫ ɞɥɹ ɦɚɫɫɢɜɨɜ ɜɫɟɯ ɬɢɩɨɜ { static int nw; int size, // ɪɚɡɦɟɪ ɷɥɟɦɟɧɬɚ count, // ɬɟɤɭɳɟɟ ɱɢɫɥɨ ɷɥɟɦɟɧɬɨɜ maxCount, // ɪɚɡɦɟɪ ɜɵɞɟɥɟɧɧɨɣ ɩɚɦɹɬɢ delta; //ɩɪɢɪɚɳɟɧɢɟ ɩɚɦɹɬɢ /* Ⱦɪɭɝɢɟ ɤɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ TBase */ } int TBase::nw=1; /* ɂɧɢɰɢɚɥɢɡɚɰɢɹ ɫɬɚɬɢɱɟɫɤɨɣ ɤɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ */
53
ɋɬɚɬɢɱɟɫɤɢɟ ɤɨɦɩɨɧɟɧɬɵ-ɮɭɧɤɰɢɢ ɦɨɝɭɬ ɜɵɡɵɜɚɬɶɫɹ ɞɨ ɫɨɡɞɚɧɢɹ ɷɤɡɟɦɩɥɹɪɨɜ ɨɛɴɟɤɬɨɜ ɷɬɨɝɨ ɤɥɚɫɫɚ ɢ ɩɨɷɬɨɦɭ ɢɦɟɸɬ ɞɨɫɬɭɩ ɬɨɥɶɤɨ ɤ ɫɬɚɬɢɱɟɫɤɢɦ ɞɚɧɧɵɦ ɤɥɚɫɫɚ: class X { static int sx1, sx2; static void fsx(int k); int x1, x2; /* Ⱦɪɭɝɢɟ ɤɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ X */ } int X::sx1=1; int X::sx2=2; int main() { ... X::fsx(3); ... }
4.8. ɉɟɪɟɨɩɪɟɞɟɥɟɧɢɟ (ɩɟɪɟɝɪɭɡɤɚ) ɨɩɟɪɚɰɢɣ ȼ ɹɡɵɤɚɯ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɨɩɪɟɞɟɥɟɧɚ ɫɟɦɚɧɬɢɤɚ ɨɩɟɪɚɰɢɣ, ɜɵɩɨɥɧɹɟɦɵɯ ɧɚɞ ɛɚɡɨɜɵɦɢ (ɩɪɟɞɨɩɪɟɞɟɥɟɧɧɵɦɢ) ɬɢɩɚɦɢ ɞɚɧɧɵɯ, ɧɚɩɪɢɦɟɪ, ɟɫɥɢ x, y ɢ z - ɩɟɪɟɦɟɧɧɵɟ ɬɢɩɚ float, ɬɨ ɡɚɩɢɫɶ x=y+z; ɩɪɟɞɩɨɥɚɝɚɟɬ ɢɧɬɭɢɬɢɜɧɨ ɨɱɟɜɢɞɧɵɟ ɞɟɣɫɬɜɢɹ: ɫɥɨɠɟɧɢɟ x ɢ y ɢ ɩɪɢɫɜɚɢɜɚɧɢɟ ɩɟɪɟɦɟɧɧɨɣ z ɩɨɥɭɱɟɧɧɨɣ ɫɭɦɦɵ. ɀɟɥɚɬɟɥɶɧɨ ɛɵɥɨ ɛɵ ɢ ɞɥɹ ɬɢɩɨɜ, ɨɩɪɟɞɟɥɹɟɦɵɯ ɜ ɩɪɨɝɪɚɦɦɟ, ɜ ɬɨɦ ɱɢɫɥɟ ɞɥɹ ɤɥɚɫɫɨɜ, ɨɩɪɟɞɟɥɢɬɶ ɫɟɦɚɧɬɢɤɭ ɢ ɚɥɝɨɪɢɬɦɵ ɨɩɟɪɚɰɢɣ ɫɥɨɠɟɧɢɹ, ɜɵɱɢɬɚɧɢɹ, ɭɦɧɨɠɟɧɢɹ ɢ ɬɚɤ ɞɚɥɟɟ, ɱɬɨɛɵ ɢɦɟɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɜɦɟɫɬɨ ɜɵɡɨɜɚ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɯ ɮɭɧɤɰɢɣ ɡɚɩɢɫɵɜɚɬɶ ɩɪɨɫɬɨ x+y ɢ ɜ ɫɥɭɱɚɟ, ɤɨɝɞɚ x ɢ y ɹɜɥɹɸɬɫɹ ɨɛɴɟɤɬɚɦɢ ɧɟɤɨɬɨɪɵɯ ɤɥɚɫɫɨɜ. ȼ C++ ɷɬɨ ɞɨɫɬɢɝɚɟɬɫɹ ɩɟɪɟɨɩɪɟɞɟɥɟɧɢɟɦ ɢɦɟɸɳɢɯɫɹ ɜ ɹɡɵɤɟ ɨɩɟɪɚɰɢɣ ɞɥɹ ɞɪɭɝɢɯ ɬɢɩɨɜ ɞɚɧɧɵɯ. ɉɟɪɟɨɩɪɟɞɟɥɟɧɧɚɹ ɨɩɟɪɚɰɢɹ ɨɛɴɹɜɥɹɟɬɫɹ ɬɚɤ: ɬɢɩ_ɪɟɡɭɥɶɬɚɬɚ operator ɡɧɚɤ_ɨɩɟɪɚɰɢɢ (ɮɨɪɦɚɥɶɧɵɟ ɩɚɪɚɦɟɬɪɵ) { ɨɩɢɫɚɧɢɟ_ɚɥɝɨɪɢɬɦɚ_ɜɵɩɨɥɧɟɧɢɹ_ɨɩɟɪɚɰɢɢ }
ɇɚɩɪɢɦɟɪ: class TPoint { int x,y; public: TPoint& operator += (const TPoint& adder); TPoint& operator -= (const TPoint& subber); friend TPoint operator - (const TPoint& one, const TPoint& two); 54
friend TPoint operator + (const TPoint& one, const TPoint& two); friend int operator == (const TPoint& one, const TPoint& two); friend int operator != (const TPoint& one, const TPoint& two); }; ɉɨɥɧɨɟ ɨɩɪɟɞɟɥɟɧɢɟ ɷɬɢɯ ɨɩɟɪɚɰɢɣ ɞɥɹ ɨɛɴɟɤɬɨɜ ɤɥɚɫɫɚ TPoint ɢɦɟɟɬ ɜɢɞ inline TPoint& TPoint::operator += (const TPoint& adder) { x+=adder.x; y+=adder.y; return *this; } inline TPoint& TPoint::operator -= (const TPoint& subber) { x-=subber.x; y-=subber.y; return *this; }
Ɉɫɬɚɥɶɧɵɟ ɨɩɟɪɚɰɢɢ ɨɩɪɟɞɟɥɹɸɬɫɹ ɚɧɚɥɨɝɢɱɧɵɦ ɨɛɪɚɡɨɦ. ɉɭɫɬɶ ɜ ɩɪɨɝɪɚɦɦɟ ɢɦɟɸɬɫɹ ɨɛɴɹɜɥɟɧɢɹ: TPoint x(12,3), y(21,30), z(18,30);
Ɍɨɝɞɚ ɦɨɠɧɨ ɡɚɩɢɫɚɬɶ x+=y; y-=z; TPoint r=x+z:
Ɉɛɳɢɟ ɩɪɚɜɢɥɚ ɩɟɪɟɨɩɪɟɞɟɥɟɧɢɹ ɨɩɟɪɚɰɢɣ ɫɜɨɞɹɬɫɹ ɤ ɫɥɟɞɭɸɳɟɦɭ: x Ⱦɜɭɦɟɫɬɧɵɟ ɨɩɟɪɚɰɢɢ ɞɨɥɠɧɵ ɢɦɟɬɶ ɞɜɚ ɩɚɪɚɦɟɬɪɚ, ɨɞɧɨɦɟɫɬɧɵɟ – ɨɞɢɧ ɩɚɪɚɦɟɬɪ, ɩɪɢɱɟɦ ɟɫɥɢ ɨɩɟɪɚɰɢɹ ɨɛɴɹɜɥɟɧɚ ɤɚɤ ɤɨɦɩɨɧɟɧɬɚ ɤɥɚɫɫɚ, ɬɨ ɧɟɹɜɧɵɦ ɩɟɪɜɵɦ ɨɩɟɪɚɧɞɨɦ ɹɜɥɹɟɬɫɹ ɷɤɡɟɦɩɥɹɪ ɨɛɴɟɤɬɚ (ɫɥɟɞɨɜɚɬɟɥɶɧɨ, ɩɪɢ ɨɩɪɟɞɟɥɟɧɢɢ ɞɜɭɦɟɫɬɧɨɣ ɨɩɟɪɚɰɢɢ ɛɭɞɟɬ ɡɚɞɚɜɚɬɶɫɹ ɨɞɢɧ ɩɚɪɚɦɟɬɪ, ɨɞɧɨɦɟɫɬɧɚɹ ɨɩɟɪɚɰɢɹ ɨɛɴɹɜɥɹɟɬɫɹ ɫ ɩɭɫɬɵɦ ɫɩɢɫɤɨɦ ɩɚɪɚɦɟɬɪɨɜ). ȿɫɥɢ ɨɩɟɪɚɰɢɹ ɩɟɪɟɨɩɪɟɞɟɥɹɟɬɫɹ ɜɧɟ ɤɥɚɫɫɚ (ɫ ɨɩɢɫɚɬɟɥɟɦ friend), ɬɨ ɞɥɹ ɞɜɭɦɟɫɬɧɨɣ ɨɩɟɪɚɰɢɢ ɞɨɥɠɧɵ ɛɵɬɶ ɡɚɞɚɧɵ ɞɜɚ ɩɚɪɚɦɟɬɪɚ, ɞɥɹ ɨɞɧɨɦɟɫɬɧɨɣ ɨɩɟɪɚɰɢɢ – ɨɞɢɧ ɩɚɪɚɦɟɬɪ. x ɉɪɢ ɩɟɪɟɨɩɪɟɞɟɥɟɧɢɢ ɫɨɯɪɚɧɹɟɬɫɹ ɩɪɢɨɪɢɬɟɬ ɢɫɯɨɞɧɨɣ ɨɩɟɪɚɰɢɢ, ɬɨ ɟɫɬɶ ɨɩɟɪɚɰɢɹ «+» ɛɭɞɟɬ ɜɵɩɨɥɧɹɬɶɫɹ ɪɚɧɶɲɟ ɨɩɟɪɚɰɢɢ «=» ɢ ɬɚɤ ɞɚɥɟɟ. x ɉɪɢ ɩɟɪɟɨɩɪɟɞɟɥɟɧɢɢ ɧɟ ɧɚɫɥɟɞɭɸɬɫɹ ɫɜɨɣɫɬɜɚ ɤɨɦɦɭɬɚɬɢɜɧɨɫɬɢ ɢ ɚɫɫɨɰɢɚɬɢɜɧɨɫɬɢ, ɬɨ ɟɫɬɶ ɪɟɡɭɥɶɬɚɬ ɜɵɪɚɠɟɧɢɹ ɯ+y-z ɦɨɠɟɬ ɨɬɥɢɱɚɬɶɫɹ ɨɬ ɪɟɡɭɥɶɬɚɬɚ ɜɵɪɚɠɟɧɢɹ y-z+x ɢ ɡɚɜɢɫɢɬ ɨɬ ɬɨɝɨ, ɤɚɤ ɨɩɪɟɞɟɥɟɧɵ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɨɩɟɪɚɰɢɢ. x ɇɟ ɞɨɩɭɫɤɚɟɬɫɹ ɩɟɪɟɨɩɪɟɞɟɥɟɧɢɟ ɨɩɟɪɚɰɢɣ «.» (ɬɨɱɤɚ), «.*» (ɨɛɪɚɳɟɧɢɟ ɤ ɭɤɚɡɚɬɟɥɸ ɧɚ ɤɨɦɩɨɧɟɧɬɭ ɤɥɚɫɫɚ ɢɥɢ ɫɬɪɭɤɬɭɪɵ), «::» (ɪɚɡɪɟɲɟɧɢɟ ɤɨɧɬɟɤɫɬɚ), ɚ ɬɚɤɠɟ ɨɩɟɪɚɰɢɣ «#» ɢ «##», ɢɫɩɨɥɶɡɭɟɦɵɯ ɩɪɢ ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɣ ɨɛɪɚɛɨɬɤɟ. x ɉɟɪɟɨɩɪɟɞɟɥɹɟɦɵɟ ɨɩɟɪɚɰɢɢ «=» (ɩɪɢɫɜɚɢɜɚɧɢɟ), «()» (ɮɭɧɤɰɢɹ), «[]» (ɢɧɞɟɤɫ), «->» (ɨɛɪɚɳɟɧɢɟ ɤ ɤɨɦɩɨɧɟɧɬɟ ɤɥɚɫɫɚ ɩɨ ɭɤɚɡɚɬɟɥɸ) ɜɫɟɝɞɚ ɞɨɥɠɧɵ ɛɵɬɶ ɤɨɦɩɨɧɟɧɬɚɦɢ ɤɥɚɫɫɚ ɢ ɧɟ ɦɨɝɭɬ ɛɵɬɶ static. 55
x ɉɟɪɟɨɩɪɟɞɟɥɹɟɦɵɟ ɨɩɟɪɚɰɢɢ new ɢ delete ɞɨɥɠɧɵ ɛɵɬɶ staticɤɨɦɩɨɧɟɧɬɚɦɢ ɤɥɚɫɫɚ. ȼ ɨɫɬɚɥɶɧɨɦ ɤ ɩɟɪɟɨɩɪɟɞɟɥɹɟɦɵɦ ɨɩɟɪɚɰɢɹɦ ɩɪɟɞɴɹɜɥɹɸɬɫɹ ɬɟ ɠɟ ɬɪɟɛɨɜɚɧɢɹ, ɱɬɨ ɢ ɤ ɮɭɧɤɰɢɹɦ.
5. ɒɚɛɥɨɧɵ ɮɭɧɤɰɢɣ ɢ ɤɥɚɫɫɨɜ 5.1. ɒɚɛɥɨɧɵ ɮɭɧɤɰɢɣ ɑɚɫɬɨ ɜɫɬɪɟɱɚɸɬɫɹ ɮɭɧɤɰɢɢ, ɪɟɚɥɢɡɭɸɳɢɟ ɨɞɧɢ ɢ ɬɟ ɠɟ ɞɟɣɫɬɜɢɹ ɞɥɹ ɚɪɝɭɦɟɧɬɨɜ ɪɚɡɥɢɱɧɵɯ ɬɢɩɨɜ. ɇɚɩɪɢɦɟɪ, ɫɨɪɬɢɪɨɜɤɚ ɦɚɫɫɢɜɚ ɩɨ ɜɨɡɪɚɫɬɚɧɢɸ ɟɝɨ ɷɥɟɦɟɧɬɨɜ ɦɨɠɟɬ ɜɵɩɨɥɧɹɬɶɫɹ ɨɞɧɢɦ ɢ ɬɟɦ ɠɟ ɦɟɬɨɞɨɦ ɢ ɞɥɹ ɞɚɧɧɵɯ ɬɢɩɚ int, ɢ ɞɥɹ ɞɚɧɧɵɯ ɬɢɩɚ double. Ɋɚɡɥɢɱɢɟ ɫɨɫɬɨɢɬ ɬɨɥɶɤɨ ɜ ɬɢɩɚɯ ɩɚɪɚɦɟɬɪɨɜ ɢ ɧɟɤɨɬɨɪɵɯ ɜɧɭɬɪɟɧɧɢɯ ɩɟɪɟɦɟɧɧɵɯ. ȼ ɛɨɥɟɟ ɩɨɡɞɧɢɟ ɜɟɪɫɢɢ ɹɡɵɤɚ ɋ++ ɜɤɥɸɱɟɧɨ ɫɩɟɰɢɚɥɶɧɨɟ ɫɪɟɞɫɬɜɨ, ɩɨɡɜɨɥɹɸɳɟɟ ɩɚɪɚɦɟɬɪɢɡɨɜɚɬɶ ɨɩɪɟɞɟɥɟɧɢɟ ɮɭɧɤɰɢɢ, ɱɬɨɛɵ ɤɨɦɩɢɥɹɬɨɪ ɦɨɝ ɩɨɫɬɪɨɢɬɶ ɤɨɧɤɪɟɬɧɭɸ ɪɟɚɥɢɡɚɰɢɸ ɮɭɧɤɰɢɢ ɞɥɹ ɭɤɚɡɚɧɧɨɝɨ ɬɢɩɚ ɩɚɪɚɦɟɬɪɨɜ ɮɭɧɤɰɢɢ. ɉɚɪɚɦɟɬɪɢɡɨɜɚɧɧɨɟ ɨɩɪɟɞɟɥɟɧɢɟ ɮɭɧɤɰɢɢ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ: template Ɂɚɝɨɥɨɜɨɤ ɮɭɧɤɰɢɢ { /* Ɍɟɥɨ ɮɭɧɤɰɢɢ */ }
ɂɦɹ ɤɥɚɫɫɚ ɹɜɥɹɟɬɫɹ ɩɚɪɚɦɟɬɪɨɦ ɢ ɡɚɞɚɟɬɫɹ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɦ, ɥɨɤɚɥɢɡɨɜɚɧɧɵɦ ɜ ɩɪɟɞɟɥɚɯ ɨɩɪɟɞɟɥɟɧɢɹ ɮɭɧɤɰɢɢ. ɏɨɬɹ ɛɵ ɨɞɢɧ ɢɡ ɩɚɪɚɦɟɬɪɨɜ ɮɭɧɤɰɢɢ ɞɨɥɠɟɧ ɢɦɟɬɶ ɬɢɩ, ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɣ ɷɬɨɦɭ ɢɞɟɧɬɢɮɢɤɚɬɨɪɭ. ɉɚɪɚɦɟɬɪɢɡɨɜɚɧɧɨɟ ɨɩɪɟɞɟɥɟɧɢɟ ɮɭɧɤɰɢɢ ɫɨɪɬɢɪɨɜɤɢ ɦɚɫɫɢɜɚ ɦɟɬɨɞɨɦ ɩɟɪɟɫɬɚɧɨɜɨɤ ɦɨɠɟɬ ɛɵɬɶ ɩɨɫɬɪɨɟɧɨ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ: template void sort (T a[], int n) { T temp; int sign; for (int k=0; k>n; k++) { sign = 0; for (i=0; ia[i+1]) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; sign++; } 56
if (sign==0) break; } return; }
ȿɫɥɢ ɜ ɩɪɨɝɪɚɦɦɟ ɛɭɞɭɬ ɨɛɴɹɜɥɟɧɵ ɦɚɫɫɢɜɵ int aint[10]; double afl[20];
ɢ ɭɫɬɚɧɨɜɥɟɧɵ ɡɧɚɱɟɧɢɹ ɷɥɟɦɟɧɬɨɜ ɷɬɢɯ ɦɚɫɫɢɜɨɜ, ɬɨ ɜɵɡɨɜ ɮɭɧɤɰɢɢ sort(aint, 10);
ɨɛɟɫɩɟɱɢɬ ɜɵɡɨɜ sort ɞɥɹ ɭɩɨɪɹɞɨɱɟɧɢɹ ɦɚɫɫɢɜɚ ɰɟɥɵɯ, ɚ ɜɵɡɨɜ ɮɭɧɤɰɢɢ sort(afl, 20);
ɨɛɟɫɩɟɱɢɬ ɜɵɡɨɜ sort ɞɥɹ ɭɩɨɪɹɞɨɱɟɧɢɹ ɦɚɫɫɢɜɚ ɫ ɷɥɟɦɟɧɬɚɦɢ ɬɢɩɚ double.
ȿɫɥɢ ɷɥɟɦɟɧɬɚɦɢ ɦɚɫɫɢɜɚ ɹɜɥɹɸɬɫɹ ɨɛɴɟɤɬɵ ɤɚɤɨɝɨ-ɥɢɛɨ ɨɩɪɟɞɟɥɟɧɧɨɝɨ ɩɪɨɝɪɚɦɦɢɫɬɨɦ ɤɥɚɫɫɚ, ɞɥɹ ɤɨɬɨɪɨɝɨ ɨɩɪɟɞɟɥɟɧɚ ɨɩɟɪɚɰɢɹ ɨɬɧɨɲɟɧɢɹ «>», ɬɨ ɮɭɧɤɰɢɹ sort ɦɨɠɟɬ ɛɵɬɶ ɜɵɡɜɚɧɚ ɢ ɞɥɹ ɬɚɤɨɝɨ ɦɚɫɫɢɜɚ. Ɋɚɡɭɦɟɟɬɫɹ, ɜ ɨɛɴɟɤɬɧɨɦ ɤɨɞɟ ɩɪɨɝɪɚɦɦɵ ɛɭɞɭɬ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɜɫɟ ɜɚɪɢɚɧɬɵ ɪɟɚɥɶɧɨ ɜɵɡɵɜɚɦɨɣ ɮɭɧɤɰɢɢ sort. ɉɚɪɚɦɟɬɪɢɡɚɰɢɹ ɮɭɧɤɰɢɢ ɫɨɤɪɚɳɚɟɬ ɨɛɴɟɦ ɢɫɯɨɞɧɨɝɨ ɬɟɤɫɬɚ ɩɪɨɝɪɚɦɦɵ ɢ ɩɨɜɵɲɚɟɬ ɟɝɨ ɧɚɞɟɠɧɨɫɬɶ. ȼ ɨɩɢɫɚɬɟɥɟ template ɦɨɠɧɨ ɭɤɚɡɵɜɚɬɶ ɧɟɫɤɨɥɶɤɨ ɩɚɪɚɦɟɬɪɨɜ ɜɢɞɚ class ɢɦɹ_ɬɢɩɚ,
ɚ ɬɚɤɠɟ ɩɚɪɚɦɟɬɪɵ ɛɚɡɨɜɵɯ ɬɢɩɨɜ. ɇɚɩɪɢɦɟɪ, ɮɭɧɤɰɢɹ template void copy(T1 a[], T2 b[], int n) { for (int i=0; i
ɱɬɨɛɵ ɬɚɤɨɟ ɤɨɩɢɪɨɜɚɧɢɟ ɛɵɥɨ ɜɨɡɦɨɠɧɵɦ.
5.2. ɒɚɛɥɨɧɵ ɤɥɚɫɫɨɜ ɉɨ ɚɧɚɥɨɝɢɢ ɫ ɩɚɪɚɦɟɬɪɢɡɨɜɚɧɧɨɣ ɮɭɧɤɰɢɟɣ ɦɨɠɧɨ ɩɨɫɬɪɨɢɬɶ ɩɚɪɚɦɟɬɪɢɡɨɜɚɧɧɨɟ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ, ɩɨɡɜɨɥɹɸɳɟɟ ɫɨɡɞɚɜɚɬɶ ɷɤɡɟɦɩɥɹɪɵ ɤɥɚɫɫɨɜ ɞɥɹ ɤɨɧɤɪɟɬɧɵɯ ɡɧɚɱɟɧɢɣ ɩɚɪɚɦɟɬɪɨɜ. ɉɚɪɚɦɟɬɪɢɡɨɜɚɧɧɵɣ ɤɥɚɫɫ ɨɩɢɫɵɜɚɟɬɫɹ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ: template class ɨɩɢɫɚɧɢɟ_ɤɥɚɫɫɚ
Ʉɚɤ ɢ ɞɥɹ ɮɭɧɤɰɢɣ, ɜ ɨɩɢɫɚɬɟɥɟ template ɦɨɠɟɬ ɛɵɬɶ ɡɚɞɚɧɨ ɧɟɫɤɨɥɶɤɨ ɩɚɪɚɦɟɬɪɨɜ. ȼ ɫɚɦɨɦ ɨɩɢɫɚɧɢɢ ɤɥɚɫɫɚ ɢɦɟɧɚ ɩɚɪɚɦɟɬɪɨɜ ɢɫɩɨɥɶɡɭɸɬɫɹ ɤɚɤ ɢɦɟɧɚ ɬɢɩɨɜ ɞɚɧɧɵɯ, ɬɢɩɨɜ ɩɚɪɚɦɟɬɪɨɜ ɮɭɧɤɰɢɣ ɢ ɬɢɩɨɜ ɡɧɚɱɟɧɢɣ, ɜɨɡɜɪɚɳɚɟɦɵɯ ɮɭɧɤɰɢɹɦɢ.
57
ȼ ɤɚɱɟɫɬɜɟ ɩɪɢɦɟɪɚ ɩɪɢɜɟɞɟɦ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ stack, ɩɪɟɞɧɚɡɧɚɱɟɧɧɨɝɨ ɞɥɹ ɩɨɫɬɪɨɟɧɢɹ ɫɬɟɤɨɜ ɮɢɤɫɢɪɨɜɚɧɧɨɝɨ ɦɚɤɫɢɦɚɥɶɧɨɝɨ ɪɚɡɦɟɪɚ ɫ ɷɥɟɦɟɧɬɚɦɢ ɩɪɨɢɡɜɨɥɶɧɨɝɨ ɬɢɩɚ. enum BOOLEAN {FALSE, TRUE}; const int EMPTY=-1; template class stack { private: Type* s; // ɍɤɚɡɚɬɟɥɶ ɧɚ ɦɚɫɫɢɜ ɫɬɟɤɚ int max_len; // Ɇɚɤɫɢɦɚɥɶɧɚɹ ɞɥɢɧɚ ɫɬɟɤɚ int top; // ɂɧɞɟɤɫ ɷɥɟɦɟɧɬɚ ɜ ɜɟɪɲɢɧɟ ɫɬɟɤɚ public: stack():max_len(100) /* ɤɨɧɫɬɪɭɤɬɨɪ ɛɟɡ ɩɚɪɚɦɟɬɪɨɜ {s=new Type[100]; top=EMPTY;} stack(int size):max_len(size) // ȼɬɨɪɨɣ ɤɨɧɫɬɪɭɤɬɨɪ {s=new Type[size]; top=EMPTY;} ~stack() {delete [] s;} // Ⱦɟɫɬɪɭɤɬɨɪ void reset() {top=EMPTY;} // Ɉɱɢɫɬɢɬɶ ɫɬɟɤ void push(Type c) {s[++top]=c;} Type pop() {return s[top—];} Type top_of() {return s[top];} BOOLEAN empty() {return BOOLEAN(top==EMPTY);} BOOLEAN full() {return BOOLEAN(top==max_len);} };
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɜ ɷɬɨɦ ɩɪɢɦɟɪɟ ɫ ɰɟɥɶɸ ɫɨɤɪɚɳɟɧɢɹ ɢɫɯɨɞɧɨɝɨ ɬɟɤɫɬɚ ɧɟ ɩɪɟɞɭɫɦɨɬɪɟɧ ɤɨɧɬɪɨɥɶ ɜɵɯɨɞɚ ɡɚ ɩɪɟɞɟɥɵ ɫɬɟɤɚ ɜ ɦɟɬɨɞɚɯ push ɢ pop. ɑɬɨɛɵ ɫɨɡɞɚɬɶ ɷɤɡɟɦɩɥɹɪ ɩɚɪɚɦɟɬɪɢɡɨɜɚɧɧɨɝɨ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ, ɧɭɠɧɨ ɭɬɨɱɧɢɬɶ ɢɦɹ ɬɢɩɚ ɡɧɚɱɟɧɢɟɦ ɩɚɪɚɦɟɬɪɚ ɜ ɭɝɥɨɜɵɯ ɫɤɨɛɤɚɯ: stack stack_of_int(50); stack <myClass> stmc(20);
/* ɋɬɟɤ ɬɢɩɚ * ɋɬɟɤ ɬɢɩɚ
ɧɚ 50 ɷɥɟɦɟɧɬɨɜ int */ ɧɚ 20 ɷɥɟɦɟɧɬɨɜ myClass */
ȼ ɩɪɢɜɟɞɟɧɧɨɦ ɩɪɢɦɟɪɟ ɜɫɟ ɤɨɦɩɨɧɟɧɬɵ-ɮɭɧɤɰɢɢ ɨɩɪɟɞɟɥɟɧɵ ɜ ɨɩɢɫɚɧɢɢ ɤɥɚɫɫɚ. Ʉɨɝɞɚ ɩɨɥɧɨɟ ɨɩɪɟɞɟɥɟɧɢɟ ɮɭɧɤɰɢɢ-ɱɥɟɧɚ ɤɥɚɫɫɚ ɡɚɞɚɟɬɫɹ ɜɧɟ ɨɩɢɫɚɧɢɹ ɤɥɚɫɫɚ, ɨɧɨ ɞɨɥɠɧɨ ɭɬɨɱɧɹɬɶɫɹ ɨɩɢɫɚɬɟɥɟɦ template. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɛɵ ɦɟɬɨɞ top_of ɛɵɥ ɨɩɪɟɞɟɥɟɧ ɜɧɟ ɨɩɢɫɚɧɢɹ ɤɥɚɫɫɚ, ɨɩɪɟɞɟɥɟɧɢɟ ɢɦɟɥɨ ɛɵ ɜɢɞ: template Type top_of() {return s[top];}
Ɉɬɦɟɬɢɦ ɧɟɤɨɬɨɪɵɟ ɫɩɟɰɢɮɢɱɟɫɤɢɟ ɱɟɪɬɵ ɨɩɢɫɚɧɢɣ ɩɚɪɚɦɟɬɪɢɡɨɜɚɧɧɵɯ ɤɥɚɫɫɨɜ: x ȿɫɥɢ ɜ ɩɚɪɚɦɟɬɪɢɡɨɜɚɧɧɨɦ ɤɥɚɫɫɟ ɨɩɪɟɞɟɥɟɧɵ friend-ɮɭɧɤɰɢɢ, ɬɨ, ɤɨɝɞɚ ɬɚɤɚɹ ɮɭɧɤɰɢɹ ɧɟ ɡɚɜɢɫɢɬ ɨɬ ɩɚɪɚɦɟɬɪɚ, ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɟɞɢɧɫɬɜɟɧɧɚɹ friend-ɮɭɧɤɰɢɹ ɞɥɹ ɜɫɟɯ ɡɧɚɱɟɧɢɣ ɩɚɪɚɦɟɬɪɚ, ɚ ɤɨɝɞɚ 58
friend-ɮɭɧɤɰɢɹ ɡɚɜɢɫɢɬ ɨɬ ɩɚɪɚɦɟɬɪɚ, ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɫɜɨɹ friend-ɮɭɧɤɰɢɹ ɞɥɹ ɤɚɠɞɨɝɨ ɡɧɚɱɟɧɢɹ ɩɚɪɚɦɟɬɪɚ. x ȿɫɥɢ ɜ ɩɚɪɚɦɟɬɪɢɡɨɜɚɧɧɨɦ ɤɥɚɫɫɟ ɢɦɟɸɬɫɹ ɫɬɚɬɢɱɟɫɤɢɟ (static)
ɤɨɦɩɨɧɟɧɬɵ, ɬɨ ɞɥɹ ɤɚɠɞɨɝɨ ɡɧɚɱɟɧɢɹ ɩɚɪɚɦɟɬɪɚ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɫɜɨɣ ɷɤɡɟɦɩɥɹɪ ɫɬɚɬɢɱɟɫɤɨɣ ɤɨɦɩɨɧɟɧɬɵ.
6. ɉɪɢɥɨɠɟɧɢɹ 6.1. Ɂɚɞɚɱɢ ɩɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɸ ɧɚ ɹɡɵɤɟ C++ 1. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɩɨɞɫɱɟɬɚ ɱɢɫɥɚ ɫɟɪɢɣ ɞɥɢɧɨɣ ɧɟ ɦɟɧɟɟ Ʉ ɩɨɥɨɠɢɬɟɥɶɧɵɯ, ɨɬɪɢɰɚɬɟɥɶɧɵɯ ɱɢɫɟɥ ɢ ɧɭɥɟɣ ɜ ɨɞɧɨɦɟɪɧɨɦ ɦɚɫɫɢɜɟ ɰɟɥɵɯ ɱɢɫɟɥ. ɋɟɪɢɟɣ ɧɚɡɵɜɚɟɬɫɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ, ɩɪɢɧɚɞɥɟɠɚɳɢɯ ɨɞɧɨɦɭ ɤɥɚɫɫɭ: int series(int n, int *mas, int *kzero, int *kplus, int *kminus, int k);
2. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɫɥɢɹɧɢɹ ɞɜɭɯ ɭɩɨɪɹɞɨɱɟɧɧɵɯ ɩɨ ɜɨɡɪɚɫɬɚɧɢɸ ɦɚɫɫɢɜɨɜ ɰɟɥɵɯ ɱɢɫɟɥ: int merge(int n, int m, int *mas1, int *mas2, int *res);
3. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɩɨɫɬɪɨɟɧɢɹ ɫɩɢɫɤɚ ɢɧɞɟɤɫɨɜ (ɧɨɦɟɪɨɜ), ɫɬɪɨɤ ɭɩɨɪɹɞɨɱɟɧɧɨɝɨ ɩɨ ɜɨɡɪɚɫɬɚɧɢɸ ɷɥɟɦɟɧɬɨɜ ɡɚɞɚɧɧɨɝɨ (k-ɝɨ) ɫɬɨɥɛɰɚ ɦɚɬɪɢɰɵ. ɗɥɟɦɟɧɬɵ ɦɚɬɪɢɰɵ - ɰɟɥɵɟ ɱɢɫɥɚ: void sort(int n, int *mas, int k, int* index);
4. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɨɩɪɟɞɟɥɟɧɢɹ ɷɥɟɦɟɧɬɚ ɦɚɬɪɢɰɵ, ɹɜɥɹɸɳɟɝɨɫɹ ɫɟɞɥɨɜɨɣ ɬɨɱɤɨɣ. ɋɟɞɥɨɜɨɣ ɬɨɱɤɨɣ ɧɚɡɵɜɚɟɬɫɹ ɷɥɟɦɟɧɬ, ɭɞɨɜɥɟɬɜɨɪɹɸɳɢɣ ɭɫɥɨɜɢɹɦ: a[k,l]=max min(a[i,j])=min max(a[i,j]) i<=n,j<=m j<=m,i<=n
ȿɫɥɢ ɫɟɞɥɨɜɨɣ ɬɨɱɤɢ ɧɟɬ, ɭɫɬɚɧɨɜɢɬɶ k=l=-1. 5. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɩɨɞɫɱɟɬɚ ɤɨɥɢɱɟɫɬɜɚ ɪɚɡɥɢɱɧɵɯ ɱɢɫɟɥ ɜ ɦɚɫɫɢɜɟ, ɫɨɞɟɪɠɚɳɟɦ n ɰɟɥɵɯ ɱɢɫɟɥ. int count(int *a, int n); 59
6. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɪɚɡɞɟɥɟɧɢɹ ɬɟɤɫɬɚ, ɡɚɞɚɧɧɨɝɨ ɫɬɪɨɤɨɣ ɥɢɬɟɪ, ɧɚ ɨɬɞɟɥɶɧɵɟ ɫɥɨɜɚ ɢ ɩɨɞɫɱɟɬɚ ɱɢɫɥɚ ɫɥɨɜ. ɉɨɞ ɫɥɨɜɨɦ ɩɨɧɢɦɚɟɬɫɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɥɢɬɟɪ, ɨɬɥɢɱɧɵɯ ɨɬ ɩɪɨɛɟɥɚ, ɨɝɪɚɧɢɱɟɧɧɚɹ ɫɥɟɜɚ ɧɚɱɚɥɨɦ ɫɬɪɨɤɢ, ɢɥɢ ɩɪɨɛɟɥɨɦ, ɢ ɫɩɪɚɜɚ – ɩɪɨɛɟɥɨɦ, ɡɧɚɤɨɦ ɩɪɟɩɢɧɚɧɢɹ ɢɥɢ ɤɨɧɰɨɦ ɫɬɪɨɤɢ. int kwords(char* ss, char* sm, int kmax); ss – ɢɫɯɨɞɧɚɹ ɫɬɪɨɤɚ, sm – ɦɚɫɫɢɜ ɫɬɪɨɤ ɞɥɢɧɨɣ ɞɨ 30 ɥɢɬɟɪ ɤɚɠɞɚɹ (ɞɥɹ ɪɚɡɦɟɳɟɧɢɹ ɜɵɞɟɥɟɧɧɵɯ ɫɥɨɜ), kmax – ɦɚɤɫɢɦɚɥɶɧɨɟ ɤɨɥɢɱɟɫɬɜɨ ɜɵɞɟɥɟɧɧɵɯ ɫɥɨɜ.
ɉɪɟɞɭɫɦɨɬɪɟɬɶ ɫɢɝɧɚɥɢɡɚɰɢɸ ɨ ɫɥɭɱɚɹɯ, ɤɨɝɞɚ ɮɭɧɤɰɢɹ ɧɟɩɪɢɦɟɧɢɦɚ (ɫɥɢɲɤɨɦ ɦɧɨɝɨ ɫɥɨɜ ɢɥɢ ɫɥɢɲɤɨɦ ɞɥɢɧɧɨɟ ɫɥɨɜɨ). 7. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɨɩɪɟɞɟɥɟɧɢɹ ɚ) ɧɚɢɛɨɥɶɲɟɝɨ ɩɪɨɫɬɨɝɨ ɱɢɫɥɚ, ɧɟ ɩɪɟɜɨɫɯɨɞɹɳɟɝɨ ɡɚɞɚɧɧɨɟ ɰɟɥɨɟ n, ɛ) ɧɚɢɦɟɧɶɲɟɝɨ ɩɪɨɫɬɨɝɨ ɱɢɫɥɚ, ɩɪɟɜɨɫɯɨɞɹɳɟɝɨ ɡɚɞɚɧɧɨɟ ɱɢɫɥɨ n. int simple(int n);
8. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɪɚɡɥɨɠɟɧɢɹ ɡɚɞɚɧɧɨɝɨ ɰɟɥɨɝɨ ɱɢɫɥɚ ɧɚ ɩɪɨɫɬɵɟ ɦɧɨɠɢɬɟɥɢ. Ɋɟɡɭɥɶɬɚɬɨɦ ɮɭɧɤɰɢɢ ɞɨɥɠɟɧ ɛɵɬɶ ɦɚɫɫɢɜ, ɫɨɞɟɪɠɚɳɢɣ ɩɪɨɫɬɵɟ ɦɧɨɠɢɬɟɥɢ, ɢ ɰɟɥɨɟ ɱɢɫɥɨ – ɤɨɥɢɱɟɫɬɜɨ ɦɧɨɠɢɬɟɥɟɣ. int simplefactor(int n, int *masfactor);
9. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɱɢɫɥɚ ɫɨɱɟɬɚɧɢɣ ɢɡ n ɡɥɟɦɟɧɬɨɜ ɩɨ m (n ɢ m – ɰɟɥɵɟ): C_nm=n!/((n-m)!*m!)
Ɋɟɡɭɥɶɬɚɬɨɦ ɮɭɧɤɰɢɢ ɞɨɥɠɧɨ ɛɵɬɶ ɰɟɥɨɟ ɢɥɢ ɜɟɳɟɫɬɜɟɧɧɨɟ ɱɢɫɥɨ, ɟɫɥɢ C_nm<32767, ɢ ɛɭɥɟɜɫɤɨɟ ɡɧɚɱɟɧɢɟ true, ɢɥɢ ɛɭɥɟɜɫɤɨɟ ɡɧɚɱɟɧɢɟ false, ɟɫɥɢ C_nm>=32767. Boolean binom(int n, int m, int *cnm, double *dcnm);
10. Ɇɧɨɝɨɱɥɟɧɵ ɩɪɟɞɫɬɚɜɥɹɸɬɫɹ ɜ ɩɚɦɹɬɢ ɗȼɆ ɰɟɥɵɦ ɱɢɫɥɨɦ n – ɫɬɟɩɟɧɶɸ ɦɧɨɝɨɱɥɟɧɚ ɢ ɦɚɫɫɢɜɨɦ ɤɨɷɮɮɢɰɢɟɧɬɨɜ a[0], a[1], ... ,a[n].
60
ɚ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɡɧɚɱɟɧɢɹ y=P(x) ɦɧɨɝɨɱɥɟɧɚ ɞɥɹ ɡɚɞɚɧɧɨɝɨ ɚɪɝɭɦɟɧɬɚ x. double valpoly(int n, double *a);
ɛ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɤɨɷɮɮɢɰɢɟɧɬɨɜ ɦɧɨɝɨɱɥɟɧɚ, ɹɜɥɹɸɳɟɝɨɫɹ ɩɪɨɢɡɜɟɞɟɧɢɟɦ ɞɜɭɯ ɞɪɭɝɢɯ ɦɧɨɝɨɱɥɟɧɨɜ, ɡɚɞɚɧɧɵɯ ɫɜɨɢɦɢ ɫɬɟɩɟɧɹɦɢ ɢ ɦɚɫɫɢɜɚɦɢ ɤɨɷɮɮɢɰɢɟɧɬɨɜ. Ɏɭɧɤɰɢɹ ɜɨɡɜɪɚɳɚɟɬ ɫɬɟɩɟɧɶ ɦɧɨɝɨɱɥɟɧɚ-ɩɪɨɢɡɜɟɞɟɧɢɹ. int polyprod(int n, double *a, int m, double *b, double *res);
ɜ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɤɨɷɮɮɢɰɢɟɧɬɨɜ ɦɧɨɝɨɱɥɟɧɚ, ɹɜɥɹɸɳɟɝɨɫɹ ɫɭɦɦɨɣ ɞɜɭɯ ɞɪɭɝɢɯ ɦɧɨɝɨɱɥɟɧɨɜ. Ɏɭɧɤɰɢɹ ɜɨɡɜɪɚɳɚɟɬ ɫɬɟɩɟɧɶ ɦɧɨɝɨɱɥɟɧɚ-ɫɭɦɦɵ. int polysum(int n, double *a, int m, double *b, double *res);
11. ȼ ɧɟɤɨɬɨɪɨɣ ɩɪɨɝɪɚɦɦɟ ɨɛɪɚɛɨɬɤɢ ɬɚɛɥɢɰ ɫɜɟɞɟɧɢɹ ɨ ɞɢɧɚɦɢɱɟɫɤɢ ɨɛɪɚɡɭɟɦɵɯ ɫɬɨɥɛɰɚɯ ɨɩɪɟɞɟɥɹɸɬɫɹ ɝɥɨɛɚɥɶɧɵɦɢ ɨɩɢɫɚɧɢɹɦɢ: // ɞɥɢɧɚ ɫɬɨɥɛɰɚ ɬɚɛɥɢɰɵ // ɦɚɤɫɢɦɚɥɶɧɨɟ ɱɢɫɥɨ ɫɬɨɥɛɰɨɜ
const int L_cln=20; const int L_tab=30; struct COLUM { char name[5]; double *adr; }; COLUM Tabl[L_tab]
// ɢɦɹ ɫɬɨɥɛɰɚ // ɭɤɚɡɚɬɟɥɶ ɧɚ ɫɬɨɥɛɟɰ // ɦɚɫɫɢɜ ɫɜɟɞɟɧɢɣ ɨ ɫɬɨɥɛɰɚɯ
Ⱦɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɫɬɨɥɛɰɚɦ ɢɫɩɨɥɶɡɭɸɬɫɹ ɢɯ ɢɧɞɟɤɫɵ ɜ ɦɚɫɫɢɜɟ Tabl. ȿɫɥɢ ɫɬɨɥɛɟɰ ɧɟ ɪɚɡɦɟɳɟɧ ɜ ɩɚɦɹɬɢ, ɬɨ ɡɧɚɱɟɧɢɟ ɩɨɥɹ adr ɪɚɜɧɨ 0. ɚ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɩɨɥɧɟɧɢɹ ɩɨɷɥɟɦɟɧɬɧɨɝɨ ɫɥɨɠɟɧɢɹ ɞɜɭɯ ɫɬɨɥɛɰɨɜ ɫ ɨɛɪɚɡɨɜɚɧɢɟɦ ɫɬɨɥɛɰɚ ɫɭɦɦ. ȼɯɨɞɧɵɟ ɞɚɧɧɵɟ – ɧɨɦɟɪɚ (ɢɧɞɟɤɫɵ ɜ Tabl) ɫɬɨɥɛɰɨɜ ɫɥɚɝɚɟɦɵɯ ɢ ɫɬɨɥɛɰɚ ɪɟɡɭɥɶɬɚɬɚ. ȿɫɥɢ ɫɬɨɥɛɟɰɪɟɡɭɥɶɬɚɬ ɧɟ ɪɚɡɦɟɳɟɧ ɜ ɩɚɦɹɬɢ, ɬɨ ɪɚɡɦɟɫɬɢɬɶ ɟɝɨ ɢ ɨɬɦɟɬɢɬɶ ɡɬɨ ɜ Tabl. int sumcol(int k, int l, int r);
Ɏɭɧɤɰɢɹ ɜɨɡɜɪɚɳɚɟɬ 0 ɩɪɢ ɭɫɩɟɲɧɨɦ ɜɵɩɨɥɧɟɧɢɢ ɨɩɟɪɚɰɢɢ ɢɥɢ 1, ɟɫɥɢ ɨɞɢɧ ɢɥɢ ɨɛɚ ɫɬɨɥɛɰɚ-ɫɥɚɝɚɟɦɵɯ ɧɟ ɪɚɡɦɟɳɟɧɵ ɜ ɩɚɦɹɬɢ. 61
ɛ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɫɬɨɥɛɰɚ ɱɚɫɬɧɵɯ ɨɬ ɞɟɥɟɧɢɹ ɷɥɟɦɟɧɬɨɜ ɨɞɧɨɝɨ ɫɬɨɥɛɰɚ ɧɚ ɷɥɟɦɟɧɬɵ ɞɪɭɝɨɝɨ ɫɬɨɥɛɰɚ. ɉɪɟɞɭɫɦɨɬɪɟɬɶ ɫɢɝɧɚɥɢɡɚɰɢɸ ɨ ɫɥɭɱɚɟ, ɤɨɝɞɚ ɷɥɟɦɟɧɬ ɫɬɨɥɛɰɚ-ɞɟɥɢɬɟɥɹ ɪɚɜɟɧ ɧɭɥɸ. Ɉɫɬɚɥɶɧɵɟ ɭɫɥɨɜɢɹ – ɤɚɤ ɜ ɜɚɪɢɚɧɬɟ (ɚ). int subcol(int k, int l, int r);
ɜ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɷɥɟɦɟɧɬɨɜ ɫɬɨɥɛɰɚ ɨɬɧɨɫɢɬɟɥɶɧɵɯ ɩɪɢɪɨɫɬɨɜ ɜ ɩɪɨɰɟɧɬɚɯ di=(ai-bi)/ai*100 ɞɥɹ ɡɚɞɚɧɧɨɝɨ ɢɫɯɨɞɧɨɝɨ ɫɬɨɥɛɰɚ. Ɉɫɬɚɥɶɧɵɟ ɭɫɥɨɜɢɹ - ɤɚɤ ɜ ɜɚɪɢɚɧɬɚɯ (ɚ) ɢ (ɛ). int relincr(int k, int r);
Ɏɭɧɤɰɢɹ ɜɨɡɜɪɚɳɚɟɬ 1, ɟɫɥɢ ɜɫɬɪɟɬɢɬɫɹ ɞɟɥɢɬɟɥɶ, ɪɚɜɧɵɣ 0, ɜ ɨɫɬɚɥɶɧɵɯ ɫɥɭɱɚɹɯ ɜɨɡɜɪɚɳɚɟɬ 0. ɝ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɩɟɱɚɬɢ ɬɚɛɥɢɰɵ, ɫɨɞɟɪɠɚɳɟɣ ɫɬɨɥɛɰɵ, ɩɟɪɟɱɟɧɶ ɤɨɬɨɪɵɯ ɡɚɞɚɧ ɫɩɢɫɤɨɦ (ɦɚɫɫɢɜɨɦ ) ɢɧɞɟɤɫɨɜ (ɧɨɦɟɪɨɜ) ɫɬɨɥɛɰɨɜ ɜ ɦɚɫɫɢɜɟ Tabl. int tabprint(int *list, int n);
12. ȼ ɧɟɤɨɬɨɪɨɣ ɩɪɨɝɪɚɦɦɟ ɚɧɚɥɢɬɢɱɟɫɤɢɯ ɩɪɟɨɛɪɚɡɨɜɚɧɢɣ ɜɵɪɚɠɟɧɢɹ, ɨɩɪɟɞɟɥɹɸɳɢɟ ɮɨɪɦɭɥɵ, ɯɪɚɧɹɬɫɹ ɜ ɜɢɞɟ ɞɢɧɚɦɢɱɟɫɤɢɯ ɫɬɪɭɤɬɭɪ – ɞɟɪɟɜɶɟɜ ɫ ɨɩɢɫɚɧɢɟɦ: enum tpn {FUNC, OPER, VAR, CNST}; // ɬɢɩɵ ɭɡɥɨɜ struct node { enum tpn tp_node; // ɬɢɩ ɭɡɥɚ int cop; /* ɬɢɩ ɨɩɟɪɚɰɢɢ ɢɥɢ ɧɨɦɟɪ ɮɭɧɤɰɢɢ, ɤɨɧɫɬɚɧɬɵ, ɩɟɪɟɦɟɧɧɨɣ */ node *al, *ar; // ɭɤɚɡɚɬɟɥɢ ɧɚ ɩɨɞɱɢɧɟɧɧɵɟ ɭɡɥɵ };
ɚ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɨɛɯɨɞɚ ɞɟɪɟɜɚ, ɡɚɞɚɧɧɨɝɨ ɭɤɚɡɚɬɟɥɟɦ ɧɚ ɟɝɨ ɤɨɪɟɧɶ, ɜ ɩɨɪɹɞɤɟ: ɥɟɜɨɟ ɩɨɞɞɟɪɟɜɨ, ɤɨɪɟɧɶ, ɩɪɚɜɨɟ ɩɨɞɞɟɪɟɜɨ. ȼɵɜɨɞɢɬɶ ɧɚ ɷɤɪɚɧ (ɩɟɱɚɬɶ) ɬɢɩ ɭɡɥɚ ɢ ɡɧɚɱɟɧɢɟ ɩɨɥɹ cop ɜ ɭɡɥɟ. void lkp(node *root);
ɛ) ȼ ɭɫɥɨɜɢɹɯ ɩɭɧɤɬɚ (ɚ) ɜɵɩɨɥɧɢɬɶ ɨɛɯɨɞ ɞɟɪɟɜɚ ɜ ɩɨɪɹɞɤɟ: ɤɨɪɟɧɶ, ɥɟɜɨɟ ɩɨɞɞɟɪɟɜɨ, ɩɪɚɜɨɟ ɩɨɞɞɟɪɟɜɨ. void klp(node *root); 62
ɜ) ȼ ɭɫɥɨɜɢɹɯ ɩɭɧɤɬɚ (ɚ) ɜɵɩɨɥɧɢɬɶ ɨɛɯɨɞ ɞɟɪɟɜɚ ɜ ɩɨɪɹɞɤɟ: ɥɟɜɨɟ ɩɨɞɞɟɪɟɜɨ, ɩɪɚɜɨɟ ɩɨɞɞɟɪɟɜɨ, ɤɨɪɟɧɶ. void lpk(node *root);
ɝ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɨɫɜɨɛɨɠɞɟɧɢɹ ɩɚɦɹɬɢ, ɡɚɧɹɬɨɣ ɧɟɤɨɬɨɪɵɦ ɩɨɞɞɟɪɟɜɨɦ. ȼɯɨɞɧɨɣ ɩɚɪɚɦɟɬɪ – ɭɤɚɡɚɬɟɥɶ ɧɚ ɤɨɪɟɧɶ ɭɞɚɥɹɟɦɨɝɨ ɩɨɞɞɟɪɟɜɚ. void deltree(node *root);
13. ȼ ɧɟɤɨɬɨɪɨɣ ɩɪɨɝɪɚɦɦɟ ɫɨɨɛɳɟɧɢɹ ɨɛ ɨɲɢɛɤɚɯ ɯɪɚɧɹɬɫɹ ɜ ɮɚɣɥɟ ɢɡ ɡɚɩɢɫɟɣ, ɤɚɠɞɚɹ ɡɚɩɢɫɶ ɫɨɫɬɨɢɬ ɢɡ ɞɜɭɯ ɫɬɪɨɤ ɩɨ 60 ɥɢɬɟɪ. ɉɨɡɢɰɢɹ ɡɚɩɢɫɢ ɜ ɞɢɫɤɨɜɨɦ ɮɚɣɥɟ ɨɩɪɟɞɟɥɹɟɬɫɹ ɤɨɞɨɦ ɨɲɢɛɤɢ – ɰɟɥɵɦ ɱɢɫɥɨɦ. ɚ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɜɨɞɚ ɫɨɨɛɳɟɧɢɹ ɨɛ ɨɲɢɛɤɟ ɧɚ ɷɤɪɚɧ. ȼɯɨɞɧɨɣ ɩɚɪɚɦɟɬɪ ɤɨɞ (ɧɨɦɟɪ) ɨɲɢɛɤɢ. Ɏɚɣɥ ɫ ɫɨɨɛɳɟɧɢɹɦɢ ɨɛ ɨɲɢɛɤɚɯ ɨɬɤɪɵɜɚɟɬɫɹ ɜ ɝɥɚɜɧɨɣ ɩɪɨɝɪɚɦɦɟ. ɛ) ɋɨɫɬɚɜɢɬɶ ɩɪɨɝɪɚɦɦɭ ɞɥɹ ɫɨɡɞɚɧɢɹ ɮɚɣɥɚ ɫɨɨɛɳɟɧɢɣ ɨɛ ɨɲɢɛɤɚɯ. ɉɪɢ ɢɫɩɨɥɶɡɨɜɚɧɢɢ ɷɬɨɣ ɩɪɨɝɪɚɦɦɵ ɩɨɥɶɡɨɜɚɬɟɥɶ ɭɤɚɡɵɜɚɟɬ ɢɦɹ ɮɚɣɥɚ, ɦɚɤɫɢɦɚɥɶɧɨɟ ɱɢɫɥɨ ɫɨɨɛɳɟɧɢɣ ɨɛ ɨɲɢɛɤɚɯ, ɚ ɡɚɬɟɦ ɡɚɞɚɟɬ ɤɨɞɵ ɨɲɢɛɨɤ ɢ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɢɦ ɫɨɨɛɳɟɧɢɹ ɜ ɩɪɨɢɡɜɨɥɶɧɨɦ ɩɨɪɹɞɤɟ. 14. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɪɟɲɟɧɢɹ ɫɢɫɬɟɦɵ n ɥɢɧɟɣɧɵɯ ɭɪɚɜɧɟɧɢɣ ɦɟɬɨɞɨɦ ɢɫɤɥɸɱɟɧɢɹ ɧɟɢɡɜɟɫɬɧɵɯ. int gauss(int n, double *a, double *b);
Ɏɭɧɤɰɢɹ ɜɨɡɜɪɚɳɚɟɬ 0 ɩɪɢ ɭɫɩɟɲɧɨɦ ɜɵɩɨɥɧɟɧɢɢ ɢ 1, ɟɫɥɢ ɦɟɬɨɞ ɧɟ ɪɚɛɨɬɚɟɬ. 15. ȼ ɩɚɦɹɬɢ ɯɪɚɧɢɬɫɹ ɦɚɫɫɢɜ ɢɡ n
63
ɛ) Cɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɩɨɫɬɪɨɟɧɢɹ ɝɢɫɬɨɝɪɚɦɦɵ (ɪɚɫɩɪɟɞɟɥɟɧɢɹ ɱɚɫɬɨɬ) ɞɥɹ ɪɟɚɥɢɡɚɰɢɢ ɫɥɭɱɚɣɧɨɣ ɜɟɥɢɱɢɧɵ. ȼɯɨɞɧɵɟ ɞɚɧɧɵɟ: n, x – ɦɚɫɫɢɜ ɡɧɚɱɟɧɢɣ ɫɥɭɱɚɣɧɨɣ ɜɟɥɢɱɢɧɵ x[1], ... ,x[n], k – ɱɢɫɥɨ ɢɧɬɟɪɜɚɥɨɜ, ɧɚ ɤɨɬɨɪɵɟ ɪɚɡɛɢɜɚɟɬɫɹ ɞɢɚɩɚɡɨɧ ɡɧɚɱɟɧɢɣ ɫɥɭɱɚɣɧɨɣ ɜɟɥɢɱɢɧɵ.Ɋɟɡɭɥɶɬɚɬ ɮɭɧɤɰɢɢ r – ɦɚɫɫɢɜ ɢɡ k ɱɢɫɟɥ, ɡɧɚɱɟɧɢɹ ɱɚɫɬɨɬ ɩɨɩɚɞɚɧɢɹ ɜ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɣ ɢɧɬɟɪɜɚɥ. 16. ȼɵɩɨɥɧɢɬɶ ɜɚɪɢɚɧɬɵ 15 (ɚ), (ɛ) ɞɥɹ ɫɥɭɱɚɹ, ɤɨɝɞɚ ɡɧɚɱɟɧɢɹ ɪɟɚɥɢɡɚɰɢɢ ɫɥɭɱɚɣɧɨɣ ɜɟɥɢɱɢɧɵ ɯɪɚɧɹɬɫɹ ɜ ɬɟɤɫɬɨɜɨɦ ɮɚɣɥɟ, ɚ ɨɫɬɚɥɶɧɵɟ ɞɚɧɧɵɟ ɫɨɨɛɳɚɸɬɫɹ ɩɨɥɶɡɨɜɚɬɟɥɟɦ, ɪɟɡɭɥɶɬɚɬɵ ɪɚɫɱɟɬɨɜ ɜɵɜɨɞɹɬɫɹ ɧɚ ɷɤɪɚɧ. 17. Ɂɧɚɱɟɧɢɹ ɧɟɤɨɬɨɪɨɣ ɮɭɧɤɰɢɢ ɩɪɟɞɫɬɚɜɥɟɧɵ ɬɚɛɥɢɰɟɣ ɫ ɩɨɫɬɨɹɧɧɵɦ ɲɚɝɨɦ, ɢ ɜ ɩɚɦɹɬɢ ɯɪɚɧɹɬɫɹ: ɱɢɫɥɨ ɬɨɱɟɤ ɬɚɛɥɢɰɵ n, ɦɚɫɫɢɜ ɡɧɚɱɟɧɢɣ ɮɭɧɤɰɢɢ y[1], ... ,y[n], ɧɚɱɚɥɶɧɨɟ ɡɧɚɱɟɧɢɟ ɚɪɝɭɦɟɧɬɚ x[1] ɢ ɲɚɝ ɩɨ ɚɪɝɭɦɟɧɬɭ. ɚ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɛɨɪɤɢ ɡɧɚɱɟɧɢɹ ɮɭɧɤɰɢɢ ɢɡ ɬɚɛɥɢɰɵ ɫ ɥɢɧɟɣɧɨɣ ɢɧɬɟɪɩɨɥɹɰɢɟɣ. ɛ) ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɡɧɚɱɟɧɢɹ ɨɛɪɚɬɧɨɣ ɮɭɧɤɰɢɢ ɞɥɹ ɮɭɧɤɰɢɢ, ɡɚɞɚɧɧɨɣ ɬɚɛɥɢɰɟɣ c ɩɨɫɬɨɹɧɧɵɦ ɲɚɝɨɦ. 18. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɛɨɪɤɢ ɡɧɚɱɟɧɢɣ ɮɭɧɤɰɢɢ, ɡɚɞɚɧɧɨɣ ɬɚɛɥɢɰɟɣ ɫ ɩɟɪɟɦɟɧɧɵɦ ɲɚɝɨɦ. Ɍɚɛɥɢɰɚ ɯɪɚɧɢɬɫɹ ɜ ɩɚɦɹɬɢ ɜ ɮɨɪɦɟ ɦɚɫɫɢɜɚ ɢɡ n ɫɬɪɨɤ ɢ ɞɜɭɯ ɫɬɨɥɛɰɨɜ (ɚɪɝɭɦɟɧɬ ɢ ɡɧɚɱɟɧɢɟ ɮɭɧɤɰɢɢ). 19. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɫɨɪɬɢɪɨɜɤɢ (ɭɩɨɪɹɞɨɱɟɧɢɹ) ɦɚɫɫɢɜɚ mas ɢɡ n ɷɥɟɦɟɧɬɨɜ ɩɨ ɜɨɡɪɚɫɬɚɧɢɸ ɡɧɚɱɟɧɢɣ ɷɥɟɦɟɧɬɨɜ: void sort(int n, int *mas);
20. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɫɨɪɬɢɪɨɜɤɢ (ɩɟɪɟɫɬɚɧɨɜɤɢ ɫɬɪɨɤ) ɦɚɬɪɢɰɵ ɢɡ n ɫɬɪɨɤ ɢ m ɫɬɨɥɛɰɨɜ ɩɨ ɜɨɡɪɚɫɬɚɧɢɸ ɷɥɟɦɟɧɬɨɜ k-ɝɨ ɫɬɨɥɛɰɚ; void sortmas(int n, int m, int *matr, int k);
21. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɭɦɧɨɠɟɧɢɹ ɦɚɬɪɢɰɵ matr ɢɡ n ɫɬɪɨɤ ɢ m ɫɬɨɥɛɰɨɜ ɧɚ ɜɟɤɬɨɪ vect (ɢɡ n ɷɥɟɦɟɧɬɨɜ) ɫ ɪɚɡɦɟɳɟɧɢɟɦ ɪɟɡɭɥɶɬɚɬɚ ɜ ɦɚɫɫɢɜɟ res: void matrvect(nt n, int m, float *matr, float *vect, float *res);
64
22. ɋɨɫɬɚɜɢɬɶ ɮɭɧɤɰɢɸ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɩɪɨɢɡɜɟɞɟɧɢɹ ɦɚɬɪɢɰ a ɢɡ n ɫɬɪɨɤ ɢ m ɫɬɨɥɛɰɨɜ ɢ b ɢɡ m ɫɬɪɨɤ ɢ k ɫɬɨɥɛɰɨɜ ɫ ɩɨɦɟɳɟɧɢɟɦ ɪɟɡɭɥɶɬɚɬɚ ɜ ɦɚɬɪɢɰɭ c: void matrprod(int n, int m, int k, float *a, float *b, float *c);
6.2. Ɂɚɞɚɱɢ ɩɨ ɪɚɡɪɚɛɨɬɤɟ ɫɢɫɬɟɦ ɨɛɴɟɤɬɨɜ 1. ɉɨɫɬɪɨɢɬɶ ɫɢɫɬɟɦɭ ɤɥɚɫɫɨɜ ɞɥɹ ɨɩɢɫɚɧɢɹ ɩɥɨɫɤɢɯ ɝɟɨɦɟɬɪɢɱɟɫɤɢɯ ɮɢɝɭɪ: ɤɪɭɝɚ, ɤɜɚɞɪɚɬɚ, ɩɪɹɦɨɭɝɨɥɶɧɢɤɚ. ɉɪɟɞɭɫɦɨɬɪɟɬɶ ɦɟɬɨɞɵ ɞɥɹ ɫɨɡɞɚɧɢɹ ɨɛɴɟɤɬɨɜ, ɩɟɪɟɦɟɳɟɧɢɹ ɧɚ ɩɥɨɫɤɨɫɬɢ, ɢɡɦɟɧɟɧɢɹ ɪɚɡɦɟɪɨɜ ɢ ɜɪɚɳɟɧɢɹ ɧɚ ɡɚɞɚɧɧɵɣ ɭɝɨɥ. 2. ɉɨɫɬɪɨɢɬɶ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ, ɫɨɞɟɪɠɚɳɟɝɨ ɢɧɮɨɪɦɚɰɢɸ ɨ ɩɨɱɬɨɜɨɦ ɚɞɪɟɫɟ ɨɪɝɚɧɢɡɚɰɢɢ. ɉɪɟɞɭɫɦɨɬɪɟɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɪɚɡɞɟɥɶɧɨɝɨ ɢɡɦɟɧɟɧɢɹ ɫɨɫɬɚɜɧɵɯ ɱɚɫɬɟɣ ɚɞɪɟɫɚ, ɫɨɡɞɚɧɢɹ ɢ ɭɧɢɱɬɨɠɟɧɢɹ ɨɛɴɟɤɬɨɜ ɷɬɨɝɨ ɤɥɚɫɫɚ. 3. ɋɨɫɬɚɜɢɬɶ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ ɞɥɹ ɩɪɟɞɫɬɚɜɥɟɧɢɹ ɤɨɦɩɥɟɤɫɧɵɯ ɱɢɫɟɥ ɫ ɜɨɡɦɨɠɧɨɫɬɶɸ ɡɚɞɚɧɢɹ ɜɟɳɟɫɬɜɟɧɧɨɣ ɢ ɦɧɢɦɨɣ ɱɚɫɬɟɣ ɤɚɤ ɱɢɫɥɚɦɢ ɬɢɩɨɜ double, ɬɚɤ ɢ ɰɟɥɵɦɢ ɱɢɫɥɚɦɢ. Ɉɛɟɫɩɟɱɢɬɶ ɜɵɩɨɥɧɟɧɢɟ ɨɩɟɪɚɰɢɣ ɫɥɨɠɟɧɢɹ, ɜɵɱɢɬɚɧɢɹ ɢ ɭɦɧɨɠɟɧɢɹ ɤɨɦɩɥɟɤɫɧɵɯ ɱɢɫɟɥ. 4. ɋɨɫɬɚɜɢɬɶ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ ɞɥɹ ɪɚɛɨɬɵ ɫ ɰɟɩɧɵɦɢ ɫɩɢɫɤɚɦɢ ɫɬɪɨɤ (ɫɬɪɨɤɢ ɩɪɨɢɡɜɨɥɶɧɨɣ ɞɥɢɧɵ) ɫ ɨɩɟɪɚɰɢɹɦɢ ɜɤɥɸɱɟɧɢɹ ɜ ɫɩɢɫɨɤ, ɭɞɚɥɟɧɢɹ ɢɡ ɫɩɢɫɤɚ ɷɥɟɦɟɧɬɚ ɫ ɡɚɞɚɧɧɵɦ ɡɧɚɱɟɧɢɟɦ ɞɚɧɧɨɝɨ, ɭɞɚɥɟɧɢɹ ɜɫɟɝɨ ɫɩɢɫɤɚ ɢɥɢ ɤɨɧɰɚ ɫɩɢɫɤɚ, ɧɚɱɢɧɚɹ ɫ ɡɚɞɚɧɧɨɝɨ ɷɥɟɦɟɧɬɚ. 5. ɋɨɫɬɚɜɢɬɶ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ ɞɥɹ ɨɛɴɟɤɬɨɜ-ɜɟɤɬɨɪɨɜ, ɡɚɞɚɜɚɟɦɵɯ ɤɨɨɪɞɢɧɚɬɚɦɢ ɤɨɧɰɨɜ ɜ ɬɪɟɯɦɟɪɧɨɦ ɩɪɨɫɬɪɚɧɫɬɜɟ. Ɉɛɟɫɩɟɱɢɬɶ ɨɩɟɪɚɰɢɢ ɫɥɨɠɟɧɢɹ ɢ ɜɵɱɢɬɚɧɢɹ ɜɟɤɬɨɪɨɜ ɫ ɩɨɥɭɱɟɧɢɟɦ ɧɨɜɨɝɨ ɜɟɤɬɨɪɚ (ɫɭɦɦɵ ɢɥɢ ɪɚɡɧɨɫɬɢ), ɜɵɱɢɫɥɟɧɢɹ ɫɤɚɥɹɪɧɨɝɨ ɩɪɨɢɡɜɟɞɟɧɢɹ ɞɜɭɯ ɜɟɤɬɨɪɨɜ, ɞɥɢɧɵ ɜɟɤɬɨɪɚ, cos ɭɝɥɚ ɦɟɠɞɭ ɜɟɤɬɨɪɚɦɢ. 6. ɋɨɫɬɚɜɢɬɶ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ ɩɪɹɦɨɭɝɨɥɶɧɢɤɨɜ ɫɨ ɫɬɨɪɨɧɚɦɢ, ɩɚɪɚɥɥɟɥɶɧɵɦɢ ɨɫɹɦ ɤɨɨɪɞɢɧɚɬ. ɉɪɟɞɭɫɦɨɬɪɟɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɩɟɪɟɦɟɳɟɧɢɹ ɩɪɹɦɨɭɝɨɥɶɧɢɤɨɜ ɧɚ ɩɥɨɫɤɨɫɬɢ, ɢɡɦɟɧɟɧɢɹ ɪɚɡɦɟɪɨɜ, ɩɨɫɬɪɨɟɧɢɹ ɧɚɢɦɟɧɶɲɟɝɨ ɩɪɹɦɨɭɝɨɥɶɧɢɤɚ, ɫɨɞɟɪɠɚɳɟɝɨ ɞɜɚ ɡɚɞɚɧɧɵɯ ɩɪɹɦɨɭɝɨɥɶɧɢɤɚ, ɢ ɩɪɹɦɨɭɝɨɥɶɧɢɤɚ, ɹɜɥɹɸɳɟɝɨɫɹ ɨɛɳɟɣ ɱɚɫɬɶɸ (ɩɟɪɟɫɟɱɟɧɢɟɦ) ɞɜɭɯ ɩɪɹɦɨɭɝɨɥɶɧɢɤɨɜ. 7. ɋɨɫɬɚɜɢɬɶ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ ɞɥɹ ɨɩɪɟɞɟɥɟɧɢɹ ɨɞɧɨɦɟɪɧɵɯ ɦɚɫɫɢɜɨɜ ɰɟɥɵɯ ɱɢɫɟɥ (ɜɟɤɬɨɪɨɜ). ɉɪɟɞɭɫɦɨɬɪɟɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɨɛɪɚɳɟɧɢɹ ɤ ɨɬɞɟɥɶɧɨ65
ɦɭ ɷɥɟɦɟɧɬɭ ɦɚɫɫɢɜɚ ɫ ɤɨɧɬɪɨɥɟɦ ɜɵɯɨɞɚ ɡɚ ɩɪɟɞɟɥɵ ɢɧɞɟɤɫɨɜ, ɜɨɡɦɨɠɧɨɫɬɶ ɡɚɞɚɧɢɹ ɩɪɨɢɡɜɨɥɶɧɵɯ ɝɪɚɧɢɰ ɢɧɞɟɤɫɨɜ ɩɪɢ ɫɨɡɞɚɧɢɢ ɨɛɴɟɤɬɚ ɢ ɜɵɩɨɥɧɟɧɢɹ ɨɩɟɪɚɰɢɣ ɩɨɷɥɟɦɟɧɬɧɨɝɨ ɫɥɨɠɟɧɢɹ ɢ ɜɵɱɢɬɚɧɢɹ ɦɚɫɫɢɜɨɜ ɫ ɨɞɢɧɚɤɨɜɵɦɢ ɝɪɚɧɢɰɚɦɢ ɢɧɞɟɤɫɨɜ, ɭɦɧɨɠɟɧɢɹ ɢ ɞɟɥɟɧɢɹ ɜɫɟɯ ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ ɧɚ ɫɤɚɥɹɪ, ɩɟɱɚɬɢ (ɜɵɜɨɞɚ ɧɚ ɷɤɪɚɧ) ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ ɩɨ ɢɧɞɟɤɫɚɦ ɢ ɜɫɟɝɨ ɦɚɫɫɢɜɚ. 8. ɋɨɫɬɚɜɢɬɶ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ ɞɥɹ ɨɩɪɟɞɟɥɟɧɢɹ ɨɞɧɨɦɟɪɧɵɯ ɦɚɫɫɢɜɨɜ ɫɬɪɨɤ ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɞɥɢɧɵ. ɉɪɟɞɭɫɦɨɬɪɟɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɨɛɪɚɳɟɧɢɹ ɤ ɨɬɞɟɥɶɧɵɦ ɫɬɪɨɤɚɦ ɦɚɫɫɢɜɚ ɩɨ ɢɧɞɟɤɫɚɦ, ɤɨɧɬɪɨɥɶ ɜɵɯɨɞɚ ɡɚ ɩɪɟɞɟɥɵ ɢɧɞɟɤɫɨɜ, ɜɵɩɨɥɧɟɧɢɹ ɨɩɟɪɚɰɢɣ ɩɨɷɥɟɦɟɧɬɧɨɝɨ ɫɰɟɩɥɟɧɢɹ ɞɜɭɯ ɦɚɫɫɢɜɨɜ ɫ ɨɛɪɚɡɨɜɚɧɢɟɦ ɧɨɜɨɝɨ ɦɚɫɫɢɜɚ, ɫɥɢɹɧɢɹ ɞɜɭɯ ɦɚɫɫɢɜɨɜ ɫ ɢɫɤɥɸɱɟɧɢɟɦ ɩɨɜɬɨɪɹɸɳɢɯɫɹ ɷɥɟɦɟɧɬɨɜ, ɩɟɱɚɬɢ (ɜɵɜɨɞɚ ɧɚ ɷɤɪɚɧ) ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ ɢ ɜɫɟɝɨ ɦɚɫɫɢɜɚ. 9. ɋɨɫɬɚɜɢɬɶ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ ɦɧɨɝɨɱɥɟɧɨɜ ɨɬ ɨɞɧɨɣ ɩɟɪɟɦɟɧɧɨɣ, ɡɚɞɚɜɚɟɦɵɯ ɫɬɟɩɟɧɶɸ ɦɧɨɝɨɱɥɟɧɚ ɢ ɦɚɫɫɢɜɨɦ ɤɨɷɮɮɢɰɢɟɧɬɨɜ. ɉɪɟɞɭɫɦɨɬɪɟɬɶ ɦɟɬɨɞɵ ɞɥɹ ɜɵɱɢɫɥɟɧɢɹ ɡɧɚɱɟɧɢɹ ɦɧɨɝɨɱɥɟɧɚ ɞɥɹ ɡɚɞɚɧɧɨɝɨ ɚɪɝɭɦɟɧɬɚ, ɨɩɟɪɚɰɢɢ ɫɥɨɠɟɧɢɹ, ɜɵɱɢɬɚɧɢɹ ɢ ɭɦɧɨɠɟɧɢɹ ɦɧɨɝɨɱɥɟɧɨɜ ɫ ɩɨɥɭɱɟɧɢɟɦ ɧɨɜɨɝɨ ɨɛɴɟɤɬɚ – ɦɧɨɝɨɱɥɟɧɚ, ɩɟɱɚɬɶ (ɜɵɜɨɞ ɧɚ ɷɤɪɚɧ) ɨɩɢɫɚɧɢɹ ɦɧɨɝɨɱɥɟɧɚ. 10. ɋɨɫɬɚɜɢɬɶ ɨɩɢɫɚɧɢɟ ɤɥɚɫɫɚ ɨɞɧɨɦɟɪɧɵɯ ɦɚɫɫɢɜɨɜ ɫɬɪɨɤ, ɤɚɠɞɚɹ ɫɬɪɨɤɚ ɡɚɞɚɟɬɫɹ ɞɥɢɧɨɣ ɢ ɭɤɚɡɚɬɟɥɟɦ ɧɚ ɜɵɞɟɥɟɧɧɭɸ ɞɥɹ ɧɟɟ ɩɚɦɹɬɶ. ɉɪɟɞɭɫɦɨɬɪɟɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɨɛɪɚɳɟɧɢɹ ɤ ɨɬɞɟɥɶɧɵɦ ɫɬɪɨɤɚɦ ɦɚɫɫɢɜɚ ɩɨ ɢɧɞɟɤɫɚɦ, ɤɨɧɬɪɨɥɶ ɜɵɯɨɞɚ ɡɚ ɩɪɟɞɟɥɵ ɢɧɞɟɤɫɨɜ, ɜɵɩɨɥɧɟɧɢɹ ɨɩɟɪɚɰɢɣ ɩɨɷɥɟɦɟɧɬɧɨɝɨ ɫɰɟɩɥɟɧɢɹ ɞɜɭɯ ɦɚɫɫɢɜɨɜ ɫ ɨɛɪɚɡɨɜɚɧɢɟɦ ɧɨɜɨɝɨ ɦɚɫɫɢɜɚ, ɫɥɢɹɧɢɹ ɞɜɭɯ ɦɚɫɫɢɜɨɜ ɫ ɢɫɤɥɸɱɟɧɢɟɦ ɩɨɜɬɨɪɹɸɳɢɯɫɹ ɷɥɟɦɟɧɬɨɜ, ɩɟɱɚɬɢ (ɜɵɜɨɞɚ ɧɚ ɷɤɪɚɧ) ɷɥɟɦɟɧɬɨɜ ɦɚɫɫɢɜɚ ɢ ɜɫɟɝɨ ɦɚɫɫɢɜɚ. 11. ɋɨɫɬɚɜɢɬɶ ɨɩɢɫɚɧɢɟ ɨɛɴɟɤɬɧɨɝɨ ɬɢɩɚ TMatr, ɨɛɟɫɩɟɱɢɜɚɸɳɟɝɨ ɪɚɡɦɟɳɟɧɢɟ ɦɚɬɪɢɰɵ ɩɪɨɢɡɜɨɥɶɧɨɝɨ ɪɚɡɦɟɪɚ ɫ ɜɨɡɦɨɠɧɨɫɬɶɸ ɢɡɦɟɧɟɧɢɹ ɱɢɫɥɚ ɫɬɪɨɤ ɢ ɫɬɨɥɛɰɨɜ, ɜɵɜɨɞɚ ɧɚ ɷɤɪɚɧ ɩɨɞɦɚɬɪɢɰɵ ɥɸɛɨɝɨ ɪɚɡɦɟɪɚ ɢ ɜɫɟɣ ɦɚɬɪɢɰɵ.
66
Ʌɢɬɟɪɚɬɭɪɚ 1. ɒɢɥɞɬ Ƚ. ɋɚɦɨɭɱɢɬɟɥɶ C++ / Ƚ. ɒɢɥɞɬ ; ɩɟɪ. ɫ ɚɧɝɥ. – ɋɉɛ. : Ȼɏȼ-ɉɟɬɟɪɛɭɪɝ, 1997. – 512 ɫ. 2. Ȼɚɛɷ Ȼ. ɉɪɨɫɬɨ ɢ ɹɫɧɨ ɨ Borland C++. ȼɟɪɫɢɢ 4.0 ɢ 4.5 / Ȼ. Ȼɚɛɷ ; ɩɟɪ. ɫ ɚɧɝɥ. – Ɇ. : ȻɂɇɈɆ, 1994. – 400 ɫ. 3. ɋɬɪɚɭɫɬɪɭɩ Ȼ. əɡɵɤ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɋ++ / Ȼ. ɋɬɪɚɭɫɬɪɭɩ ; ɩɟɪ. ɫ ɚɧɝɥ. – Ɇ. : Ɋɚɞɢɨ ɢ ɫɜɹɡɶ, 1995. – 352 ɫ. 4. ɗɥɢɚɫ Ɇ. ɋɩɪɚɜɨɱɧɨɟ ɪɭɤɨɜɨɞɫɬɜɨ ɩɨ ɹɡɵɤɭ ɋ++ ɫ ɤɨɦɦɟɧɬɚɪɢɹɦɢ / Ɇ. ɗɥɢɚɫ, Ȼ. ɋɬɪɚɭɫɬɪɭɩ ; ɩɟɪ. ɫ ɚɧɝɥ. – Ɇ. : Ɇɢɪ, 1992. – 371 ɫ. 5. Ʉɟɪɧɢɝɚɧ Ȼ. əɡɵɤ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ ɋɢ / Ȼ. Ʉɟɪɧɢɝɚɧ, Ⱦ. Ɋɢɬɱɢ. – Ɇ. : Ɋɚɞɢɨ ɢ ɫɜɹɡɶ, 1989. – 428 ɫ. 6. ɂɪɷ ɉ. Ɉɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɨɟ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɟ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ C++ / ɉ. ɂɪɷ ; ɩɟɪ. ɫ ɚɧɝɥ. – Ʉɢɟɜ : ɇɂɉɎ Ⱦɢɚɋɨɮɬ Ʌɬɞ, 1995. – 386 ɫ.
67
ɋɈȾȿɊɀȺɇɂȿ 1. Ɉɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɣ ɩɨɞɯɨɞ ɜ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɢ x 1.1. Ɍɟɯɧɨɥɨɝɢɢ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ x 1.2. ɋɭɳɧɨɫɬɶ ɨɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɨɝɨ ɩɨɞɯɨɞɚ ɤ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɸ 2. ɇɚɱɚɥɶɧɵɟ ɫɜɟɞɟɧɢɹ ɨ ɹɡɵɤɟ C++ x 2.1. ɇɚɡɧɚɱɟɧɢɟ ɹɡɵɤɚ C++, ɢɫɬɨɪɢɱɟɫɤɢɟ ɫɜɟɞɟɧɢɹ x 2.2. Ⱥɥɮɚɜɢɬ, ɛɚɡɨɜɵɟ ɬɢɩɵ ɢ ɨɩɢɫɚɧɢɟ ɞɚɧɧɵɯ x 2.3. ɋɬɪɭɤɬɭɪɵ ɢ ɨɛɴɟɞɢɧɟɧɢɹ x 2.4. Ɉɩɟɪɚɰɢɢ ɢ ɜɵɪɚɠɟɧɢɹ x 2.5. Ɉɩɟɪɚɬɨɪɵ C++ x 2.6. Ɏɭɧɤɰɢɢ x 2.7. Ȼɢɛɥɢɨɬɟɤɚ ɜɪɟɦɟɧɢ ɜɵɩɨɥɧɟɧɢɹ 3. ɉɪɟɩɪɨɰɟɫɫɨɪɧɵɟ ɫɪɟɞɫɬɜɚ ɜ C ɢ C++ x 3.1. Ɉɫɧɨɜɧɵɟ ɩɨɧɹɬɢɹ ɩɪɟɩɪɨɰɟɫɫɨɪɧɨɣ ɨɛɪɚɛɨɬɤɢ x 3.2. ɉɪɟɩɪɨɰɟɫɫɨɪɧɵɟ ɩɟɪɟɦɟɧɧɵɟ x 3.3. Ɇɚɤɪɨɨɩɪɟɞɟɥɟɧɢɹ (ɦɚɤɪɨɫɵ) x 3.4. ɍɫɥɨɜɧɚɹ ɤɨɦɩɢɥɹɰɢɹ 4. Ɉɛɴɟɤɬɧɨ-ɨɪɢɟɧɬɢɪɨɜɚɧɧɵɟ ɫɪɟɞɫɬɜɚ C++ x 4.1. Ɉɛɴɟɤɬɧɵɟ ɬɢɩɵ ɞɚɧɧɵɯ x 4.2. Ʉɨɧɫɬɪɭɤɬɨɪɵ ɢ ɞɟɫɬɪɭɤɬɨɪɵ x 4.3. ɉɪɨɢɡɜɨɞɧɵɟ ɤɥɚɫɫɵ x 4.4. ɉɪɢɦɟɪ ɩɨɫɬɪɨɟɧɢɹ ɫɢɫɬɟɦɵ ɤɥɚɫɫɨɜ x 4.5. ȼɢɪɬɭɚɥɶɧɵɟ ɮɭɧɤɰɢɢ x 4.6. «Ⱦɪɭɠɟɫɬɜɟɧɧɵɟ» (friend) ɮɭɧɤɰɢɢ x 4.7. ɋɬɚɬɢɱɟɫɤɢɟ ɤɨɦɩɨɧɟɧɬɵ ɤɥɚɫɫɚ x 4.8. ɉɟɪɟɨɩɪɟɞɟɥɟɧɢɟ (ɩɟɪɟɝɪɭɡɤɚ) ɨɩɟɪɚɰɢɣ 5. ɒɚɛɥɨɧɵ ɮɭɧɤɰɢɣ ɢ ɤɥɚɫɫɨɜ x 5.1. ɒɚɛɥɨɧɵ ɮɭɧɤɰɢɣ x 5.2. ɒɚɛɥɨɧɵ ɤɥɚɫɫɨɜ 6. ɉɪɢɥɨɠɟɧɢɹ x 6.1. Ɂɚɞɚɱɢ ɩɨ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɸ ɧɚ ɹɡɵɤɟ C++ x 6.2. Ɂɚɞɚɱɢ ɩɨ ɪɚɡɪɚɛɨɬɤɟ ɫɢɫɬɟɦ ɨɛɴɟɤɬɨɜ Ʌɢɬɟɪɚɬɭɪɚ
68
3 3 6 8 8 9 14 16 26 29 32 37 37 38 39 40 41 41 44 46 47 49 52 53 54 56 56 57 59 59 65 67
ɍɱɟɛɧɨɟ ɢɡɞɚɧɢɟ
ɈɋɇɈȼɕ əɁɕɄȺ ɉɊɈȽɊȺɆɆɂɊɈȼȺɇɂə C++ ɋ ɉɊɂɆȿɇȿɇɂȿɆ ɌȿɏɇɈɅɈȽɂɂ ɈȻɔȿɄɌɇɈ-ɈɊɂȿɇɌɂɊɈȼȺɇɇɈȽɈ ɉɊɈȽɊȺɆɆɂɊɈȼȺɇɂə ɍɱɟɛɧɨ-ɦɟɬɨɞɢɱɟɫɤɨɟ ɩɨɫɨɛɢɟ ɞɥɹ ɜɭɡɨɜ
ɋɨɫɬɚɜɢɬɟɥɶ ɑɟɪɧɵɲɨɜ Ɇɚɤɫɢɦ Ʉɚɪɧɟɥɶɟɜɢɱ
ɉɨɞɩɢɫɚɧɨ ɜ ɩɟɱɚɬɶ 29.08.07. Ɏɨɪɦɚɬ 60×84/16. ɍɫɥ. ɩɟɱ. ɥ. 4. Ɍɢɪɚɠ 50 ɷɤɡ. Ɂɚɤɚɡ 1762. ɂɡɞɚɬɟɥɶɫɤɨ-ɩɨɥɢɝɪɚɮɢɱɟɫɤɢɣ ɰɟɧɬɪ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ. 394000, ɝ. ȼɨɪɨɧɟɠ, ɩɥ. ɢɦ. Ʌɟɧɢɧɚ, 10. Ɍɟɥ. 208-298, 598-026 (ɮɚɤɫ) http://www.ppc.vsu.ru; e-mail: [email protected] Ɉɬɩɟɱɚɬɚɧɨ ɜ ɬɢɩɨɝɪɚɮɢɢ ɂɡɞɚɬɟɥɶɫɤɨ-ɩɨɥɢɝɪɚɮɢɱɟɫɤɨɝɨ ɰɟɧɬɪɚ ȼɨɪɨɧɟɠɫɤɨɝɨ ɝɨɫɭɞɚɪɫɬɜɟɧɧɨɝɨ ɭɧɢɜɟɪɫɢɬɟɬɚ. 394000, ɝ. ȼɨɪɨɧɟɠ, ɭɥ. ɉɭɲɤɢɧɫɤɚɹ, 3. Ɍɟɥ. 204-133.
69