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

UptoLike

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

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<<n);
ɛɭɞɟɬ ɜɵɞɟɥɟɧɢɟ ɢɡ ɰɟɥɨɝɨ ɛɟɡɡɧɚɤɨɜɨɝɨ
x n ɛɢɬɨɜ, ɧɚɱɢɧɚɹ ɫ ɛɢɬɚ ɫ ɧɨɦɟ-
ɪɨɦ
p, ɢ ɫɞɜɢɝ ɜɵɞɟɥɟɧɧɵɯ ɛɢɬɨɜ ɜɩɪɚɜɨ, ɬɨ ɟɫɬɶ ɜɵɞɟɥɟɧɢɟ n-ɪɚɡɪɹɞɧɨɝɨ
ɰɟɥɨɝɨ, ɯɪɚɧɹɳɟɝɨɫɹ ɜ ɦɚɲɢɧɧɨɦ ɫɥɨɜɟ
x ɧɚɱɢɧɚɹ ɫ p-ɝɨ ɪɚɡɪɹɞɚ.
ȼɋɢ/ɋɢ++ ɢɦɟɟɬɫɹ ɤɨɧɫɬɪɭɤɰɢɹ, ɤɨɬɨɪɚɹ ɧɚɡɵɜɚɟɬɫɹ ɭɫɥɨɜɧɵɦ ɜɵɪɚ-
ɠɟɧɢɟɦ. ɍɫɥɨɜɧɨɟ ɜɵɪɚɠɟɧɢɟ ɫɬɪɨɢɬɫɹ ɩɨ ɫɯɟɦɟ:
ɭɫɥɨɜɢɟ?ɜɵɪɚɠɟɧɢɟ1:ɜɵɪɚɠɟɧɢɟ2;
ȼ ɤɚɱɟɫɬɜɟ ɭɫɥɨɜɢɹ ɦɨɠɟɬ ɜɵɫɬɭɩɚɬɶ ɥɸɛɨɟ ɫɤɚɥɹɪɧɨɟ ɜɵɪɚɠɟɧɢɟ. ȿɫ-
ɥɢ ɪɟɡɭɥɶɬɚɬ ɜɵɱɢɫɥɟɧɢɹ ɭɫɥɨɜɢɹ ɧɟɧɭɥɟɜɨɣ, ɬɨ ɡɧɚɱɟɧɢɟɦ ɜɫɟɝɨ ɜɵɪɚɠɟ-
25
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 ɢɦɟɟɬ ɬɢɩ unsigned, ɨɫɜɨɛɨɠɞɚɸɳɢɟɫɹ ɥɟɜɵɟ ɪɚɡɪɹɞɵ ɡɚɩɨɥɧɹ-
ɸɬɫɹ ɧɭɥɹɦɢ, ɚ ɩɪɢ e1 ɬɢɩɚ signed ɜ ɨɫɜɨɛɨɠɞɚɸɳɢɯɫɹ ɥɟɜɵɯ ɪɚɡɪɹɞɚɯ
ɩɨɜɬɨɪɹɟɬɫɹ ɡɧɚɤɨɜɵɣ ɪɚɡɪɹɞ.
    ɇɚɞ ɰɟɥɵɦɢ ɨɩɟɪɚɧɞɚɦɢ ɞɨɩɭɫɬɢɦɵ ɨɩɟɪɚɰɢɢ ɩɨɪɚɡɪɹɞɧɨɝɨ ɥɨɝɢɱɟ-
ɫɤɨɝɨ ɭɦɧɨɠɟɧɢɹ, ɥɨɝɢɱɟɫɤɨɝɨ ɫɥɨɠɟɧɢɹ ɢ ɢɫɤɥɸɱɚɸɳɟɝɨ «ɢɥɢ» (ɨɬɪɢɰɚ-
ɧɢɹ ɪɚɜɧɨɡɧɚɱɧɨɫɬɢ). ȼ ɷɬɢɯ ɨɩɟɪɚɰɢɹɯ ɨɩɟɪɚɧɞɵ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɤɚɤ ɩɨ-
ɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɛɢɬɨɜ, ɢ ɨɩɟɪɚɰɢɹ ɜɵɩɨɥɧɹɟɬɫɹ ɧɚɞ ɤɚɠɞɨɣ ɩɚɪɨɣ ɫɨɨɬ-
ɜɟɬɫɬɜɭɸɳɢɯ ɪɚɡɪɹɞɨɜ ɢɡ ɨɛɨɢɯ ɨɩɟɪɚɧɞɨɜ. ɇɚɩɪɢɦɟɪ, ɪɟɡɭɥɶɬɚɬɨɦ ɜɵɪɚ-
ɠɟɧɢɹ
     x>>(p-n+1))&(~(~0<