C primer Plus Chap 05

C primer Plus Chap 05

C primer Plus 第五章代码与练习记录。

运算符、表达式和语句

  • fx1.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #include <stdio.h>
    int main(void)
    {
    int x, y;
    // x = (2+3) * 6;
    // x = (12+6)/2*3;
    // y = x = (2+3)/4;
    y = 3 + 2*(x = 7/2);
    printf("x = %d, y = %d.\n", x, y);

    return 0;
    }
    输出结果:
    1
    x = 3, y = 9.
  • fx2.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <stdio.h>
    int main(void)
    {
    int x;
    // x = (int)3.8 + 3.3;
    // x = (2+3)*10.5;
    // x = 3/5*22.0;
    x = 22.0 * 3 / 5;
    printf("x = %d.\n", x);


    return 0;
    }
    输出结果:
    1
    x = 13.
  • fx3.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #include <stdio.h>
    #include <float.h>
    int main(void)
    {
    int i = 0;
    float n;
    printf("watch out! here come a bunch of fractions!\n");
    while (i++ < 30)
    {
    printf("%d\t", i);
    n = 1.0 / i;
    printf("%f\t",n);
    }
    printf("that's all, folks!\n");

    return 0;
    }
    输出结果:
    1
    1	1.000000	2	0.500000	3	0.333333	4	0.250000	5	0.200000	6	0.166667	7	0.142857	8	0.125000	9	0.111111	10	0.100000	11	0.090909	12	0.083333	13	0.076923	14	0.071429	15	0.066667	16	0.062500	17	0.058824	18	0.055556	19	0.052632	20	0.050000	21	0.047619	22	0.045455	23	0.043478	24	0.041667	25	0.040000	26	0.038462	27	0.037037	28	0.035714	29	0.034483	30	0.033333	that's all, folks!
  • fx4.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include <stdio.h>
    #define S_TO_M 60
    int main(void)
    {
    int sec, min, left;
    printf("this program converts seconds to minutes and seconds.\n");
    printf("just enter the number of seconds.\n");
    scanf("%d", &sec);
    printf("the sec is %d\n", sec);
    while(sec>0)
    {
    min = sec / S_TO_M;
    left = sec % S_TO_M;
    printf("%d sec is %d min and %d sec.\n", sec, min, left);
    printf("please enter the number of seconds:\n");
    scanf("%d", &sec);
    }
    printf("bye!\n");

    return 0;
    }
    输出结果:
    1
    2
    3
    4
    5
    6
    7
    8
    this program converts seconds to minutes and seconds.
    just enter the number of seconds.
    123
    the sec is 123
    123 sec is 2 min and 3 sec.
    please enter the number of seconds:
    0
    bye!
  • fx5.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <stdio.h>
    #define FORMAT "%s! C is cool!\n"
    int main(void)
    {
    int num = 10;
    printf(FORMAT, FORMAT);
    printf("%d\n", ++num);
    printf("%d\n", num++);
    printf("%d\n", num--);
    printf("%d\n", num);

    return 0;
    }
    输出结果:
    1
    2
    3
    4
    5
    6
    %s! C is cool!
    ! C is cool!
    11
    11
    12
    11
  • xf6.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #include <stdio.h>
    int main(void)
    {
    char c1, c2;
    int diff;
    float num;

    c1 = 'S';
    c2 = '0';
    diff = c1 - c2;
    num = diff;
    printf("%c%c%c: %d %3.2f\n", c1, c2, c1, diff, num);

    return 0;
    }
    输出结果:
    1
    S0S: 35 35.00
  • fx7.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #include <stdio.h>
    #define TEN 10
    int main(void)
    {
    int n = 0;
    while(n++ < TEN)
    printf("%5d", n);
    printf("\n");

    return 0;
    }
    输出结果:
    1
    1    2    3    4    5    6    7    8    9   10
  • fx8.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include <stdio.h>
    int main(void)
    {
    int a = 96;
    while(a++ < 103)
    printf("%5c", a);
    printf("\n");

    return 0;
    }
    输出结果:
    1
    a    b    c    d    e    f    g
  • fx9.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include <stdio.h>
    int main(void)
    {
    int x = 0;
    while(++x < 3)
    printf("%4d", x);
    printf("\n");
    return 0;
    }
    输出结果:
    1
    1   2
  • fx9b.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include <stdio.h>
    int main(void)
    {
    int x = 100;

    while(x++ < 103)
    printf("%4d\n", x);
    printf("%4d\n", x);
    return 0;
    }
    输出结果:
    1
    2
    3
    4
    101
    102
    103
    104
  • fx9c.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #include <stdio.h>
    int main(void)
    {
    char ch = 's';
    while(ch < 'w')
    {
    printf("%c\t", ch);
    ch++;
    }
    printf("\n");
    return 0;
    }
    输出结果:
    1
    s	t	u	v
  • fx10.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #include <stdio.h>
    #define MESG "COMPUTER BYTES DOG"
    int main(void)
    {
    int n = 0;

    while(n<5)
    {
    printf("%s\n", MESG);
    n++;
    }
    printf("that's all.\n");
    return 0;
    }
    输出结果:
    1
    2
    3
    4
    5
    6
    COMPUTER BYTES DOG
    COMPUTER BYTES DOG
    COMPUTER BYTES DOG
    COMPUTER BYTES DOG
    COMPUTER BYTES DOG
    that's all.
  • xt1.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #include <stdio.h>
    int main(void)
    {
    int x;
    printf("please enter a number:\n");
    scanf("%d", &x);
    x = x + 10;
    printf("%d\n", x);

    return 0;
    }
    输出结果:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    please enter number of nimutes:
    26
    26 minutes is about 0 hour and 26 minutes
    please enter number of nimutes:
    160
    160 minutes is about 2 hour and 40 minutes
    please enter number of nimutes:
    0
    done!
  • xt2.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <stdio.h>
    int main(void)
    {
    int number, count;
    printf("please enter a number:\n");
    scanf("%d", &number);
    count = number + 10;
    while(number++ <= count)
    printf("%d\t", number-1);
    printf("\n");

    return 0;
    }
    输出结果:
    1
    2
    3
    please enter a number:
    12
    12 13 14 15 16 17 18 19 20 21 22
  • xt3.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #include <stdio.h>
    int main(void)
    {
    const D_TO_W = 7;
    int day, week, left;
    printf("please enter the days:\n");
    scanf("%d", &day);
    while(day > 0)
    {
    week = day / D_TO_W;
    left = day % D_TO_W;
    printf("%d days are %d weeks, %d days.\n", day, week, left);
    printf("please enter the days:\n");
    scanf("%d", &day);
    }
    printf("done!\n");

    return 0;
    }
    输出结果:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    please enter the days:
    7
    7 days are 1 weeks, 0 days.
    please enter the days:
    26
    26 days are 3 weeks, 5 days.
    please enter the days:
    0
    done!
  • xt5.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #include <stdio.h>
    void cubed(float n);
    int main(void)
    {
    float number;
    printf("enter 0 to quit the programe.\n");
    printf("please enter a number:\n");
    scanf("%f", &number);
    while(number > 0)
    {
    cubed(number);
    printf("please enter a number:\n");
    scanf("%f", &number);
    }
    printf("the end!\n");

    }

    void cubed(float n)
    {
    printf("%.6f's cubed is %.6f.\n", n, n*n*n);
    }
    输出结果:
    1
    2
    3
    4
    5
    6
    7
    enter 0 to quit the programe.
    please enter a number:
    2
    2.000000's cubed is 8.000000.
    please enter a number:
    0
    the end!

C primer Plus Chap 05
https://ywmy.xyz/2022/05/14/C-primer-Plus-Chap-05/
作者
ian
发布于
2022年5月14日
许可协议