C Primer Plus(第6版)第七章复习题答案

7.11复习题
1.

a. false
b. true
c. false
2.

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>
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;
}


重点是,else 与 离它最近的 if 匹配, 从上向下看

4.

a. 1
b. 0
c. 1
d. 6
e. 10
f. 0
5.

下面的程序将会打印如下效果:
*#%*#%$#%*#%*#%$#%*#%*#%$#%*#%*#%
// 注意 else的范围只有后面的第一条语句printf('*');
6.

下面的程序将会打印如下内容:

fat hat cat Oh no !
hat cat Oh no !
cat Oh no !
7.
修改后的程序如下

#include  <stdio.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 case", lc, uc, oc);

    return 0;
}

8.
Your are 65, Here is your gold watch.
Your are 65, Here is your gold watch.
Your are 65, Here is your gold watch.
Your are 65, Here is your gold watch.
Your are 65, Here is your gold watch.
Your are 65, Here is your gold watch.
Your are 65, Here is your gold watch.
无限循环
9.
q    //输入
Step 1
Step 2
Step 3    //注意 执行标签语句
c    //输入
Step 1
h    //输入
Step 1
Step 3
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 == 'h')            //这几句还可以这样写
                    printf("Step 3 \n");    // if (ch != 'h')
                else                //    printf("Step 2 \n");
                {                // printf("Step 3 \n");
                    printf("Step 2 \n");    //
                    printf("Step 3 \n");    //
                }                //
            }
        }
    }
    printf("Done \n");

    return 0;
}


 

  • 13
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值