C Primer Plus(第六版)第12章 编程练习答案(有题目)

这篇博客提供了C Primer Plus第六版第12章的编程练习答案,包括12-1至12-9的题目输出。作者鼓励读者通过留言讨论问题,并提到如有需要PDF版书籍,可在评论区留下邮箱。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有问题欢迎留言讨论~如果要书pdf可以在评论里留下邮箱
IDE: Xcode


在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


12-1

#include <stdio.h>

void critic(int *unit_p); //不使用全局变量,就通过指针将参数带下来

int main(void)
{
   
    int units;

    printf("How many pounds to a firkin of butter?\n");
    scanf("%d", &units);
    while(units != 56)
        critic(&units);
    printf("You must have looked it up!\n");

    return 0;
}

void critic(int *unit_p)
{
   
    printf("No luck, my friend. Try again.\n");
    scanf("%d", unit_p);
}

Output:
在这里插入图片描述

12-2

//pe12-2a.c
#include <stdio.h>

int gb_mode = 0;
int gb_distance = 0;
double gb_fuel = 0;

void set_mode(int mode)
{
   
    if(mode == 0 || mode == 1) //只有mode的输入是有效值,才会对应的更新global变量
        gb_mode = mode;
    else{
   
        if(gb_mode)
            printf("Invaild mode specified. Mode 1(US) used.\n");
        else
            printf("Invaild mode specified. Mode 0(metric) used.\n");
        
    }
}


void get_info(void)
{
   
    if(gb_mode){
        //US mode
        printf("Enter distance traveled in miles: ");
        scanf("%d", &gb_distance);
        printf("Enter fuel consumed in gallons: ");
        scanf("%lf", &gb_fuel);
        
    }else{
              //metric mode
        printf("Enter distance traveled in kilometers: ");
        scanf("%d", &gb_distance);
        printf("Enter fuel consumed in liters: ");
        scanf("%lf", &gb_fuel);
    }
}


void show_info(void)
{
   
    double consumption;
    
    if(gb_mode){
         //US mode
        consumption = gb_distance / gb_fuel;
        printf("Fuel consumption is %.1f miles per gallon.\n", consumption);
    }else{
               //metric mode
        consumption = gb_fuel / gb_distance * 100;
        printf("Fuel consumption is %.2f liters per 100 km.\n", consumption);
    }
}

//pe12-2a.h

extern int gb_mode;
extern int gb_distance;
extern double gb_fuel;

extern void set_mode(int mode);
extern void get_info(void);
extern void show_info(void);

//pe12-2b.c

#include <stdio.h>
#include "pe12-2a.h"

int main(void)
{
   
    int mode;
    
    printf("Enter 0 for metric mode, 1 for US mode: ");
    scanf("%d", &mode);
    while(mode >= 0)
    {
   
        set_mode(mode);
        get_info();
        show_info();
        printf("Enter 0 for metric mode, 1 for US mode: ");
        printf(" (-1 to quit): ");
        scanf("%d", &mode);
    }
    printf("Done.\n");
    return 0;
}

Output:
在这里插入图片描述

12-3

//  pe12-2b.c

#include <stdio.h>
#include "pe12-2a.h"

int main(void)
{
   
    int mode = 0,last_mode = 0;
    
    printf("12-3 / Enter 0 for metric mode, 1 for US mode: ");
    scanf("%d", &mode);
    while(mode >= 0)
    {
   
        mode = set_mode(mode, last_mode); //因为不能有全局变量,所以在set mode后需要把mode值作为return值带出来
        last_mode = mode;//因为不能有全局变量,所以在main中新建一个变量last_mode记录上次使用的mode
        get_info(mode);//因为不能有全局变量,所以mode作为参数带下去
        printf("Enter 0 for metric mode, 1 for US mode: ");
        printf(" (-1 to quit): ");
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值