Элементы программирования на Си: Методические указания для студентов специальности 5102

  • View
    223

  • Download
    0

Embed Size (px)

Transcript

  • T..

    5102

    1998

  • 2

    681.3.06 (076)

    .. : 5102 - , , 1998. - 52 .

    -. - . . , - .

    :6 .

    . .-. , ..

    p --

    , 1998

  • 3

    1. 1. 4

    1.1 4

    1.2 9

    1.3 9

    1.4 9

    2. 2. 11

    2.1 11

    2.2 15

    2.3 15

    3. 3. 16

    3.1 16

    3.2 20

    3.3 20

    4. 4. 23

    4.1 23

    4.2 26

    5. 5. 27

    5.1 27

    5.2 31

    5.3 31

    6. 6. 32

    6.1 32

    6.2 37

    6.3 37

    7. 7. 40

    7.1 40

    7.2 43

    7.3 44

    1 48

    2 50

    52

  • 4

    1

    : - -

    .

    1.1

    . ,

    ,

    :

    1. , -

    "" . ""

    .

    2. , "" ""

    :

    double float long int short char

    unsigned

    .

    3.

    ,

    . "" "" .

    -

    float double, char short -

    int. .

    -

    , .

  • 5

    :

    if () 1; if () 1;

    else 2;

    , 1, ,

    1 ,

    2 2.

    , -

    { }.

    .

    if (i

  • 6

    !=

    = ,

    !

    &&

    !! .

    int, -

    0 () 1

    ().

    if. -

    :

    1 ? 2 : 3;

    "1" .

    "1" (), "3", -

    . "1"

    (), "2".

    .

    max.

    max=(a

  • 7

    switch() {

    case 1:1;

    case 2:2;

    . . .

    case N:N;

    default:;

    }

    "" "" (

    ). -

    . default ,

    .

    . default .

    , , -

    break . -

    .

    . -

    sign. :

    switch(sign) {

    case '-':x=y-z; break;

    case '+':x=y+z; break;

    case '*':x=y*z; break;

    case '/':x=y/z; break;

    default:printf(" \n");

    }

    ,

    .

    : break;

    switch. -

    -

  • 8

    . -

    ,

    , .

    goto ;

    , . -

    . , -

    32 . -

    , .

    . , ,

    .

    s : (float). -

    , int,

    float.

    #include

    #include

    void main()

    { int a=3,b=5,c=7;

    float s,p;

    if (a>=(b+c)||b>=(a+c)||c>=(a+b))

    printf(" \n");

    else {

    s=(float)1/2*(a+b+c);

    p=sqrt(s*(s-a)*(s-b)*(s-c));

    printf("p=%f\n",p);

    }

    }

    1.2

    1. .

    2. .

    3. .

  • 9

    1.3.

    1. .

    2. unsigned .

    3. .

    4. .

    5. .

    1.4.

    1. (x1,y1), B(x2,y2), C(x3,y3). -

    , .

    2. , x1,x2,x3,x4 -

    , . NF , -

    F .

    3. , -

    .

    4. , -

    -

    .

    5. 1 9 " ",

    , "" ""

    "".

    6. "

    ", "" .

    7. , -

    : (, , , , , , ) -

    , . - ( -

    ), - .. (, , , )

    8. a, b, c ,

    a>=b>=c.

    9. . cos(x),

    1+|x|, (1+ x x) (1+ x x).

  • 10

    10. a, b, c, d, e, f. -

    , a x + b y=c d x + e y=f,

    .

    11. a, b, c.

    , 0, - 1, -

    - 2, - 3.

    12. .

    F 1, x, y, z , 0

    .

    13. .

    F 1, x, y, z -

    , 0 .

    14. .

    f 1, 3

    x, 0 .

    15. .

    .

    2

  • 11

    : .

    2.1

    while

    :

    while() ;

    , ,

    . , -

    . , .

    ;. . -

    , ,

    .

    .

    index=1; while(index++

  • 12

    , . "3"

    , .

    "" .

    . 0 9.

    #include

    void main()

    { int i;

    for(i=0;i

  • 13

    for(n=3;ans

  • 14

    do , -

    . , -

    . , . ,

    .

    . -

    do.

    do {

    printf(" Y N\n");

    scanf("%c",&c);

    }

    while(c!='Y' && c!='N')

    ,

    .

    : continue;

    .

    () .

    . 100.

    for(i=0;i

  • 15

    -

    .

    . -

    , .

    for(;;){

    scanf("%d",&num);

    if(!num) break:

    }

    2.2

    1. ?

    2. ?

    3. for.

    4. - - .

    2.3

    1. S = 1 + 2/2 + (2 4)/(2+4) + ...

    +(2 4 6 ...(2 N))/(2+4+6+..+(2 N)) N.

    2. S = sqrt(3 + sqrt(6 + sqrt(9 + ... + sqrt(96 + sqrt(99)...).

    3. S = 1/sqrt(1 3) + 1/sqrt(3 5) + ... + 1/sqrt(199 201).

    4. F(0)=F(1)=1,

    F(i)=F(i-1)+F(i-2), i=2,3,... 35- .

    5. , N, N - -

    , 1.

    6. , N, M

    N - , 1

  • 16

    9. S = (1 + 1/3) * (1/5 + 1/7) * (1/9 +1/11 + 1/13) *...* (1/33

    + 1/35 + 1/37 + 1/39 + 1/41 + 1/43).

    10. S = 4/2 + (4 7)/(2 6) +(4 7 10)/(2 6 10) + ... +

    (4 7 10 ...301)/(2 6 10 ... 398).

    11. S = cos(1 +cos(2 + ... + cos(39+ cos(40)...).

    12. S = sh(x) = x + x^3/3! + x^5/5! + ...+ x^(2 N+1)/(2 N+1)!

    N.

    13. S = cos(x) = 1 - x^2/2! + x^4/4! +..+ (-1)^N

    x^(2 N)/(2 N)! N.

    14. S = Ln(1+x) = x - x^2/2 + x^3/3 +..+ (-1)^(N-1) x^N/N

    N |x|

  • 17

    :

    page[1] - ,

    bigmas[0][0]

    . -

    .

    :

    int s[2]={1,2,3};

    ,

    . .

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

    -

    . -

    , .

    :

    int s[1][2]={{4,5,6},{7,8,9}}; int f[1][2]={10,11,12,13,14};

    char p[2][2]={{'n'},{'y'}};

    s . f

    ( f - 2 3 )

    5 ( 0,0 0,1 0,2 1,0 1,1). -

    p 2 : p[0][0]='n' p[1][0]='y'.

    , , -

    "", .

    ,

    .

  • 18

    C -

    , , -

    .

    , ,

    . - ,

    , , .

    int a[10] 10 , ..

    10 , a[0], a[1],..., a[9]. a[i]

    i- .

    pa ,

    int pa;

    : pa=&a[0] pa a, .. pa

    a[0]. x= pa a[0] x.

    pa , -

    pa+1 , , , pa - i i-

    pa, pa+1 i- . , pa

    a[0], *(pa+1) a