362
Ï Ï Ï ð ð ð î î î ã ã ã ð ð ð à à à ì ì ì ì ì ì ÷ ÷ ÷ ë ë ë à à à ë ë ë û û û í í í ¿ ¿ ¿ í í í ä ä ä ñ ñ ñ ý ý ý í í í î î î é é é ë ë ë ã ã ã î î î ë ë ë ò ò ò 1 Ò Ò Ý Ý Ð Ð Ã Ã ¯ ¯ ¯ ¯ Í Í Á Á ¯ ¯ Ë Ë Ý Ý Ã Ã -¯íäñýí îéëãîëò -Èäåíòèôèêàòîð -Òîãòìîë áîëîí õóâüñàã÷ -Ñàíàõ îé -Àíõíû ïðîãðàìì

C++ xelnii surax bichig

Embed Size (px)

Citation preview

  • 1

    -- --

    -- --

    --

  • 2

    . . , . , . , . . . . , , 313682, 323956 . .

    .

    . .

    , . : , , , , , , , , . .

  • 3

    , . .

    . . .

    . . Microsoft Word, Excel, Access, PowerPoint, PageMaker Microsoft Windows . . . C++ .

    . :

    1. ( Designing ) 2. ( Coding ) 3. ( Compilnig ) 4. ( Testing ) 5. ( Developing ) 1. .

    . , , . . , 2 . . 2 . 2 . 2 1

  • 4

    1 , 2 . , , 3- .

    2. . , . , . , C++- .

    3. . Compile Run . . . , , , . 2- . .

    4. . . , . , .

    5. . , , , , . , .

  • 5

    ,,

    . . , . C++- .

    asm auto break case catch char class const continue default delete do double else enum extern float for friend goto if inline int long new operator private protected public register return short signed sizeof static struct switch template this throw try typedef union unsigned virtual void volatile while ,

    , , . . :

    1 .

    . . .. +, -, *, /, ?, !, `, ~, , ,

    ;, :, @, #, $, %, ^, &, (, ), =, \, {, }, [, ], , .

    . . ,

    . , Fvar fvar .

  • 6

    .

    .

    . . . DEFINE .

    #define

    .

    #define month 12 // #define degree 30 // #define pi 3.1412 // #define name . // #define question Sure? //

    //

    . . .

    . 23 , 100 . 100 23 . . 25 . 23 25 .

  • 7

    . 23 . ,

    #define num 23

    , num

    .

    #define num 25

    .

    23 25 .

    # #define . #define , 3- .

    . , , . , . . ++- .

    char 0..9,A..Z,a..z,*/+-@#$%^&*(){}[];;.,?`~

    short -128..127 unsigned short 0..255 int -32768..32767 unsigned int 0..65535 long -2147483648.. 2147483647 unsigned long 0..4294967295

  • 8

    float 1.17549435e-38..3.40282347e+38 double 2.2250738585072014e-308 ..

    1.7976931348623157e+308

    .

    .

    ,

    , . .

    int ivar1; // int ivar2=-10; // float fvar1=2.57; // unsigned int ivar3=500; // char ch1,ch2=*,ch3; //

    .

    .

    2 .

    , .

    , .

    ,,

  • 9

    . . . .. . . , 640000 640000 640000 . . . 640000 0, 639999 .

    , . . .

    char 1 short 1

    unsigned short 1 signed int 2

    unsigned int 2 long 4

    unsigned long 4

    . , , , .

  • 10

    ( ) ? . . . , (0 1) . , . 8 . 28=256 . 10 . 10- . 0 1 2- 2- . 10- 2- .

    10- 156 .

    156=1*102+5*101+6*100

    10- . 2-

    : 156=1*27+1*24+1*23+1*22

    156=1*27+0*26+0*25+1*24+1*23+1*22+0*21+0*20

  • 11

    . 2- 10011100

    . 10-

    10- 2- 2- .

    10- 2-

    0 00000000 1 00000001 2 00000010 3 00000011 4 00000100 5 00000101 6 00000110 7 00000111 8 00001000

    . . . . . . . . . . 255 11111111

    .

    .

    8 .

    . .

    .

  • 12

    . .

    CCOONNSSTT

    . const . #define .

    #define =

    :

    const int month=12 const int degree=30 const double pi=3.1412 const char name[]=. const char question[]=Sure?

    . . const . #define . . .

    a = pi * 2

    . pi const

    pi - . , .

  • 13

    #define pi .

    a = 3.1412 * 2

    .

    ++ - , , . . . *.h .

    //defs.h void fun1() { } //my.cpp #include defs.h void main() { . . . . . fun1(); . . . . . }

    #include

    ( ) .

  • 14

    . ++ . ++- , . Borland C++- library . #include . ( ) , (< >) . , , , math.h .

    #include

    .

    CC++++

    . , .

    .

    //hello.cpp #include #define greeting = ?; void main() { cout

  • 15

    Compile Run .

    ?

    .

    .

    . . 2- iostream.h . . greeting .

    void main()

    . C++- ( {} ) . , , . . . .

    cout

  • 16

    ( *.EXE ) . .

    . .

    . .

    . , , , .

  • 17

    2 . C++- 2 .

    . . #define const .

    . . .

    , . 1 . . 1 8 256 .

    , , . #include .

  • 18

    22--

    --

    -- --

    -- --

  • 19

    22--

    . ( ) . .. . . 2 .

    , . . 2 .

    . C++ (char, int, float .) ( ) . , , . . : 2 , .

    RELAT .

    //relat.cpp #include void main()

  • 20

    { int numb; cout > numb; cout

  • 21

    < == != >= =44) (harry!=12) (7

  • 22

    . . . C++ for, while, do 3 .

    FFOORR

    C++- , . for BASIC . . . .

    0- 14 FORDEMO .

    //Fordemo.cpp #include void main() { int j; for (j=0; j

  • 23

    0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 ? For for 3 . , , . ( ) . j . . cout

  • 24

    . . . .

    . j- 1- . ++j . j- . . --j j- .

    j=j+1 j=j-1

    .

    . += -= . .

    j+=3 j-=5

  • 25

    j- 3- ( 5- ) . .

    j=j+3 j=j-5

    ?? 15

    . j- 0 j 14 . . j = 15 . . 0- , , .

    . ( { } ) . .

    Cubelist . 1- 10 .

    //cubelist.cpp #include

  • 26

    #include void main() { int numb; for (numb=1; numb

  • 27

    . . int cube .

    int cube=numb*numb*numb;

    .

    cube=10; cube .

    . .

    .. . For 1 , cubelist . . .

    .

  • 28

    for (numb=1; numb

  • 29

    Watch , . . .

    Watch . .

    FFOORR

    , . . Factor ( ). : 5- 5*4*3*2*1=120 .

    //factor.cpp void main() { unsigned int numb; unsigned long fact=1; cout > numb; for (int j=numb; j>0; j--) fact*=j; cout

  • 30

    numb, 1 (numb > 0), 1- . unsigned long . :

    : 10 3628800

    FFOORR .

    j .

    for (int j=numb; j>0; j--)

    C++- .

    . .

    For

    . ( , ) .

  • 31

    . .

    For (j=0, alpha=100; j>n; }

    . .

    1

  • 32

    27 33 144 9 0

    While for

    . .

    . 0 n!=0 .

    : .

    cin >> n .

    WWhhiillee While4 while

    . Cubelist Cubelist 4 . 4 4 9999- . 4 . .

  • 33

    //while4.cpp #include #include void main() { int pow=1; int numb=1; while (pow

  • 34

    . .

    1 1 2 3 5 8 13 21 34 55 ..

    . . , .

    //fibo.cpp #include void main() { const unsigned long limit=4294967295; unsigned long next=0; unsigned long last=0; while (next

  • 35

    267914296 433494437 701408733 1134903170 1836311903 2971295073

    unsigned long . while . Limit . .

    (next

  • 36

    do .

    Divdo , .

    //divdo.cpp #include void main() { long dividend, divisor; char ch; do { cout > dividend; cout > divisor; cout

  • 37

    . .

    : 11 : 3 3, 2 ? (Y/N) y : 222 : 17 13, 1 ? (Y/N) n

    ??

    . For . do while . .

    . , ?

  • 38

    . , ; , , ; , ..

    . . C++- . Ifelse . ( else - .) 2 switch . . .

    IIFF IF

    . Ifdemo .

    //ifdemo.cpp #include void main() { int x; cout > x; if (x>100) cout

  • 39

    If . If While . If , While - . .

    : 2000 100-

    100-

    .

    IIFF

    () .

    //if2.cpp #include void main() { int x; cout > x; if (x>100) { cout

  • 40

    : 12345 12345 100-

    . .. , , , . prime- . .( 1 .)

    //prime.cpp #include #include void main() { unsigned long n, j; cout > n; for (j=2; j

  • 41

    0- . . :

    : 13 : 22229 : 22231 11- .

    (

    .)

    for {} . If .

    EExxiitt (())

    . exit . . . 0 . ( (*.bat) errorlevel .)

  • 42

    IIFF EELLSSEE if

    . . , ifelse .

    if () { } else { }

    . ifelse.cpp

    .

    //ifelse.cpp #include void main() { int x; cout > x; if (x>100) cout

  • 43

    while ifelse . getche . Chcount .

    //chcount.cpp #include #include void main() { int chcount=0; int wdcount=1; char ch=a; while (ch!=\r) { ch=getche(); if (ch= ) wdcount++; else chcount++; } cout

  • 44

    ch . ( getch() getche() .)

    space ( ) wdcount , chcount . .. . . :

    This is an example =4 =15

    ENTER

    getche() \r .

    //chcnt2.cpp #include #include void main() { int chcount=0; int wdcount=1; char ch; while ((ch=getche())!=\r) { if (ch= ) wdcount++; else chcount++;

  • 45

    } cout

  • 46

    adifelse .

    //adifelse.cpp #include #include void main() { char dir=a; int x=10, y=10; cout

  • 47

    , . .

    : 10,10 (n, s, e, w): n : 10,9 (n, s, e, w): e : 11,9 (n, s, e, w):

    .

    . . . . .

    ifelse

    . .

    //badelse.cpp #include void main() { int a, b, c; cout > a >> b >> c; if (a==b) if (b==c)

  • 48

    cout

  • 49

    SSWWIITTCCHH

    , switch . . .

    //platters.cpp #include void main() { int speed; cout > speed; switch(speed) { case 33: cout

  • 50

    ( ) . :

    33, 45, 78- : 33

    BBrreeaakk case break

    . ( !) . , case . break .

    DDEEFFAAUULLTT

    //adswitch.cpp #include #include void main() { char dir=a; int x=10, y=10; while (dir!=\r) { cout

  • 51

    case n : y--; break; case s : y++; break; case e : x++; break; case w : x--; break; case \r : cout \n; break; default : cout \n; break; } } }

    Adswitch switch

    Default . case- default . Try again . break .

    SSwwiittcchh IIffeellssee switch , ifelse

    ? .

    If (SteamPressure*Factor>56) . . . . . else if (VoltageIn+VoltageOut

  • 52

    ifelse . switch- . (a

  • 53

    2 .

    Min=(Alpha

  • 54

    . 8- , . .

    cout

  • 55

    AANNDD C++

    . Advenand.cpp Adswitch . (7,11) , .

    //advenand.cpp #include #include #include void main() { char dir=a; int x=10, y=10; while (dir!=\r) { cout

  • 56

    If x 7-, y 11- . and && . x==7, y==11 . . C++- 3 .

    && AND || OR ! NOT

    OORR

    . . Advenor.cpp .

    //advenor.cpp #include #include #include void main() { char dir=a; int x=10, y=10; while (dir!=\r) { cout

  • 57

    case n: y--; break; case s: y++; break; case e: x++; break; case w: x--; break; } } }

    If x 15- , x

    15- . .

    NNOOTT not

    . ( 2 , 3 ). . ! , . : x 7 (x==7) , !(x==7) .

    . . . C++ 0

  • 58

    , 0- .

    7- , . 7- .

    //advenor.cpp #include #include #include void main() { char dir=a; int x=10, y=10; while (dir!=\r) { cout

  • 59

    . ( ) . ( ) .

    I !, ++, --, -

    II *, /, % III +, -

    IV , = V ==, !=

    VI && VII ||

    VIII ? :

    IX =, +=, -=, *=, /=, %=

    C++-

    . switch break . . conitinue, goto 2 .

  • 60

    BBRREEAAKK

    . . Showprim .

    //showprim.cpp #include #include void main() { const unsigned char WHITE=219; const unsigned char GRAY=176; unsigned char ch; for (int count=0; count

  • 61

    ch- GRAY , . , . Break .

    CCoonnttiinnuuee break

    . , . .. . continue .

    divdo . . . 0 0- , Division by zero . , divdo2 .

    //divdo2.cpp #include void main() { long dividend, divisor; char ch; do { cout > dividend; cout > divisor; if (divisor==0) {

  • 62

    cout

  • 63

    , . . 0- , 0- .

    C++- 3 . for . while do .

    , . .

    4 . If . ifelse , . switch . , 2 .

    and or , not .

    break , continue , . goto .

  • 64

    . : , , , , , .

  • 65

    33--

    -- --

    --

  • 66

    33--

    float, int, char

    . . , , . , . C++- .

    .

    . . .

    C++- . OOP- . Basic Pascal (record) .

    . , , ,

  • 67

    . , .

    //parts.cpp #include struct part { int partnum; int modelnum; float cost; }; void main() { part part1; part1.partnum=6244; part1.modelnum=373; part1.cost=217.55; cout

  • 68

    struct part { int partnum; int modelnum; float cost; };

    .

    Struct . . 3 . ( ; ) . ( .).

    :

    part part1; part part1

    . . ? 3 . 2 , 4 , 8 .

    struct C++- . .. struct part part1 .

  • 69

    . .

    part1.partnum=6244;

    , ( . ), 3 . part1- partnum . , . . .

    . .

    //partscom.cpp #include struct { int partnum; int modelnum; float cost;

  • 70

    } part1; void main() { part1.partnum=6244; part1.modelnum=373; part1.cost=217.55; cout

  • 71

    cout

  • 72

    struct Distance { int feet; float inches; }; void main() { Distance d1, d3; Distance d2=(11, 6.25); cout > d1.feet; cout > d1.inches; d3.inches=d1.inches+d2.inches; d3.feet=0; if (d3.inches >=12.0) { d3.inches-=12.0; d3.feet++; } d3.feet+=d1.feet+d2.feet; cout

  • 73

    d3 = d1 + d2 . Int, float header . .

    . , 2 , distance .

    //englarea.cpp #include struct Distance { int feet; float inches; }; struct Room { Distance length; Distance width; }; void main() { Room dining; Dining.length.feet=13; Dining.length.inches=6.5; Dining.width.feet=10;

  • 74

    Dining.width.inches=0.0; Float l=Dining.length.feet+Dining.length.inches/12; Float w=Dining.width.feet+Dining.width.inches/12; cout

  • 75

    Room distance . . distance {13, 6.5} , distance {10, 0.0} .

    .

    . Cardsharp 3 , . . .

    struct card { int number; int suit; }

    2- 14

    . 11- 14 , , , . 0- 3 . , , , .

    const int clubs=0; // const int diamonds=1; // const int hearts=2; // const int spades=3; // const int jack=11; // const int queen=12; //

  • 76

    const int king=13; // const int ace=14; // struct card { int number; int suit; } void main() { card temp, chosen, prize; int position; card card1={7, clubs}; cout

  • 77

    } if (chosen.number==prize.number && chosen.suit==prize.suit) cout

  • 78

    . . . Pascal .

    . .

    //dayenum.cpp #include enum days_of_week {Sun,Mon,Tue,Wed,Thu,Fri,Sat}; void main() { days_of_week day1, day2; day1=Mon; day2=Thu; int diff=day2 day1; cout

  • 79

    enum . , . . 7 . .

    . day1, day2 . . Day1 Mon , day2 Thu . . . :

    =3 1- 2- .

    . . 0, 1 . Sun 0, Sat 6 . day1 = 5 . .

    C++-

    .

  • 80

    .

    chcount . space ( ) . .

    //wdcount.cpp #include #include enum boolean {false,true}; void main() { boolean isWord=false; char ch=a; int wordcount=0; do { ch=getche(); if (ch== || ch==\r) { if (isWord) { wordcount++; isWord=false; } } else if (!isWord) isWord=true; } while (ch!=\r); cout

  • 81

    , . . isWord . isWord . if else , else .

    .

    //cardenum.cpp #include const int jack=11; // const int queen=12; // const int king=13; // const int ace=14; // enum Suit {clubs,diamonds,hearts,spades}; struct card { int number; Suit suit; } void main()

  • 82

    { card temp, chosen, prize; int position; card card1={7,clubs}; cout

  • 83

    0 . , 0- .

    Enum Suit {clubs=1,diamonds,hearts,spades};

    diamonds = 2, hearts = 3, spades

    = 4 . . , . :

    day1.suit=hearts; cout

  • 84

    . , . .

    , . .

  • 85

    44--

    --,,

    -- --

    -- --

    --

  • 86

    44--

    ,

    . .

    . . .

    Basic- , Pascal- .

    , (*) .

    //table.cpp #include void starline(); void main() { starline(); cout

  • 87

    } void starline() { for (int j=0; j

  • 88

    Void , . void . . (;) . prototype () .

    3 . .

    starline();

    ,

    . (;) . .. . , .

    . .

    void starline() { for (int j=0; j

  • 89

    . , , .

    . .

    //table2.cpp #include void starline() { for (int j=0; j

  • 90

    . .

    starline () . .

    //tablearg.cpp #include void repchar(char, int); void main() { repchar(-, 43); cout

  • 91

    . . repchar () 2 . .

    repchar(-, 43);

    ,

    . .

    ------------------------------------------- =========================================== char -128 to 127 int -32768 to 32767 double -2147483648 to 2147483647 -------------------------------------------

    , .

    void repchar(char, int);

    ,

    ch n . . , , .

  • 92

    . . .

    //vararg.cpp #include void repchar(char, int); void main() { char chin; int nin; cout > chin; cout > nin; repchar(chin, nin); } void repchar(char ch, int n) { for (int j=0; j

  • 93

    .

    . distance .

    //engldisp.cpp #include struct Distance { int feet; float inches; }; void engldisp(Distance); void main() { Distance d1, d2; Cout > d1.feet; Cout > d1.inches; Cout > d2.feet; Cout > d2.inches; Cout

  • 94

    { cout

  • 95

    ( char ch , int n ), . . . .

    . . .

    //convert.cpp #include float lbstokg(float); void main() { float lbs, kgs; cout > lbs; kgs=lbstokg(lbs); cout

  • 96

    :

    () : 182 () 82.553741

    . . void . lbstokg .

    . , .

    kgs=lbstokg(lbs);

    lbstokg kgs

    .

    rreettuurrnn lbstokg pounds

    . kilograms . return .

    return kilograms;

  • 97

    kgs

    . , . . (void ) int . :

    Somefunc();

    int

    .

    convert - convert2 .

    //convert2.cpp #include float lbstokg(float); void main() { float lbs; cout > lbs; cout

  • 98

    return 0.453592*pounds; }

    kgs

    . lbstokg kilograms , . .

    .

    //retstrc.cpp #include struct Distance { int feet; float inches; }; Distance addeng(Distance, Distance); void engldisp (Distance); void main() { Distance d1, d2, d3; Cout > d1.feet; Cout > d1.inches; Cout > d2.feet;

  • 99

    Cout > d2.inches; d3=addeng(d1, d2); Engldisp(d1); cout

  • 100

    . dd3 . main()- 3 . .

    . , . , . .

    . , . . .

  • 101

    .

    //ref.cpp #include void main() { void intfrac(float, float &, float &); float number, intpart, fracpart; do { cout > number; intfrac(number, intpart, fracpart); cout

  • 102

    . . Return . . , ( & ) . , . .. fpart fracpart . fpart fracpart . ( & ) . . , , . n number . , .

    .

    , . order .

  • 103

    //reforder.cpp #include void main() { void order(int &,int &); int n1=99, n2=11; int n3=22, n4=88; order(n1, n2); order(n3, n4); cout

  • 104

    numb1 n1- , numb2 n2- numb1 n3- , numb2 n4- . . , . . , .

    Distance

    () . : 6 8 0.5 4 3 .

    //referst.cpp #include struct Distance { int feet; float inches; }; void scale(Distance &, float); void engldisp(Distance); void main() { Distance d1={12, 6.5}, Distance d2={10, 5.5},

  • 105

    Cout

  • 106

    3 . .

    . , , .

    (*) 45

    starline() , repchar() . 45 charline() . 3 . 3 , help 3 . .

    //overload.cpp #include void repchar(); void repchar(char); void repchar(char, int);

  • 107

    void main() { repchar(); repchar(=); repchar(+, 30); } void repchar(); { for (int j=0;

  • 108

    .

    . Distance , float .

    //overengl.cpp #include struct Distance { int feet; float inches; }; void engldisp(Distance); void engldisp(float); void main() { Distance d1; float d2; cout > d1.feet; cout > d1.inches; cout > d2; cout

  • 109

    void engldisp(Distance); { cout

  • 110

    . . , .

    . , , , , , .

    . . . ( ), . , , .

  • 111

    . .

    //inline.cpp #include inline float lbstokg(float pounds) { return 0.453592 * pounds; } void main() { float lbs; cout > lbs; cout

  • 112

    .

    overload .

    } void repchar(char ch, int n); { for (int j=0;

  • 113

    . , .

    . Distance , float .

    //overengl.cpp #include struct Distance { int feet; float inches; }; void engldisp(Distance); void engldisp(float); void main() { Distance d1; float d2; cout > d1.feet; cout > d1.inches; cout > d2; cout

  • 114

    void engldisp(Distance); { cout

  • 115

    . . , .

    . , , , , , .

    . . . ( ), . , , .

  • 116

    . .

    //inline.cpp #include inline float lbstokg(float pounds) { return 0.453592 * pounds; } void main() { float lbs; cout > lbs; cout

  • 117

    .

    overload .

    //missarg.cpp #include void repchar(char=*, int=45); void main() { repchar(); repchar(=); repchar(+,30); } void repchar(char ch,int n); { for (int j=0; j

  • 118

    .

    void repchar(char ch=*, int n=45);

    ,

    . .. , repchar(30) . 2 . .

    repchar(, 30)

    ( 100 ). .

    . , , 3 .

  • 119

    .

    void somefunc() { int somevar; float othervar; // }

    . auto .

    void somefunc() { auto int somevar; auto float othervar; // }

    . .

  • 120

    ( ). somevar othervar somefunc . . , . , . . . , , .

    . , unknown variable ( ) . .

    void somefunc() { int somevar; float othervar;

  • 121

    somevar=10; othervar=123.45; nextvar=15; // } void main() { int nextvar; somevar=20; // othervar=987.65; // nextvar=35; }

    nextvar

    . somevar othervar . . .

    . 0 . . , . .

    int n=33;

  • 122

    .

    . . .

    //extern.cpp #include #include char ch=a; void getachar(); void putachar(); void main() { while (ch!=\r) { getachar(); putachar(); } } void getachar(); { ch=getch(); } void putachar(); { cout

  • 123

    } getachar

    . putachar . .

    I`m typing

    ch

    , . . .

    . . . , .

  • 124

    . , . 0 .

    . , . . ch , .

    .

    . 15 .

    . , , ( ) .

  • 125

    . getavg() .

    //static.cpp #include float getavg(float); void main() { float data=1, avg; while (data!=0) { cout > data; avg=getavg(data); cout

  • 126

    . , 0 . total, count .

    . ( ).

    .

    0 0

    .

  • 127

    .

    //retref.cpp #include int x; int& setx(); void main() { setx()=92; cout

  • 128

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

    int ( *ptr ) ()

    .

    int ( *ptr )

    int .

    int *ptr () ,

    int .

  • 129

    . . , . . , .

    . 1- N N- . , 1- 1, 2- 2, 3- 6, 7- 5040 . 0- 1 . N! .

    1! = 1 2! = 1*2 3! = 1*2*3 10! = 1*2*3*4*5*6*7*8*9*10 N! = 1*2*3**(N-1)*N

    .

    N! = 1*2*3**(N-1)*N = N*(N-1)**3*2*1 = N*(N-1)! N- (N-1)-

    N- . . N- (N-1)- . (N-1)- (N-2)-

  • 130

    . 1- 1 . .

    5! 5*4!

    4! 4*3!

    3! 3*2!

    2! 2*1!

    1! 1

    ,

    . .

    1! 1

    2! 2*1! = 2*1 = 2

    3! 3*2! = 3*2 = 6

    4! 4*3! = 4*6 = 24

    5! 5*4! = 5*24 = 120

    .

    void main() { int num; cout

  • 131

    cin >> num; if (num

  • 132

    ) )

    fact (4-

    )

    fact (5-

    ) 1 1

    main fact 5

    . 1- 5*fact(4) . fact 4 . 1- . 4*fact(3) fact 3 . fact 1 1 4 fact . 2*1 2 3 fact , 3*2=6 2 fact , 4*6=24 1 fact , 5*24=120 main .

    . , . , . (num

  • 133

    if (num

  • 134

    x GETS_VALUE_OF y;

    if (x==y)

    if (x EQUALS y) .

    #define .

    #define

    . \ . .. .

    #define GETS_VALUE_OF \

    =

    . .

  • 135

    . #define- . .

    #define EQUALS == #define EQ EQUALS

    . .

    #define NINE 9

    #define

    . .. , . , EQUALS == .

    #define . . . .

    #define PRESS_KEY \ cout

  • 136

    void main() { . . . . . PRESS_KEY; . . . . . }

    #define \ .

    . . .

    ++- . . , .

    #define PRINT(name, age) \ cout

  • 137

    PRINT(, 28); . . . . . }

    .

    #define MUL2(num) 2*num void main() { int a1,a2; a1=MUL2(5); a2=MUL2(2+3); }

    1- 10

    , 2- 10 7 . 2*2+3=7 . MUL2(2+3) 2*2+3 . . 7 . .

    #define MUL2(num) 2 * (num)

    2*2+3 , 2*(2+3) , 10 .

  • 138

    , , . .

    . , . . , .

    . . .

    . .

    . , .

    .

    .

  • 139

    , . . .

    . .

    . , .

    . #define . . , .

  • 140

    55--

    -- -- --

    -- --

    --tthhiiss

  • 141

    55--

    .

    (Object Oriented Programming) C++ 1983 New Jersey Murray Hill Bell Laboratories- . . ++ .

    C . : , , , , , , , .

    , . : , , , .

    , .

    (procedure oriented programming) . 40 . .

    . .

  • 142

    , . . (compiler) .

    . : , , , , . 1960- . ( Object Oriented Programming-OOP) . 1967 Simula-67 . . 1980- - Smalltalk . C, Pascal - C++, Pascal 5.5, Object Pascal .

    (Class) . . .

    . : . , 2 . .

    , .

  • 143

    . (inheritance - )

    child . parent . .

    . : .

    . (code) . , . , . .

  • 144

    . .

    #include class smallobj { private: int somedata; public: void setdata(int d) { somedata=d; } void showdata() { cout

  • 145

    ((CCoonnssttrruuccttoorrss))

    setdata . . . . .

    .

    class Counter { private: unsigned int count; public: Counter() {count = 0;} void inc_count() int get_count() }; . . . Counter c1; cout

  • 146

    ~ . :

    class Counter { private: unsigned int count; public: Counter() {count = 0;} ~Counter() { } void inc_count() int get_count() {return count;} };

    ,

    . .

    .

    #include class Distance { private: int feet; float inches; public: Distance() { } Distance(int ft, float in) { feet = ft; inches = in; } void getdist() { cout > feet;

  • 147

    cout > inches; } void showdist() {cout

  • 148

    . . . :

    dist3 = dist2

    . . Distance add_dist(Distance)

    . . static . . . .

    Class foo { private: static int count; public: foo() {count++} int getcount() {return count;} };

    .

  • 149

    -

    . .

    :

    dist3.add_dist(dist1, dist2); .

    dist3 = dist1 + dist2;

    , . .

    C++ +, *,

  • 150

    ~Counter() { } void inc_count() int get_count() {return count;} void operator ++() {count++;} };

    operator

    ++ .

    c1++; ++c1;

    .

    .

    .

    c1 = c2++;

    void

    . . :

    Counter operator ++() { count++; counter temp; temp.count = count; return temp; };

    temp

    . . .

    Counter() { count = 0;} Counter(int c) {count = c;}

    .

  • 151

    Counter operator ++ () { count++; return Counter(count); }

    Counter(int c) {count = c;}

    .

    . , , .

    .

    dist3.add_dist(dist1, dist2);

    + .

    dist3 = dist1 + dist2;

    add_dist +

    .

    class Distance { . . . Distance operator + (Distance);

  • 152

    . . . }; Distance Distance::operator + (Distance d2) { int f=feet+d2.feet; float i=inches+d2.inches; if (i>=12.0) { i-=12.0; f++; } return Distance(f, i); }

    +

    .

    dist3=dist1+dist2; dist1

    dist2 . 2 .

    dist4=dist1+dist2+dist3;

    . .

    . . . boolean operator < (Distance); . . . boolean Distance::operator < (Distance d2) { . . . }

  • 153

    .

    float f; int t; t=f;

    .

    t = int(f);

    .

    ,,

    . .

    .

    . . . operator float() { return (feet*12+inches)*2.54 } . . . float mtrs=float(dist2);

    . .

    Distance(float meters) { float fltfeet=meters*100/2.54/12; feet=int(fltfeet); inches=12*(fltfeetfeet);

  • 154

    } . . . Distance dist1 = 2.35; . . . dist1 = 1.0;

    . . 1 , .

    .

    objecta=objectb; objecta- destination, objectb- source

    . objecta- , objectb- .

    -

    . - (base class) (derived class) . . . (decrement --) . .

    #include

  • 155

    class Counter { protected: unsigned int count; public: Counter() { count = 0;} Counter(int c) {count = c;} int get_count() {return count;} Counter operator ++ () { count++; return Counter(count); } }; class CountDn : public Counter { public: Counter operator -- () { count--; return Counter(count); } }; void main() { CountDn c1; cout

  • 156

    protected .

    (protected) private . . private . private, protected 2 . Counter count private protected .

    . .

    class CountDn : public Counter { public: CountDn() : Counter() { } CountDn (int c) : Counter(c) { } Counter operator -- () { count--; return Counter(count); } };

    . .

  • 157

    . . . . (sign) .

    enum posneg{pos, neg}; class DistSign : public Distance { private: posneg sign; public: DistSign():Distance() { sign = pos; } DistSign(int ft, float in, posneg sg=pos) :Distance(ft, in) { sign = sg; } void getdist() { Distance::getdist(); char ch; cout > ch; sign=(ch=='+') ? pos:neg; } void showdist() { cout

  • 158

    Distance::showdist(); . .

    . abstract .

    PPuubblliicc PPrriivvaattee C++

    . :

    Distance : public DistSign . public

    public . private public . .

    class A { private: int privdataA; protected: int protdataA; public: int pubdataA; }; class B : public A { public: void funct()

  • 159

    { int a; a=privdataA; // a=protdataA; //ok a=pubdataA; //ok } }; class C : private A { public: void funct() { int a; a = privdataA; // a = protdataA; //ok a = pubdataA; //ok } }; void main { int a; B objB; a=objB.privdataA; // a=objB.protdataA; // a=objB.pubdataA; //ok C objC; a=objC.privdataA; // a=objC.protdataA; // a=objC.pubdataA; // }

    .

  • 160

    (Multiple Inheritance) . .

    class A {}; class B {}; class C : public A, public B {};

    ? .

    . , , , (24), .

    . ,

    . (Type) .

    Distance . (Lumber)

    .

    . .

    .

    #include #include

  • 161

    const int LEN = 40; class Type { private: char dimensions[LEN]; char grade[LEN]; public: Type() { strcpy(dimensions, " "); strcpy(grade, " "); } Type(char di[], char gr[]) { strcpy(dimensions, di); strcpy(grade, gr); } void gettype () { cout > dimensions; cout grade; } void showtype() { cout

  • 162

    public: Distance() { feet=0; inches=0.0;} Distance(int ft, float in) { feet=ft; inches=in;} void getdist() { cout > feet; cout > inches; } void showdist() { cout

  • 163

    cout

  • 164

    }; class B { public: void show() {cout

  • 165

    .

    #include class Base { public: void show() { cout

  • 166

    Base Base

    . .

    #include class Base { public: virtual void show() { cout

  • 167

    ptr Derv1, Derv2 . show . .

    Base show . . .

    virtual void show()=0;

    0

    .

    Base . . (abstract) .

    VViirrttuuaall

  • 168

    . .

    class Parent { protected: int Basedata; }; class Child1 : public Parent { }; class Child2 : public Parent { }; class GrandChild : public Child1, public Child2 { public: int getdata() {return Basedata;} };

    getdata Basedata

    . Child1, Child2 Parent Parent .

    Parent

    Child1 Child2

    GrandChild

  • 169

    subobject . Child1, Child2 Basedata GrandChild ambiguity . . .

    . . . class Child1 : virtual public Parent { }; class Child2 : virtual public Parent { }; class GrandChild : public Child1, public Child2 { public: int getdata() {return Basedata;} };

    .

    (share) . Basedata .

    private

    protected . . private . private . . ?

    (friend) . .

    #include

  • 170

    class beta; class alpha { private: int data; public: alpha() { data = 3; } friend int frifunc(alpha, beta); }; class beta { private: int data; public: beta() {data = 7; } friend int frifunc(alpha, beta); }; int frifunc(alpha a, beta b) { return (a.data + b.data); } void main() { alpha aa; beta bb; cout

  • 171

    . . Distance + . .

    Distance d1, d2, d3; d1=d2+10.0;

    + d2 10.0 . 10.0 . .

    d3 = 10.0 + d2;

    10.0 .

    .

    class Distance { . . . friend Distance operator+(Distance,Distance); . . . }; Distance operator+(Distance d1,Distance d2) { int f=d1.feet+d2.feet; float i=d1.inches+d2.inches; if (i>=12.0) { i-=12.0; f++; }; return Distance(f, i); }

    .

  • 172

    private . . .

    #include class alpha { private: int data1; public: alpha() { data1 = 99; } friend class beta; }; class beta { public: void func1(alpha a) {cout

  • 173

    .

    . . . . :

    class gamma { private: static int total; public: static showtotal(); { cout

  • 174

    alpha a2(a1); ,

    (copy constructor) . .

    alpha a2=a1;

    . .

    #include #include class String { private: char *str; public: String() { str = NULL;} String(char *s) { str=new char[strlen(s)+1]; strcpy(str, s); } void changestr() { delete str; str = " "; } void showstr() { cout

  • 175

    s2=s1; s2.showstr(); s1.changestr(); s2.showstr(); }

    2 s2.showstr()

    s1 . s2 = s1

    s1, s2 . changestr s1, s2 . . , .

    .

    void operator = (String& s) { if (str == NULL) delete str; str = new char[strlen(s.str)+1]; strcpy(str, s.str); }

    s1 = s2 s1

    s2 str . .

    .

    .

  • 176

    . . .

    String(String& s) { if (str == NULL) delete str; str = new char[strlen(s.str)+1]; strcpy(str, s.str); }

    String(String s) .

    tthhiiss

    this . this .

    .

    bool compare(char *str) { return strcmp(this->str, str); }

    .

    String& operator = (String& s) { if (str==NULL) delete str; str=new char[strlen(s.str)+1]; strcpy(str, s.str); return *this; }

  • 177

    . , . .

    . . .

    . . .

    . child class, parent class .

    . . ~ .

    , . . .

    . . static .

    . .

    . .

    .

  • 178

    3 . private , .. . public . protected .

    . .

    this . .

  • 179

    66--

    --

    -- --

    --

  • 180

    66--

    . . C++- . . , . , , . .

    Pascal, Basic . , . . string .

  • 181

    . .

    //replay.cpp #include void main() { int age[4]; cout

  • 182

    . . . . 4 . .

    . . , . (4 * 2 ) 8 . 0 . (n-1) (n !). 0-3 4 .

  • 183

    2 . .

    cin >> age[j];

    .

    cout

  • 184

    total+=sales[j]; float average=total/SIZE; cout

  • 185

    . .

    //days.cpp #include void main() { int month, day, total_days; int days_per_month[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; cout > month; cout > day; total_days=day; for (int j=0; j

  • 186

    4 (31, 28, 31, 30) .

    . .

    , .

    int days_per_month[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    0 . .

    . , . .

    //salemon.cpp #include #include const int districts=4; const int months=3; void main() {

  • 187

    int d, m; float sales[districts][months]; cout

  • 188

    1 2 3 1 3964.23 4135.87 4397.98 2 867.75 923.59 1037.01 3 12.77 378.32 798.22 4 2983.53 3983.73 9494.98

    .

    float sales[districts][months];

    . , sales districts months . 3 .

    2 .

    sales[d][m]

    . sales[ d, m ] .

    salemon

    . . : 2 ,

  • 189

    .

    C++- . setw() . .

    cout

  • 190

    . . saleinit .

    //saleinit.cpp #include #include const int districts=4; const int months=3; void main() { int d, m; float sales[districts][months]= {{1432.07, 234.50, 654.01}, {322.00, 13838.32,17589.88}, {9328.34, 934.00, 4492.30}, {12838.29, 2332.63, 32.93}}; cout

  • 191

    , , .

    . .

    //salefunc.cpp #include #include const int districts=4; const int months=3; void display(float[districts][months]); void main() { float sales[districts][months]= {{1432.07, 234.50, 654.01}, {322.00, 13838.32, 17589.88}, {9328.34, 934.00, 4492.30}, {12838.29, 2332.63, 32.93}}; display(sales); } void display(float funsales[districts][months]) { int d, m; cout

  • 192

    for (m=0; m

  • 193

    , . . .. funsales sales .

    struct .

    //partaray.cpp #include const int size=4; struct part { int ModelNumber; int PartNumber; float cost; } void main() { int n; part apart[size]; for (n=0; n apart[n].PartNumber; cout > apart[n].cost; }

  • 194

    for (n=0; n

  • 195

    apart[n].ModelNumber

    . : (stack) .

    . , . .

    MS-DOS . . Stackaray .

    //Stackaray.cpp #include const int max=100; class Stack { private: int st[max]; int top;

  • 196

    public: Stack() { top=0; } void push(int var) { st[++top]=var; } int pop() { return st[top--]; } }; void main() { Stack s1; s1.push(11); s1.push(22); cout

  • 197

    3: 66 4: 55 5: 44 6: 33

    .

    . push top- .

    st[++top]=var;

    pop

    top- .

    return st[top--];

    Stack

    OOP(Object Oriented Programming )- . (queue), (set), (linked list) .

    . .

  • 198

    DDiissttaannccee Distance

    . .

    //englaray.cpp #include const int max=100; class Distance { private: int feet; float inches; public: void getdist() { cout > feet; cout > inches; } void showdist() { cout

  • 199

    cout

  • 200

    , .

    do

    . max 100 . 100- ? . . , . , .

    . do .

    If (n>=max) { cout

  • 201

    . 100 . .

    dist[j].showdist();

    . , . getdist .

    dist[n++].getdist();

    (increment operator) . n++ n- .

    . CARDOBJ . card

  • 202

    52 .

    //cardaray.cpp #include #include #include enum Suit { clubs, diamonds, hearts, spades }; const int jack=11; const int queen=12; const int king=13; const int ace=14; enum Boolean { false, true }; class card { private: int number; Suit suit; public: card() { }; void init(int n, Suit s) { Suit=s; number=n; } void display(); Boolean isEqual(card); }; void card::display() { if (number>=2 && number

  • 203

    case ace: cout

  • 204

    cout

  • 205

    3 J 5 K Q 10 8 2 6 A 4 J 8 10 2 Q 10 5 A K 7 5 A 2 9 7

    . . C++- . , . randomize() . stdlib.h, time.h 2 .

    random() . . 0- 51- random (52) . , .

    . .

  • 206

    .

    //stringin.cpp #include const int max=80; void main() { char str[max]; cout > str; cout

  • 207

    . . C++- .

    //safetyin.cpp #include #include const int max=20; void main() { char str[max]; cout > setw(max) >> str; cout

  • 208

    char str[]=Thou art too dear for my possessing; cout > var1 >> var2 >> var3;

    .

    15 25 35

  • 209

    . var1 15, var2 25, var3 35 . . space .

    //blanksin.cpp #include const int max=80; void main() { char str[max]; cout

  • 210

    void main() { cout

  • 211

    { char str1[]= ; char str2[max]; for (int j=0; j

  • 212

    void main() { char str1[]= ; char str2[max]; strcpy(str2, str1); cout

  • 213

    .

    . , . . ( )

    //strpart.cpp #include #include class part { private: char partname[30]; int partnumber; float cost; public: void setpart(char pname[], int pn, float c) {

  • 214

    strcpy(partname, pname); partnumber=pn; cost=c; } void showpart() { cout

  • 215

    C++-

    . ,

    str2=str1 .

    . string .

    //strobj.cpp #include #include const int sz=80; class String { private: char str[sz]; public: String() { str[0]=\0; } String(char s[]) { strcpy(str, s); } void display() { cout

  • 216

    void main() { String s1( !); String s2= !; String s3; cout

  • 217

    display , concat .

    String s3( !); String s2= !; s3=s1; s3.concat(s2); cout

  • 218

    . , 10 3 , 7 . .

    2 , . 4 , 7 ( 0 ). .

    char Aimags1[ 3 ][ 11 ]= { , , - }; char *Aimags2[ 3 ]= { , , - };

    . 7- . .

    Aimags1[0][2] Aimags2[0][2]

    .

    .

    Aimags1[0][2]

  • 219

    0 Aimags1 1- . 0*11 . Aimags1 11 . 2 2 3 .

    2 .

    Aimags2[0][2] 0 Aimags2

    1- 0*4 . 4 . 2 2 .

  • 220

    . . , . . . . , 2 .

    . .

    . 0- null . string.h . 2 . .

  • 221

    77--

    --

    -- --

    -- --

    --

  • 222

    77--

    Turbo C++ Borland C++-

    . . .. , . .

    Turbo C++- 2 .

    . ( ) . IBM- .

    CGA, EGA, VGA, SVGA . ( ) , , , .

    . .

  • 223

    , .

    . 80 25 . ( 4025, 8043- ) cout . . .

    , MS-DOS . UNIX , Turbo Borland C++- .

    WWiinnddooww(())

    conio.h . 4 , , , . . ( .)

  • 224

    . (scroll) .

    , . .

    //window.cpp #include #include const int len=500; class box { private: int left, top, right, bottom; char str[len]; public: box(int l, int t, int r, int b, char s[]) { left=l; top=t; right=r; bottom=b; strcpy(str, s); window(left, top, right, bottom); cputs(str); } void erase() { window(left, top, right, bottom); clrscr(); } }; void main()

  • 225

    { clrscr(); char s1[]=The disk drive is jammed\n\r\ or on fire, or the CPU\n\r\ is flooded. Correct the\n\r\ situation and try again. \n\r\ Press Enter to continue: ; char s2[]=This is a string that will wrap at the\ right edge of the window, probably\ breaking words inappropriately in the\ process.; box a(25, 6, 55, 18, s1); getch(); a.erase(); box b(1, 1, 25, 10, s2); getch(); }

    box 5

    . 4 4 , .

    ccppuuttss(()) cout-

    . , . . cputs() .

  • 226

    \n . , 13- ( ), 10- ( ) 2 . , 2 . \n 13- . cputs() \n . \r 10- . \n\r .

    ccllrrssccrr(()) clrscr()

    . 2 . ( ) . box erase() .

    2 , 2 . .

    bbooxx

  • 227

    box . , . box . IBM- . .

    //boxes.cpp #include #include const int LEFT=25; const int TOP=6; const int RIGHT=55; const int BOTTOM=18; const int LEN=500; class box { private: int left, top, right, bottom; char str[len]; int color; public: box() { left=LEFT; top=TOP; right=RIGHT; bottom=BOTTOM; color=WHITE; } box(int l, int t, int r, int b, int c=WHITE) { left=l; top=t; right=r; bottom=b; color=c; } void text(char s[]) { strcpy(str, s); } void draw();

  • 228

    void erase() { window(left, top, right, bottom); clrscr(); } }; void box::draw() { erase; window(left, top, right, bottom+1); int width=right-left+1; int height=bottom-top+1; textcolor(color); for (int j=1; j

  • 229

    clrscr(); box a; box b(1, 1, 31, 8, YELLOW); a.text(\The time, \ said the \n\r\ Duchess, \is surely ripe;\n\r\ make haste, lest seconds\n\r\ spoil.\); b.text(Should you continue \n\r\ along the preset path you\n\r\ risk investigation\n\r\ for felonious chicanery.); a.draw(); getch(); a.erase(); b.draw(); getch(); b.erase(); b.text(); b.draw(); getch(); }

    box 2

    . . , . text() . , , . draw() , erase() . draw() .

    gotoxy() .

  • 230

    . , .

    putch() . . .

    5 5 . draw() textcolor() . .

    0 BLACK() 1 BLUE() 2 GREEN() 3 CYAN() 4 RED() 5 MAGENTA() 6 BROWN() 7 LIGHTGRAY( ) 8 DARKGRAY( ) 9 LIGHTBLUE( ) 10 LIGHTGREEN( ) 11 LIGHTCYAN() 12 LIGHTRED( ) 13 LIGHTMAGENTA( ) 14 YELLOW() 15 WHITE()

    128 BLINK()

  • 231

    . textbackground() . 8 .

    draw() window() . , . bottom . . window() . . . Press any key to continue: 3 window() .

    . 2000 (8025) 307200 (640480) . , .

  • 232

    graphics.lib Options Libraries linker- . .

    . . ezball . set() , draw() . ball RAD .

    //ezball.cpp #include #include const int RAD=75; class ball { private: int xCo, yCo; public: ball() { xCo=0; yCo=0; } void set(int x, int y) { xCo=x; yCo=y; } void Draw() { circle(xCo, yCo, RAD); } }; void main() { int driver, mode;

  • 233

    driver=DETECT; initgraph(&driver, &mode, C:\\BORLANDC\\BGI); ball b1; ball b2; ball b3; b1.set(320, 150); b2.set(255, 150); b3.set(385, 150); b1.draw(); b2.draw(); b3.draw(); getch(); closegraph(); }

    iinniittggrraapphh(())

    . initgraph() .

    2 . , . . . .

  • 234

    initgraph() . . .

    CGA CGA EGA EGA VGA VGA

    graphics.h . EGA EGA VGA EGA, CGA, VGA .

    . . .

    CGAC0 320x200, 4 , 0 CGAC1 320x200, 4 , 1 CGAC2 320x200, 4 , 2 CGAC3 320x200, 4 , 3

    CGA

    CGAHI 640x200, 2 EGALO 640x200, 16

    EGA EGAHI 640x350, 16

    VGA VGALO 640x200, 16

  • 235

    VGAMED 640x350, 16 VGAHI 640x480, 16

    . CGA 4 4 . EGA VGA . .

    int driver=EGA; int mode=EGAHI; initgraph(&driver, &mode, c:\\borlandc\\bgi);

    . DETECT . .

    initgraph()

    2 . , , & . .

    ,

  • 236

    . . 6- .

    initgraph() 3

    . . C++- {C++- } \BGI . (C++- C:\BORLANDC . C:\\BORLANDC \\BGI .) *.BGI . CGA CGA.BGI , EGA VGA EGAVGA.BGI .

    . ( ) . ( \ ) C++- (\n ..) 2 , . C++- , . .

  • 237

    cciirrccllee(()) ball 3

    , set() . draw() . draw() C++- circle() .

    circle() 3 2 , 3 . RAD , xCo, yCo .

    circle(xCo, yCo, RAD);

    cclloosseeggrraapphh(())

    . , .

    , closegraph() .

  • 238

    . .

    //colorbal.cpp #include #include const int RAD=75; class ball { private: int xCo, yCo; int linecolor; int fillcolor; public: ball() { xCo=0; yCo=0; linecolor=WHITE; fillcolor=WHITE; } void set(int x, int y, int lc, int fc) { xCo=x; yCo=y; linecolor=lc; fill color=fc; } void Draw() { setcolor(linecolor); setlinestyle(SOLID_LINE, 0, THICK_WIDTH); circle(xCo, yCo, RAD); setfillstyle(SOLID_FILL, fillcolor); floodfill(xCo, yCo, linecolor); } }; void main() { int driver, mode; driver=DETECT; initgraph(&driver, &mode, C:\\BORLANDC \\BGI);

  • 239

    ball b1; ball b2; ball b3; b1.set(100, 150, YELLOW, RED); b2.set(200, 150, YELLOW, GREEN); b3.set(300, 150, YELLOW, BLUE); b1.draw(); b2.draw(); b3.draw(); getch(); closegraph(); }

    ball set()

    4 . 2 , 2 . draw() .

    setcolor(linecolor); setlinestyle(SOLID_LINE, 0, THICK_WIDTH); circle(xCo, yCo, RAD); setfillstyle(SOLID_FILL, fillcolor); floodfill(xCo, yCo, linecolor);

    .

    sseettccoolloorr(())

    . line ()

  • 240

    , . .

    EGA VGA 16 . . ball .

    sseettlliinneessttyyllee(())

    , . .

    0 SOLID_LINE() 1 DOTTED_LINE() 2 CENTER_LINE( ) 3 DASHED_LINE() 4 USERBIT_LINE( )

    . 4 . 0 . 3 .

    0 NORM_WIDTH(1 ) 3 THICK_WIDTH(3 )

  • 241

    sseettffiillllssttyyllee(())

    . . .

    0 EMPTY_FILL() 1 SOLID_FILL() 2 LINE_FILL(---) 3 LTSLASH_FILL(///()) 4 SLASH_FILL(///()) 5 BKSLASH_FILL(\\\()) 6 LTBKSLASH_FILL(\\\()) 7 HATCH_FILL(+++) 8 XHATCH_FILL(XXX) 9 INTERLEAVE_FILL(3 ) 10 WIDE_DOT_FILL(. . . . .) 11 CLOSE_DOT_FILL(. . . . . . . . .) 12 USER_FILL()

    2

    . . 3 , , 3 .

    ffllooooddffiillll(()) floodfill()

    . setfillstyle() , .

  • 242

    4 . 4 . .

    .

    //restline.cpp #include #include const int w=75; class rect { private: int xCo, yCo; int linecolor; int fillcolor; public: rect() { xCo=0; yCo=0; linecolor=WHITE; fillcolor=WHITE; } void set(int x, int y, int lc, int fc) { xCo=x; yCo=y; linecolor=lc; fill color=fc; } void Draw() { setcolor(linecolor);

  • 243

    setlinestyle(SOLID_LINE, 0, THICK_WIDTH); rectangle(xCo-w, yCo-w, xCo+w, yCo+w); setfillstyle(SOLID_FILL, fillcolor); floodfill(xCo, yCo, linecolor); line(xCo-w, yCo+w, xCo+w, yCo-w); } }; void main() { int driver, mode; driver=DETECT; initgraph(&driver, &mode, C:\\BORLANDC \\BGI); rect r1; rect r2; rect r3; r1.set(80, 150, YELLOW, RED); r2.set(250, 150, YELLOW, GREEN); r3.set(420, 150, YELLOW, BLUE); r1.draw(); r2.draw(); r3.draw(); getch(); closegraph(); }

    2

    .

    rreeccttaannggllee(()) .

    4 .

    rectangle(left, top, right, bottom);

  • 244

    lliinnee(()) 2 .

    2 , 2 .

    line(x1, y1, x2, y2);

    , ,

    .

    ball rect . fillpoly() .

    //multshap.cpp #include #include const int w=75; class shape { protected: int xCo, yCo; int linecolor; int fillcolor; public: shape() {

  • 245

    xCo=0; yCo=0; linecolor=WHITE; fillcolor=WHITE; } void set(int x, int y, int lc, int fc) { xCo=x; yCo=y; linecolor=lc; fill color=fc; } void Draw() { setcolor(linecolor); setlinestyle(SOLID_LINE, 0, THICK_WIDTH); setfillstyle(SOLID_FILL, fillcolor); } }; class ball : public shape { public: ball() : shape() { } void set(int x, int y, int lc, int fc) { shape::set(x, y, lc, fc); } void Draw() { shape::draw(); circle(xCo, yCo, w); floodfill(xCo, yCo, linecolor); } }; class rect : public shape { public: rect() : shape() { } void set(int x, int y, int lc, int fc) { shape::set(x, y, lc, fc); } void Draw() { shape::draw(); rectangle(xCo-w, yCo-w, xCo+w, yCo+w); floodfill(xCo, yCo, linecolor);

  • 246

    moveto(xCo-w, yCo+w); lineto(xCo+w, yCo-w); } }; class tria : public shape { public: tria() : shape() { } void set(int x, int y, int lc, int fc) { shape::set(x, y, lc, fc); } void Draw() { int triarray[]={ xCo, yCo-w, xCo+w, yCo+w, xCo-w, yCo+w }; shape::draw(); fillpoly(3, triarray); } }; void main() { int driver, mode; driver=DETECT; initgraph(&driver, &mode, C:\\BORLANDC\\BGI); ball b1; rect r2; tria t3; b1.set(80, 150, YELLOW, RED); r2.set(250, 150, YELLOW, GREEN); t3.set(420, 150, YELLOW, BLUE); b1.draw(); r2.draw(); t3.draw(); getch(); closegraph(); }

  • 247

    sshhaappee 2 ball rect

    . . set() draw() .( draw() .)

    C++- . shape , ball rect . shape 2 .

    tria

    . shape . . .. . . , 4 .

    int polyarray[]={x1, y1, x2, y2, x3, y3, x4, y4}

  • 248

    ,

    4 .

    2 . . . ( , . )

    fillpoly(4, polyarray);

    2

    . 2 3 , 3 4 . 4 1 , . . . drawpoly() .

    . 3 777 . C++- .

  • 249

    3 . 4 . : , , , . . , , . .

    //slot.cpp #include #include #include #include #include const int w=15; const int mar=10; class shape { protected: int xCo, yCo; int linecolor; int fillcolor; public: shape() { xCo=0; yCo=0; linecolor=WHITE; fillcolor=WHITE; } void set(int x, int y, int lc, int fc) { xCo=x; yCo=y; linecolor=lc; fill color=fc; } void draw()

  • 250

    { setcolor(linecolor); setlinestyle(SOLID_LINE, 0, THICK_WIDTH); setfillstyle(SOLID_FILL, fillcolor); } }; class ball : public shape { public: ball() : shape() { } void set(int x, int y, int lc, int fc) { shape::set(x, y, lc, fc); } void draw() { shape::draw(); circle(xCo, yCo, w); floodfill(xCo, yCo, linecolor); } }; class rect : public shape { public: rect() : shape() { } void set(int x, int y, int lc, int fc) { shape::set(x, y, lc, fc); } void draw() { shape::draw(); rectangle(xCo-w, yCo-w, xCo+w, yCo+w); floodfill(xCo, yCo, linecolor); moveto(xCo-w, yCo+w); lineto(xCo+w, yCo-w); } }; class tria : public shape { public: tria() : shape()

  • 251

    { } void set(int x, int y, int lc, int fc) { shape::set(x, y, lc, fc); } void draw() { int triarray[]={ xCo, yCo-w, xCo+w, yCo+w, xCo-w, yCo+w }; shape::draw(); fillpoly(3, triarray); } }; class noshape : public shape { public: void erase() { int border[]={ xCo-w-mar, yCo-w-mar, xCo+w+mar, yCo-w-mar, xCo+w+mar, yCo+w+mar, xCo-w-mar, yCo+w+mar }; setfillstyle(SOLID_FILL, DARKGRAY); fillpoly(4, border); } }; class cherry : public ball, public noshape { public: cherry() : ball() { } void set(int x, int y) { ball::set(x, y, WHITE, RED); noshape::set(x, y, WHITE, RED); } void draw() { erase(); ball::draw(); } }; class grape : public ball, public noshape {

  • 252

    public: grape() : ball() { } void set(int x, int y) { ball::set(x, y, WHITE, BLUE); noshape::set(x, y, WHITE, BLUE); } void draw() { erase(); ball::draw(); } }; class square : public rect, public noshape { public: square() : rect() { } void set(int x, int y) { rect::set(x, y, WHITE, GREEN); noshape::set(x, y, WHITE, GREEN); } void draw() { erase(); rect::draw(); } }; class pyramid : public tria, public noshape { public: pyramid() : tria() { } void set(int x, int y) { tria::set(x, y, WHITE, GREEN); noshape::set(x, y, WHITE, GREEN); } void draw() { erase(); tria::draw(); } }; class wheel : public shape { private:

  • 253

    cherry ch; grape gr; square sq; pyramid py; public: wheel() { xCo=0; yCo=0; } void set(int x, int y) { xCo=x; yCo=y; ch.set(xCo, yCo); gr.set(xCo, yCo); sq.set(xCo, yCo); py.set(xCo, yCo); } void draw(); }; void wheel::draw() { setcolor(WHITE); rectangle(xCo-w-mar, yCo-w-mar, xCo+w+mar, yCo+w+mar); switch(random(4)) { case 0 : ch.draw(); break; case 1 : gr.draw(); break; case 2 : sq.draw(); break; case 3 : py.draw(); break; } } void main() { const int number=60; int driver, mode; driver=DETECT; initgraph(&driver, &mode, c:\\borlandc\\bgi); randomize(); wheel w1;

  • 254

    wheel w2; wheel w3; w1.set(100, 100); w2.set(160, 100); w3.set(220, 100); for (int j=0; j

  • 255

    set() .

    void cherry::set(int x, int y) { ball::set(x, y, WHITE, RED); noshape::set(x, y, WHITE, RED); }

    . . noshape . erase() .

    ball . noshape . noshape shape .

    wheel shape- . , .

    wheel 3

    . set() . wheel cherry, grape, square, pyramid

  • 256

    . set() , . draw() , . draw() , . , . , .

    C++-

    . : 1. 2. 3.

    .

    sound(100); delay(20); nosound(); dos.h

    . sound()

    . 15- 3000

  • 257

    . .

    delay() . delay() . (1/1000 ) . .. delay(500) .

    nosound() .

    . . .

    . , , , . .

  • 258

    gstring . . : , , , , , .

    . .

    //grast.cpp #include #include #include class gstring { protected: char str[80]; int xCo, yCo; int font; int direction; int size; int color; int horzjustify; int vertjustify; int multx, divx; int multy, divy; public: gstring() { str[0]=\0; xCo=0; yCo=0; font=DEFAULT_FONT; direction=HORIZ_DIR; size=4; color=WHITE;

  • 259

    horzjustify=LEFT_TEXT; vertjustify=TOP_TEXT; multx=1; divx=1; multy=1; divy=1; } void drawtext() { moveto(xCo, yCo); settextstyle(font, direction, size); setcolor(color); settextjustify(horzjustify, vertjustify); setusercharsize(multx,divx,multy,divy); outtext(str); } void settext(char s[]) { strcopy(str, s); } void setposition(int x, int y) { xCo=x; yCo=y; } void SetFont(int f) { font=f; } void SetDirection(int d) { Direction=d; } void SetSize(int s) { size=s; } void SetColor(int c) { color=c; } void SetHJust(int hj) { horzjustify=hj; } void SetVJust(int vj) { vertjustify=vj; } void SetHorzSize(int m, int d) { size=0; multx=m; divx=d; } void SetVertSize(int m, int d) { size=0; multy=m; divy=d; } }; void main() { int driver, mode; driver=DETECT; initgraph(&driver, &mode, c:\\borlandc\\bgi); gstring s1, s2, s3, s4, s5, s6;

  • 260

    s1.SetText( ); s2.SetText( ); s2.SetFont(GOTHIC_FONT); s2.SetPosition(0, 75); s3.SetText( ); s3.SetFont(SANS_SERIF_FONT); s3.SetPosition(600, 0); s3.SetDirection(VERT_DIR); s4.SetText( ); s4.SetFont(TRIPLEX_FONT); s4.SetPosition(300, 150); s4.SetHJust(CENTER_TEXT); s5.SetText( ); s5.SetFont(SANS_SERIF_FONT); s5.SetSize(6); s5.SetPosition(300, 225); s5.SetDirection(CENTER_TEXT); s6.SetText( ); s6.SetFont(TRIPLEX_FONT); s6.SetPosition(0, 300); s6.SetHorzSize(2, 3); s6.SetVertSize(4, 1); s1.drawText(); s2.drawText(); s3.drawText(); s4.drawText(); s5.drawText(); s6.drawText(); getch(); closegraph(); }

    gsrting 6

    .

  • 261

    . 2 sans_serif . 3 4 5 . 6 .

    drawtext() . .

    mmoovveettoo(()) C++

    (Current Position) . . (0, 0) .

    sseetttteexxttssttyyllee(()) ,

    . C++ 5 .

    DEFAULT_FONT TRIPLEX_FONT Times SMALL_FONT SANS_SERIF_FONT Helvetica GOTHIC_FONT

  • 262

    initgraph()- 3- . CHR . : TRIP.CHR Triplex , SANS.CHR Sans Serif Font .

    2 HORIZ_DIR () VERT_DIR () . , .

    . 1 2 1 2 .

    sseetttteexxttjjuussttiiffyy(())

    . , .

    LEFT_TEXT

    (. ) CENTER_TEXT

    (. ) RIGHT_TEXT

    (. )

    TOP_TEXT

  • 263

    CENTER_TEXT

    BOTTOM _TEXT

    sseettuusseerrcchhaarrssiizzee(())

    , . 0 . 4 . 4 . . : 2 5 2 5/2 ( 4 ) 10 . 2 .

    oouutttteexxtt(())

    . . .

  • 264

    C++-

    2 . , . window() .

    . initgraph() closegraph() .

    C++- , , , , . , , , .

  • 265

    88--

    --

    -- --

    -- -- -- --

  • 266

    88--

    , C++- . .

    .

    - -

    -

    - - ,

    C++- . . C++- . , .

    C++- . . , , .

    C- .

  • 267

    new delete , , .

    .

    . . .. 0- 1, 2, 3 . 640KB 655359-, 1MB 1048575- .

    . . .

    &&

    . varaddr .

    //varaddr.cpp #include void main() { int var1=11; int var2=22;

  • 268

    int var3=33; cout

  • 269

    8 .

    . stack- ( ) . heap .

    & & (4 ) .

    . . , , . .

    . .. int int- . .

    //ptrvar.cpp #include void main() { int var1=11; int var2=22;

  • 270

    cout

  • 271

    Float* fptr;

    var1 11

    . ptr 0x8f4ffff4 .

    . . , . . ptrvar

    Ptr=&var1;

    .

    var1 .

    . , ( ) , . . .

    . ?.

  • 272

    . ptracc .

    //ptracc.cpp #include void main() { int var1=11 ; int var2=22 ; int *ptr ; ptr=&var1 ; cout

  • 273

    //ptrto.cpp #include void main() { int var1, var2; int *ptr; ptr=&var1; *ptr=37; var2=*ptr; cout

  • 274

    VVOOIIDD

    . . , int float . . void .

    void *ptr;

    .

    //ptrvoid.cpp #include void main() { int intvar; float flovar; int *ptrint; float *ptrflo; void *ptrvoid; ptrint=&intvar; //ptrint=&flovar; // //ptrflo=&intvar; // ptrint=&flovar; ptrvoid=&intvar; ptrvoid=&flovar; }

  • 275

    Ptrvoid

    .

    . 8 . arrnote .

    //arrnote.cpp #include void main() { int intarray[5]={31, 54, 77, 52, 93}; for (int j=0; j

  • 276

    //ptrnote.cpp #include void main() { int intarray[5]={31, 54, 77, 52, 93}; for (int j=0; j

  • 277

    . . Intarray intarray , . Intarray intarray++ 7++ ( windows . .). .

    //ptrinc.cpp #include void main() { int intarray[5]={31, 54, 77, 52, 93}; int *ptrint; ptrint=intarray; for (int j=0; j

  • 278

    . . .

    . .

    //passref.cpp #include void main() { void centimize(double &); double var=10.0 cout

  • 279

    var=10 inches var=25.4 centimeters

    .

    //passptr.cpp #include void main() { void centimize(double *); double var=10.0 cout

  • 280

    5

    . . .

    //passarr.cpp #include const int max=5; void main() { void centimize(double *); double varray[max]={10.0, 43.1, 95.9, 59.7, 87.3}; centimize(varray); for (int j=0; j

  • 281

    *ptrd++ 2 ? , ? . . (++) (*) .

    2 . 4 reforder

    . 2 2 1 .

    //ptrorder.cpp #include void main() { void order(int*, int*); int n1=99, n2=11; int n3:=22; n4:=88; order(&n1, &n2); order(&n3, &n4); cout

  • 282

    void order(int* numb1, int* numb2) { if (*numb1 > *numb2) { int temp=*numb1; *numb1=*numb2; *numb2=temp; } }

    Order reforder

    . .

    n1=11; n2=99; n3=22; n4=88;

    order ptrsort

    .

    //ptrsort.cpp #include void main() { void bsort(int*, int); const int N=10; int arr[N]={37,84,62,91,11,65,57,28,19,49}; bsort(arr, N); for (int j=0; j

  • 283

    void order(int*, int*) int j, k ; for (j=0; j < N-1; j++) for (k=j+1; k < n; k++) order(ptr+j, ptr+k); } void order(int* numb1, int* numb2) { if (*numb1 > *numb2) { int temp=*numb1; *numb1=*numb2; *numb2=temp; } }

    .

    11 19 28 37 49 57 62 65 84 91

    char . .

    2 .

  • 284

    //twostr.cpp #include void main() { char str1[]= ; char *str2= ; cout

  • 285

    .

    //ptrstr.cpp #include void main() { void dispstr(char *); char str[]= ; dispstr(str); } void dispstr(char * ps) { cout

  • 286

    //copystr.cpp #include void main() { void copystr(char *, char *); char* str1= \ .; char str2[80]; copystr(str2, str1); cout

  • 287

    2 . const src . dest . copystr .

    . .

    5 . . .

    .

    //ptrtostr.cpp #include const int DAYS=7; void main() { char* arrptrs[DAYS]={, , , , , , } ; for (int j=0; j < DAYS; j++) cout

  • 288

    . , .

    .. NNeeww DDeelleettee

    . : Int arr[100] 100

    200 , arr . . . :

    cin >> size; int arr[size];

  • 289

    . .

    NNEEWW

    , . Newintro .

    //newintro.cpp #include #include void main() { char *str= .; int len=strlen(str) ; char *ptr; ptr=new char[len+1]; strcpy(ptr, str); cout

  • 290

    strcpy .

    DDEELLEETTEE new

    , , . . delete .

    Delete ptr;

    ptr

    , . , . .

    . . .

    NNeeww ssttrriinngg

    New

    . 7 string .

  • 291

    . .

    //newstr.cpp #include #include class string { private: char* str; public: string(char* s) { int length=strlen(s) ; str=new char[length+1] ; strcpy(str, s); } ~string() { delete str; } void display() { cout

  • 292

    .

    () , . () . . . .

    . Distance dist .

    . new . .

    //englptr.cpp #include class Distance { private: int feet; float inches; public: void getdist() { cout

  • 293

    cin >> feet; cout > inches; } void showdist() { cout

  • 294

    distptr->getdist();

    :

    : 10 : 6.25 10-6.25 : 6 : 4.75 6 4.75

    . person .

    //ptrobjs.cpp #include class person { protected: char name[40]; public: void setName() { cout > name; } void printName() { cout

  • 295

    void main() { person* persPtr[100]; int n=0; char choice; do { persPtr[n]=new person; persPtr[n]-> setName(); n++; cout > choice; } while (choice==y); for (int j=0; j

  • 296

    persPtr[j]->getName();

    .

    . .

    new

    . , .

    link , linked list . Link . .

    //linklist.cpp #include struct link {

  • 297

    int data; link *next; }; class linklist { private: link *first; public: linklist() { first=NULL; } void additem(int d); void display(); }; void linklist::additem(int d) { link *newlink=new link; newlink-> data=d; newlink-> next=first; first=newlink; } void linklist::display() { link *current=first; while (current!=NULL) { cout next; } } void main() { linklist li; li.additem(25); li.additem(36); li.additem(49); li.additem(64);

  • 298

    li.display(); }

    first NULL . .

    Additem . ( ) .

    link *newlink=new link;

    link ,

    newlink . . 2 . , first .

    newlink->next=first; first=newlink;

    . . next NULL . ,

    cout

  • 299

    current=NULL

    current=current->next .

    .

    64 49 36 25

    . . .

    .

    link link . . . .:

    Class sample { sample *ptr ; // sample obj ; //. }

  • 300

    LLiinnkklliisstt

    . , link , , , , , .

    linklist . , . . .

    .

    ptrobjs ptrsort order, bsort person .

    //persort.cpp #include #include class person { protected: char name[40]; public: void setName() { cout

  • 301

    cin >> name; } void printName() { cout choice; } while (choice==y); cout

  • 302

    int j, k ; for (j=0; j < N-1; j++) for (k=j+1; k < n; k++) order(pp+j, pp+k); } void order(person* pp1, person* pp2) { if (strcmp((*pp1)->getName(), (*pp2)->getName()) > 0) { person* tempptr=*pp1; *pp1=*pp2; *pp2=tempptr; } }

    100

    . .

    : Washington (y/n)? y : Adams (y/n)? y : Jefferson (y/n)? y : Madison (y/n)? n : Washington Adams Jefferson Madison : Adams Jefferson Madison Washington

  • 303

    . . .

    bsort 2 . . person . . person , .

    Order strcmp

    2 .

    0 2

    , . ,

  • 304

    , new delete , . , . , . watch . , . .

    .

    . . . (&) .

    . .. . (*) .

  • 305

    .

    . .

    . .

    .

    New , . . delete .

    (->) .

    . .

    . int** ptr .

  • 306

    99--

    --

  • 307

    99--

    ((SSttrreeaammss))

    . . .

    .

    istream

    >> ostream

  • 308

    cout, cin- . cout ostream_withassign . ostream_withassign ostream .

    istream istream_withassign cin .

    , iostream.h . fstream.h .

    ios

    . . ios streambuf .

    istream ostream ios . istream >> -, ostream

  • 309

    .

    ,,

    .

    #include void main() { ofstream outfile("test.txt"); outfile

  • 310

    getline() . . \n . 3- .

    .

    ,, , get(), put()

    . :

    . . . outfile.put(str[j]); . . . infile.get(ch); . . .

    ,, , .

    .

    #include class person { protected: char name[40]; int age; public: void getdata(void) { cout > name; cout > age; } };

  • 311

    void main() { person pers; pers.getdata(); ofstream outfile("person.dat"); outfile.write((char *)&pers, sizeof(pers)); }

    write()

    . . . sizeof .

    . read() .

    infile read((char *)&pers, sizeof(pers));

    ffssttrreeaamm fstream

    . .

    . . . fstream file; file.open(person.dat,ios::app|ios::out|ios::in); file.write((char *)&pers, sizeof(pers)); . . . file.write((char *)&pers, sizeof(pers)); file.seekg(0); file.read((char *)&pers, sizeof(pers)); . . . if (!file.eof())

  • 312

    file.read((char *)&pe