2011_c++프로그래밍길잡이

320
C ++ 프로그래밍 길잡이 김 삼 묘

Upload: sam-myo-kim

Post on 27-Dec-2015

355 views

Category:

Documents


19 download

DESCRIPTION

이 교재는 서울어코드 활성화 사업의 지원을 받아 경북대학교 IT대학 컴퓨터학부의 전공과목을 이수하는 데 필요한 프로그래밍 능력을 기르기 위하여 개발한 것이다. 교제는 프로그래밍의 바탕이 되는 하드웨어와 기본 데이터의 구조, 등 컴퓨터학개론의 주요 내용을 압축하여 복습하고, 이를 바탕으로 C++의 기초부터 시작하여 객체지향 프로그래밍까지 이론과 실습을 병행하여 공부하도록 구성하고 있다. 따라서 이 교재는 컴퓨터학개론을 이수한 컴퓨터학부의 신입생뿐 아니라 프로그래밍에 어려움을 겪는 자율전공부 학생과 진급, 편입 또는 복학생이 단기에 프로그래밍 능력을 기르는데 유용하게 쓰일 것이다.

TRANSCRIPT

  • C++

  • 3

    IT . , , C++ . , .

    , () . .

    I , , C++ , C++ Class . I hardware hardware data 2 .

    I , . , LAN , .

    , , , , array pointer . C++

  • 4 C++

    class class .

    I linked list class template , . , , .

    . . ( .) .

    , . M. Mano Computer System Architecture, Deitel C++ How To Program . I C++ . .

    , , . C++ . , , . .

    2011 11

  • 5

    / 3

    I Hardwar Data Representation / 9 1 Computer Hardware 11

    1.1 Doping Semiconductor() / 111.2 Transistor Logic Gates / 161.3 Logic Circuits / 191.4 ALU(Arithmetic/Logic Unit) / 271.5 Central Processing Unit(CPU) / 381.6 Control Unit( ) / 391.7 Computer Organization( ) / 401.8 Peripheral Devices() / 42

    2 Data Representation( ) 452.1 Number Systems( ) / 452.2 / 462.3 Signed Binary Number Representation( 2 ) / 502.4 Floating Point Number Representation( ) / 522.5 / 542.6 : ASCII Code / 582.7 (Audio and Image Representation) / 59

  • 6 C++

    Computing / 61 3 Machine Language Programming 63

    3.1 Instruction Format( ) / 643.2 Machine Cycle( ) / 663.3 Program Repetition( ) / 71

    4 Computer Networks 754.1 LAN(Local Area Network) / 754.2 Internet World Wide Web / 764.3 Network Communication Protocols( ) / 794.4 Data Compression Techniques( ) / 844.5 Communication Error Error Detection/Correction / 88

    5 915.1 / 915.2 / 925.3 / 94

    C++ Programming / 99 6 C++ 101

    6.1 C++ Development Environments( ) / 1016.2 C++ / 1026.3 Variable Name( ) / 1066.4 Assignment Operator( ) Arithmetic Operator / 1086.5 Operator Precedence( ) / 1116.6 Conditional Control Statements( ) / 1116.7 / 1216.8 For-repetition / 122

  • 7

    7 (function) 1337.1 Math Library Function / 1337.2 Function Control Flow / 1347.3 Writing User Defined Functions / 1357.4 Data Type (precedence rule) / 1397.5 Random Number Generator rand() / 1407.6 Storage Classe( ) Scope Rule( ) / 1467.7 Recursive Functions( ) / 1547.8 (alias) Pointer / 1587.9 (function call) / 1607.10 / 164

    8 Arrays() 1698.1 Number Array( ) / 1698.2 Character Array() / 1748.3 Array / 1768.4 Multidimensional Array( ) / 1868.5 Array / 191

    C++ Class / 195 9 Classes Data Abstraction 197

    9.1 Data : Structured Data Type / 1989.2 Class Definition / 2009.3 Class (scope) (information hiding) / 2059.4 Constructor() Destructor() / 210

    10 Class Template Operator Overloading 21510.1 Class Template / 21510.2 Operator Overloading( ) / 223

  • 8 C++

    11 Class Class 23311.1 Composite() Class / 23311.2 Class Inheritance() / 238

    12 File I/O() 245

    I. Linked List(Dynamic Data Structure) 251

    1. List Node Linked List / 2522. List Operations / 2533. List Class / 2574. List Class Test Program / 263

    . 267#1: if / 267#2: While / 269#3: While / 269#4: rand() / 270#5: / 274#6: Array / 276 : Knight / 283 : Simulation / 296

    303

  • 9

    Hardware Data Representation

    1 Computer Hardware2 Data Representation( )

  • 11

    1

    Computer Hardware

    . ? , . C++ (machine independent) , , ( ) .

    . .

    1.1 Doping Semiconductor()

    (logic element logic gate) , . . 1.1 ; (silicon), (phosphorus) (boron) . 8, 9, 7 .

  • 12 Hardware Data Representation

    silicon, phosphorus boron doping . Silicon 1.2 . silicon 8 .

    Silicon

  • 1 Computer Hardware 13

    (a)(b)

    (a) Ingot (b) Wafer

    silicon phosphorus boron .( doping .) (ingot, 1.3-(a)) diamond ( 1.3-(b)) (wafer).

    Wafer doping silicon ? 1.1 . 1.4 wafer . Phosphorus ( ) 9 silicon 8 . (free electron) , .

    Silicon phosphorus boron( 1.4 ) doping . , boron 7 , silicon (hole) . . . ,

  • 14 Hardware Data Representation

    Phosphorus( ) boron() doping silicon

    (, hole) , , . silicon phosphorus boron doping silicon .

    1.5 doping silicon hole . , , hole . doping silicon ( wafer) . ( phosphorus doping . Boron doping . , .)

  • 1 Computer Hardware 15

    hole

    1.6-(a) phosphorus doping wafer wafer . 1.6-(b) wafer , . gate() 3 . 1.6-(c) gate () , gate ( 1.6-(d)).

    ( 0 volt, ) . ( ) drain, (

  • 16 Hardware Data Representation

    ) source . gate .

    (b)

    (c) (d)

    (a)

    Transistor

    1.2 Transistor Logic Gates

    (logic element logic gate) . 1.6-(c), (d) , 1.7-(a) . gate, doping silicon wafer. , source 0 volt, gate drain +0.2 volt .( .)

    1.7-(a) logic element (, source, gate, drain) V+( 0.2 volt ) 1, 0 volt 0 ( 1.7-(c)). ( 0 1 2 .

  • 1 Computer Hardware 17

    .) 1.7-(a) 1.7-(d) (inverter) truth table() . (Gate A ( V+, 1), silicon wafer drain source inverter 0 volt .)

    1.7-(b) gate NAND logic element . gate A B , 1.7-(e) truth table . logic element 1.8 . ( NAND gate logic element . .)

    (a) (b)(c)

    (d) (e)

    (a), (b): Inverter NAND gate , (c): Bit , (d), (e): Gate

  • 18 Hardware Data Representation

    logic element , , (truth table)

    1.9-(a) logic element logic circuit( ) . algebraic expression( )

  • 1 Computer Hardware 19

    . (0 1) , AND, OR, apostrophe( ) NOT logic F . 1.9-(b) block diagram( ) .

    (a)

    (b)

    (a) Logic circuit algebraic notation (), (b) block diagram

    1.3 Logic Circuits

    . block diagram .

    1.3.1 Decoder()

    1.10 38(three by eight ) decoder . Bit n 2 2n . bit 3 000 111 23 = 8 . , , decoder action .

  • 20 Hardware Data Representation

    1.10-(a) x, y, z 1, 0, 1 . logic element 8 decoder . 1 x, y, z . x, y, z 101 101 1 0. 1 xyz 101 .

    (a)

    (b)

    (a) 38 decoder logic circuit, (b) block diagram

    1.11 38 decoder 416 decoder . d 0 38 decoder , 1 decoder . (, enable 0 decoder) 0 1.10 .

  • 1 Computer Hardware 21

    Decoder

    1.3.2 Multiplexer()

    (5 senses) . . . , (CPU) , , , . multiplexer. 1.12 4 bit 41 multiplexer.

    41 multiplexer logic circuit block diagram

  • 22 Hardware Data Representation

    4 (selection) . I1 . 0 0, 1 1 .

    1.3.3 Storage Device( )

    . 1.13 SR flip-flop . flip-flop 1 S(set), R(reset) 1, 0 ( 1.13-(a)), 0 0, 1 ( 1.13-(b)), S R 0 . (S, R 1 .)

    (a) (b)

    (c) (d)

    (a), (b): SR flip-flop , (c): Block diagram, (d):

  • 1 Computer Hardware 23

    Q 1 flip-flop 1 , 0 flip-flop 0 . (clock pulse) . (Clock pulse 0 1 , 0 .) 1.13-(c) block diagram, 1.13-(d) SR flip-flop truth table. Q(t) , Q(t+1) .

    SR flip-flop flip-flop . 1.14 flip-flop .

    flip-flop

    1.15 4 SR flip-flop 4bit register. register clear(, 0 ) , load . 1.15-(b) block diagram.

  • 24 Hardware Data Representation

    (a) (b)

    (a) 4-bit register loading input, (b) block diagram

    1.3.4 RAM(Random Access Memory)

    register . RAM(random access memory) . 1.16 flip-flop logic gate bit cell( ) RAM .

    bit cell bit(, 0 1) (select) 1 . bit cell SR flip-flop 1 S, R 1, 0 , 0 S, R 0, 1 logic gate . S, R 0 (output) y (0 1) . 1.16-(d) bit cell block diagram. RAM block diagram .

  • 1 Computer Hardware 25

    (a) (b)

    (c) (d) bit bit cell.

    (a) 1 , (b) 0 , (c) , (d) block diagram

    RAM PC gigabyte RAM . , 1.17-(a) . RAM bit cell 2 , bit cell word . word 4 bits . word 16, 32, 64 bits . RAM word , RAM 4 words.

  • 26 Hardware Data Representation

    17-(a) 1 . 2 decoder , (select) 1 bit cell . , , address selection input data inputs read/write 0 .

    (a)

    (b)

    RAM . (a) (), (b) bit cell block diagram

  • 1 Computer Hardware 27

    1.4 ALU(Arithmetic/Logic Unit)

    . . . .

    1.4.1 Adder()

    1.18-(a) bit bit (carry) (half adder). ( bits, bits , .) 1.18-(b) truth table. 1.18-(c) bits (full adder). half adder (carry) OR gate . bits 1, 1, 1 1, carry 1 . 1.18-(d) full adder block diagram.

    (a)(b)

    (c) (d)

    (a) (half adder), (b) , (c) (full adder), (d) block diagram

  • 28 Hardware Data Representation

    1.19(a) 4 full adder (parallel adder) . PC 16 32 bits . 2 , bit . carry . parallel adder carry ripple adder .

    (b)

    (a)

    (a) parallel adder (b) block diagram

    1.4.2

    ; , , , . parallel adder ? , . ( , .) , . ,

  • 1 Computer Hardware 29

    . ( , . .)

    (complement) . 1.20 ( ) . . . . 2 C( 1001) 2 C* C F(: 10000) , F C . (F C F = 2n ( 24 = 10000) . F , C bit (, 0 1, 1 0) ( C 1 .) 1 . , C 2 1 1 .

    2 (2's complement)

  • 30 Hardware Data Representation

    1.21 . A B , B B* . ( , . .) B .

    A B (, ) A - B . . , A B , B B* A , ( .) .

    1.22 , . A B B* A . (A B .) B A , (, ) A B* . .

  • 1 Computer Hardware 31

    . (subtrahend) . . .

    , . 2 1 1 . 1 bit inverter . 1 parallel adder . (2 2 .)

    1.23 parallel adder A, B, C0 B C0 . 4 A - B , A B 1 B' adder C0 1 . , A B A B 2 . , (A + B' +C0) = (A + B' +1) = A - B.

  • 32 Hardware Data Representation

    Parallel adder

    1.23 parallel adder logic circuit . 1.24 parallel adder B (, Bi) adder logic circuit . S1S0 00, Bi FA(full adder) 0 , S1S0 01 Bi , S1S0 10 Bi 1 (, ), S1S0 11 Bi 1 FA . 1.23-(b) parallel adder . ( C0 i 1 Ci-1 .)

    S1S0C0 = 110 S = A - 1 (, A decrement ( ))? 1 2 11 . . 1 ( 1). parallel adder 2 A + 111 S = A - 1. 1.24 S1S0C0 = 111 adder 2 A + 111 +1 S = A - 1 + 1 = A.

  • 1 Computer Hardware 33

    (a)

    (b)

    (a) Adder .(b) S0, S1, C0 S.

    1.4.3 Logic Unit( )

    . (logic operation) . .

    1.25-(a) A B (, i) bit (Ai, Bi) AND, OR, XOR Ai 1 (, bits ) . , 41 multiplexer .

  • 34 Hardware Data Representation

    (a) (b)

    (a) (logic operation) , (b)

    1.26-(a) Arithmetic/Logic Unit(ALU). , ALU A, B , S2 multiplexer . ( x don't care, .)

    (a) (b)

    (a) ALU i , (b)

    1.27 4 bits ALU . PC 16 bits 32 bits ALU bit .

  • 1 Computer Hardware 35

    4-bit ALU

    0, , , ALU . 1.28 status register( ).

    (status) Register

    1.28 C carry (C4), S F (1 , 0 ), Z NOR gate 0 1. overflow V . 1 4 , overflow

  • 36 Hardware Data Representation

    . C3 C4 . bits XOR (exclusive OR) V . XOR 1 C3 C4 overflow . ( 2 .)

    1.4.4 Shifter()

    (bit pattern) shifter () . 2 shift 0 . 4 . ( shift .)

    1.29 2 , shifter . ALU F shift . i bit Di i-1 bit Fi-1 . bit ( bit IR) , 0 1 . shift . shift , shifter .

    (a)

    (b)

    (a) (shifter), (b)

  • 1 Computer Hardware 37

    1.4.5 Counter()

    ( , ) . memory A, B ALU . counter .

    Counter 0 1 . 1.30 JK flip-flop 4 bits counter . ( counter 1 .)

    Counter

  • 38 Hardware Data Representation

    1.5 Central Processing Unit(CPU)

    central processing unit(CPU) 1.31 , ALU shifter register . ( CPU PC .) Input data RAM data . Output data RAM register .

    Central Processing Unit(CPU)

  • 1 Computer Hardware 39

    1.6 Control Unit( )

    . A, B A B RAM CPU register ALU RAM register . . (, ) . 1.32 schedule timer( ). decoder counter .

    Schedule Timer

    counter 0 , t0 1 ti 0. Counter 1 ( ) . 3 , t3 t3 1 . ( , decoder enable .)

    1.33 schedule timer (control unit) . instruction register , decode schedule timer schedule . control logic (instruction) (decode) schedule time .

  • 40 Hardware Data Representation

    Control Unit

    Instruction register operation code( ) operand( , ) . Instruction register , 16, 32, ( ) 64 bits . Instruction 3 .

    1.7 Computer Organization( )

    , (peripheral devices) computer organization( ) . , . 1.34 von Neumann . (input out equipment) block diagram . ( program counter register. register 3 .)

  • 1 Computer Hardware 41

    von Neumann

    Intel 8080

  • 42 Hardware Data Representation

    1.35 Intel . address buffer data buffer register, . B, C, D, E, F, L register. 8 bits internal bus CPU data 8 data line. , RAM bus line .

    1.36 internal bus RAM bus . controller() CPU .

    Bus

    1.8 Peripheral Devices()

    (peripheral device) monitor, printer, hard disk, , . (, driver) . mass storage device( ) hard disk USB(Universal Serial Bus, ) .

  • 1 Computer Hardware 43

    1.8.1 Hard Disk

    Hard disk . RAM . ( RAM .) Hard disk disk .

    1.37 disk . track track sector . Data sector data track disk, sector . Read/write head disk data . ( , disk 1, 0.)

    Disk

  • 44 Hardware Data Representation

    1.8.2 USB(Universal Serial Bus, )

    USB (connector) (protocol) 1990 . USB , , , . USB (USB MSC UMS) . ( USB , USB protocol (USB flash ) .)

    USB (connector) , (protocol) .

  • 45

    2

    Data Representation( )

    1 , . . , interface ( ) . . .

    2.1 Number Systems( )

    2 . (Logic gate 0 volt 0, , 0.2 volt 1 .) 2.1 10 0 15 16, 8, 2 .

  • 46 Hardware Data Representation

    10, 16, 8 2

    10 10 2 . 8 16 . . 2 . RAM 16 . 8 .

    2.2

    10 16(hexadecimal), 8(octal) 2(binary) . 10 r , 10 r , r . 2.2 10 8 , 2 16 .

  • 2 Data Representation( ) 47

    10 8, 2, 16 ()

    (fraction) r , r , r . 2.3 . 10 8 , . 8 2 2 , 2 4 16 16 . , .

    10 8, 2, 16

  • 48 Hardware Data Representation

    10 2, 8 16 . . ( , . .)

    r 10 r (polynomial) . , 10 567.25 2 1011.11 2.4 .

    r 10

    10 . . . . a3a2a1a0 r . . . d3d2d1d0 . . 2.5 d0, d1, d2, d3 .

  • 2 Data Representation( ) 49

    10 r

    10 .f1f2f3. . . . , r .e1e2e3 . . . , . 2.6 r .

    10 r

  • 50 Hardware Data Representation

    2.3 Signed Binary Number Representation( 2 )

    10 2 . 2 . ? - . , ASCII 8 bits 2 .

    2.7 1bit 4 2 . bit . bit 0 , 1 . 0 00. . .0(+0) 10. . .0(-0) . 1 0 00. . .0 11. . .1 .

    2 ()

  • 2 Data Representation( ) 51

    2 , 100. . .0( -8) . 2 1 ( ) . , n bits 2 2n-1-1 ~ -2n-1.

    Excess Notation( ) , . bit . 2.8 4 bits 1000 0 , . +7 -8 . 2 (-8) . bit 1 (, 0 ) , 0 . ( .)

    (Excess Notation)

  • 52 Hardware Data Representation

    2.4 Floating Point Number Representation( )

    16, 32 ( ) 64 bits , bits . 2.9 (floating point notation) .

    m (mantissa) , e (exponent) , s (sign) (0 , 1 ) . e (excess notation) .

    Floating point notation

    2.10 32 bits mantissa() 23 bits, exponent() 8 bits, 1bit . PC .

    (real number), (mantissa) . mantissa . , . , 2 110.101 0.1101012011 . . floating point number .

    2.10 1 1 bit .

  • 2 Data Representation( ) 53

    , 23 24 bits .( 1 .)

    32-bit floating point representation ()

  • 54 Hardware Data Representation

    2.5

    2.3 n bits 2 (2s complement) +(2n-1-1) ~ -2n-1. 32 bits 2.11 . , . overflow .

    32 bits 2

    1 1.28( (status) Register) 2 n bits overflow . , carry , exclusive OR logic gate Cn Cn-1 1 status register overflows bit (V) . overflow Cn Cn-1 1? carry bit .

    2.7 2 . bit 0, 1. (augend) 2 .

    An-1An-2 . . . . A0 Bn-1Bn-2 . . . . B0 . bits An-1 Bn-1 overflow . Cn Cn-1 ? (Cn carry, Cn-1 carry.) Cn-1 1 Cn, 1 + 1 + 0 carry, 1. Cn-1 0 Cn, 0 + 1 + 0 carry, 0. Cn Cn-1 . Cn Cn-1 = 0.

  • 2 Data Representation( ) 55

    bits , Cn Cn-1 = 1 overflow . ( . Hint: .)

    . 32 bits floating point .

    2.12-(a) 32 bits floating point (1-2-24)2127 . ( . 1 , excess 128(2 10000000) .) positive floating point 2.12-(b) 0.5-128 . -(1-2-24)2127 ~ -0.52-128.

    2.13 floating point . 2.11 overflow (negative, positive) underflow . . , ? . , bit 32 bits . floating point number , (error) .

    floating point number . , floating point number . ( .)

  • 56 Hardware Data Representation

    (a)

    (b)

    (c)

    32-bit

    32-bit

  • 2 Data Representation( ) 57

    floating point ? . 2.14 2 (number line) . ( 0 .)

    , 2 , 2 . , .

  • 58 Hardware Data Representation

    2.6 : ASCII Code

    The American Standard Code for Information Interchange (ASCII, ) control character, , 128 7 bits 2 . ( 2.15 ). ASCII code 2 code 10 2 . 10 65 A 2 code 1000001, 10 48 0(zero) 2 code 0110000.

    10 digit (0~9) ASCII code integer . , 123 digit ASCII code (text) , 10 123 2 .

    ASCII Code

  • 2 Data Representation( ) 59

    ASCII 128 code , column 32 control character . ASCII code . UTF-8 (Unicode Transformation Format 8) . code 7 bits 8 bits (1byte) , 32 bits . UTF-8 code ASCII code code .

    2.7 (Audio and Image Representation)

    ( ) . , (, ) . , digitize( ) . 2.16 . (source data) .

  • 60 Hardware Data Representation

    image 3 RGB(three primary colors: Red, Green, Blue) . 2.17 pixel() 0~255 3 (R, G, B) . (R, G, B) . 3 8 bits(0~255) , 16 bits .

    Color image

  • 2 Data Representation( ) 61

    Computing

    3 Machine Language Programming4 Computer Networks5

  • 63

    3

    Machine Language Programming

    . , .

    (a sequence of instructions). Machine language program , (decode) . (1.6 control unit( ) instruction register decoder .)

    6 C++ . , 6 . machine language instruction 6 C++ . .

    1980 machine language programming assembly programming , . , machine language program .

  • 64 Computing

    machine language . machine language program

    instruction() . Machine instruction , . machine instruction program .

    3.1 Instruction Format( )

    Program . , (instruction). .

    instruction .

    3.1 instruction register 16 bits, 4 bits operation code( ), 12 bits operand, operate . operand (12 bits) 4 bits 3 r, x, y, .

    format 1 r x y, 4 bits 1(2 0001) memory xy(8 bits) r register . ( M[xy] R[r] .) , operation code 4 bits operand 12 bits . description() . 3.1 3.2 7 machine language program .

  • 3 Machine Language Programming 65

    16, 32, 64 bits, , operation code . ( .)

  • 66 Computing

    3.2 Machine Cycle( )

    Machine cycle . Machine cycle machine language program . 3.3 RAM (16) A0 . word, bit cell 8, 2 byte. 16 bits . RAM 5 .

    CPU RAM

    CPU register 16 bits. PC(program counter) RAM register, instruction register, decoder, ALU . control control unit ( ) 3

  • 3 Machine Language Programming 67

    . CPU RAM

    . Machine cycle fetch, translate, execute . 3.4 - 3.6 .

    Machine cycle fetch

    3.4 machine cycle fetch() . control unit PC(program counter) (A0) RAM (15) (A1) IR(instruction register) . ( RAM word instruction , .) program counter ( A2) .

  • 68 Computing

    Machine Cycle Translate

    Machine Cycle Execute

  • 3 Machine Language Programming 69

    3.5 translate() . instruction register (decoder ) . 3.6 execution() . (156C) RAM 6C (25) 5 register execution .

    Execution machine cycle cycle . program counter ( A2) fetch A2 A3 ( 166D) instruction register .

    RAM A2, A3 (166D) machine cycle (fetch, translate, execute) 3.7 . ( RAM 6D (41) 6 register .)

    A2, A3 166D

    3.8 A4, A5 5056, 5 6 register 0 register .

  • 70 Computing

    A4, A5 5056

    3.9 RAM A6, A7 306E(0 register RAM 6E ) .

    A2, A3 166D

  • 3 Machine Language Programming 71

    3.10 A8, A9 C000(HALT) . ( (HALT) (, hardware software) (operating system) .)

    A8, A9 C000

    3.3 Program Repetition( )

    machine language instruction hardware . RAM machine cycle, fetch, translate, execute . , , RAM .

  • 72 Computing

    . machine language program .

    3.11 RAM A4 B0 HALT machine language program. AA AE 3 .

    machine language program

    3.12 . operation code B ( AA B1B0 AE B0AA). B1B0 1 register 0 register , instruction register B0 PC(program counter) , B0 . ( 0 register . ( 3.2 .)) AE B0AA 0 register 0 register AA .( AA .)

  • 3 Machine Language Programming 73

    3.12 , register 0, 1, 2 00, 03, 01 . ( .) AA register 1 0 B0 (C000) . register 1 register 0 ( 03, 00) AC (5002) 0 register 01 . B0AA AA .

    AA AE 3 AA B1B0 0 register 1 register 03 B0 (C000) .

    3.11 machine language program

    register 1 , . C++ .

  • 75

    4

    Computer Networks

    computer network. Computer network LAN(local area network) , Internet .

    Computer network message www(world wide web) . network communication network computing cloud computing .

    computer network , communication (error) (communication load) data compression( ) .

    4.1 LAN(Local Area Network)

    Local area network . (

  • 76 Computing

    , WAN(wide area network) .) LAN (topology) , 4.1 4 .

    4.1 ring( circular, ) . Bus . Star server-client , (, server) (, client) . irregular() .

    LAN

    4.2 Internet World Wide Web

    Internet LAN router LAN network. 4.2 domain LAN gateway WAN(wide area network) router Internet

  • 4 Computer Networks 77

    . Internet IP (Internet protocol address) .

    Internet Connection

    4.3 IP network identifier( ) , host address . Network identifier ICANN (Internet Corporation for Assigned Numbers and Names) , host address LAN (local system administrator) .

    IP

    IP 2 10 . 4.4 . (10 2 8 bits 10 . host address .)

  • 78 Computing

    IP ()

    World Wide Web(WWW) Internet file Internet . WWW Internet WWW .

    WWW file URL(universal resource locator) , 4.5 . File host computer URL file host computer IP , file directory file .

    WWW URL ()

  • 4 Computer Networks 79

    Email 4.6 LAN Email , mail server IP id . Mail server mail LAN .

    Email ()

    4.3 Network Communication Protocols( )

    Network communication protocol network .

    4.3.1 Token Ring Protocol

    Network communication protocol 4.7 ring LAN token ring protocol . protocol token ring ( 4.7-(a)-(b)). Message token , token message ( ) ( 4.7-(c)-(d)). message token ring , message message ( 4.7-(e)-(f)). message , message token ring message ( 4.7-(g)-(h)).

  • 80 Computing

    (a)

    (e)

    (b)

    (d)(c)

    (f)

    (g) (h)

    Token Ring Protocol

  • 4 Computer Networks 81

    4.3.2 CSMA/CD Protocol

    CSMA/CD(Carrier Sense, Multiple Access with Collision Detection; / ) protocol ( multiple access) (collision detection) protocol.

    protocol . . , . , ( ) . . .

    (a) (b)

    (c) (d)

    CSMA/CD Protocol

  • 82 Computing

    CSMA/CD protocol . (carrier) sensor . ( sensor .) 4.8-(b) message sensor message (collision) .

    . ( .) Sensor message . ( 4.8-(c)-(d)).

    3.4.3 Internet Protocol

    Internet protocol TCP/IP(transport control protocol/Internet protocol) (suite ) protocol . protocol 4.9.1 4 (layer) .

    Internet Protocol (1/3)

  • 4 Computer Networks 83

    Internet protocol layer .

    (1) Application layer( ): message destination() transport() layer .

    (2) Transport layer( ): Message packet(, ) packet destination address packet network layer .

    (3) Network layer: packet intermediate address( ) link() layer . ( Internet traffic .)

    (4) Link layer: Communication line protocol packet .

    Internet Protocol (2/3)

    Packet . ( 4.9.2 .) Link layer packet network layer , packet network layer .

  • 84 Computing

    packet network layer packet transport layer . Transport layer message packet , message application layer . ( packet packet .)

    Link layer packet network layer . 4.9.3 packet layer . ( packet .)

    Internet Protocol (3/3)

    4.4 Data Compression Technique( )

    Internet , . software data compression technique ( ). .

  • 4 Computer Networks 85

    4.4.1 Run Length Encoding

    Run length encoding ( ) 4.10 binary data 1 0 . ( 10 2 .) 0 1 data data .

    Run Length Encoding ()

    4.4.2 Relative Encoding

    compression . 4.11 frame .

    A sequence of picture frames

    Relative Encoding

  • 86 Computing

    source data . data .

    4.4.3 Frequency Dependant Encoding

    Frequency dependant encoding( ) (David A. Huffman) Huffman coding . 2 code , code , code . ( .) , 2 code . 4.12 code .

    Huffman code . 4.12-(a) ( B, D) node , node( 5) node node .

    tree root node node root tree . ( .) 4.12-(b) tree .

    leaf node tree 4.12-(c) root edge 0 1 ( 1 0) . root leaf node edge bit , leaf node code . 4.12-(d) .

  • 4 Computer Networks 87

    (a)

    (c)(b)

    (d)

    Huffman Coding ()

    4.4.4 Image Compression

    Image() . .

  • 88 Computing

    Lempel-Ziv-Welch (LZW) Compression Technique

    GIF(Graphic Interchange Format, file .GIF) image (lossless) . GIF image 3 bytes RGB (red, green, blue) pixel 1byte palette() . game, , . (GIF file LZW , GIF .)

    JPEG(Joint Photographic Expert Group)

    . (lossy) (lossless) , . 10:1 .

    JPEG file , JPEG/Exif JPEG/JFIF . World Wide Web .

    4.5 Communication Error Error Detection/Correction

    Data error (detect), , (correct) . .

  • 4 Computer Networks 89

    4.5.1 Parity Bit

    . ( , , block, ) parity bit , , 4.13 , data 1 parity bit 1, 0 . (, parity bit data bit 1 .)

    Parity bit error

    Data parity bit , data parity hardware software . error parity (, 1 ) error . error data . error (detect) (correct) .

    4.5.2 Hamming Distance

    Richard Hamming error detect correct . Hamming distance() 2 bit . 4.14 code Hamming distance 3 .

  • 90 Computing

    Hamming Distance ()

    code (BEEF) 4.15 . B E error code code B E , . error F code . . ( Hamming distance code .)

    Error Detection/Correction

  • 91

    5

    hardware , , network .

    . ( C++ .)

    5.1

    3 machine language program RAM , , RAM , . ( data .) hardware .

  • 92 Computing

    ? 3 machine language 2 code . . , , compiler() translater() machine language program hardware . , (1) , (2) ( ), (3) .

    5.2

    hardware 2 code machine language . .

    5.2 . , hardware machine language , . , C++ 3 high level languages .

  • 5 93

    Assembly language 5.3 (2 hexadecimal ) machine language code . ( assembly language machine language .)

    Machine Language Assembly Language ()

    Assembly language machine language . , 300 . .

    5.4 1 100 high level programming language program. FORTRAN , PASCAL C++ .

  • 94 Computing

    C++ IT .

    High Level Programming Languages ()

    5.3

    5.5 . program , , . .

    algorithm , . . algorithm . C++ ? . , algorithm . algorithm .

  • 5 95

    .

    (1) , mile

    , kilometer . meter .

    (2)

    . . .

    mile kilometer program . : miles () : kilometers () : 1 mile 1.609 kilometer.

  • 96 Computing

    (3) Algorithm

    . ( algorithm .) algorithm . algorithm .

    (a) Algorithm () Mile . () Mile kilometer . () kilometer .

    (b) Algorithm (refinement) () Mile . - mile message . - . () Mile kilometer . - kilometer = mile X 1.609 () kilometer . - 2 (: 10 miles = 16.09 kilometers)

    (4) Algorithm

    algorithm algorithm . ( algorithm .)

    (5) Algorithm :

    C++ . , algorithm , . 5.6 algorithm C++ . ( . .)

  • 5 97

    (6) Program Debugging ( /)

    high level language program machine language program compiler . , algorithm . . algorithm .

    5.6 compile 5.7 error message . 14 (variable name) message. distance distanse spelling . compile 5.8 error message .

    C++ ()

  • 98 Computing

    Compiler Error Message (1)

    Compiler Message (2)

    (7)

    input data (10) 5.9 .

    Execution Results ()

  • 5 99

    C++ Programing

    6 C++ 7 (function)8 Arrays()

  • 101

    6

    C++

    IT high level programming C++ . C++ 1980 AT&T Stroustrup , 1970 AT&T C 1967 Simula . , C++ C . C Dartmouth College BASIC, Microsoft Visual Basic, Visual C++, C# .NET . ( Visual C++ .)

    6.1 C++ Development Environments( )

    (development environments) (resources) . computer hardware editor(), compiler, library, debugger, . 6.1 .

  • 102 C++ Programing

    C++ program

    resource() hard disk . library . linker. Compiler high level language machine language disk , object code . Loader object code RAM hardware .

    6.2 C++

    C++ . (output) . 6.2

  • 6 C++ 103

    Welcome to C++! . C++ .

    C++

    // comment() . compiler object code .

    #include preprocessor . 3 std::cout

  • 104 C++ Programing

    (main)

    6.2 n . () (control character) . ( escape sequence .) 6.4 control character .

  • 6 C++ 105

    () . () cout

  • 106 C++ Programing

    6.3 Variable Name( )

    variable name ( variable) . variable 6.6 .

    Data type ( integer, character, floating point number, ) variable name , memory data type variable name . (int high_value = 100;) variable , data type variable comma .

    Variable Name

    Variable alphabet , variable . 6.6 myScore variable .

  • 6 C++ 107

    variable , , , , . , , , (underscore) . ( variable name, , . .)

    Variable C++ keyword() . 6.7 keyword . .

    C++ keyword

    data type int, double, variable name ? keyword C++ keyword . variable keyword . variable keyword .

    6.8 integer number . .

  • 108 C++ Programing

    Integer

    >> keyboard data variable . data variable . data variable ALU

  • 6 C++ 109

    . operator assignment operator =. operator 6.9 . C++ assignment operator variable . = ( ) . Assignment operator variable .

    Assignment Operator ()

    6.10 arithmetic operator assignment operator . cin, cout std:: . (main) using std::cin; . cin, cout . endl escape sequence n cin name space . cin endl . comment cin, cout, endl using std:: using namespace std; .

    integer num1 num2 (sum), (difference), (product), (quotient) (remainder) . ( operator .) integer integer ( ) . (zero). ( ) , . ( .)

  • 110 C++ Programing

    integer variable num1, num2 variable . variable , main variable . variable debugging, , .

    Arithmetic Operator Assignment Operator ()

  • 6 C++ 111

    6.5 Operator Precedence( )

    . , . 6.11 operator (precedence) . .

    6.6 Conditional Control Statements( )

    3 machine language programming , . .

  • 112 C++ Programing

    Relational Operator Logical Operator

    . (condition) (control) ( ) .

    C++ relational operator ( ) logical operator ( ) . ( 1 hardware . C++ operator .)

    6.6.1 Relational Operator( ) Logical Operator( )

    6.12 C++ relational operator logical operator . operator == assignment operator = . (

  • 6 C++ 113

    operator .)

    6.6.2 if, if-else

    C++ if if-else . 6.13 if , control . if (TRUE) , (FALSE) if . if , .

    if control flow

    6.14 if-else control flow( ) . if , , else .

    if-else control flow

  • 114 C++ Programing

    6.15 if ({}) 3 , (compound statements) . ( block .) (swap) ( temp) . ( , , , .)

    (compound statement) ()

    6.16 if-else . if-else .

    if-else

  • 6 C++ 115

    . , , . debugging . .

    6.6.3 Ternary Conditional Operator (?:)

    operator, logical operator !(NOT) , binary operator, (operand) . Ternary conditional operator 3 operator, if-else . operator 6.17 .

    Ternary Conditional Operator (?:)

    6.6.4 While

    if if-else . , while . 6.18 while (control flow) . 0 10 . (

  • 116 C++ Programing

    .)

    while control flow

    if . . . (grammatical error) (logical error) . .

    . (counting) (sentinel) .

    6.6.4.1 Counter Controled Repetition ( )

    , data . 6.19 while control variable ( ) .

    (1) control variable , (2) , (3) , (4) control variable .

  • 6 C++ 117

    Counter Controled Repetition

    while 100 . . relational operator

  • 118 C++ Programing

    6.20.1 preprocessor () variable . #include .

    Sentinel Controled Repetition Program(1/3)

    total gradeCount variable, 0(zero) . 6.20.2 while .

    total = total + grade;

    While total grade total . total 0 , total grade . gradeCount 0 . grade data variable . int grade = 0; . ( , .)

  • 6 C++ 119

    Sentinel Controled Repetition Program(2/3)

    6.20.2 sentinel -1. prompt ( message) sentinel . while data while prompt data , data while . data , sentinel .

    Sentinel . -1 . sentinel . EOF (end of file). (EOF (control sequence). control key z , computer system .)

    6.20.3 . gradeCount 0(zero) average . . total gradeCount total/gradeCount () . , floating point number .

  • 120 C++ Programing

    (double) total. total . ( static_cast, .)

    Sentinel Controled Repetition Program(3/3)

  • 6 C++ 121

    do-while control flow

    6.7

    while .

    counter = counter +1; total = total + grade;

    assignment operator variable name . variable name 6.22 .

    ()

  • 122 C++ Programing

    .

    counter += 1; total += grade;

    counter .

    Increment Decrement

    / , / / . . .

    6.8 For-repetition

    while (1) control variable , (2) , (3) , (4) control variable .

  • 6 C++ 123

    Counter Controled while-repetition

    while (1), (2), (4) debugging . 6.25 for-repetition .

    Counter Controled for-repetition

  • 124 C++ Programing

    for-repetition while (1), (2), (4) . For (1), (2), (3), (4) while . (1) control variable for . (2) , (3) for , (4) control variable (2) .

    6.26 1 100 for .

    For-repetition ()

    for control variable .

  • 6 C++ 125

    For counter controled repetition . Sentinel controled repetition . ( 6.20.2 while for .) while for , . . 6.26 6.27 for while . ( .)

    for-repetition ()

    6.8.1 Repetition 1: compound interest ()

    For (compound interest) . 5% 1000 10 ( ) . (principal) p, (annual interest) r , n a .

    a = p(1+r)n

  • 126 C++ Programing

    For-repetition (1/2)

    6.28.1 . #include pow (preprocessor) .

    For-repetition (2/2)

    6.28.2 . cout setw(10) Earning cursor 10

  • 6 C++ 127

    . (right justified) . cout

  • 128 C++ Programing

    6.29.2 sentinel controlled while EOF . While , .

    (grade = cin.get()) != EOF

    grade = cin.get() , keyboard grade (assignment statement).

    Repetition : Computing frequency (2/5)

    grade EOF (TRUE), EOF (FALSE) . , grade EOF while- repetition .

    while switch . action . ( if-else .)

  • 6 C++ 129

    Switch . ( case label .) switch . Case label . , switch colon(:) , break switch .

    6.29.3 switch n( ) t (tab, ) (space) . ( , data , .)

    Switch case default: . default case switch .

    Repetition : Computing frequency (3/5)

  • 130 C++ Programing

    while frequency .

    Repetition : Computing frequency (4/5)

    6.29.5 .

    Repetition : Computing frequency (5/5)

    . Switch A (character) . Enter -1 to end .

  • 6 C++ 131

    A A . .

    6.8.3 Relational Operator == Assignment Operator =

    operator == = debugging . 6.30 num1 == num2 num1 = num2 .

    num2 num1 error message . if num1 = num2 TRUE FALSE num2 , num1 . C++ 0(zero) FALSE 0 TRUE if num2 0 num1 is not equal to num2 , num1 is equal to num2 .

    == = if-else

    6.8.4 Break Continue

    Break 6.31 while, for, switch , continue 6.32 . switch break continue . Break continue (switch ) . control .

  • 132 C++ Programing

    Break control

    Continue control

  • 133

    7

    (function)

    , (argument) input output ( ) system . math sin(x) input x sine . , function program .

    . .

    system function , game (random number) rand() . , function function .

    7.1 Math Library Function

    for-repetition ( 6.28) math pow( ) . math library , .

  • 134 C++ Programing

    Math library #include . 7.1 math library function .

    Math Library Functions

    7.2 Function Control Flow

    7.2 function control . Function function argument() . , argument function parameter , , ( return z) .

    Math function , function source , .

  • 7 (function) 135

    Function control flow

    7.3 Writing User Defined Functions

    function 7.3 .

    Writing User Defined Functions

    7.4 main() function prototype ( ) . return data type, parameter type . compiler main() . main() . (Prototype parameter type variable name

  • 136 C++ Programing

    compiler .) 7.4 square() main()

    .

    User Defined Function (1)

    7.5 integer return .

    function prototype main() prototype (main() ) . , . , main() , .

  • 7 (function) 137

    User Defined Function (2)

  • 138 C++ Programing

    void

    function , , return function , parameter . Parameter main() , return 7.6 void .

    Parameter , , void parameter ( ) .

    Function Parameter, Argument Return Type

  • 7 (function) 139

    Function program function prototype, argument, function parameter data type . 7.7 data type . argument a c integer, b double, parameter x n integer, y double. return z data type prototype function return type double .

    7.4 Data Type (precedence rule)

    type data type (precedence rule) . a integer b double a + b , 7.8 precedence rule double integer , integer double type double .

    date type int(integer), double(floating point number) char(character) . data type . integer , bit sign() type . Computer hardware , int 16 bits, short int 8 bits, long int 32 bits . double 32 bits, float double 16 bits, long double double 2 64 bits type. (char) 8 bits (, 1byte) , integer variable unsigned char type .

    type bool TRUE() FALSE() data type. FALSE 0(zero) TRUE 1 .

    Precedence rule . type data data type . , floating point number integer . floating point number integer . Long integer short integer .

  • 140 C++ Programing

    Data type (precedence rule)

    7.5 Random Number Generator rand()

    (random number generator) rand() 0 ~ RAND_MAX , game , computer simulation, . rand() . 7.9, 20 , 1 ~ 6 20 5 .

    rand() 1

  • 7 (function) 141

    7.10.1 - 7.10.3 rand() 6000 . face i(=1 ~ 6) frequencyi 1 i . 7.10.1 frequuencyi 0(zero) .

    frequency 7.10.3 . 1000 . ( . rand() random .)

    rand() 2 (1/3)

  • 142 C++ Programing

    rand() 2 (2/3)

    rand() 2 (3/3)

  • 7 (function) 143

    7.5.1 rand() seed : srand(unsigned int)

    rand() . rand() . debug , (simulate) . rand() game , game , game .

    rand() . , seed srand(..). rand() seed 100 srand(100); seed 55 srand(55); rand() . 7.11 seed rand() .

    rand() seed

  • 144 C++ Programing

    Seed rand() seed . seed . 7.12.1 time(0) seed srand(time(0)); .

    7.12.1 - 7.12.2 game . game .

    rand() game (1/2)

  • 7 (function) 145

    rand() game (2/2)

    enum(enumeration) data type ( WON, LOST, CONTINUE) . enum type . ( game .)

  • 146 C++ Programing

    7.6 Storage Class( ) Scope Rule( )

    variable variable . variable .

    (1) (attributes): variable data type, name, size (, bit ) value().

    (2) Storage class ( ): , automatic class static class . variable automatic class . Static class variable automatic class , , .

    (3) Scope(): variable , . x x scope . scope , .

    (4) Linkage(): file , file A global variable int x; file B extern int x; file (link) .

    (2) (3) variable . 7.13.1 - 7.13.4 main() integer variable x x , x scope .

    7.13.1 main() prototype int x = 10; . x (global

  • 7 (function) 147

    variable) . main() block (, {}) block int x = 3; int x = 6; x . block x (cout

  • 148 C++ Programing

    7.13.2 x , main() block x .

    Variable Scope (2/4)

    7.13.3 main() . localScope x int x = 25; 10 . . x int x = 25; x . , x , . automatic variable . automatic .

    staticScope x static int x = 50; . int x = 50; static main() static . .

  • 7 (function) 149

    Variable Scope (3/4)

    globalScope x x . x scope main() global global variable x .

    7.13.4 x . , scope rule static .

  • 150 C++ Programing

    Variable Scope (4/4)

    7.6.1 Static Variable

    static variable. automatic variable main() static . Static ? ( .) .

    7.14.1 . main() callCount() ( 10 10) . compile error , 1 .

  • 7 (function) 151

    Counting Function Call I:

    . callCount() num automatic variable 1 . 7.14.2 num static variable .

    Counting Function Call I:

  • 152 C++ Programing

    . callCount() main() (start number) . compile error , compiler . 7.14.3 .

    main() startNum scope callCount() . ( scope rule .) startNum 7.14.4 main() startNum global variable .

    Counting Function Call II:

    rand() seed srand . .

  • 7 (function) 153

    Counting Function Call II:

    7.6.2 Scope (resolution operator) ::

    block , block . 7.14.5 (global variable) x main() block .

    Scope Resolution Operator (::)

  • 154 C++ Programing

    main() x main() x global x . main() global x . (scope resolution operator) :: ::x . ( 6 using std::cout; scope resolution operator .)

    7.7 Recursive Functions( )

    argument , parameter void (call) , return . (recursive function), .

    factorial . n factorial n! 7.15 recursive relation ( ) . recursive relation recursive function.

    Factorial

    , factorial recursive 7.16 while for iterative . recursive function . recursive recursive .

  • 7 (function) 155

    Factorial non-recursive function

    7.17 factorial recursive , n! = n * (n-1)! . , n! n (n - 1)! . (n - 1)! ? (n - 1)! = (n - 1) * (n - 2)! (n - 2)! (n - 1) . , 1! (= 1) , n! . ( factorial parameter n return type unsigned long .)

    Factorial non-recursive function

    recursive function ? , recursive .

  • 156 C++ Programing

    7.18 main() cout

  • 7 (function) 157

    7.7.1 Recursive Function

    Fibonacci 1) 0 1 0, 1, 1, 2, 3, 5, ..., (sequence) . n n Fibonacci Fn recursive relation . 7.19 n Fibonacci recursive function .

    n Fibonacci recursive function

    7.20 recursive factorial 0! 10! , 7.21 n Fibonacci recursive .

    Using factorial function

    1) Fibonacci system .

  • 158 C++ Programing

    Using recursive fibonacci function

    7.8 (alias) Pointer

    ; name, data type, size(bit , ) value . memory ( ) data type . int n = 10; , memory n 10 .

    . data . , .

  • 7 (function) 159

    7.8.1 (alias)

    . , memory . 7.22 data (alias) . . ( (2) . .) .

    7.8.2 Pointer

    Pointer memory . non-negative ( ) pointer unsigned integer , pointer data type .

    7.23 pointer . (1) int *ptr; integer ptr . pointer ptr , (?? ) . (2) ptr 25 , 25 ( 100) ptr . (3) 10 memory x . (4) x (&x) pointer finger . ( 7.22 (2) int &b = a; .)

  • 160 C++ Programing

    Pointer

    7.9 (function call)

    (call) , parameter , parameter data type (value) argument parameter . call by value( ) .

    data . data call by value . Parameter . ( integer .)

    , , (, pointer) . call by reference( ) . .

  • 7 (function) 161

    7.9.1 (call by reference with name)

    7.24 (call by reference with name) . (call by value) parameter ( x) &(ampersand) . ( prototype .)

    & , , parameter x argument a (alias) . myFunction , x 10 a , x = 55; a = 55; . myFunction parameter x argument a .

    parameter . Parameter & call by reference, call by value . prototype . parameter call by value, parameter call by reference (with name) .

  • 162 C++ Programing

    return . (value) return . integer value return int . integer int & . .

    7.25.1 - 7.25.3 (call by reference with name) call by value argument . ( .)

    main()

  • 7 (function) 163

    7.9.2 Pointer (call by reference with pointer)

    argument argument parameter . parameter pointer , argument . 7.26 main() a , &a argument parameter pointer x .

    pointer int *x = &a; . myFunction *x = 55; pointer x , x 55 , return *x - 50; x , 55 50 5 return .

  • 164 C++ Programing

    Pointer

    return pointer return . prototype , parameter call by value, parameter call by reference, parameter pointer call by reference , return data pointer, . ( data pointer . .)

    7.10

    . main() prototype parameter return data , main() parameter argument . .

  • 7 (function) 165

    7.10.1 parameter (default)

    argument , parameter default . Parameter default prototype . ( int .)

    Parameter default

    prototype, myFunction , argument 0 . argument . main() myfunction(); , parameter x, y, z default 1, 2, 3 myFunction . myFunction(a); , parameter x argument a , y z default 2 3 .

    Default parameter parameter .

    7.10.2 Function Overloading: parameter

    parameter list() . parameter list signature() signature . parameter list .

  • 166 C++ Programing

    Function Overloading

    7.10.3 Function Template ( )

    Parameter data type data type data type . , data type , template() .

    7.28.1 parameter data (type int double) (square) function template . argument data type ( int a) compiler typename T type compile . Function template main() .

    Function Template()

  • 7 (function) 167

    7.28.2 - 7.28.3 function template data type, int, double, char .

    Function Template (1/2)

  • 168 C++ Programing

    Function Template (2/2)

  • 169

    8

    Array , index,

    Array()

    Array type data for . array , sorting() algorithm array . Array number array character array , .

    8.1 Number Array( )

    8.1 . array memory 5 a . (cell) 0 4( - 1) index . , , cell ( ? ) . Array a i (, index i) a[i]

  • 170 C++ Programing

    . 8.2 array .

    , , 0 . array .

    Array

    array . Array data data , array . 8.3 array arraySize const(, constant) . (Array index 0 array for i < arraySize; .)

    Array const

    8.4 5 integer array 0 8 . (Array

  • 8 Array() 171

    .)

    Array 1

    8.5 integer array .

    Array 2

  • 172 C++ Programing

    8.6 array (histogram) . ( .)

    Histogram

    8.7 rand() 5000 . 7.5 , frequency array . ++frequency [1 + rand() % 6]; . ( array index (expression) .)

  • 8 Array() 173

    integer array. int double double array . Array cell .

  • 174 C++ Programing

    8.2 Character Array( )

    Character array array cell array , . Character array array cell null 0 . Character array string , null string control character( ).

    8.8 . (a) (b) array . (b) , string .

    8.9 . array for while array . array . cin string array null string . array cout array null . Lee .

  • 8 Array() 175

    string

    8.10 cin , (n) .

  • 176 C++ Programing

    8.3 Array

    Array (call by reference with name) . , parameter array 8.11 array [ ] array integer parameter . call by reference , , argument myArray .

    Array

    8.3.1 Array : Linear Search Binary Search

    Array , array . (search) . , web , integer array. .

    . . data , .

  • 8 Array() 177

    sort() . .

    (exponentially) . 8.12 . . sort , , class . , sort , , 1/2 , 1/2 . 1/2, 1/4, 1/8, . . . . 210 = 1,024 1,024 , sort 10 . binary search(2 ) .

    (b)

    (a)

    (a) Sort (), (b) search ()

  • 178 C++ Programing

    8.13 integer array binary search .

    Binary Search (2 )

    , index . ( i j, (i + j) /2 ( ) .

    8.14 linear search( ) . Array .

    Linear Search ( )

    8.15 binary search (2 ) while repetition . parameter array, parameter , parameter , key. While low

  • 8 Array() 179

    Iterative Binary Search ( 2 )

    8.16 2 recursive() . 2 , recursive .

    Recursive Binary Search ( 2 )

  • 180 C++ Programing

    8.17 3 . ( main() .)

    Search

  • 8 Array() 181

    binary search linear search . , sort , . Sort search .

    sorting , n data , nlog(n) sort . 2 . sort , search sort .

    8.3.2 Bubble Sort

    Sort . sorting algorithm bubble sort . , , array data . 8.18 integer array sort .

    . . array . , . , .

  • 182 C++ Programing

    Bubble Sorting

  • 8 Array() 183

    8.19 bubble sorting . flag (, for ) data , sort , for sorting .

    Function Bubble Sort

    8.20.1 - 8.20.3 mean (), median () mode(, ) . Median sort , function bubbleSort .

    1 5 100 integer array response . ( .)

  • 184 C++ Programing

    : main() (1/3)

  • 8 Array() 185

    : functions (2/3)

    : output (3/3)

  • 186 C++ Programing

    Mode frequency main() array freq function mode . (Array frequency idea .)

    Median sort . Array printArray sort .

    8.4 Multidimensional Array( )

    1 array dimension array . 8.21 2 array 3 array , 2 array , , . 2 array 1 array , n array n-1 array . array 0 (row major).

    array. (a) , (b) , (c)

  • 8 Array() 187

    8.22 2 array parameter array . (row) array parameter , (column) . , array , (call by reference with name) .

    2 array

    8.23.1 - 8.23.3 2 array , , , . 2 array (row) (students) (column) (exams) global prototype . prototype 2 array parameter (column) , (exams) .

  • 188 C++ Programing

    2 array : main() (1/3)

  • 8 Array() 189

    2 array : functions (2/3)

  • 190 C++ Programing

    2 array : output (3/3)

    , , 2 ( 1 ) average . .

  • 8 Array() 191

    8.5 Array

    Array array data type, , , compiler memory array array . array array index .

    array array pointer() . Array pointer . pointer debug .

    8.24 array charArray charArray[0] pointer . Pointer unsigned integer . pointer .

    Pointer array

    array (call by reference with name). Array pointer array pointer . , pointer (call by reference with pointer) .

  • 192 C++ Programing

    8.25 my_function prototype , parameter , pointer , value . main() , arraya , arrayb ( ) pointer . main() .

    parameter

  • 8 Array() 193

    8.26 character array pointer . character array . system islower(char) toupper(char) . Pointer .

    Array pointer

  • 8 Array() 195

    C++ Class

    9 Classes Data Abstraction10 Class Template Operator Overloading11 Class Class 12 File I/O()

  • 197

    9

    Class Data Abstraction

    C C++ . (object oriented programming language) C++ . C++ (object) .

    , 9.1 , data , . .

    : Data

    (object oriented) 9.2 (abstract) . .

  • 198 C++ Class

    , . , .

    Object Oriented

    9.1 Data : Structured Data Type

    (object oriented) data , structured data type. data type . Structured data type , object .

    9.3.1 - 9.3.2 integer hour, minute, second Time data type . data type integer, double, char, data type .

    Structured data type function prototype main() . structure data type Time, integer int , Time dinnerTime; dinnerTime Time structured data .

    24 (0~23), (universal) printStandard (AM), (PM) (standard) . t.hour, t.minute, member operator (.) Time t member hour, minute, .

  • 9 Class Data Abstraction 199

    Structured Data Type: (1/2)

    data type , dinnerTime.hour = 25; structured data type . object oriented data .

  • 200 C++ Class

    Structured Data Type: (2/2)

    9.2 Class Definition

    Data structured data type, data data class . 9.4.1 - 9.4.5 data class Time . 9.4.1 class Time , Time, class public private . Public , private data ( hour, minute, second), , . Public private data .

  • 9 Class Data Abstraction 201

    Class Time (1/5)

    Class , class , prototype , class . Public class ( Time()) , (constructor) . constructor class , , return void return type .

    Class Time (2/5)

    Class . (return type , return type ) class scope resolution operator (Time::) class .

  • 202 C++ Class

    9.4.3 setTime parameter private hour, minute, second . class parameter data class set () , data message .

    Class Time (3/5)

    9.4.4 printUniversal() printStandard() structured data type Time 9.3.2 .

    Class Time (4/5)

  • 9 Class Data Abstraction 203

    9.4.5 main() class . Time t;, int num; integer num , t Time class . t Time class object .

    Class object data member class data member function class . member operator(.) t.printUniversal(); object t printUniversal() .

    Class Time (5/5)

  • 204 C++ Class

    class constructor object . constructor Time() private data member hour, minute, second 0 . object t t.printUniversal() 00:00:00. t.setTime(25, 60, 60); data .

    9.5 class object data type alias() , object pointer . lunchTime Time class object, lunchTimePtr lunchTime pointer , lunchTimeAlias lunchTime . pointer object member, lunchTimePtr printStandard(); member operator (.) () .

    Object Pointer Alias

  • 9 Class Data Abstraction 205

    9.3 Class (scope) (information hiding)

    Object oriented class object class information security ( ) . 9.6 class (scope) . Class , main() public domain() data private domain data public domain . private domain data member hour public domain member operator t1.hour = 7; .

    Class Scope ( )

    9.6.1 - 9.6.5 (sales person) class SalesPerson . SalesPerson class , Time class , 3 file . Time class class , class member , main() .cpp file, time.cpp .

  • 206 C++ Class

    . debug . class member main() . ( main() client() .)

    9.6.1 SalesPerson class .h file salesp.h . header file . class preprocessor #ifndef SALESP_H . . . . #endif header file file include .

    Class SalesPerson (1/5)

  • 9 Class Data Abstraction 207

    9.6.2 - 9.6.5 class SalesPerson member function , file salesp.cpp . file C++ file .cpp . private domain (totalAnnualSales()) utility function .

    Class SalesPerson (2/5)

    file constructor SalesPerson() preprocessor #include "salesp.h" . compiler . #include system include . file include file (quotation mark) .

  • 208 C++ Class

    Class SalesPerson (3/5)

  • 9 Class Data Abstraction 209

    9.6.4 class client main(). preprocessor #include "salesp.h" header file compile SalesPerson class class link . 9.6.5 .

    Class SalesPerson (4/5)

    Class SalesPerson (5/5)

  • 210 C++ Class

    9.4 Constructor() Destructor()

    Class constructor class object class . constructor default destructor .

    9.7.1 - 9.7.3 Time class ( 9.4.1) constructor default parameter 0 parameter setTime hour, minute, second . Constructor . header file time2.h, time2.cpp .

    Default parameter constructor

  • 9 Class Data Abstraction 211

    Default parameter constructor ()

    9.7.3 client 4 Time default (standard time) .

    Default parameter constructor

  • 212 C++ Class

    9.8.1 - 9.8.4 Time class destructor destructor . class header time3.h, time3. cpp .

    Destructor constructor class ~ constructor . constructor destructor , constructor , destructor , . (, static , data type main() , main() static destructor .)

    class destructor printStandard() . , static , , . class constructor , destructor . 9.8.4 .

    Destructor (1/4)

  • 9 Class Data Abstraction 213

    Destructor (2/4)

    Destructor (3/4)

  • 214 C++ Class

    Destructor (4/4)

  • 215

    10

    Class Template Operator Overloading

    7.10.3 function template() data type parameter . class idea , class template .

    array class object operator . C++ operator type operand operator overloading ( ) . operator overloading .

    10.1 Class Template

    Function template , class template , . Class template function template . 10.1 type array function template, 10.2 integer, double, char array .

  • 216 C++ Class

    Function Template

    Compiler function template type data type data compile . argument data type int, function template typename T int .

    Function Template

  • 10 Class Template Operator Overloading 217

    10.2 template function argument data type . Function template function template function prototype class header file compile , main() , header file include .

    class template . stack class template . Stack data last-in-first-out (LIFO) . ( first-in-first-out (FIFO) .)

    stack array . stack data 100 . 100 (index 0~99) array data (push) (pop). Stack , 100 data full, empty .

    10.3 stack . Array index stack (top) data push index , pop ( index data return) index .

    10.4.1 - 10.4.5 type data stack class template .

    Stack Operations

  • 218 C++ Class

    10.4.1 - 10.4.2 header file stack.h . template function template . (Typename T , type T .) Constructor parameter default 10.

    constructor destructor bool (TRUE FALSE) return . const( , bool isEmpty() const; ) constant object . , constant object constant . bool push(const T&); bool pop(T&) (call by reference with name) .

    Private domain data member size stack , top stack data index, stackPtr stack, array pointer, pointer array , array 0 data pointer .

    Class Template Stack (1/4)

  • 10 Class Template Operator Overloading 219

    10.4.2 - 10.4.3 template class Stack . Typename T function template template , Stack:: T typename template class Stack .

    class constructor private domain data member . , (header) , block({ }) colon(:) variable name list ( ) . ( block (assignment statement) .)

    constructor parameter s 0 s , default 10 size , top -1 .

    Stack::Stack(int s) :size(s > 0? s: 10), top(-1), stackPtr(new T[size]) { . . . .} //end constructor

    Class Template Stack (2/4)

  • 220 C++ Class

    Class Template Stack (3/4)

    stackPtr(new T[size]) new memory , size array array return . new return array pointer stackPtr . array compile memory , new array , new array . (dynamic memory allocation) .

    10.4.3 constructor, destructor class Stack . template compile

  • 10 Class Template Operator Overloading 221

    class cpp file stack.cpp . template class header stack.h main() include file . ( line #endif 10.4.1 #ifndef class .)

    push stackPtr[++top] pop stackPtr[top--] . ++top , top , top-- , top . top stack push , pop .

    10.4.4 file stack.h template class Stack client main(). 4 double data 4 stack push pop . push pop . while .

    while(intStack.pop(intValue)) cout

  • 222 C++ Class

    Class Template Stack (4/4)

  • 10 Class Template Operator Overloading 223

    10.2 Operator Overloading( )

    data type operator +, -, =, array class object operator (overload) . class object parameter class .

    10.5 MyClass transferSecrete(myClass &) . parameter object secrete object secrete . client main() object b data member secrete object a secrete .

    class object parameter object .

    Class object

    Operator overloading , . assignment operator '=' overload

  • 224 C++ Class

    10.6 'operator=' operator = .

    object b private object c private b = c; , operator= parameter object c private data class object b private . Compiler compile b = c b.operator=(c) compile .

    Operator '=' overload

    10.2.1 Overloading Operator '='

    Operator overloading rational number class . Rational number . (, 0 .) 10.6.1 class RationalNumber header file rationalNumber.h , 10.6.2 - 10.6.3 class file rantionalNumber.cpp . Constructor default 1/1 set (numerator)

  • 10 Class Template Operator Overloading 225

    (denominator) . Set 0 . (Client .)

    RationalNumber class operator '=' overloading .

    RationalNumber Class Definition (1/3)

    RationalNumber Class Definition (2/3)

  • 226 C++ Class

    RationalNumber Class Definition (3/3)

    10.6.4 RationalNumber class operator '=' overload object private variable object operator . operator= prototype ( (a)) class definition , ( (b)) rationalNumber.cpp file . compiler client a = b; a.operator=(b); compiler .

    operator '=' overloading pointer this. Class object this pointer . a = b; (, a.operator=(b);) , this object a pointer.

    object a object b , a b object , parameter object b object a . a b a = a object ( ) .

  • 10 Class Template Operator Overloading 227

    if(this != &r) { . . . . } // end if

    return *this; a = b = c; . assignment operation , b = c b a = b . return *this; assignment operation = .

    (b)

    (a)

    Overloading Operator '='

    10.2.2 Friend Operator Overloading

    operator overload operand = overload . a = b; a.operator=(b) class operator= . cout

  • 228 C++ Class

    . . friend private domain data .

    10.7.1 - 10.7.2 friend class . Friend prototype public private domain public domain , class ( MyClass::) .

    Class header friend

    Friend

  • 10 Class Template Operator Overloading 229

    operator overload friend 10.6 RationalNumber class . 10.8.1 friend prototype RationalNumber class definition ( 10.6.1) , 10.8.2 10.6.2 - 10.6.3 .

    friend operator '=' overload operator=(..) parameter . operator(..) parameter type istream class, cin class object.

    operator overloading: Friend function prototypes

    operator overloading: Friend function definitions

  • 230 C++ Class

    operator overloading: client

    10.8.3 operator '=', '>>', '

  • 10 Class Template Operator Overloading 231

    operator overloading: (1/4)

    operator overloading: (2/4)

    operator overloading: (3/4)

  • 232 C++ Class

    operator overloading: (4/4)

  • 233

    11

    Class Class

    . C++ . class class . , data class object , class class .

    class (composite) class . class class , , .

    11.1 composite() Class

    class 11.1.1 - 11.1.2 Date class Employee class 11.2.1 - 11.2.4 .

    Employee class Date class object Employee class ( 11.2.1) class ( 11.2.2) Date class header preprocessor #include "Date.h" .

  • 234 C++ Class

    Employee class Date class object, , .

    Composite class , Date class utility checkDay parameter . .

    Date Class Function Definition (1/2)

  • 11 Class Class 235

    Date Class Function Definition (2/2)

  • 236 C++ Class

    11.2.1 Employee class header file . class , const . (C++ const data . data const .)

    Employee Class Definition

    11.2.2 constructor Employee system cstring class strlen strncpy . strlen parameter string, return, strncpy parameter string parameter parameter (copy) .

  • 11 Class Class 237

    Employee Class Function Definition (1/2)

    Employee Class Function Definition (2/2)

  • 238 C++ Class

    Employee Class : Client

    11.2 Class Inheritance()

    class (inherit) . class . .

    operator overload . , operator , friend . friend , friend .

    class 11.3 . (hierarchy system) . , , . , . Venn diagram .

  • 11 Class Class 239

    Class Inheritance

    class . class , class , inherit . class class inherit . class inherit class class data .

    Employee class ( 11.2.1 - 11.2.4) , class. class 11.4.1 (salary) PaidEmployee class . (Employee class class .)

    PaidEmployee Class Definition

  • 240 C++ Class

    PaidEmployee class 11.4.2 - 11.4.3 Employee class salary data . ( italic Employee class .)

    PaidEmployee Class Function Definition(1/2)

  • 11 Class Class 241

    PaidEmployee Class Function Definition(2/2) PaidEmployee class Employee class .

    Employee class class . class Employee class PaidEmployee class . ( software .) 11.5.1 - 11.5.2 PaidEmployee2 class.

    PaidEmployee2 Class Definition

  • 242 C++ Class

    PaidEmployee2 Class Function Definition

    Class 11.5.1 class class header file( Employee.h) include class semicolon(:) type class (: public Employee) .

    type class data class client , public, protected, private type . type public , class private domain data public domain , public domain data .

    type protected private class public domain data . ( public type . .)

  • 11 Class Class 243

    Client Program Using PaidEmployee2 Class

  • 245

    12

    File I/O()

    , keyboard monitor C++ . hard disk . cout, cin C++ iostream class object, preprocessor #include namespace std class object cin, cout, .

    file , C++ fstream class client include object . 12.1 file , , .

    . classGrade.dat file outputFile ofstream object . ( parameter ios::out file .)

    ofstream outputFile("classGrade.dat", ios::out);

  • 246 C++ Class

    file outputFile FALSE error message . File , while keyboard file .

    12.2 file classGrade.dat record outputGrade() monitor . ios::in file .

    Output to a File

  • 12 File I/O() 247

    Reading from a File

  • 248 C++ Class

    include header file I/O class template basic_ifstream basic_ofstream . file open ifstream ofstream class template typename char class. file class template basic_fstream , class template tyename char fstream .

    class object ios::in ios::out file-open mode(file ) . Ofstream class object mode parameter ios::out default . ( file mode .) (open) file , object member function close() .

    outputFile.close():

    Mode ios::ateios::ateios::inios::outios::truncios::binary

    file (append).File . Data file . file . file . ( .)File . Binary (text ) data file .

    File I/O Mode

    file file . file sequential file . Sequential file data data . , sequential file update , file file update .

    , computer hardware RAM , random access file . ( file .)

  • 12 File I/O() 249

    . Linked List(Dynamic Data Structure).

  • 251

    Linked List(Dynamic Data Structure)

    pointer (dynamic data structure) . array data . Array integer data type data class object .

    array , array . array , .

    linked list . ( linked list .) linked list class template .

  • 252

    1. List Node Linked List

    List node list data , L1 data node (pointer ) . ( pointer .)

    Node

    node L2 linked list. List pointer firstPtr lastPtr , node node , (address) . List node node node NULL(= 0) .

    (a)

    (b)

    Linked List

  • Linked List(Dynamic Data Structure) 253

    pointer firstPtr lastPtr NULL list . L2-(a) node pointer node . pointer , ( ) (a) (b) . ( .)

    2. List Operations

    Linked list array list data data list list data , . list operation .

    (1) Insert node at front ( node ) (2) Insert node at back ( node ) (3) Remove node from front ( node ) (4) Remove node from back ( node )

    2.1 Insert node at front

    L3.1 - L3.2 list front node . newPtr node . newPtr nextPtr = firstPtr; node node node . firstPtr = newPtr; firtPtr node . ( node newPtr .)

  • 254

    Insert Node at Front(1/2)

    L3.2 list , firstPtr NULL node , fistPtr lastPtr newPtr .

    Insert Node at Front(2/2)

    2.2 Insert Node at Back

    Node . L4 . lastPtr nextPtr = newPtr; node node lastPtr = newPtr; lastPtr node . List node L3.2 .

  • Linked List(Dynamic Data Structure) 255

    Insert Node at Back

    2.3 Remove Node from Front

    List node L5.1 . node (delete) . tempPtr pointer. Delete node , memory . node pointer node ( node dangling node .) .

    Remove Node from Front

  • 256

    delete tempPtr; tempPtr node , (garbage collector) node memory . (Delete parameter pointer .)

    List node list L5.2 . List node . , list operation .

    Remove Node from Front: node

    2.4 Remove Node from Back

    List node . List lastPtr node . node , preceding node . Preceding node firstPtr list . Preceding node precedingPtr list node L6.1 .

    Remove Node from Back: using precedingPtr

  • Linked List(Dynamic Data Structure) 257

    List node L5.2 list . L6.1 precedingPtr idea . List node

    node node, preceding node firstPtr L6.2 .

    Last Node Preceding Node

    while , precedingPtr node precedingPtr node node lastPtr node node . ( while pointer .)

    3. List Class1)

    list list operation List class . List class type data class template . (List class 10.1 class template .)

    L7.1 template class ListNode . list node , list class node List class friend

    1) list class H.M. Deitel P. J. Deitel C++ How To Program(2005, Prentice Hall) edit .

  • 258

    . ListNode class template class List; . ( main() main() prototype .) ListNode private domain data node pointer nextPtr .

    Node object constructor data value data nextPtr 0(NULL) . (Constructor private data member .)

    Class ListNode Definition

  • Linked List(Dynamic Data Structure) 259

    L7.2 List class , L7.3 - L7.6 List class . Private domain utility getNewNode(..) node parameter data (pointer) return insert node .

    L7.3 constructor , list object . Destructor list object list . List class .

    Class List Definition

  • 260

    List Class Constructor Destructor

    List class L7.2 L7.6 class definition #ifndef LIST_H . . . . #endif list.h file . #endif 7.6 . List class template compile client program main() typename T compile .

    L8.1 template function testList(..) #include "list.h" List class client main() compile .

  • Linked List(Dynamic Data Structure) 261

    Remove Node from Front and from Back

  • 262

    Insert Node at Front and at Back

    List Class

  • Linked List(Dynamic Data Structure) 263

    4. List Class Test Program

    L8.1 - L8.2 List class . L8.1 testList(..) function template parameter .

    List Class Test Program(1/2)

  • 264

    parameter List class object , parameter integer, double character, typename (string) . ( L8.2 main() data type List object , .)

    List Class Test Program(2/2)

    parameter , instruction() List class object list operation , (choice) operation list object . List node insert insert data value insert , remove value . (List class insert remove parameter (call by reference with name) .) Operation print() list .

  • Linked List(Dynamic Data Structure) 265

    L8.3 integer list class object character list class object .

    List Class Test Output

  • 267

    #1: if

    .

    Temperature() Feeling

    4 or lower5 - 1011 - 1516 - 2425 - 27above 28

    very coldcoldcoolcomfortablewarmhot

    (a) C++ .

    Enter the current temperature: 6It is cold.

    (b) C++ . , 0 -1 .

  • 268

    Enter the current temperature: 6It is cold.Enter the current temperature: 20It is comfortable.Enter the current temperature: -1End computing. Thank you!

    (c) (b) EOF C++ . .

    : . (1) Source code : (comments), (spacing), (indentation).

    . , . ( .)

    (2) : #include , , semicolon(;) ({ }) . .

    * .

  • 269

    #2: While

    C++ . integer 1 . ( 2.2 2 2.5 .)

    computer system . , preprocessor #include INT_MAX computer integer . INT_MAX "Yes, they are equal!", "They are different!" . ( while .)

    #3: While

    (positive integers) (greatest common divisor, GCD) .

    , (subtrahend) (difference) . 0 , GCD.

    14 21 , GCD .

    (21 - 14) = 7, (14 - 7) = 7, (7 - 7) = 0.

    GCD 7. 3 11 ,

    (11 -3) = 8, (8 - 3) = 5, (5 - 3) = 2, (3 - 2) = 1, (1 - 1) = 0.

  • 270

    GCD 1. : GCD C++ . (Data

    prompt message .)

    #4: rand()

    1. (range) (random number)

    (a) (b) random number 30 . 10 . rand() preprocessor #include .

    (a) -20 n 20 (b) 0 n < 1

    Hint: (a) L, U random integer number

    .

    L + rand() % (U - L + 1)

    (b) 1 (double). rand() RAND_MAX (double)rand()/(RAND_MAX + 1) . . rand()/(RAND_MAX +1) 0. 1 (double)rand() rand() . RAND_MAX preprocessor .

    #include

  • 271

    2.

    20 C++ . 10 .

    { 13, 7, 1, 10, 19, 5, 30 }

    Hint: 0 6 P1 switch .

    switch

    3. (histogram)

    rand() .

    (1) (function) .

    (a) integer k 1 rand() 10 k (frequency) .

    (b) (%) . (: (percentage) , percentage = 100* (frequency /k); . .)

    (c) (bar graph) . (*) percentage 2 . 5 13% , 132 = 26 .

  • 272

    1: ****************** 2: **************************** 3: **************** 4: ******************* 5: ************************** . . 10: ********************

    (2) main() k k argument (a), (b), (c) (1) histogram .

    (3) k = 100, k = 1000, k = 100000 graph .

    4. random number generator ( )

    myRand() random number generator . , congruence ( residue) .