跳转至

复习题

第五章 运算符、表达式与语句 复习题

  1. 假设所有变量的类型都是int,下列各项变量的值是多少?

    1
    2
    3
    4
    a. x = ( 2 + 3 ) * 6; // 30
    b. x = (12 + 6) / 2 * 3; // 27
    c. y = x = (2 + 3) / 4 ;// x= y=1
    d. y = 3 + 2 * (x = 7/2); // x =3 ;y = 9
    

  2. 假设所有变量的类型是int,下列各项变量的值是多少?

    1
    2
    3
    4
    a. x = (int)3.8 + 3.3; // 6
    b. x = (2 + 3) * 10.5; // 50
    c. x = 3/5 * 22.0; // 0
    d. x = 22.0 * 3 / 5; // 13 
    

  3. 对下列各表达式求值:

    1
    2
    3
    4
    5
    6
    a. 30.0 / 4.0 * 5.0; // 37.5 
    b. 30.0 / (4.0 * 5.0); // 1.5
    c. 30 / 4 * 5; // 35
    d. 30 * 5 / 4; // 37
    e. 30 / 4.0 * 5; // 37.5
    f. 30 / 4 * 5.0; // 35.0
    

  4. 请找出下面的程序中的错误

    int main(void)
    {
        int i = 1,
        float n;
        printf("Watch out! Here come a banch of fractions!\n");
        while (i < 30)
            n = 1/i;
            printf(" %f",n);
        printf("That's all , folks !\n");
        return; 
    }
    
    修改后的程序为:
    #include <stdio.h>
    int main(void)
    {
        int i = 1;
        flaot n;
        printf("Watch out! Here come a banch of fractions!\n");
        while (i++ < 30) // i = 1 构成死循环
        {
            n = 1.0/i;
            printf(" %f \n",n);
        }
        printf("That's all , folks !\n");
        return 0;
    }
    

  5. 这是程序清单 5.9 的另一个版本,从表面上看,该程序只使用了一条scanf()语句,比程序清单5.9 简单,请找出不如原版之处。

    #include <stdio.h>
    #define S_TO_M 60
    int main(void)
    {
        int sec, min, left;
    
        printf("This program converts seconds to minutes and ");
        printf("seconds.\n");
        printf("Just enter the number of seconds.\n");
        printf("Enter 0 to end the program.\n");
        while (sec > 0) {
            scanf("%d", &sec);
            min = sec/S_TO_M;
            left = sec % S_TO_M;
            printf("%d sec is %d min, %d sec. \n", sec, min, left);
            printf("Next input?\n");
        }
        printf("Bye!\n");
        return 0;
    }
    

5.9 程序清单

#include <stdio.h>
#define SEC_PER_MIN 60
int main(void)
{
    int sec,min,left;
    printf("Convert seconds to minutes and seconds!\n");
    printf("Enter the number of seconds (<=0 to quit):\n");
    scanf("%d",&sec);
    while (sec > 0)
    {
        min = sec / SEC_PER_MIN;
        left = sec % SEC_PER_MIN;
        printf("%d seconds is %d minutes, %d seconds.\n",sec,min,left);
        printf("Enter next value (<=0 to quit):\n");
        scanf("%d"&sec);
    }
    printf("Done !\n");

    return 0;
}

答案:。。。。。。

  1. 下面的程序将打印出什么内容?

    #include <stdio.h>
    #define FORMAT "%s! C is coll!\n"
    int main(void)
    {
        int num =10 ;
        printf(FORMAT,FORMAT); // %s! C is coll! 换行 ! C is coll!
        printf("%d\n",++num); // 11 
        printf("%d\n",num++); // 11
        printf("%d\n",num--); // 12  上一步加完,然后再--
        printf("%d\n",num); // 11
        return 0;
    }
    

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

    #include <stdio.h>
    int main(void)
    {
        char c1,c2;
        int diff;
        float num;
        c1 = 's';
        c2 = 'O';
        diff = c - c2; //ASCII码值 83 - 79
        num = diff;
        printf("%c%c%c:%d  %3.2f\n",c1,c2,c1,diff,num); // SOS:4 4.00
        return 0;
    }
    

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

    #include <stdio.h>
    #define TEN 10
    int main(void)
    {
        int n  = 0;
        while (n++ < TEN)
            printf("%5d",n);
        printf("\n");
        return 0;
    }
    
    打印结果如下:
    1    2     3    4     5     6    7     8     9     10
    

  4. 修改上一个程序,使其可以打印字母a~z。

    #include <stdio.h>
    int main(void)
    {
        char ch_lower = 'a';
        char ch_upper = 'A';
        while (ch_lower <= 'z'&& ch_upper <= 'Z')
            printf("%c%c  ",ch_upper++,ch_lower++);
        printf("\n");
        return 0;
    }
    

  5. 下面的程序会打印出什么?
    a.
        int x = 0;
        while (++x < 3)
            printf("%4d",x); // 1    2
    
    b.
        int x = 100;
    
        while (x++ < 103)
            printf("%4d\n",x); //注意换行 101    102    103    104
            printf("%4d\n",x); //注意换行 101    102    103    104
    
    c.
        char ch = 's';
        while (ch < 'w')
        {
            printf("%c",ch); //打印单个字符
            ch++;
        }
        printf("%c\n",ch); // 注意换行 stuvw
    

11.下面的程序会打印什么?

#define MESG "COMPUTER BYTES DOG"
#include <stdio.h>
int main(void)
{
    int n = 0;
    while (n < 5)
        printf("%s\n",MESG);
        n++;
        // 程序有问题,会一直打印 COMPUTER BYTES DOG ,直到关闭程序为止,类似造成死循环。
    printf("That's all.\n");
    return 0;
}

  1. 分别编写一条语句,完成下列任务(或者说,使其具有以下副作用)

    1
    2
    3
    4
    a. 将变量x的值增加10 // x = x + 10; 或 x+=10;
    b. 将变量x的值增加1  // x++,++x 或 x+=1
    c. 将a与b之和的两倍赋给c // c = 2*(a+b)
    d. 将a与b的两倍之和赋给c // c = a+2*b
    

  2. 分别编写一条语句,完成下列任务

    1
    2
    3
    4
    a. 将变量 x 的值减少1 // x-- 或 --x 或 x -=1
    b.  n 除以 k 的余数赋给 m // m = n%k
    c. q 除以 b 减去 a并将结果赋给 p // p = q/b - a
    d. a  b 之和除以 c  d 的乘积并将结果赋给 x // x = (a + b)/(c*d)