跳转至

复习题

第七章 分支和跳转控制语句 复习题

  1. 判断下列表达式时true还是false。

    1
    2
    3
    a. 100 > 3 && 'a' > 'c'; // false
    b. 100 > 3 || 'a' > 'c'; // true
    c. !(100>3); // false
    

  2. 根据下列描述的条件,分别构造一个表达式

    1
    2
    3
    4
    a. number等于或大于90但是小于100 // number>=90 && number < 100;
    b. ch不是字符q或k // ch !== 'q' || ch !== 'k';
    c. number在19之间包括1和9),但不是5 // number >=1 && number <=9 && number!==5;
    d. number不在19 // number < 1 && number > 9;
    

  3. 下面的程序关系表达式过于复杂,而且还有些错误,请简化并改正。

    #include <stdio.h> 
    int main(void)
    {
        int weight, height; /* weight以磅为单位,height以英寸为单位 */
    
        scanf("%d, weight, height);
        if(weight < 100 && height > 64)
            if(height >= 72)
                printf("You are very tall for your weight.\n");
            else if(height < 72 && > 64)
                printf("You are tall for your weight.\n");
        else if(weight > 300 && !(weight <= 300)
                    && height < 48)
            if(!(height >= 48))
                printf(" You are quite short for your weight.\n");
        else
            printf("Your weight is ideal.\n");
    
       return 0;
    }
    

修改版本如下:

#include <stdio.h>
int main(void)
{   
    int weight, height;

    scanf("%d %d", &weight, &height);  
    if (weight < 100 && height > 64)
        if (height >= 72)
            printf("You are very tall for your weight. \n");
        else        
            printf("You are tall for your weight. \n");
    else if (weight > 300 && height < 48)   
        printf(" Your are quite short for your weight. \n"); 
        else
            printf("Your weight is ideal. \n");

    return 0;
}

  1. 下列各个表达式的值是多少?

    1
    2
    3
    4
    5
    6
    a. 5>2 // true,即是1
    b. 3+4 > 2 && 3<2 // false,即是0
    c. x>=y || y > x // 1
    d. d = 5 + (6 > 2) // 6 
    e. 'X' > 'T' ? 10 : 5 // 10 
    f. x > y ? y > x : x > y // 0 
    

  2. 下面的程序将打印什么?

    #include <stdio.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;
    }
    // 打印结果为:*#%*#%$#%*#%*#%$#%*#%*#%$#%*#%*#%
    

  3. 下面的程序将打印什么?

    #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;
    }
    /*打印结果为: 
    fat hat cat Oh no!
    hat cat Oh no!
    cat Oh no!
    */
    

  4. 下面的程序有哪些错误?

    #include <stdio.h>
    int main(void)
    {
        char ch;
        int lc = 0;
        int uc = 0;
        int oc = 0;
        while ((ch = getchar())!='#')
        {
            if('a' <= ch >= 'z')
                lc++;
            else if (!(ch < 'A') || !(ch) > 'z')
                uc++;
            oc++;
        }
        printf(%d lowercase, %d uppercase, %d other,lc,uc,oc);
        return 0;
    }
    
    修改后的程序:
    #include <stdio.h>
    #include <ctype.h>
    int main(void)
    {
        char ch;
        int lc = 0;
        int uc = 0;
        int oc = 0;
        while ((ch = getchar())!='#')
        {
            if(islower(ch))
                lc++;
            else if (isupper(ch))
                uc++;
            else
                oc++;
        }
        printf("%d lowercase, %d uppercase, %d other \n",lc,uc,oc);
        return 0;
    }
    

  5. 下面的程序将打印什么?

    #include <stdio.h>
    int main(void)
    {
        int age = 20;
        while (age++ <= 65) // 
        {
            if ((age % 20)==0)
                printf("You are %d . Here is a raise.\n",age);
            if (age =65)
                printf("You are %d . Here is your gold watch.\n",age);
        }
        return 0;
    }
    /*打印结果:死循环打印如下内容
    You are %d . Here is your gold watch. 
    */
    

  6. 给定下面的输入时,以下程序将打印什么?

    /*
    q
    c
    h
    b
    */
    #include <stdio.h>
    int main(void)
    {
        char ch;
    
        while((ch = getchar()) != '#')
        {
            if(ch == '\n')
                continue;
            printf("Step 1\n");
            if(ch == 'c')
                continue;
            else if(ch == 'b')
                break;
            else if(ch == 'g')
                goto laststep;
            printf("Step 2\n");
            laststep: printf("Step 3\n");
        }
        printf("Done!\n");
        return 0;
    }
    /*打印结果为:
    q
    Step 1
    Step 2
    Step 3
    c
    Step 1
    g
    Step 1
    Step 3
    b
    Step 1
    Done!
    */
    

  7. 重写复习题9,但这次不能使用continue 和 goto语句。

    #include <stdio.h>
    int main(void)
    {
        char ch;
    
        while((ch = getchar()) != '#')
        {
            if(ch != '\n')
            {
                printf("Step 1\n");
                if(ch != 'c')
                {
                    if(ch == 'b')
                        break;
                    if(ch != 'g')
                        printf("Step 2\n");
                    printf("Step 3\n");
                }
            }
        }
        printf("Done!\n");
        return 0;
    }