首页 > 其他 > 详细

C Primer Plus 第7章 C控制语句:分支和跳转 复习题与编程练习

时间:2015-11-20 02:01:23      阅读:337      评论:0      收藏:0      [点我收藏+]
复习题
1、确定哪个表达式为true,哪个为false。
    a.100 > 3 && ‘a‘ > ‘c‘
    b.100 > 3 || ‘a‘ > ‘c‘
    c.!(100>3)
答:
a.false
b.true
c.false
2、构造一个表达式来表示下列条件:
a.number等于或大于90,但是小于100
b.ch不是字符q也不是字符k
c.number界于1到9之间(包括1和9),但是不等于5
d.number不在1到9之间
答:
a.number >= 90 && number < 100
b.ch != ‘q‘ && ch != ‘k‘
c.number >= 1 && number <= 9 && number != 5
d.number < 1 || number > 9
3、下面程序中的关系表达式过于复杂,并有些错误,请简化并改正它。
 #include <stdio.h> 
 1
 int main(void)
 2 {
 3     int weight, height; /* weight以磅为单位,height以英寸为单位 */
 4 
 5     scanf("%d, weight, height);
 6     if(weight < 100 && height > 64)
 7         if(height >= 72)
 8             printf("You are very tall for your weight.\n");
 9         else if(height < 72 && > 64)
10             printf("You are tall for your weight.\n");
11     else if(weight > 300 && !(weight <= 300)
12             && height < 48)
13         if(!(height >= 48))
14             printf(" You are quite short for your weight.\n");
15     else
16         printf("Your weight is ideal.\n");
17 
18     return 0;
19 }
答:
第5行:应该是scanf("%d %d", &weight, &height);。在scanf()中不要忘记使用&运算符。这一行前面也应该有提示输入的语句。但第6行已经保证height>64,因此,不需要任何测试,并且
else if应该是else。
第9行:它的意思是(height<72&&height>64)。但是表达式的第一部分是不必要的,因为既然程序已经到达了这个else if,那么height必然小于72。因此一个简单的(height>64)就可以了。
第11行:条件冗余;第二个表达式(weight不是小于或等于300的)与第一个子表达式意义相同。所需要的只是一个简单的(weight>300)。但是这里还有更多的问题。第11行属于一个不正确的if!很明显,这个else是与第6行中的if相匹配的。但是根据if的“最就近原则”,它会与第7行的if相匹配。(因此会在weight小于100并且height小于或等于64时到达第11行。这就使得在到达该语句时weight不可能超过300。)
第7到10行:应该用花括号括起来。这样第11行就会是第6行而不是第7行的可选情况。而如果到第9行的else if由一个简单的else替代了,就不再需要花括号了。
第13行:应该简化为if(height<48)。其实这一行完全可以忽略,因为第12行已经作了这种测试。
第15行:这个else与第13行的if相匹配。把第13行和第14行括在花括号中可以强制使这个else与第6行的if相匹配。或者,按照建议,简单地删除第13行。
下面是一个正确的版本:
#include <stdio.h>
int main(void)
{
    int weight, height; /* weight以磅为单位,height以英寸为单位 */
    printf("Enter your weight in pounds and ");
    printf("your height in inches.\n");
    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(" You are quite short for your weight.\n");
    else
        printf("Your weight is ideal.\n");

    return 0;
}
4、下列每个表达式的数值是多少?
a.5 > 2
b.3 + 4 > 2 && 3 < 2
c.x >= y || y > x
d.d = 5 + (6 > 2)
e.‘X‘ > ‘T‘ ? 10 : 5
f. x > y ? y > x : x > y
答:
a.1
b.0
c.1(如果第一个表达式为假则第二个为真,反之亦然;只需要一个为真的表达式,结果就为真。)
d.6
e.10
f.0
5、下列程序将打印出什么?
#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;
}
答:
*#%*#%$#%*#%*#%$#%*#%*#%$#%*#%*#%
6、下列程序将打印出什么?
#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;
}
答:
fathatcatOh no!
hatcatOh no!
catOh no!
7、下列程序有什么错误?
 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     char ch;
 5     int lc = 0;    /*统计小写字符
 6     int uc = 0;    /*统计大写字符
 7     int oc = 0;    /*统计其他字符
 8 
 9     while((ch = getchar()) != ‘#‘)
10     {
11         if(‘a‘ <= ch >= ‘z‘)
12             lc++;
13         else if(!(ch < ‘A‘) || !(ch > ‘Z‘)
14             uc++;
15         oc++;
16     }
17     printf("%d lowercase, %d uppercase, %d other, lc, uc, oc");
18     return 0;
19 }
答:
第5行到第7行的注释应该以*/结尾,或者用//来代替/*。表达式‘a‘ <= ch >= ‘z‘应该被写成这样:ch >= ‘a‘ && ch <= ‘z‘。或者用一种更简单也更通用的方法:包含ctype.h文件并使用islower()。顺便提一下,‘a‘ <= ch >= ‘z‘在C中是合法的,只是不具有正确的意义。因为关系运算符是从左到右结合的,所以这个表达式被解释为(‘a‘ <= ch) >= ‘z‘。圆括号中表达式的值为1或0(真或假),然后检查这个值来看它是否大于或等于‘z‘的数值编码。0和1都不能满足这个条件,所以整个表达式的值总是为0(假)。在第二个判断表达式中,在第二个表达式中,||应该为&&,尽管!(ch < ‘A‘)是合法的,而且意义也正确,但ch >= ‘A‘更为简单。‘Z‘后面需要有两个结束圆括号,而不是一个。再一次更简单的方法是使用isuper()。应该在oc++;语句前面放置一个else,否则。每输入一个字符,它都会加1。在printf()调用中的控制表达式应该用双引号引起来。
下面是一个正确的版本:
#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", lc, uc, oc);
    return 0;
}
8、下列程序将打印出什么?
/* retire.c*/
#include <stdio.h>
int main(void)
{
    int age = 20;

    while(age++ <= 65)
    {
        if((age % 20) == 0)      /* age能被20整除吗?*/
            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 65. Here is your gold watch.
9、当给定下述输入时,下列程序将打印出什么?
q
c
g
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  (当ch == ‘q‘时,laststep: printf("Step 3\n");语句也要打印出来)
c
Step 1
g
Step 1
Step 3 (当ch == ‘g‘时,通过goto语句跳转到laststep: printf("Step 3\n");,前面的语句
printf("Step 2\n")
;就不要打印出来了
b
Step 1
Done!
10、重写题目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;
}
编程练习
1、
#include <stdio.h>
#include <ctype.h>
int main(void)
{
    int s_ct = 0; //统计空格数
    int l_ct = 0; //统计换行数
    int o_ct = 0; //统计除空格和换行符之外的其他字符
    char ch;

    printf("Please enter text to be analyzed(# to terminate): \n");
    while((ch = getchar()) != ‘#‘)
    {
        if(ch == ‘ ‘)
            s_ct++;
        if(ch == ‘\n‘)
            l_ct++;
        if(ch != ‘ ‘ && ch != ‘\n‘)
            o_ct++;
    }
    printf("The number of spaces,lines,others: %d, %d, %d", s_ct, l_ct, o_ct);
    return 0;
}
2、
#include <stdio.h>
int main(void)
{
    char ch;
    int count = 0;

    printf("Please enter text to be analyzed(# to terminate): \n");
    while((ch = getchar()) != ‘#‘)  // 当读取\n时,又是一种什么情况,比如说要从几行来键入???
    {
        count++;
        printf("%c/%d ", ch, ch);
        if(count % 8 == 0)
            printf("\n");
    }
    //printf("Each character and its ASCII code value:");
    return 0;
}
3、
#include <stdio.h>
int main(void)
{
    int even_count = 0;
    float even_sum = 0;
    int odd_count = 0;
    float odd_sum = 0;
    int number;
    printf("Please enter text to be analyzed(# to terminate): \n");
    while(scanf("%d", &number) == 1)
    {
        if(number == 0)
            break;
        if(number % 2 == 0)
        {
            even_count++;
            even_sum += number;
        }
        else
        {
            odd_count++;
            odd_sum += number;
        }
    }
    printf("The number of odd: %d; The average of odd: %.2f\n", odd_count, odd_sum / odd_count);
    printf("The number of even: %d; The average of even: %.2f\n", even_count, even_sum / even_count);
    return 0;
}
4、
待续。。。





















C Primer Plus 第7章 C控制语句:分支和跳转 复习题与编程练习

原文:http://www.blogjava.net/BeautifulMan/archive/2015/11/20/428296.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!