Practicum Programming

Tags:

Embed Size (px)

Citation preview

  • ,

    -

    - ""

    2011

  • ,

    -

    - ""

    2011

  • . . . I - "". / .: .. , .. , .. , .. , .. , .. . .: "", 2011. 120 .

    ( ___ ________ .)

    . .

    I - ""

    : . . . . . . . . . . . .

    : . . , . . , .

    : . . , . . , ..

    03056, . -56, . , 37

  • 4

    1

    ++ ....................................................................................................................... 7 1.1. .......................................................................... 7 .............................................................................. 7 / ..................................................................... 9 ................................. 10 ++........................................................................ 12 .............................................................................................. 13 .............................................................................. 14 ................................................................................ 15 .

    ................................... 16 1.2. ............................................................... 17 1.3. ............................................................................... 17 ............................................................................. 17

    1.4. ..................... 19 1.5. ...................................................................... 19

    2 , ............................................................................................... 20

    2.1. ........................................................................ 20 ? ................................................................................... 20 : switch break ..................................... 20 .................................................................................. 21 while............................................................................................... 21 dowhile ....................................................................................... 22 ................................................................ 22 for ......................................................................................... 23 ....................................................................................... 24 .............................................................. 25

    2.2. ............................................................................................. 26 2.3. ............................................................... 27 2.4. ............................................................................... 28 ............................................................................. 28 ......................................................... 29

    2.5. ...................................................................... 30 3 ................................... 32

  • 5

    3.1. ........................................................................ 32 ............................................. 33 ................................................ 34 ................................................................................. 34

    3.2. ............................................................... 36 3.3. ............................................................................... 36 ............................................................................. 36 ......................................................... 38

    3.4. ...................................................................... 40 4 ................................ 41

    4.1. ........................................................................ 41 .............................................................................................. 41 ............................................................................................... 42 ........................................................................... 43 ................................................................................... 46 .................................................................. 51

    4.2. ............................................................... 54 4.3. ............................................................................... 54 ............................................................................. 54 ......................................................... 54 ............................................................... 59

    4.4. ...................................................................... 60 5 ................................... 62

    5.1. ........................................................................ 62 .............................................................. 64 ++ .......................................................................................... 71

    5.2. ............................................................................... 75 ............................................................................. 75 .................................................................... 76 ......................................................... 78

    5.3. ...................................................................... 78 6 .................................... 79

    6.1. ........................................................................ 79 .......................................................................................... 79 ++........................................................................................... 80

    6.2. ............................................................... 84 6.3. ............................................................................... 84 ............................................................................. 84 ......................................................... 84

    6.4. ...................................................................... 87

  • 6

    7 ................................. 88 7.1. ........................................................................ 88 ..................................................................... 88 ............................................................................... 93 ........................................ 94

    7.2. ............................................................................................... 99 7.3. ............................................................. 101 7.4. ............................................................................. 101 ........................................................................... 101 ....................................................... 101

    7.5. .................................................................... 102 8 ............ 103

    8.1. ...................................................................... 103 .................................................................. 103 .......................................................................... 104 .................................................................. 105 ........................................................................... 106

    8.2. ............................................................................................. 108 8.3. ............................................................. 112 8.4. ............................................................................. 113 8.5. .................................................................... 127

    ..................................................................................... 128

  • 1 ++

    7

    1 ++

    : ++.

    1.1.

    C++ - , ,

    . , , , , SimpleFunc(5,1.9). SimpleFunc . 3. ++

    main(). . main() -

    . , . , , , . ++ . 1.1.

    ++ ( 1.1 1.2).

    1.1. Hello, world! #include

    // using namespace std; // // int main() // main()

  • 1 ++

    8

    { // main() cout

  • 1 ++

    9

    // // cout

  • 1 ++

    10

    . / , printf() scanf(). 5. , ++

    , : 1. ,

    . . 2. . . ,

    ++ , .

    3. ( ) / , , . ++ , , , sin(), cos(), sqrt(), . , , . ( main() .) .

    () . , #, , . , . , , . ,

    . , #, . #include

    #include

  • 1 ++

    11

    ... #include ,

    ++, . , , . ' , ( , INCLUDE).

    , , "file.h". . , . ++, , Microsoft Visual C++, Borland

    C++, , . , AT&T C++ GNU C++ Unix Linux, , . - (, Unix vi, ed, emacs).

    Windows. , Borland Builder C++ : bcc32 unit1.cpp Visual Studio: mc unit1.cpp - ,

    /? h. , ,

    unit1.exe.

  • 1 ++

    12

    ++ ,

    , . , . ++ .

    . : 3.4 x. - .

    , , . ++ . +, -, *, / ,

    , . : ( ) ( ). , , . %

    ( ). , 5%2 = 1. .

    , . , . ,

    ( ). . : a*sin(x+k)+b*cos(x-k)

    pow(2, n+l)-l = ,

    .

    , . .

    , : .

  • 1 ++

    13

    . , .

    . ', . ' , . ++ . - , . () ,

    . : bool int char float double : (int a[5]) (int*a) void ( ). , '

    , . ++

    : short long signed unsigned ++ ,

    . , :

  • 1 ++

    14

    struct () union (') enum () lass ()

    signed unsigned (, ). short long ' int. , : float ; double ; long double . ' (

    ), sizeof: cout

  • 1 ++

    15

    . (, int x). (, int x=1). , : char ch;

    int count =1; const double pi=3,141592; extern int error_number; /*, , */ char *name =Njal; // ( ) char *season[]={spring, summer, autumn, winter}; // double sqrt(double); //

    . if , . : if ( ) 1;

    if ( ) 1; else 2; else if,

    : if ( ) 1;

    else if 2; else if 3; else if n; else n+1; ( ),

    onepamop1; ( ), onepamop1 ,

  • 1 ++

    16

    onepamop2 else. . , if, {}. .

    x y: if(x

  • 1 ++

    17

    20 2 0 10 2 0 5 2 1 2 2 0 1

    , : 2010 = 101002. 511:

    511 16 15=F 31 16 15=F 1

    : 51110 = 1FF16. b , n,

    011... aaaa nn , : n = an * bn + an-1 * bn-1 + + a1 * b + a0 . .

    : 1111 : 11112 = 1 * 23 + 1 * 22 + 1 * 21 + 1 = 1510 16 : 168 = 1 * 81 + 6 = 1410 FF : FF16 = 15 * 161 + 15 = 25510

    1.2. 1. . 2. . 3. .

    1.3.

    1. a, b, c ,

    , . 2. a>=1. : 1/ sin(). 3. . , -

    .

  • 1 ++

    18

    4. a, b, c. , a>=b>=c, a b.

    5. x, y r, .

    6. :

    10),sin(1010,2

    10,1

    xxxax

    xx

    y

    . .

    7) 33

    4

    a

    xy ; 13) a

    xxysin

    3cos ;

    8) x

    xy 1)42(4log ; 14) axaxtgy 321 ;

    9)

    x

    ay 1ln ; 15) a

    xy 13 ;

    10) ax

    xxy

    2cos

    2)2sin( 3 ; 16) 22 b

    xtgabx

    y ;

    11) xa

    xycos2

    42 ; 17) 2

    21

    342

    x

    xaxy

    ;

    12) 2

    3cos2)1(

    333 x

    x

    xxy

    ; 18) 3)2(5)(

    233

    ax

    xaa

    xxy

    .

  • 1 ++

    19

    1.4.

    1. , 31.

    2. 1000. 3. 11001. 4. 01000.

    1.5. 1. ++? 2. ++

    ? 3. ? ? 4. . 5. ? 6. ++? 7. ? 8. . 9. . 10. . 11. ++ / ?

  • 2 ,

    20

    2 ,

    : ,

    .

    2.1.

    ? ++ if... else.

    . : ( ) ? 1 : 2 , 1,

    2. . , x ? : max=(x>y) ? x:;

    cout

  • 2 ,

    21

    . , switch. , case, switch. , case, . , , default ( ). break.

    switch. break , , case default , oepa break. case default

    switch.

    . . ,

    (). ++ :

    while, for do while. while while . ,

    , -, , -, , . while : while ()

    ; .

    . , {}.

  • 2 ,

    22

    ( ), , . ( ) , ( ). while

    , , , , . . while

    10 , . int i=1, rez=1;

    while (i++

  • 2 ,

    23

    , , .

    10 . i=1;

    while( i++>rez; if (rez==15) break; cout

  • 2 ,

    24

    . for : for (1; 2; 3) ; 1 .

    . , fr . 2. . 2 , ( ). 2 , . 3 .

    , {}. y. : int i, rez=1;

    for ( i=1; i

  • 2 ,

    25

    cout

  • 2 ,

    26

    for (char chr='A'; chr

  • 2 ,

    27

    { int total=0, gradeCounter, grade, average; gradeCounter=1; while (gradeCounter grade; total=total+grade; gradeCounter=gradeCounter+1; } average=total/10; cout

  • 2 ,

    28

    2. , 1, 2 3.

    3. .

    2.4.

    1 , : 1. ,

    . 2. . 3. . 4.

    . 5. ,

    . 2 ,

    . 1. ,

    , ..

    2. , , , .

    3. , , , .

    3 :

    while, for. 1. .

  • 2 ,

    29

    2. , 300.

    3. , 100, .

    4. 10 .

    5. , 100, .

    6. 10 .

    7. .

    8. , 3. 9. n. (

    n 1 n, n!=123n).

    10. . .

    11. n.

    12. n m. 13. y=x2

    [1,5] 1. 14. 1 1000,

    3. 1. , 1000 (

    fn f0=f1=1; fn=fn-1+fn-2 n=2,3,... 1, 1, 2, 3, 5, 8, 13, ...).

    2. , m (m>1) ( fn f0=f1=1; fn=fn-1+fn-2 n=2,3,... 1, 1, 2, 3, 5, 8, 13, ...).

    3. ( , , , , 6=1+2+3) .

    4. , ( )?

  • 2 ,

    30

    5. n. , (, , 1221).

    6. , , . , .

    7. n 1/2+3/4+5/6+...

    8. 5 , . 9. ,

    n. 10. ,

    An=(x*n)/n!. 11. n . ,

    .

    12. , .

    13. , , .

    14. n. , , n .

    15. y=sin(x)*x, [c,d] 0.001.

    16. , . , 2p-1, p .

    17. , 20%, .

    18. 1+2-3+4-5+...+n, n .

    2.5. 1. if? 2.

    . 3. ++? 4. , ? 5. fr? 6. ?

  • 2 ,

    31

    7. ?

  • 3

    32

    3

    : .

    3.1.

    main(). main() ( ) . , , , , . main(), -

    . . , , , . ++ : ; ; . : .

    : /* */

    [_ ] ([_]) { /* */ _ } , .

    , (int). , void. . void. ,

    .

  • 3

    33

    : return []. return . . return, . .

    , . , ;. , .

    . ++ , . ++ . '_(_);

    f(x); , , h=f(x); . f , , h , , return.

    . . .

    , . . : void foo(int i, int j = 7); //

    void foo(int i = 3, int j); // void foo(int i, int j = 7, int k = 8); //

  • 3

    34

    void foo(int i = 1, int j = 7, int k = 8); // void foo(int i, int j = 7, int k); //

    . . . : int max(int, int);

    double max(double, double); ++

    inline. , , [1]. inline double cube (double x)

    { return (x*x*x); } .

    , inline .

    . , , , , . ++ . , . 3.1.

    #include

    int fact(int n); int main()

  • 3

    35

    { int m; cout > m; cout

  • 3

    36

    cout

  • 3

    37

    6) 2

    3cos2)1(

    333 x

    x

    xxy

    ; 20) 2

    21

    342

    x

    xaxy

    ;

    7) 3

    )2sin(72

    aaxxy ; 21)

    ax

    xysin

    1

    )22(2log

    )83(ln

    ;

    8) 6

    )4(3log

    xxy ; 22)

    xyx

    x

    yxy

    23

    233

    1

    ;

    9) 322

    522

    z

    xx

    xzx

    y

    ; 23) 22sin

    b

    xtg

    bxbxy

    ;

    10) 2)1(

    333

    23

    x

    xx

    axx

    y ; 24) 43

    2

    425

    zx

    zxxy ;

    11) 3

    22log3

    2 xaxy ; 25)

    22

    5)(

    a

    bay ;

    12) ba

    baay

    cos3sin

    22

    ; 26) 92

    5

    55

    x

    zx

    y ;

    13) yxctg

    x

    yx

    x

    y 3

    33

    335

    ; 27)

    )(2log

    )2(sin

    ax

    axy ;

    14) )5(10log

    55

    x

    yxy ; 28)

    xzxz

    x

    zzy

    22

    33 ;

  • 3

    38

    1. n. 1!+2!+3!+......+n!

    ( n 1 n, n!=123n).

    2. n :

    !...

    !2!11

    2

    nxxx n .

    3. n :

    23

    2...233

    23232

    22231

    21

    nn .

    4. n

    3*3*...

    3*339

    3*324

    3*311

    nnn .

    5. . , , .

    6. , b, c, d. , , . .

    7. , . , . , , .

    8. , , ( , , 12421).

    9. , , ( , , , 6 25, 36 625).

    10. , , , , .

    11. . 12. ,

    . 13. x, y.

    y), xy, -f(xy) xy, ,f(

    2

    22x ,

    cbacbaf

    52),,( .

  • 3

    39

    14. x, y. 5y) g(2x,-)2y g(1.5x,+y) g(x, ,

    5

    0

    22

    5

    0 242),(

    i i

    ibiai iabai

    ibia

    bag .

    15. x, y. )3g(y

    y)- xg(1.3x,+y) 1.8g(x, ,

    5

    0 )!5(

    3322

    5

    0 !),(

    i i

    ibiai i

    ibia

    bag .

    16. x, y. )5),5,55(max()5,()5,( xyxtytxt , 2

    22)(),(b

    ababat . .

    17. x, y. ))2,2(,min()3,(),32( yxtyxyxtyxyxt ,

    22

    ),(bb

    aababat .

    . 18. x, y.

    0),max(,

    2

    1

    0),min(,2

    2

    32

    232

    yxyyx

    xyx

    yxyyxyx

    t .

    . 19. ( n k)

    : Cnk=Cn-1k-1+Cn-1k, Cn0=Cnn=1. Cnk, .

  • 3

    40

    20. fn f0=f1=1; fn=fn-1+fn-2 n=2,3,... , n fn: () ; () n

  • 4

    41

    4

    : ,

    .

    4.1.

    ++ &,

    , . , vr , &vr . ++ . ,

    '. - '. : *

  • 4

    42

    (. 4.1): int vr = 1;

    int* ptr = &vr; // ptr vr

    . 4.1.

    ++ ( ) *. * , , . . . ptr , *ptr , ptr. : int *ptr; //

    *ptr=1; // ( // vr, ) * &.

    [1]: int a = 1, b;

    int* ptr = &a; // a cout

  • 4

    43

    . & (), -. .

    4.1. #include

    using namespace std; int main() { int t = 13, int &r = t;// t // r t cout

  • 4

    44

    . int masl[492];// 492 ,

    // int main() { double mas2[250];// 250 double static char mas3[20]; // 20 int mas4[2][4]; // int ... } [] ,

    , , . . , . n-1, n .

    . , . ,

    , : int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; ,

    , .

    . . , char code[] = {'a', 'b', 'c'}; code 3. ( )

    .

  • 4

    45

    , , [1]. , : . , , . () [1]. int mas[4];

    int* ptr = mas;

    . 4.1 &mas[0]

    ptr &mas[1] ptr+1

    &mas[2] ptr+2

    &mas[3] ptr+3

    mas[0] *ptr

    mas[1] *(ptr+1)

    mas[2] *(ptr+2)

    mas[3] *(ptr+3)

    , ptr+i &mas[i] , i ( ) ptr &mas ( i=0..3). , ptr+i , &mas[i] . [1]: mas = ptr;

    ++mas; mas = mas + 3; ptr

    . [1]: const int N = 10;

    int mas[N]; int* ptr = mas; // ptr = &mas[0] // 1 : ptr int sum1 = 0; for (ptr = mas; ptr < &mas[N]; ++ptr) sum1 += *ptr; // 2 : int sum2 = 0;

  • 4

    46

    for (int i = 0; i < N; ++i) sum2 += mas[i]; // sum2 += *(mas + i);

    . : int mas[4][2]; // 4 2

    int *ptr; ptr=mas

    mas. mas &ms[0][0] . ptr+1 mas[0][1], : mas[1][0], mas[1][1], mas[2][0] . .; ptr+5 mas[2][1]. , , : . 4.2 ptr ptr+1 ptr+2 ptr+3 ptr+4 ptr+5 ...

    mas[0][0] mas[0][1] mas[1][0] mas[1][1] mas[2][0] mas[2][1] ...

    ,

    . ,

    . ++

    new delete. . ( , heap) , , [1]. new (

  • 4

    47

    ), delete . new . new _;

    new _ ; new _[]; new

    , , . , new 0, . delete : delete ;

    delete[] ; .

    . , . . int n; // n

    cin >> n; // int* mas = new int[n]; // delete[] mas; // mas n .

    int* mas = new int[n] : int, .

    : int n, *mas; // n - , mas

    int cin >> n; mas = new int[n]; // delete[] mas; // delete[] mas

    .

  • 4

    48

    ! delete new. ! . 4.2 ,

    #include

    using namespace std; int main() { int *a; int n; cout

  • 4

    49

    cin>>n; cout

  • 4

    50

    for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ cout

  • 4

    51

    ,

    . . ,

    . , . , , . , ( ) . char[]

    ' , \0 ( ). - . 4.4. , ,

    . : //

    #include ; using namespace std; int dl(char[]);// dl int main() { char p[]=""; cout

  • 4

    52

    return i; } : : 7. - ,

    , . . 4.5. ,

    , 5 . : //

    #include using namespace std; int sum(float x[5]) { float s=0; for(int i=0;i

  • 4

    53

    z. 5,

    x. , , . . z. .

    , ( ). 4.6.

    ,

    . #include

    using namespace std; float summa(int n,float a[][3]) { float s=0; for(int i=0;i

  • 4

    54

    4.2. 1. . 2.

    . 3. .

    4.3.

    . : . 1. . 2. . 3. . 4. . 5. ( ) . 6. . , . 7. . , , .

    1. nxm :

    1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0

  • 4

    55

    2. nxn :

    0 1 1 1 1 02 0 1 1 0 42 2 0 0 4 42 2 0 0 4 42 0 3 3 0 40 3 3 3 3 0

    3. nxn :

    6 1 1 1 1 52 6 1 1 5 42 2 6 5 4 42 2 5 6 4 42 5 3 3 6 45 3 3 3 3 6

    4. nxm :

    1 2 3 4 5 6 12 11 10 9 8 7 13 14 15 16 17 1824 23 22 21 20 19

    5. nxn : - , , ( fn f0=f1=1; fn=fn-1+fn-2 n=2,3,... 1, 1, 2, 3, 5, 8, 13, ...).

    6. nxn : , .

    7. nxn. .

  • 4

    56

    8. nxm :

    1 8 9 16 17 242 7 10 15 18 233 6 11 14 19 224 5 12 13 20 21

    9. nxm :

    1 5 9 13 172 6 10 14 183 7 11 15 194 8 12 16 20

    10. nxn , N 1, , .

    11. nxm :

    19 20 21 22 23 2418 17 16 15 14 137 8 9 10 11 126 5 4 3 2 1

    12. nxn. ( , : i- i- ).

    13. nxn :

    1 3 4 10 112 5 9 12 196 8 13 18 207 14 17 21 2415 16 22 23 25

    14. nxm. , , . , .

  • 4

    57

    15. nxm , . , .

    16. nxn , .

    17. nxn. .

    18. nxm . .

    19. nxn. . 20. nxm. , .

    21. nxm , . , .

    22. nxn. .

  • 4

    58

    23. nxm 3, . , 9.

    24. nxm. , .

    25. nxn. , , . , .

    26. nxm. .

    27. nxn. . 28. 1 N, , , (1/2)*N*(1+N). . 29. "" - , n , n , . 30. nxm. . .

  • 4

    59

    1. ,

    . () : () 3 3, 5 5, 7 7, . {a, b, c} a < b < c b, {a, b, c, d} a < b < c < d (b + c)/2. . (, 20 20) 2000 2000. 1...255. 3 3 :

    mas[i][j] {mas[i-1][j-1], mas[i-1][j], mas[i-1][j+1], mas[i][j-1], mas[i][j], mas[i][j+1], mas[i+1][j-1], mas[i+1][j], mas[i+1][j+1]}.

    2. ,

    . ( ). mas[i][j]

    :

  • 4

    60

    () : 1 2 1 [ 1][ 1] [ 1][ ] [ 1][ 1]

    [ ][ ] 0 0 0 [ ][ 1] [ ][ ] [ ][ 1]1 2 1 [ 1][ 1] [ 1][ ] [ 1][ 1]

    x

    mas i j mas i j mas i jG i j mas i j mas i j mas i j

    mas i j mas i j mas i j

    = mas[i-1][j-1] + 2*mas[i-1][j] + mas[i-1][j+1] mas[i+1][j-1] 2*mas[i+1][j] - mas[i+1][j+1].

    ( ). () :

    1 0 1 [ 1][ 1] [ 1][ ] [ 1][ 1][ ][ ] 2 0 2 [ ][ 1] [ ][ ] [ ][ 1]

    1 0 1 [ 1][ 1] [ 1][ ] [ 1][ 1]y

    mas i j mas i j mas i jG i j mas i j mas i j mas i j

    mas i j mas i j mas i j

    = mas[i-1][j-1] + 2*mas[i][j-1] + mas[i+1][j-1] mas[i-1][j+1] 2*mas[i][j+1] - mas[i+1][j+1]. [i, j] :

    2 2[ ][ ] [ ][ ] [ ][ ]y yG i j G i j G i j .

    (, 20 20) 2000 2000. 1...255.

    4.4. 1. ? ? ? 2. ? 3. , ? 4. ? ? &? 5. ? 6. ? . 7. ? ?

  • 4

    61

    8. ? 9. ? ? 10. new delete? ? 11. ? ? 12. ? ? .

  • 5

    62

    5

    :

    ++.

    5.1. /++ /

    , . . /++ :

    / ( fstream) / ( stdio.h). ++. ++

    . - ++ /. , ++ ++. /++ , (. 5.1).

    . 5.1. /++

    .

    , () . , ,

  • 5

    63

    . . , . , main()

    , exit(), . , ,

    abort(), . .

    , . FILE. , . ++.

    . ++ . - . :

    1. /. 2. . :

    1. . C++ .

    , : . ,

    (, ) ( ).

    . ( /). , 10 .

    . .

    .

  • 5

    64

    fopen(),

    printf() (fprintf()), scanf() (fscanf()), fclose(). f ,

    . . fopen() : FILE* fopen (const char * _, const char *

    ) : _ , ,

    C:\\WINDOWS\\system.ini. . /++ .

    . 5.1.

    r . . w . , . , . a . , . ,

    . r+ . . w+ . , . a+ . , . -

    . , . b

    t ( ).

  • 5

    65

    FILE, 0 . . . #include

    int main() { using namespace std; // FILE * my = fopen(myfile.txt, r); if(!my) printf( !); }

    fprintf() : int fprintf (FILE * , const char * , ...) : FILE,

    . . ,

    %[][][.][] ,

    , . . 5.2

    /

    c d i -234 f 34.32 s Hello, World! u 123

  • 5

    66

    . . 5.3 flags

    - . , 0

    . 5.4 width

    . , , .

    , . . 5.5 .precision

    . d, i, u , . , . f . s .

    . 5.6 length

    H short int unsigned short int ( i, d, u)

    l long int unsigned long int ( i, d, u)

    L long double ( f)

  • 5

    67

    , . : fprintf(stdout, %3.1f, 3.141)

    // PI 1 , 3.1 fprintf() ,

    .

    (. 5.7) int fscanf (FILE * stream, const char * format, ...) : stream FILE,

    ; format . ,

    [=%[*][width][modifiers]type=]

    . 5.7 * , . width , . modifiers , length fprintf() type , . , specifier fprintf(). c, d, f, s,

    u. .

    , . 5.2 test.in 2

    test.out.

  • 5

    68

    #include #include int main() { using namespace std; const char *input = "test.in", *output = "test.out"; int a, b; // FILE * in = fopen(input, "r"); if(!in) { perror(input); return -1; } // 2 if(fscanf(in, "%d %d", &a, &b) != 2) { printf(" 2 .\n"); return -2; } // FILE * out = fopen(output, "w"); if(!out) { perror(output); return -3; } // fprintf(out, "%d", a + b) ; return 0; }

  • 5

    69

    , (, ). size_t fread (void * ptr, size_t size, size_t count,

    FILE * stream); // size_t fwrite (const void * ptr, size_t size, size_t count, FILE * stream); // : ptr ,

    ; size ; count ; stream FILE,

    . fread() (fwrite())

    () . 5.3 . //

    struct { int version; char name[20]; int data_size; } header; FILE* input = fopen(test.hdf, r+b) // if(!fread(&header, sizeof header, 1, input)) { printf( !); exit(-1); } printf(%d, header.version); header.version++;

  • 5

    70

    // if(!fwrite(&header, sizeof header, 1, input)) { printf( !); exit(-2); } (

    ) int fseek (FILE * stream, long int offset, int

    origin) : stream FILE,

    ; offset origin; origin , offset.

    : SEEK_SET ; SEEK_CUR ; SEEK_END . 0, . , fseek(f, 20, SEEK_SET);

    // 20 ,

    long int ftell ( FILE * stream), stream

    FILE, . , . FILE* f = fopen(test.txt, r);

    fseek(f, 0, SEEK_END); long size = ftell(f); int fclose (FILE *

    stream) stream FILE, . : fclose(in);

  • 5

    71

    ++

    ++. / ifstream ofstream. ifstream ofstream (class). , . - . , ,

    / cin cout ( 1), ifstream ofstream. : ifstream ( const char * filename, mode);

    ofstream ( const char * filename, mode); void open ( const char * filename, mode);

    void open ( const char * filename, mode); : filename , /etc/passwd; mode . ++ :

    . 5.8 app . . ate . binary . in . out . trunc . , .

    (|).

    , ios::binary | ios::in .

  • 5

    72

    / //

    ifstream in1(test.in); // , open() ifstream in2; in2.open(test.in); // ofstream out1(test.out); // , open() ifstream out2; out2.open(test.out); ifstream ofstream

    (endl, width, setf, hex) (getline()), cin cout (. 1). , : ofstream o(test.txt);

    o

  • 5

    73

    int a, b; in >> a >> b; ofstream out("test.out"); if(!out) { cerr

  • 5

    74

    int n, m; in >> n >> m; // int** mat = new int* [n]; for(int i = 0; i < n; ++i) mat[i] = new int[m]; // for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) in >> mat[i][j]; double sum = 0; // for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) sum += mat[i][j]; // ofstream out("tst4.out"); if(!out) { cerr

  • 5

    75

    5.2.

    1. test.in . . test.out. cstdio. 2. test.in . . test.out. fstream. 3. test.in NxN. . test.out. cstdio. 4. test.in NxN. . test.out. fstream. 5. test.in . test.out test.in, Hello. test.out. cstdio. 6. test.in . test.out test.in, Hello. test.out. fstream. 7. test.in . Hello World. test.out. cstdio. 8. test.in . Hello World. test.out. fstream. 9. test.in . . (: Hello, World! hELLO, wORLD!). test.out. cstdio. 10. test.in . . (: Hello, World! hELLO, wORLD!). test.out. fstream.

  • 5

    76

    1. , ++ ( *.cpp) /* */. cstdio. 2. , ++ ( *.cpp) /* */. fstream. 3. , ++ ( *.cpp) // . cstdio. 4. , ++ ( *.cpp) // . fstream. 5. , - (, ) . cstdio. 6. , - (, ) . fstream. 7. , () . (*.txt)? (*.exe)? cstdio. 8. , () . (*.txt)? (*.exe)? fstream. 9. , ($H = - \sum_{i=0}^{255} f_i \log f_i$, $f_i$ -- $i$). Word (*.doc) (*.zip). cstdio. 10. , ($H = - \sum_{i=0}^{255} f_i \log f_i$, $f_i$ -- $i$). Word (*.doc) (*.zip). fstream. 11. , ( 8). cstdio.

  • 5

    77

    12. , ( 8). fstream. 13. , ( 8). cstdio. 14. , ( 8). fstream. 15. . This program (*.exe)? cstdio. 16. . This program (*.exe)? fstream. 17. , 5 . cstdio. 18. , 5 . fstream. 19. , ( ). cstdio. 20. , ( ). fstream. 21. , ( ). cstdio. 22. , ( ). fstream. 23. , ( ). cstdio.

  • 5

    78

    24. , ( ). fstream.

    1. , . [.., .., .., .. " "]. 2. , . 3. , N- (N > 2) . , N- [.., .., .., .. " "].

    5.3. 1. ++? 2. ?

  • 6

    79

    6

    :

    STL ++ C.

    6.1.

    (namespace) ++,

    ' , . . namespace []

    { // } , ,

    ::. , using namespace. , : using namespace [] 6.1.

    #include

    using namespace std; namespace spaceA { int MyVal=10; }

  • 6

    80

    namespace spaceB { int MyVal=20; } namespace spaceC { int MyVal=30; } void Test() { using namespace spaceB; cout

  • 6

    81

    string hi(hello); // string lo=greetings; // string es=; // , - : string name(Fred);

    name = Flintstone; // ' .

    (>) , . 6.2. -

    #include

    // STL #include using namespace std; int main() { string s; while (cin >> s) // // cin cout great) if (lo < hi)

  • 6

    82

    , : cout

  • 6

    83

    char ch1 = s0[3]; cout

  • 6

    84

    string lo=greetings; // string g(lo); // string es=; // string s; // return 0; }

    6.2. 1. . 2.

    . 3. .

    6.3.

    1. . 2. , . 3. . 4. . 5. '' ''. 6. ''. 1. , ,

    , . 2. , ,

    "". , "" . "", , .

    3. , , . . . , ', , , '.

  • 6

    85

    4. --. , 1-5-94, 01-05-94. , , , .

    5. , () . , . , , , "!", - "!".

    6. , ( ) .

    7. . . - , , =3, ..

    8. , , .

    9. , ', , : , ).

    10. , Z. 11. , ,

    1 , 1 . 12. , ,

    . , , .

    13. . , , , . ( , ).

    14. . , .

    15. , ' , . .

    16. , .

    17. , .

  • 6

    86

    18. "" "" ( , , "" ""). .

    19. , ( , , "" ""). .

    20. i- . 21. , - .

    . , .

    22. , "*". . , .

    23. : ) ; ) ; ) . . 24. , 2 ,

    . . , .

    25. , , - . .

    26. . . , .

    27. .

    28. ( ) , [X, Y]. X Y , .

    29. , ( ). , - .

    30. , , , . - , : .

    31. , 1 5 .

  • 6

    87

    - . , , , , .

    32. ', .

    33. - .

    34. , .

    35. ( ) , [X, Y]. X Y , , , .

    36. , .

    37. , .

    38. "" ?

    6.4. 1. STL C++? 2. ++ ? 3. ? 4.

  • 7

    88

    7

    : ++ .

    7.1.

    ( ,

    exception handling) C++. , . . , , , , . . C++

    : try, catch throw. , , try.

  • 7

    89

    try, ( throw), ( catch) . , try,

    . - catch, try. try catch: try {

    // } catch (type1 arg) { // } catch (type2 arg) { // } catch (type arg) { // } try ,

    . , main(). - catch. try catch. T, catch , . , , catch,

  • 7

    90

    () , catch. try ( , try , catch, try). , arg . , catch type, arg . - , . throw: throw ; throw try,

    - , . ,

    catch, . C++, unexpected() terminate(). terminate() abort(), . , terminate(), abort()

    unexpected(), . , , .

  • 7

    91

    terminate() set_terminate(), unexpected() set__unexpected(). catch .

    , , throw . double mean(double a, double b) throw(char *); , -, ,

    . , abort(). -, - , , . , , char *, double. double multipleErrorsFunction(double z) throw(char

    *, double); ,

    , . , . double simple(double z) throw ();

    // try , ,

  • 7

    92

    try. - , (...): catch(...)

    { // } try,

    throw . catch (char* s)

    { cout

  • 7

    93

    , try ,

    , , , . , , , try catch. . C++

    . . , , ( ). , , , . , . , , , , , . , .. , , , . , , , .., . , ( ). - .

  • 7

    94

    , , , . , , . , , , , try. , , . . , - , . , , , try . , . ++

    main(): void main(int argc, char *argv[])

  • 7

    95

    argc () argv . . , , , . . argc , . 7.1. ,

    #include

    using namespace std; void main(int argc, char *argv[]) { cout

  • 7

    96

    argv[0] , argc 1. , main() argv

    ( ). for argv. ( , , argc). 7.2. ,

    #include

    using namespace std; void main(int argc, char *argv[]) { int i; for (i = 0; i < argc; i++) cout

  • 7

    97

    #include using namespace std; void main(int argc, char *argv[]) { int i; for (i = 0; argv[i] != NULL; i++) { cout

  • 7

    98

    . , , .

    ecvt(), fcvt(), gcvt().

    atof(), atoi(), atol(). 7.4. atof(), atoi(), atol() #include

    main( ) { char *s; double x; int i; long l; s=" -2309.12E-15"; x=atof(s); cout

  • 7

    99

    i=atoi(s); cout

  • 7

    100

    cin >> a; cout

  • 7

    101

    7.3. 1. . 2. . 3. .

    7.4.

    1

    . 1.

    . 2.

    ++. 3.

    : try; ,

    .

    1. , 7.4,

    .

  • 7

    102

    2. assert .

    3. .

    7.5. 1. ?

    ++? 2. ?

    ? 3. assert

    ? 4. ++ ? 5. ,

    ++?

  • 8

    103

    8

    : ,

    .

    8.1.

    ,

    , / . , . ,

    , . - , . ( ),

  • 8

    104

    . ( , .) -

    .

    . 8.1.

    , . new .

    .

    .

  • 8

    105

    . 8.2.

    . .

    . 8.3. -

    , . , . ( , )

    . - . , ( ) .

  • 8

    106

    . 8.4. ,

    , ++ . , (linker) . , . . ++ , , , . , Microsoft C++, Project. Unix Linux make.

  • 8

    107

    . , , . ( , ). , , . , , , .

    . , #include , . . , .

    : ,

    , ; main(),

    ; . .

    , , , .

  • 8

    108

    , . : ; , #define

    const; ; ; ; .

    (project list). #include . ,

    , [3].

    8.2. ,

    . . : , -

  • 8

    109

    ++, . , module.h

    // FILE module.h

    #include #include using namespace std; struct element { string str; element* next; }; element* EnterList(); int Count(element*); // FILE module.cpp with functions body #include "module.h" element* EnterList() { element *first, *current; string answer; cout

  • 8

    110

    cin>> current->str; cout> answer;

    while(answer != "n") { current->next = new element; current = current->next; cout >current->str; cout> answer; } current->next = NULL; return first; } int Count(element* list) { int result = 0; if(!list) { cout

  • 8

    111

    result++; list=list->next; } return result; } // FILE project1.cpp with main() function #include "module.h" int main() { element *current, *top; top = EnterList(); current = top; while(current!=0) { coutstr next; } cout

  • 8

    112

    .

    8.3. 1. . 2. .

    - . 3. . 4.

    . 23

    :

  • 8

    113

    (), (, ) ( new, delete );

    , , ; ( ) ;

    ( ), , , ;

    ; .

    8.4. . 1.

    () ( ). , : ) (T) ; ) (, i, j, ), , i, ,

    , j- ; ) (T, i, j), i- j- ; ) (, i, j), i- j- . ) (), ; ) (),

    (#) . ( fgets())

  • 8

    114

    2.

    () ( ). , : ) (, i, j), i- j- ; ) (, j), j- ; ) (, , i, j), , , ,

    , i j , i , j ; ) (T,c)

    ; ) (), ; ) (),

    (#) . ( fgets())

    3. ()

    (. ) (. )

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    typedef double ;

    struct { ; * ; };

  • 8

    115

    L, L1 L2 , , 1 2- , . , : ) , L ; ) L ; ) L E1 2; ) L; ) ; ) . 4. ()

    (. ) (. )

    typedef char ;

    struct { ; * ; }; L, L1 L2 , , 1

    2- , . , : ) , L ; ) L ;

  • 8

    116

    ) ASCII L, . ) . 5. ()

    (. ) (. )

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    typedef char [10];

    struct { ; * ; }; L, L1 L2 , , 1 2

    , . , L, ; ) ; ) , ; ) ; ) ; ) . 6. ()

    (. ) (. )

  • 8

    117

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    typedef char [10];

    typedef file of ; rr [1..50] f ; , , : ) f ; ) ( ). 7. ()

    (. ) (. )

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    L, L1 L2 , , 1 2- , . , ) L E, ;

  • 8

    118

    ) L : L1 L2 L { = double}. ) L2 , , ) (

    ). 8. ()

    (. ) (. )

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    L , , 1 , . { = char*} , ) L ; 6) L ; ) L; ) L E1 E ; ) . 9. ()

    (. ) (. )

  • 8

    119

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    L , , 1 , . { = char*} , ) L 1

    , L; ) L 1 2

    ; ) L,

    , , ; ) L ; ) . 10. ()

    (. ) (. )

  • 8

    120

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    L , , 1 , . { = char*} , : ) L ; ) L , ; ) L ,

    ) L ; ) . 11. ()

    (. ) (. )

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    L , , 1 , . { = int}

  • 8

    121

    , 0. , : ) ; ) ,

    ; ) ; ) . 12. ()

    (. ) (. )

    L , , 1 , . { = char*} , : ) L1 L2; ) , L1 L2; ) , L1 (L2) ; ) L1 (L2)

    ; ) .

  • 8

    122

    13. () (. ) (. )

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    L , , 1 , . { = char} , : ) L1 L2; ) L1

    L2, L1; ) L1 (L2),

    , ; ) L

    ; ) . 14. ()

    (. ) (. )

  • 8

    123

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    L , , 1 , . { = double} , : ) , L; ) L; ) L; ) L E1 E2; ) . 15. ()

    (. ) (. )

    L , , 1 , . { = double}. , :

  • 8

    124

    ) L ; ) L1 L ; ) L; ) L . ) . 16. ()

    (. ) (. )

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    L , , 1 , . { = double} , L,

    , : ) L1 L2; ) L1 L2; ) L1, L2; ) L1 L2,

    . ) L . 17. ()

    (. ) (. )

  • 8

    125

    E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    L , , 1 , . { = double} ,

    L1 L2 ( = double) ; ) L; ) L1 L2

    L1. ) . 18. L , , 1 ,

    . typedef * ;

    struct { char* ; * '; }; , : ) L ,

    L ; ) L ) L ; ) L;

  • 8

    126

    ) L, .

    e) . 19. 0111 *...**)( AXAXAXAXP nnnn (. 14 ),

    Ai =0, E1 ...L

    L

    E2 En nil

    E1 ...E2 En nil

    ( . 14 XXXXP 840 *3*52)( . , ,

    - ) (,q),

    q; 6) (, ),

    ) (, q), q; ) (, v),

    , ' v ; (, S (s',') 52y**40-3y**8+y); ) (),

    ( ) - .

  • 8

    127

    20. L ()

    (. 15) ( E *char)

    L E1 E2 En...

    .15 , : ) , L ; ) L, ; ) , L ,

    ( ) ; ) L

    E, E L ; ) .

    8.5. 1. ? 2. ? . 3. . 4. ++ ? 5. .

  • 128

    1. . - C++. , 2001. 464 . 2. . ++. . , 2008. 1104 . 3. - C++. . , 2007. 1184 . 4. . . , . . - C++. , 2007. 800 .