369
Бьерн Страуструп. Язык программирования С++ Второе дополненное издание Языки программирования / С++

Бьярн Страуструп - Язык программирования C++

Embed Size (px)

Citation preview

  • . ++

    / ++

  • . ++

    2

    ..................................................................................................................8 : ..............................................................................................................................................8

    ...................................................................................................................................................9 .......................................................................................................................10

    ......................................................................................................11 .......................................................................................................................11

    ................................................................................................................12 ............................................................................................................................................12 ................................................................................................................................13 ....................................................................................................................................................13 ..........................................................................................................................13 ....................................................................................................................................14 ++ .............................................................................................................................15 .......................................................................................................................16 .................................................................................................................................17 ++ .............................................................................................18 ..........................................................................................................................19 ..............................................................................................................19 .......................................................................................................................................20

    1. ++ .....................................................................................................................22 1.1 ...............................................................................................................................................22 1.2 ................................................................................................................22 1.2.1 .......................................................................................................23 1.2.2 ..........................................................................................................24 1.2.3 ...........................................................................................................................25 1.2.4 ............................................................................................................27 1.2.5 - ..............................................................................28

    1.3 " ".......................................................................................................................................29 1.3.1 ...................................................................................................29 1.3.2 ........................................................................................30 1.3.3 ........................................................................................................................31 1.3.4 ...........................................................................................................31 1.3.5 .............................................................................................................................................32 1.3.6 ...............................................................................................................................................33

    1.4 ..............................................................................................................35 1.4.1 .................................................................................................................35 1.4.2 .........................................................................................................36 1.4.3 ..................................................................................................................................37 1.4.4 ............................................................................................................37 1.4.5 ......................................................................................................................38 1.4.6 ............................................................................................................39

    1.5 - ................................................................40 1.5.1 ...............................................................................................................................40 1.5.2 ...................................................................................................................................41 1.5.3 ..........................................................................................................42 1.5.4 .....................................................................................................................................43

    1.6 ............................................................................................................................44 2. ...........................................................................................................46

    2.1 .............................................................................................................................................46 2.1.1 ..........................................................................................................................47 2.1.2 .............................................................................................................................48 2.1.3 .....................................................................................................................48

    2.2 .....................................................................................................................................................49 2.3 .......................................................................................................................................................49

  • . ++

    3

    2.3.1 .................................................................................................................................50 2.3.2 ..........................................................................................................51 2.3.3 ............................................................................................................................52 2.3.4 void..............................................................................................................................................52 2.3.5 ..........................................................................................................................................53 2.3.6 .............................................................................................................................................53 2.3.7 ........................................................................................................................55 2.3.8 ........................................................................................................................................56 2.3.9 ...................................................................................................................58 2.3.10 .............................................................................................................................................59

    2.4 ..............................................................................................................................................61 2.4.1 .............................................................................................................................61 2.4.2 .....................................................................................................62 2.4.3 ...................................................................................................................62 2.4.4 ...............................................................................................................................................63 2.4.5 ....................................................................................................................................................63

    2.5 ....................................................................................................................64 2.5.1. ....................................................................................................................................65

    2.6. ....................................................................................................................................66 2.6.1 ...................................................................................................................................................66 2.6.2. ......................................................................................................................................67

    2.7 .............................................................................................................................................68 3. ........................................................................................................70

    3.1 .............................................................................................................................................70 3.1.1 .........................................................................................................................................70 3.1.2 ...................................................................................................................................74

    3.1.3 .........................................................................................................................................76 3.1.4 ............................................................................................................................77 3.1.5 ..............................................................................................................................................78 3.1.6 ........................................................................................................79

    3.2 ......................................................................................................................................79 3.2.1 ................................................................................................................................................83 3.2.2 ..........................................................................................................................83 3.2.3 ...................................................................................................................84 3.2.4 ....................................................................................................85 3.2.5 ........................................................................................................................86 3.2.6 .............................................................................................................................87

    3.3 ...................................................................................................................................90 3.3.1 ..................................................................................................................90 3.3.2 goto ...................................................................................................................................92

    3.4 ...................................................................................................93 3.5 ..............................................................................................................................................94

    4..............................................................................................................................................................97 4.1 ....................................................................................................................................................97 4.2 ................................................................................................................................................97 4.3 .................................................................................................................................99

    4.3.1 ..................................................................................................100 4.3.2 ............................................................................................102

    4.4 ........................................................................................104 4.5 ........................................................................................................................105 4.6 ...................................................................................................................................................106

    4.6.1 ............................................................................................................................106 4.6.2 .......................................................................................................................107 4.6.3 ......................................................................................................................107 4.6.4 ....................................................................................................................109

  • . ++

    4

    4.6.5 -.............................................................................................................................110 4.6.6 ...............................................................................................................111 4.6.7 ...............................................................................................113 4.6.8 ..................................................................................................114 4.6.9 .....................................................................................................................115

    4.7 .......................................................................................................................................118 4.8 ............................................................................................................................................120

    5. .......................................................................................................................................................122 5.1 .......................................................................................................................122 5.2 ........................................................................................................................................123 5.2.1 - .....................................................................................................................................123

    5.2.2 ..............................................................................................................................................123 5.2.3 ..................................................................................................................................124 5.2.4 ..................................................................................................................................127 5.2.5 ...........................................................................................................................................128 5.2.6 ....................................................................................................................................129

    5.3 ......................................................................................................................129 5.3.1 ...........................................................................................................130 5.3.2 .............................................................................................................132

    5.4 ..........................................................................................................................................135 5.4.1 ...............................................................................................................................................135 5.4.2 ....................................................................................................................136 5.4.3 ...........................................................................................................................137 5.4.4 .........................................................................................................................138 5.4.5 ..........................................................................................................................139 5.4.6 ...............................................................................................................139

    5.5 .............................................................................................................142 5.5.1 ....................................................................................................................142 5.5.2 .......................................................................................................................143 5.5.3 ............................................................................................................................144 5.5.4 ...............................................................................................................144 5.5.5 ................................................................................................................146 5.5.6 .........................................................................................................................146

    5.6 ............................................................................................................................................147 6............................................................................................................................................................149

    6.1 .......................................................................................................................149 6.2 ...............................................................................................................................149

    6.2.1 - .................................................................................................................................151 6.2.2 .......................................................................................................152 6.2.3 ..............................................................................................................................153 6.2.4 .........................................................................................................................................153 6.2.5 ......................................................................................................................155

    6.3 ..............................................................................................................................156 6.4 ............................................................................................................158

    6.4.1 ...............................................................................................................................158 6.4.2 ............................................................................................................................160 6.4.3 ....................................................................................................................162

    6.5 ...............................................................................................................164 6.5.1 ..................................................................................165 6.5.2 ..........................................................................................................165 6.5.3 ..........................................................................................................167

    6.6 ..................................................................................................................................171 6.6.1 ...........................................................................................................................171 6.6.2 ...............................................................................................................173

    6.7 ..................................................................................................................................174

  • . ++

    5

    6.7.1 ...........................................................................................................175 6.7.2 .......................................................................................................................178

    6.8 ............................................................................................................................................179 7............................................................................................................................................................181

    7.1 ..................................................................................................................................................181 7.2 ...........................................................................................................................181

    7.2.1 .........................................................................................................182 7.2.2 ............................................................................................183 7.2.3 ..........................................................................183

    7.3 ...............................................................................184 7.3.1 .................................................................................................................................184 7.3.2 ................................................................................................................186 7.3.3 .............................................................................................................................187

    7.4 ................................................................................................................................................189 7.5 ...................................................................................................................................189 7.6 ...............................................................................................................190 7.7 ..............................................................................................................................................192 7.8 ........................................................................................................................................194 7.9 ..............................................................................................................................195 7.10 .......................................................................................................................196 7.11 ...................................................................................................................................198 7.12 ....................................................................................................................................... 200 7.13 ................................................................................................................................ 201 7.14 .......................................................................................................................................... 201

    8. .......................................................................................................................... 204 8.1 .................................................................................................................................................. 204 8.2 ............................................................................................................................ 204 8.3 ...................................................................................................................... 206

    8.3.1 ..................................................................................................... 207 8.3.2 .................................................................................................... 209 8.3.3 ............................................................................................................................ 212 8.3.4 .......................................................................................................................................... 213

    8.4 ................................................................................................................... 215 8.4.1 ............................................................................. 216 8.4.2 ............................................................... 217 8.4.3 ................................................................................. 218 8.4.4 ............................................................................................................. 219 8.4.5 .................................................... 219

    8.5 .................................................................................... 221 8.6 ..................................................................................................................... 222 8.7 ................................................................................................... 224

    8.7.1 .................................................................... 225 8.8 ........................................................................................................................... 225 8.9 ............................................................................................................................................ 231

    9............................................................................................................................................................ 232 9.1 ................................................................................................................................. 232

    9.1.1 .................................................................. 234 9.1.2 ....................................................................................... 235

    9.2 .................................................................................................................. 235 9.3 ......................................................................................................................... 237

    9.3.1 ..................................................................................................... 238 9.3.2 ........................................................................................................ 240

    9.4 .................................................................................................................................... 243 9.4.1 ....................................................................................................... 244 9.4.2 ............................................................................................................................ 246

  • . ++

    6

    9.4.3 .......................................................................................................................... 246 9.4.4 ................................................................................................. 248

    9.5 ......................................................................................... 249 9.6 ............................................................................................................................... 251

    9.6.1 ...................................................................................................... 252 9.7 ........................................................................................................ 253 9.8 ....................................................................................................... 254 9.9 ............................................................................................................................................ 256

    10. ......................................................................................................................................... 257 10.1 ........................................................................................................................................... 257 10.2 .................................................................................................................................................. 258 10.2.1 ............................................................................................................... 259 10.2.2 ..................................................................................................... 260

    10.3 ..................................................................................................................................................... 261 10.3.1 ................................................................................................................. 261 10.3.2 ......................................................................................................................... 263 10.3.3 ........................................................................................................ 265

    10.4 .................................................................................................................................. 266 10.4.1 ios.......................................................................................................................................... 266 10.4.1.1 ................................................................................................................ 266 10.4.1.2 ............................................................................................................................. 267 10.4.1.3 ............................................................................................................... 268 10.4.1.4 ............................................................................................................................. 269 10.4.1.5 ................................................................................................................. 270 10.4.1.6 ........................................................................................................... 270

    10.4.2 ................................................................................................................................ 271 10.4.2.1 - .......................................................................... 273

    10.4.3 ostream................................................................................................................................. 274 10.4.4 istream.................................................................................................................................. 275

    10.5 .................................................................................................................................... 275 10.5.1 ......................................................................................................................... 276 10.5.2 ......................................................................................................................... 277 10.5.3 ................................................................................................................................... 278

    10.6 - ...................................................................................................................................... 279 10.7 .......................................................................................................................................... 281

    11. ............................................................................................. 283 11.1 ................................................................................................................................................ 283 11.2 .................................................................................................................................... 285 11.3 ................................................................................................................................. 287 11.3.1 ................................................................................................................................ 289 11.3.2 ..................................................................................................................... 290 11.3.3 .................................................................................................................... 291 11.3.3.1 1: .................................................................................................... 292 11.3.3.2 2: ..................................................................................... 293 11.3.3.3 3: ............................................................................................... 294 11.3.3.4 4: ........................................................................................... 294 11.3.3.5 ............................................................................................... 295 11.3.3.6 ............................................................................................................ 295

    11.3.4 .................................................................................................................... 296 11.3.5 .................................................................................................................................. 298 11.3.6 .............................................................................................................................. 298 11.3.7 ............................................................................................................................. 299

    11.4 ........................................................................................................................... 299 11.4.1 .............................................................................................................. 299 11.4.2 ............................................................................................................................................. 301

  • . ++

    7

    11.4.3 ..................................................................................................................... 302 11.5 ........................................................................................................................................... 303 11.6 ................................................................................................ 304

    12. ++......................................................................................................... 306 12.1 ......................................................................................... 306 12.1.1 ................................................................................................................... 307 12.1.2 ......................................................................................................... 309 12.1.3 ............................................................................ 309 12.1.4 .......................................................................................................................... 312

    12.2 ................................................................................................................................................... 314 12.2.1 ?.......................................................................................................... 315 12.2.2 ............................................................................................................................ 316 12.2.3 . ...................................................................................... 318 12.2.4 ........................................................................................................ 319 12.2.5 .................................................................................................. 321 12.2.6 ............................................................................................................. 324 12.2.7 ............................................................................................................. 325 12.2.7.1 ............................................................................................................................. 326 12.2.7.2 ........................................................................................................................... 328

    12.2.8 ....................................................................................................... 328 12.3 .......................................................................................................................................... 330 12.4 .................................................................................................................... 332 12.5 ........................................................................................................................................... 334

    13. .............................................................................................. 335 13.1 ................................................................................................................................................ 335 13.2 ................................................................................................................................ 336 13.3 .............................................................................................................................. 338 13.4 ..................................................................................................................................... 342 13.5 ....................................................................................................... 344 13.5.1 ....................................................................................................................... 346 13.5.2 Type_info............................................................................................................................... 347 13.5.3 ................................................................... 349 13.5.4 .......................................................................... 350 13.5.5 ...................... 350

    13.6 .......................................................................................................................... 351 13.7 ................................................................................................................ 354 13.8 .......................................................................................................................... 355 13.9 ............................................................................................................................ 358 13.10 .......................................................................................................................... 361 13.10.1 ........................................................................................................................... 362 13.10.2 ................................................................................................................ 363 13.10.3 ......................................................................................... 366

    13.11 ........................................................................................................................................ 368

  • . ++

    8

    . " ++" , . , , ++, , ( ) . . , , . ++. . :

    . , -

    . ,

    . . , ,

    , - . , ++ . ++.

    : ++ . - - AT&T Bell Laboratories (-, ). . (), (). , , . . . ++ - " ++ ".

  • . ++

    9

    " "

    ( )

    , ++. , . , ++, . , . . ++ - . , , , . , . , "" . ++. 1 10 , . 11 13 . ++. , , , . , , , -, , , . ++ . - , . , ++ , . ++ , - -, . , , . , , , . , , . , , , , , , .., - " 100% " , . "" , . . , , . , . . , . , . , ++, . , .

  • . ++

    10

    , , , , , , , , , , , , , , , . ++ 1985 1991 . . : , , . , ++, , , X3J16 ++.

    -, .

  • . ++

    11

    " , ".

    (..)

    ++ - , . . , . , , . . , . , . -. , , . ++ . - . , , , , . ++ , . , ++ , : , -, , , . ++ , "" (, , , ..). . , ++ . , . ++ . , , ++. , . ++, .

    ++ , . , , , , , , , , , , , . /. ++ , , , , . : , , , , , , . , , , , , , ++, Bell Labs , , 26-27 1985 . -, .-

  • . ++

    12

    " - ,- ".

    .

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

    . 1 10 . 11 13 ++. . ++ . , , , . , C++ , , . , , , , , . , . , , . 1 ++. . . , - . . 2, 3 4 ++, : , . , , . . 5 - 8 , . 5 - . , (), , , , , . 6 , . ( ) , . 7 , , , , , . 8 , .. ++, . 9 , . 10 ostream istream, -. 11 - 13 , ++ . 11 . 12 ++ . 13 . ++. $$2.3.4, 3.4 2.

  • . ++

    13

    R, , $$R.8.5.5.

    ++. , . , , , , , , , ++, , , , .. ++, , . , , , DEC VAX 11/8550 10- UNIX [25]. , ++, . " ++", .. . , . , , , .

    . . , . , . , , (*1) , (*2) , (*3) - . , . (*1) , . , , , (*5) . 2 - 4. ([1]) . 5 - 8. , - . ++ .

    ++ . , : , - . , . ++ . , . , . ++ . , . , , . , . , , 16 , , 32- .

  • . ++

    14

    ++ , : UNIX. ++ . , , , . ++ , . . , , . , , , .

    ++ [8], . , . , , BCPL [13]. BCPL ++. BCPL, , ++ - VALOF. SIMULA-67 [2,3]; ( c ). inspect SIMULA-67 ++. . ++ , , -68 [24]. ++ . , . , . , : , - static const, (protected), . , ++ , . [10,18,20,21 23]. - , ( ) CLU. CLU [11], ML [26]. , 1985 1991 .. ( , - ), ++, . , " " [16], , 1980 . , , . SIMULA-67 , . " " . , . , , . ++ , , 1983 ., ++ . ++ ( ) , 1983 . . ++ . + . , . , ++ , ++. D, , - . ++ [12]. ++ ,

  • . ++

    15

    , . - . ++ . , . " ++" " ++". , , . . ++, . 1987 . , ++ [22]. , ++ . , ++ . AT&T Bell Laboratories , . , .. , AT&T. , . 20 , ANSI ++. " ++" [4]. . , Hewlett-Packard 1989 . ANSI X3J16. , ++ ANSI ( ) ISO ( ). ++ , . , complex, vector stack, . . . , . task, [19] 13 $$6.8 , " ". . task . . , , . . - ($$10), . ($$10.4.2). map ($$8.8) .. Pool ($$13.10), ($$5.5.6). Vector, Map, Slist sort, 8.

    ++ ++ :

    (1) , ; (2) ; (3) ; (4) UNIX.

    , , " " , , , . , " " ( ) . ++ , , , . , ,

  • . ++

    16

    (., , [15]). : (1) , ++, , ++ ; (2) , ++ ; (3) , ; ++ ; (4) ++ , , , . ++ , , .

    , ++ [14]. ANSI [27] , , , " ". , , void* ANSI , ++. , ++ , . ++ , ($$R.18). ++ ANSI C, [9]. , , ++. ++ . , ++ . , () , ++ , (. " " ). , ++. , [8] ++. ++ .

    ++ , , , C++. , , , , , .. , . new, delete throw, , ++ . ++ ( ) , . , , . , . ++ , . , . . ++ , , . , , . , , . , , , .

  • . ++

    17

    ++ . . (, 1000 ) , . , , . 10 000 , , , . ++ , , 25000 . , . , , . , , , . , , . , , . , , , .. ++ , . . , . , ++ , , . , . ++ , . , . 11 12. , , .. , . , , , . , , . ++ : ++, , .., , . ++ , , , , .

    : , , . , " ". "" , . . , " ", , . , , . , , , , . - . , , , . ; , . , , , .. . .

  • . ++

    18

    . , ++ .

    ++ , : , , , , , . , , , , . . , . . - ; : , . , , . , . , : ? () ? ? , , , , . , , , , . , : , , .. , . , , . ++ , , . , . , , , , . "" - () . A B. "A B", "A B", "A B" - . "A B", , . , , - , .. , . . , , . () ($$6.5). , , , .. . , . . . , , . ++ ($$5.4.1). , , , , , .

  • . ++

    19

    , , , . . . , " T", T . , , . - , , , . , , , , . , . " ++?" " ?". : " , , , " " ". ++ , , .

    " ", ++. , , . , . . , , . , ! , .

    [1] , , . , .

    [a] "" , . [b] "" ,

    . [c] , ,

    . [d] ,

    . [2] ,

    , : [] . [b] ( ). [c] -. [d] friend ( , [], [b] [c]). [e] - . [f] " "; . [g] - .

    , ++ . ++. " " ($$R.18). , ++ , . ++: const ($$2.5)

  • . ++

    20

    enum ($$2.5.1), ; inline ($$4.6.2), , ; ($$8), . , , , ++ , . malloc(), new ($$3.2.6). , , . , - , ; ($$2.6.2). void*, , . - , - . , ++, extern "C" ($$4.4). , , , - .

    . , , , .

    [1] A.V.Aho, J.E.Hopcroft, and J.D.Ulman: Data Structures and Algoritms. Addison-Wesley, Reading, Massachusetts. 1983.

    [2] O-J.Dahl, B.Myrhaug, and K.Nugaard: SIMULA Common Base Language. Norwegian Computing Ctnter S-22. Oslo, Norway. 1970

    [3] O-J.Dahl and C.A.R.Hoare: Hierarhical Program Construction in Structured Programming. Academic Press, New York. 1972. pp. 174-220.

    [4] Margaret A.Ellis and Bjarne Stroustrup: The Annotated C++ Reference Manual. Addison-Wesley, Reading, Massachusetts. 1990.

    [5] A.Goldberg and D.Rodson: SMALLTALK-80 - The Language and Its Implementation. Addison-Wesley, Reading, Massachusetts. 1983.

    [6] R.E.Griswold et.al.: The Snobol14 Programming Language. Prentice-Hall, Englewood Cliffs, New Jersy, 1970.

    [7] R.E.Griswold and M.T.Griswold: The ICON Programming Language. Prentice-Hall, Englewood Cliffs, New Jersy. 1983.

    [8] Brian W.Kernighan and Dennis M.Ritchie: The C Programming Language. Prentice-Hall, Englewood Cliffs, New Jersy. 1978. Second edition 1988.

    [9] Andrew Koenig and Bjarne Stroustrup: C++: As Close to C as possible - but no closer. The C++ Report. Vol.1 No.7. July 1989.

    [10] Andrew Koenig and Bjarne Stroustrup: Exception Handling for C++ (revised). Proc USENIX C++ Conference, April 1990. Also, Journal of Object Oriented Programming, Vol.3 No.2, July/August 1990. pp.16-33.

    [11] Barbara Liskov et.al.: CLU Reference Manual. MIT/LCS/TR-225. [12] George Orwell: 1984. Secker and Warburg, London. 1949. [13] Martin Richards and Colin Whitby-Strevens: BCPL - The Language and Its Compiler. Cambridge

    University Press. 1980. [14] L.Rosler: The Evolution of C - Past and Future. AT&T Bell Laboratories Technical Journal. Vol.63

    No.8 Part 2. October 1984. pp.1685-1700. [15] Ravi Sethi: Uniform Syntax for Type Expressions and Declarations. Software Practice & Experience,

    Vol.11. 1981. pp.623-628. [16] Bjarne Stroustrup: Adding Classes to C: An Exercise in Language Evolution. Software Practice &

    Experience, Vol.13. 1983. pp.139-61. [17] Bjarne Stroustrup: The C++ Programming Language. Addison-Wesley. 1986.

  • . ++

    21

    [18] Bjarne Stroustrup: Multiple Inheritance for C++. Proc. EUUG Spring Conference, May 1987. Also USENIX Computer Systems, Vol.2 No 4, Fall 1989.

    [19] Bjarne Stroustrup and Jonathan Shopiro: A Set of C classes for Co-Routine Style Programming. Proc. USENIX C++ conference, Santa Fe. November 1987. pp.417-439.

    [20] Bjarne Stroustrup: Type-safe Linkage for C++. USENIX Computer Systems, Vol.1 No.4 Fall 1988. [21] Bjurne Stroustrup: Parameterized Type for C++. Proc. USENIX C++ Conference, Denver, October

    1988. pp.1-18. Also, USENIX Computer Systems, Vol.2 No.1 Winter 1989. [22] Bjarne Stroustrup: Standardizing C++. The C++ Report. Vol.1 No.1. January 1989. [23] Bjarne Stroustrup: The Evolution of C++: 1985-1989. USENIX Computer Systems, Vol.2 No.3.

    Summer 1989. [24] P.M.Woodward and S.G.Bond: Algol 68-R Users Guide. Her Majesty's Stationery Office, London.

    1974. [25] UNIX Time-Sharing System: Programmer's Manual. Research Version, Tenth Edition. AT&T Bell

    Laboratories, Murray Hill, New Jersy, February 1985. [26] Aake Wilkstroem: Functional Programming Using ML. Prentice-Hall, Englewood Cliffs, New Jersy.

    1987. [27] X3 Secretariat: Standard - The C Language. X3J11/90-013. Computer and Business Equipment

    Manufactures Association, 311 First Street, NW, Suite 500, Washington, DC 20001, USA. 11.

  • . ++

    22

    1. ++ " , , ".

    (" VI", II)

    ++. . . - , .

    1.1 ++ , :

    ; ; - .

    . $$1.2 "", "" "-" . , . " " ". ++ - " ". , , - . C ++. - " " " - ". , . $$1.3 . , , , , - . 2, 3 4. $$1.4 , . , , , , , , . 5, 7, 8 9. $$1.5 - . , , . 6. $$1.6 , ++ . , , . - , , , , . , . , .

    1.2 - - ,

  • . ++

    23

    "" . - , : , - . . , , , ( , ). , , . , . , - , , . () , . , , . - . , . , , . , , , . . , , , . :

    [1] . [2]

    , . [3] . [4] ,

    . [5] ,

    . . . : ", , ". , , ++ - . , , - . , . .

    1.2.1 (, , ) : , ; ! , . . , , , , , (, , , ...) .. , 60, 68, .

  • . ++

    24

    . , :

    double sqrt ( double arg ) { // } void some_function () { double root = sqrt ( 2 ); // .. }

    // , . .

    1.2.2 . . , . : , ; , " ". , . "" . - . :

    [1] (, push () pop ()). [2] , (, )

    . [3] .

    -2 , . , :

    // , : void push ( char ); char pop (); const int stack_size = 100;

    , stack.h, :

    #include "stack.h" // static char v [ stack_size ]; // ``static'' // / static char * p = v; // void push ( char c ) { // }

    char pop () { //, , }

  • . ++

    25

    , , , . : v p , .. (), . :

    #include "stack.h" //

    void some_function () { push ( 'c' ); char c = pop (); if ( c != 'c' ) error ( "" ); }

    , , , .. , , , . ++ , ($$5.4.3 $$5.4.4). ++, , , , .. .

    1.2.3 , . , :

    class stack_id { /* ... */ }; // stack_id // //

    stack_id create_stack ( int size ); // // void push ( stack_id, char ); char pop ( stack_id ); destroy_stack ( stack_id ); //

    , , , , "", . "" . , . "" , , "" . , , . ( ), . , (- ) , ( stack_id ). , , , , :

    void f () { stack_id s1; stack_id s2;

    s1 = create_stack ( 200 ); // : s2 push ( s1,'a' );

  • . ++

    26

    char c1 = pop ( s1 ); destroy_stack ( s2 ); // // : s1 s1 = s2; // // , // s2 - }

    , , , , . , Clu, ++ , , , . , , , . . , , , . " " , $$4.6. : , ; . , , , , . , , :

    class complex { double re, im; public: complex(double r, double i) { re=r; im=i; } complex(double r) // float->complex { re=r; im=0; } friend complex operator+(complex, complex); friend complex operator-(complex, complex); // friend complex operator-(complex) // friend complex operator*(complex, complex); friend complex operator/(complex, complex); // ... };

    (.. ) complex . (private): re im , complex. :

    complex operator + ( complex a1, complex a2 ) { return complex ( a1.re + a2.re, a1.im + a2.im ); }

    : void f () { complex a = 2.3; complex b = 1 / a; complex c = a + b * complex ( 1, 2.3 ); // ... c = - ( a / b ) + 2; }

  • . ++

    27

    ( ) .

    1.2.4 " ". . , . . , , shape (). , : (circle), (triangle) (square). :

    class point { /* ... */ }; class color { /* ... */ };

    shape : enum kind { circle, triangle, square };

    class shape { point center; color col; kind k; // public: point where () { return center; } void move ( point to ) { center = to; draw (); } void draw (); void rotate ( int ); // };

    " " k , , draw () rotate (), , ( , k -). draw () :

    void shape :: draw () { switch ( k ) { case circle: // break; case triangle: // break; case square: // break; } }

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

  • . ++

    28

    1.2.5 - , (, , ..) (, - , , , ..). - , . , , - . . , . , :

    class shape { point center; color col; // ... public: point where () { return center; } void move ( point to ) { center = to; draw(); } virtual void draw (); virtual void rotate ( int ); // ... };

    , , (.. ) , virtual (). ++ : " , ". , :

    void rotate_all ( shape v [], int size, int angle ) // "v" "size" // "angle" { int i = 0; while ( i

  • . ++

    29

    , . , , , , - . , , , . , - , , . . . . : , "" , . , - , , [2] [6], 11. , , , , - . , , - .

    1.3 " " , , . - . ++ ( ), - . .

    1.3.1 ++ :

    main () { }

    , main, . { } ++ . () main. ++ main(), . - . , Hello, World! ( !):

    #include

    int main () { cout

  • . ++

    30

    \n ( ), Hello, world!

    , main(), , . , - "" . / 10.

    1.3.2 . , . ,

    int inch;

    , inch int, .. inch . - , . . , , . , " " , :

    char short int long

    . : float double long double

    . char , ( ). int , ( ). :

    + (, ) - (, ) * () / () % ( )

    : == () != ( ) < ( ) = ( )

    ++ , :

    double d; int i; short s; // ... d = d + i; i = s * i;

    = .

  • . ++

    31

    1.3.3 :

    char v [ 10 ]; // 10 :

    char * p; // [] " ", * " ". , v 10 : v [ 0 ] ... v [ 9 ]. :

    p = & v [ 3 ]; // p 4- v & .

    1.3.4 ++ . if, switch while. . , i, - c:

    #include

    int main () { const float fac = 2.54; float x, in, cm; char ch = 0;

    cout > x; // cin >> ch // if ( ch == 'i' ) { // in = x; cm = x * fac; } else if ( ch == 'c' ) { // in = x / fac; cm = x; } else in = cm = 0; cout , , ; . switch () . :

    switch ( ch ) { case 'i': in = x; cm = x * fac;

  • . ++

    32

    break; case 'c': in = x / fac; cm = x; break; default: in = cm = 0; break; }

    break . . , default. default . , 10 :

    int v1 [ 10 ]; int v2 [ 10 ]; // ... for ( int i=0; i

  • . ++

    33

    , , , . , , . , : - , - :

    int pow ( int, int ); double pow ( double, double ); //... x = pow ( 2,10 ); // pow ( int, int ) y = pow ( 2.0, 10.0 );// pow ( double, double )

    ; 7. " ", " ". , . , :

    void swap ( int * p, int * q ) { int t = * p; * p = * q; * q = t; }

    * ( ), , . :

    void f ( int i, int j ) { swap ( & i, & j ); }

    , : void swap (int & r1, int & r2 ) { int t = r1; r1 = r2; r2 = t; }

    void g ( int i, int j ) { swap ( i, j ); }

    T T& " T". , . , swap .

    1.3.6 ++ "". "" , - . , , . extern , . :

    extern "C" double sqrt ( double ); extern ostream cout;

  • . ++

    34

    - , . , . , sqrt math.h, , 4, :

    #include //... x = sqrt ( 4 );

    , , . , , -, ($$4.3). , , . ( - ) , . - /usr/include/CC. , , , . :

    #include "math1.h" #include "/usr/bs/math2.h"

    math1.h math2.h /usr/bs. , , . header.h :

    // header.h

    extern char * prog_name; extern void f();

    main.c : // main.c

    #include "header.h" char * prog_name = ", "; int main () { f(); }

    f.c: // f.c #include #include "header.h" void f () { cout

  • . ++

    35

    1.4 ( ) . . , , , . .

    1.4.1 , . . :

    class vector { // ... public: void init ( init size ); // init () // vector // ... };

    void f () { vector v; // v v.init ( 10 ); // }

    . , . , ( ). . , , . , . ++ . , , ~ ( ++ ). :

    class vector { int sz; // int * v; // public: vector ( int ); // ~vector (); // int& operator [] ( int index ); // };

    vector : vector::vector ( int s ) { if ( s

  • . ++

    36

    vector : vector::~vector () { delete [] v; // , // v }

    ++ new , ( , " "). . , (., , $$9.4).

    1.4.2 , , . . vector:

    void f () { vector v1 ( 100 ); vector v2 = v1; // v2, // v1 v1 = v2; // v2 v1 // ... }

    v2 v1. , :

    class vector { int * v; int sz; public: // ... void operator = ( const vector & ); // vector ( const vector & ); // };

    , vector . :

    void vector::operator = ( const vector & a ) // { if ( sz != a.sz ) error ( " =" ); for ( int i = 0; i < sz; i++ ) v [ i ] = a.v [ i ]; }

    " " , , , :

    vector::vector ( const vector & a ) // { sz = a.sz; // v = new int [ sz ]; // for ( int i = 0; i < sz; i++ ) // v [ i ] = a.v [ i ];

  • . ++

    37

    }

    ++ T(const T&) T. T T. T(const T&) .

    1.4.3 , ? , , Vector. , , , "" :

    template < class T > class Vector { // T T * v; int sz; public: Vector ( int s ) { if ( s v1 ( 100 ); // 100 Vector < complex > v2 ( 200 ); // 200 // v2 [ i ] = complex ( v1 [ x ], v1 [ y ] ); // ... }

    , , . , Clu . - , .

    1.4.4 , (, , " "). , -68 Clu . vector. , , ? vector , , ( ,

  • . ++

    38

    ). : , . . :

    class vector { // class range { }; // ... };

    vector::operator[]() , . " " ("throw the exception"):

    int & vector::operator [] ( int i ) { if ( i < 0 || sz complex ( 1 ) a = b + complex ( 2 ); a = b + 2; // : 2 -> complex ( 2)

  • . ++

    39

    ++ , , . , , "" (, , , ) ( ). :

    complex a = 2; complex b = a + 2; // : operator + ( a, complex ( 2 )) b = 2 + a; // : operator + ( complex ( 2 ), a )

    "+" , . , complex , - .

    1.4.6 , - , : ,- , . :

    template < class T > class stack { public: virtual void push ( T ) = 0; // virtual T pop () = 0; // };

    =0 , , stack , .. . , :

    class cat { /* ... */ }; stack < cat > s; // : -

    void some_function ( stack & s, cat kitty ) // { s.push ( kitty ); cat c2 = s.pop (); // ... }

    , . . , :

    template < class T > class astack : public stack < T > { // // - // ... public: astack ( int size ); ~astack (); void push ( T ); T pop (); };

    :

  • . ++

    40

    template < class T > class lstack : public stack < T > { // ... };

    : void g () { lstack < cat > s1 ( 100 ); astack < cat > s2 ( 100 ); cat Ginger; cat Snowball; some_function ( s1, Ginger ); some_function ( s2, Snowball ); }

    , , , (.. g()), (.. some_function()) . , .

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

    1.5.1 - - - , . , , p. p->rotate(45)? ++ , , rotate() . , p->rotate() , p , rotate . , ( ), . , . , , , $$1.2.5:

    class shape { // ... public: // ... virtual void rotate ( int ); // ... };

    p , , :

  • . ++

    41

    T * p;

    T - shape . , , p, rotate(), int. , p->rotate(45) . shape::rotate() , . , rotate , , . , , circle::rotate, . , rotate . shape :

    center vtbl: color &X::draw &Y::rotate ...

    ...

    vtbl , vtbl, , ... . X Y , . circle X Y circle. , .

    1.5.2 . , " -". ++ . , , , . , , Smalltalk, " -". :

    template < class T > class stack { T * p; int sz; public: stack ( int ); ~stack (); void push ( T ); T & pop (); };

    , plane ():

    stack < plane * > cs ( 200 ); void f () { cs.push ( new Saab900 ); // : // plane*, car* cs.push ( new Saab37B ); // : Saab 37B - // , .. plane cs.pop () -> takeoff (); cs.pop () -> takeoff (); }

  • . ++

    42

    , :

    // // ; ++ Stack s; // // void f () { s.push ( new Saab900 ); s.push ( new Saab37B ); s.pop () -> takeoff (); // : Saab 37B - cs.pop () -> takeoff (); // : // }

    , , , ++. , , . ++ , Smalltalk , . , ++ , , .

    1.5.3 A B, B A. .. B A -. , , B A1 A2. . . displayed task. , , - , . , , :

    class my_displayed_task: public displayed, public task { // }; class my_task: public task { // // , .. displayed // }; class my_displayed: public displayed { // // .. task // };

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

    class task

  • . ++

    43

    { public: void trace (); // ... }; class displayed { public: void trace (); // ... }; class my_displayed_task:public displayed, public task { // trace () }; void g ( my_displayed_task * p ) { p -> trace (); // : }

    ++ - , . : , , , . ++ , , :

    class my_displayed_task:public displayed, public task { // ... public: void trace () { // displayed::trace (); // trace () displayed task::trace (); // trace () task } // ... }; void g ( my_displayed_task * p ) { p -> trace (); // }

    1.5.4 ( - , ) " ". , ? , - , : , , , -. :

    class window { // ... protected: Rectangle inside; // ... }; class dumb_terminal : public window

  • . ++

    44

    { // ... public: void prompt (); // ... };

    window inside Rectangle (protected), - , , dumb_terminal::prompt(), , . window::inside . (, "" ) , , (, " " ). : , , , - . . , - , . ++ (private) . complex shape. , .. , . $$6.6 .

    1.6 ++ " ", - . . , , - , , , , :

    - ; - ; -

    ; - .

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

  • . ++

    45

    . , , . , , . , . , . , , . ++ , , , ($$12.2). ++, , . , , " ". , , , .

  • . ++

    46

    2. " ".

    (.. )

    (char, int, float ..) (, , ..). , , , . , , , , . ++ . , . , ++, . . 5 7.

    2.1 () , ++. , , , . , :

    char ch; int count = 1; char* name = "Njal"; struct complex { float re, im; }; complex cvar; extern complex sqrt(complex); extern int error_number; typedef complex point; float real(complex* p) { return p->re; }; const double pi = 3.1415926535897932385; struct user; template abs(T a) { return a

  • . ++

    47

    ( extern . #4.2): extern int error_number; extern int error_number;

    "" , : struct complex { float re, im; }; typedef complex point; float real(complex* p) { return p->re }; const double pi = 3.1415926535897932385;

    , "" ; , , :

    int count = 1; char* name = "Bjarne"; //... count = 2; name = "Marian";

    : char ch;

    , , .

    2.1.1 . , . ( " "), , . ( " "), , . ; .. , . ( ) . :

    int x; // x void f() { int x; // x x x = 1; // x { int x; // x x = 2; // x } x = 3; // x } int* p = &x; // x

    . , . - , , . , . , i x, . :: , :

    int x; void f2() { int x = 1; // x ::x = 2; // x

  • . ++

    48

    }

    . ( , - . $$R.3.2). , , . :

    int x; void f3() { int x = x; // }

    . , : " ". , ::, . :

    int x = 11; void f4() // { int y = x; // x int x = 22; y = x; // x

    } y x, .. 11, x, .. 22. , :

    void f5(int x) { int x; // }

    x . , .

    2.1.2 "", , . "", , *p[a+10]=7. , " ". : " - , , " ($$R.3.7). (lvalue - left value, .. ) ", ". (. $$2.5). , const, .

    2.1.3 , , . , ( ) . static, . , " " , :

    int a = 1; void f() { int b = 1; // f() static int c = a; // cout

  • . ++

    49

  • . ++

    50

    complex. , . , int complex, . , . , sizeof ( , ) new ( ). :

    int main() { int* p = new int; cout

  • . ++

    51

    , int, . (. $$R.7.1). char. 8 . ++ char, sizeof(char) 1. char . , unsigned char , , , . , unsigned char char . , signed char . , , . , . , , (, int) . , , , . :

    1==sizeof(char)

  • . ++

    52

    2.3.3 ( ) , :

    * & [] ()

    , , . : int* a; float v[10]; char* p[20]; // 20 void f(int); struct str { short length; char* p; };

    $$R.8. , , :

    int v[10]; // i = v[3]; // int* p; // i = *p; //

    , , , * & , [] () - . , , . , [] , *, :

    int* v[10]; // int (*p)[10]; //

    , . . . , :

    int x, y; // int x; int y;

    , , ( ). :

    int* p, y; // int* p; int y; int* y; int x, *p; // int x; int* p; int v[10], *p; // int v[10]; int* p;

    , , , .

    2.3.4 void void , . void . , .

    void f(); // f void* pv; //

    void*. , void* (). , void*. , . void* , . . , . :

  • . ++

    53

    void* malloc(unsigned size); void free(void*); void f() // { int* pi = (int*)malloc(10*sizeof(int)); char* pc = (char*)malloc(10); //... free(pi); free(pc); }

    : () - , pi void*, malloc(), int. ; $$3.2.6.

    2.3.5 T T T*. , T* T. , , :

    int* pi; char** cpp; // char int (*vp)[10]; // 10 int (*fp)(char, char*); // // char char*, int

    - (), .. , . . * . :

    char c1 = 'a'; char* p = &c1; // p c1 char c2 = *p; // c2 = 'a'

    , p,- c1, , c1, 'a'. c2 *p 'a'. . , , ( ):

    int strlen(char* p) { int i = 0; while (*p++) i++; return i; }

    -: , .

    int strlen(char* p) { char* q = p; while (*q++) ; return q-p-1; }

    ; $$4.6.9

    2.3.6 T T[size] " size T". 0

  • . ++

    54

    size-1. : float v[3]; // : // v[0], v[1], v[2] int a[2][5]; // , char* vpc; // 32

    , : extern "C" int strlen(const char*); // char alpha[] = "abcdefghijklmnopqrstuvwxyz"; main() { int sz = strlen(alpha); for (int i=0; i

  • . ++

    55

    int bad = v[4,1]; // int good = v[4][1]; //

    , , , 5 char:

    char v[2][5];

    , - .

    char v[2][5] = { { 'a', 'b', 'c', 'd', 'e' }, { '0', '1', '2', '3', '4' } }; main() { for (int i = 0; i

  • . ++

    56

    . p T*, , p T. p+1 , p-1 - . , () p+1 sizeof(T) , p.

    main() { char cv[10]; int iv[10]; char* pc = cv; int* pi = iv; cout

  • . ++

    57

    , address, . , , . : ++ , , . address , , . () . :

    address jd; jd.name = "Jim Dandy"; jd.number = 61;

    struct , . : address jd = { "Jim Dandy", 61, "South Street", "New Providence", {'N','J'}, 7974 };

    ($$5.2.4). , jd.state "NJ". '\0', "NJ" , , jd.state. c , ->. :

    void print_addr(address* p) { cout name

  • . ++

    58

    struct no_good { no_good member; };

    ( no_good). ( ) , . :

    struct list; // struct link { link* pre; link* suc; list* member_of; }; struct list { link* head; };

    list, link . , , . :

    class S; // 'S' - extern S a; S f(); void g(S);

    , S : void h() { S a; // : S - f(); // : S - g(a); // : S - }

    2.3.9 , . , :

    struct s1 { int a; }; struct s2 { int a; };

    : s1 x; s2 y = x; // :

    , , : s1 x; int i = x; // :

    , , , , . , typedef, , . :

    typedef char* Pchar; Pchar p1, p2; char* p3 = p1;

    .

  • . ++

    59

    2.3.10 . , (.$$7). X& X. :

    int i = 1; int& r = i; // r i int x = r; // x = 1 r = 2; // i = 2;

    , .. , . , . , , ,

    int ii = 0; int& rr = ii; rr++; // ii 1

    ++ , rr++ rr; ++ , .. ii. , : , . , rr, &rr. , . , (.. , ; . $$R.3.7). T . , &T , T. :

    [1] -, , (.$$R.8.4.3); [2] ; [3] , .

    : double& dr = 1; // : const double& cdr = 1; //

    : double* cdrp; // , double temp; temp = double(1); cdrp = &temp;

    : , , . , . (.$$R.6.3). , . :

    void incr(int& aa) { aa++; } void f() { int x = 1; incr(x); // x = 2 }

    , , incr aa x. , , , . , ,

  • . ++

    60

    : int next(int p) { return p+1; } void inc(int* p) { (*p)++; } void g() { int x = 1; x = next(x); // x = 2 inc(&x); // x = 3 }

    , , , . . .

    pair: struct pair { char* name; // int val; // };

    , . find(), , . pair (: ). - . , , :

    const int large = 1024; static pair vec[large+1]; pair* find(const char* p) /* // "pair": // p, , "pair", // "pair" */ { for (int i=0; vec[i].name; i++) if (strcmp(p,vec[i].name)==0) return &vec[i]; if (i == large) return &vec[large-1]; return &vec[i]; }

    value(), , ( ):

    int& value(const char* p) { pair* res = find(p); if (res->name == 0) { // , // res->name = new char[strlen(p)+1]; strcpy(res->name,p); res->val = 0; // 0 } return res->val; }

    () value() , ( ) . , , :

    const int MAX = 256; //

  • . ++

    61

    main() // { char buf[MAX]; while (cin>>buf) value(buf)++; for (int i=0; vec[i].name; i++) cout

  • . ++

    62

    U unsigned. , L long. :

    void f(int); void f(unsigned int); void f(long int); void g() { f(3); // f(int) f(3U); // f(unsigned int) f(3L); // f(long int) }

    2.4.2 double. , , . :

    1.23 .23 0.23 1. 1.0 1.2e10 1.23e-15

    , . , 65.43 e-21 , :

    65.43 e - 21

    . float, , f:

    3.14159265f 2.0f 2.997925f

    2.4.3 , , , 'a' '0'. , , , . , , . , , ASCII, '0' 48, EBCDIC, 240. . , , :

    NL(LF) \n HT \t VT \v BS \b CR \r FF \f BEL \a \ \\ ? \? ' \' " \" NUL \0 ooo \ooo hhh \xhhh

    , . - char. , , ( \) (

  • . ++

    63

    \x). . , . :

    '\6' '\x6' 6 ASCII ack '\60' '\x30' 48 ASCII '0' '\137' '\x05f' 95 ASCII '_'

    . , (. ). , , .

    2.4.4 - , :

    " " , : ('\0'), 0.

    sizeof("asdf")==5;

    " ", "asdf" char[5]. "" char[1]. , s strlen(s)==sizeof(s)-1, strlen() '\0'. \. , " \. '\n', :

    cout

  • . ++

    64

    , , . , 0. , . ( ) " " .

    2.5 const, , :

    const int model = 90; const int v[] = { 1, 2, 3, 4 };

    , . - const, , :

    model = 200; // model++; //

    , const , , . const:

    const char* peek(int i) // - { return hidden[i]; }

    , , , . , , const, (, "" ). - , , . , , ( ) , . , , , . , . , : . "" const, , , :

    const char* pc = "asdf"; // pc[3] = 'a'; // pc = "ghjk"; //

    , , * const. :

    char *const cp = "asdf"; // - cp[3] = 'a'; // cp = "ghjk"; //

    , , const, : const char *const cpc = "asdf"; // - const cpc[3] = 'a'; // cpc = "ghjk"; //

    , , . . - const, , :

    char* strcpy(char* p, const char* q); // *q

  • . ++

    65

    , .. . , const, , :

    int a = 1; const int c = 2; const int* p1 = &c; // const int* p2 = &a; // int* p3 = &c; // *p3 = 7; // c

    2.5.1. , , const. :

    enum { ASM, AUTO, BREAK };

    , , . 0 , :

    const ASM = 0; const AUTO = 1; const BREAK = 2;

    , : enum keyword { ASM, AUTO, BREAK };

    . int. ( int ) . :

    void f() { keyword k = ASM; int i = ASM; k = i // k = keyword(i); i = k; k = 4; // }

    , int : int . keyword int, , , . ,

    keyword key; switch (key) { case ASM: // - break; case BREAK: // - break; }

    , keyword . . :

    enum int16 { sign=0100000,

  • . ++

    66

    most_significant=040000, least_significant=1 };

    , .

    2.6. , , . :

    [1] ; [2] .

    , - . , . , , , , , . , , , .

    2.6.1 , ( : , ) char, char ++ , . , , . , , . . , :

    struct sreg { unsigned enable : 1; unsigned page : 3; unsigned : 1; // unsigned mode : 2; unsigned : 4; // unsigned access : 1; unsigned length : 1; unsigned non_resident : 1; };

    DEC PDP11/45 (, ). : , . ($$R.3.6.1 $$R.9.6), . : . sreg :

    sreg* sr0 = (sreg*)0777572; //... if (sr0->access) { // // sr0->access = 0; }

    , , . , , . , , ,

  • . ++

    67

    , char! , char int , . - .

    2.6.2. , . , :

    struct entry { char* name; char type; char* string_value; // type == 's' int int_value; // type == 'i' }; void print_entry(entry* p) { switch(p->type) { case 's': cout string_value; break; case 'i': cout int_value; break; default: cerr

  • . ++

    68

    int i; int* p; }; }; fudge a; a.i = 4095; int* p = a.p; //

    , .. int int* , , . , ($$3.2.5). , . , fudge, , 0:

    fudge.p = 0; int i = fudge.i; // i 0

    , . , fudge :

    union fudge { int i; int* p; };

    () , . , (. $$5.4.6).

    2.7 1. (*1) "Hello, world" (. $$1.3.1). 2. (*1) $$2.1 : ,

    ; , , .

    3. (*1) : ; 10 ; 10 ; ; ; -; -; . .

    4. (*1.5) , . sizeof.

    5. (*1.5) , 'a' 'z' '0' '9' . . , .

    6. (*1) 0 . : .$$2.6.2.

    7. (*1.5) , double. 8. (*2) : char,

    short,int,long, float, double, long double, unsigned, char*, int* void*? - ? , int* ? ? , ?

    9. (*1) , ++ ? ? - , ?

    10. (*1) , . int*. , int&.

  • . ++

    69

    11. (*1) str : char str[] = "a short string"; "a short string"?

    12. (*1.5) . , . : - , - , .

    13. (*1) typedef : unsigned char, unsigned char, , , , 7 , 7 8 7 .

    14. (*1) f(char), g(char&) h(const char&) , 'a', 49, 3300, c, uc, sc, c - char, uc - unsigned char sc - signed char. ? ?

  • . ++

    70

    3. " "

    ( )

    ++ , , . . , . , .

    3.1 . . . , :

    r=2.5 area=pi*r*r

    ( pi ). : 2.5 19.635

    2.5, - 19.635. : , , . , , , , , , . , (200 ), , ++.

    3.1.1 :

    : END // END - - END

    -: PRINT // PRINT - '\n' ';' PRINT -

    : + -

    : / *

  • . ++

    71

    : NUMBER // ++ NAME // ++ '_' NAME = - ( )

    , , , . - , *, /, +, - ( ) =. . , . . ++, , . , . (, END, NUMBER, + -) get_token(). expr(), term() prim(). , . , . get_token(). get_token() curr_tok. curr_tok token_value:

    enum token_value { NAME, NUMBER, END, PLUS='+', MINUS='-', MUL='*', DIV='/', PRINT=';', ASSIGN='=', LP='(', RP=')' }; token_value curr_tok;

    , get_token() , curr_tok , . . , , . "" . expr() . , :

    double expr() // { double left = term(); for(;;) // ``'' switch(curr_tok) { case PLUS: get_token(); // '+' left += term(); break; case MINUS: get_token(); // '-' left -= term(); break; default: return left; } }

    . , , . , 2-3+4