C primer Plus Chap 07

C primer Plus Chap 07

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

C语言控制语句:分支和跳转

  • fx5.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #include <stdio.h>
    #include <ctype.h>
    int main(void)
    {
    int num;
    for(num=1; num<=11; num++)
    {
    if(num % 3 == 0)
    putchar('$');
    else
    putchar('$');
    putchar('#');
    putchar('%');
    }
    putchar('\n');

    return 0;
    }
    输出结果:
    1
    $#%$#%$#%$#%$#%$#%$#%$#%$#%$#%$#%
  • fx6.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #include <stdio.h>
    int main(void)
    {
    int i=0;
    while(i<3)
    {
    switch(i++)
    {
    case 0:printf("fat");
    case 1:printf("hat");
    case 2:printf("cat");
    default:printf("oh no!");
    }
    putchar('\n');
    }
    return 0;
    }
    输出结果:
    1
    2
    3
    fathatcatoh no!
    hatcatoh no!
    catoh no!
  • fx7.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #include <stdio.h>
    int main(void)
    {
    char ch;
    int lc=0;
    int uc=0;
    int oc=0;
    while((ch=getchar())!='#')
    {
    if(ch>='a' && ch<='z')
    lc++;
    else if(ch>='A' && ch<='Z')
    uc++;
    else
    oc++;
    }
    printf("%d lowercase, %d uppercase, %d other\n", lc, uc, oc);

    return 0;
    }
    输出结果:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    a
    b
    c
    A
    B
    C
    1
    2
    3
    #
    3 lowercase, 3 uppercase, 12 other

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