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

  • Published on
    08-Dec-2016

  • View
    222

  • 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[1], pa+i a[i], a *(pa+i)

    a[i].

    . " 1 "

    , ,

    . , pa+i i, , -

    pa, , pa. -

    , .

    -

    , .. . -

    . -

    , pa=&a[0]

    : pa=a , , -

    a[i] *(a+i). a[i],

    *(a+i); . & -

    , , &a[i] a+i : a+i - i-

    . , pa - , -

    : pa[i] *(pa+i). ,

  • 19

    , ,

    .

    ,

    . , pa=a pa++ -

    . - , , -

    a=pa a++, p=&a .

    . -

    . .

    #include

    void main()

    {

    int p[5]={1,2,3,4,5};

    int *ref;

    ref=p;

    printf("\n");

    for(int i=0;i

  • 20

    ptr=&arr[0][0][0];

    arr L , - M

    N. M N .

    arr[i][j[k]. -

    ptr - 0- M N

    ptr+i*(M*N) - i- M N

    ptr+i*(M*N)+j*N - j- i- M N

    ptr+i*(M*N)+i*N+k - arr[i][j][k]

    *(ptr+i*(M*N)+i*N+k) - arr[i][j][k]

    3.2

    1. ?

    2. .

    3.

    3.3

    1.

    1. , N .

    ?

    2. , N .

    3 ?

    3. , N . -

    , -

    .

    4. , N . -

    , .

    5. , N . -

    .

  • 21

    6. , N .

    , - -

    .

    7. , N . -

    , - .

    8. , N . -

    , .

    9. , N . -

    ,

    .

    10. , N . -

    , , .

    11. , N . -

    .

    12. , N .

    ,

    .

    13. , N . -

    , .

    14. , N . , -

    .

    .

    15. , N . , -

    .

    , .

    2.

    1. . , , -

    .

    2. . ,

    , .

    3. . -

    ( ).

  • 22

    4. .

    ( ).

    5. . -

    , , - ( -

    ), - ( ).

    6. . -

    ( ) .

    , , -

    .

    7. . -

    ( ) . -

    , .

    8. .

    , .

    9. . , -

    , .

    10. . , -

    .

    11. .

    ( ).

    12. . -

    ( ) .

    , , -

    .

    13. . -

    ( ) .

    , .

    14. .

    ( ).

    15. . -

    , ,

    .

    4

  • 23

    : , .

    4.1

    -

    .

    , ,

    .

    .

    . -

    . -

    N (N - ).

    ,

    . , , -

    , , .. -

    .

    ( )

    . N-1 (N -

    ), N-1 ,

    .

  • 24

    ,

    ().

    , , .. -

    .

    " "

    . -

    -

    , , ,

    "-

    " . -

    , , K K+1, , -

    . -

    K- , .. "" -

    .

    , -

    () . -

    , , .

    -

    . , -

    ( )

    .

    ,

    .

  • 25

    -

    , , ,

    . , ,

    D.

    D=(N+1)/2 ( N -

    ).

    (D) .

    D(i+1)=floor((D(i)+1)/2)

    floor -

    . D=1.

    ()

    . -

    , . -

    ,

    (

    0).

    .

    .

    4.2

    1. , ,

    .

    2. , -

    .

    3. ,

    .

    4. , 3, ..

    .., -

    - .

  • 26

    5.

    .

    6. -

    .

    7. ,

    . -

    .

    8. ,

    .

    9. ,

    . -

    .

    10. , , -

    , .

    11. , ( ) -

    .

    12. , , 3, -

    .

    13. . , , -

    .

    14. .

    .

    15. . -

    .

    5

    : - .

    5.1.

  • 27

    - , .

    , -

    \0.

    .

    (\0).

    . -

    .

    , -

    .

    .

    . .

    #include

    void main()

    {

    int top,bot;

    char string[10],temp; /* -

    */

    scanf("%s",string);

    /* & , -

    */

    for(top=0,bot=10;top

  • 28

    ( , ),

    , .

    . , -

    .

    #include

    void main()

    {

    char ch;

    ch=getchar();

    putchar(ch);

    }

    . , .

    #include

    #define STOP

    void main()

    {

    char ch;

    while((ch=getchar())!=STOP)

    putchar(ch);

    }

    , while, -

    : getchar(); -

    ch; .

    STOP, -

    .

    .

  • 29

    1. .

    char *strcat(char *s1,char *s2)

    .

    2. .

    char *strchr(char *s,int c)

    ( s)

    c.

    .

    3. .

    int strcmp(char *s1,char *s2)

    . - int:

    s1s2

    4. .

    char *strcpy(char *s1,char *s2)

    , s1 -

    s2.

    5. ( ).

    int strlen(char *s)

    ,

    "" "". -

    .

    "", isalpha(c) c -

    isupper(c) c -

  • 30

    islower(c) c -

    isdigit(c) c - 0 9

    isxdigit(c) c -

    isalnum(c) -

    isspace(c) c - , , , -

    gets(char *string) puts(char * string).

    . , ,

    , 'y'.

    #include

    #include

    void main()

    {

    char string[10];

    char *ptr;

    printf(" \n");

    gets(string);

    ptr=strchr(string,'y');

    printf(" %s\n",ptr);

    }

    5.2

    1. ?

    2. .

    3. .

    4. .

    5. , .

    5.3

  • 31

    1. , , -

    "" .

    2. , ,

    .

    3. , ,

    .

    4. , ,

    .

    5. N .

    .

    6. N . -

    .

    7. N .

    .

    8. , .

    , , -

    .

    9. , .

    , , .

    10. , . -

    .

    11. , . -

    ( ).

    12. N , .

    .

    13. N , .

    (

    ).

    14. , . -

    :

    , -.

    15. N , .

    :

    'a'.

  • 32

    6

    : , - .

    6.1

    ( cpp ) pp

    ppp p. p

    p p , , .

    - pp, p , -

    . , p

    p , .

    struct {

    type1 id11,id12,...,id1n;

    type2 id21,id22,...,id1m;

    .........................

    typei idk1,idk2,...,idkp;

    } [];

    idij - p ; typei - ; - -

    .

    . date1 date2.

    .

    struct {

    int year;

    short day;

    } date1, date2;

  • 33

    . -

    .

    struct __

    {

    ;

    };

    .

    , .

    . (, , ).

    struct data{

    int day;

    char month[10];

    int year;

    };

    struct data d1,d2,d3; /* */

    .

    struct data d1={4,"",1998};

    typedef.

    .

    :

    typedef _ ;

  • 34

    - ,

    . -

    .

    float:

    typedef float real;

    float real,

    .

    . "", .

    typedef struct{

    int day;

    char month[10];

    int year;

    }data;

    data d1,d2,d3;

    -

    , , , -

    .

    .

    . .

    struct tree{

    int number;

    struct tree *left;

    struct tree *right;

    };

    ".", -

    .

    , :

    d1.day

  • 35

    d2.year

    .

    . .

    data * ptr;

    ptr=&d2;

    ptr->day=4;

    ptr->month="";

    ptr->year=1998;

    :

    (*ptr).day=4;

    (*ptr).month="";

    (*ptr).year=1998;

    . , -

    data.

    data d[5];

    . , -

    .

    .

    #include

    const int n=3;

    typedef struct{

    int num;

    char fam[20];

    char name[15];

    }student;

  • 36

    student mas[n];

    student *ptr=&mas[0];

    void vvod(student *p)<