C Primer Plus (第六版) 第十二章_编程练习答案

本章节包含了C Primer Plus第六版第十二章的所有编程练习答案,包括no1.c到no9.c共9个C语言程序实例,涵盖了该章的关键概念和技术应用。
摘要由CSDN通过智能技术生成

no1.c

//不使用全局变量,重写程序清单12.4

# include <stdio.h>

void critic(int * pti);

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 * pti)
{
	printf("No luck , my friend.Try again.\n");
	scanf("%d" , pti);
}

no2.c

//在美国,通常以英里/加仑来计算油耗;在欧洲,以升/100公里来计算.下面是程序的一部分,提示
//用户选择计算模式(美制或公制),然后接收数据并计算油耗.
//pel2-2b.c
//与 pel2-2a.c一起编译
# include <stdio.h>
# include "pel2-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 ;
}

/*pel2-2a.c*/
/*与no2.c一起编译*/
# include <stdio.h>
static int mode ;
static double juli ;
static double youliang ;

void set_mode(int m)
{
	if (m > 1)
	{
		printf("Invalid mode specified. Mode %d(US) used.\n" , mode);
		return ;
	}
	mode = m ;
}

void get_info(void)
{
	switch (mode)
	{
		case 0 :
			printf("Enter distance traveled in kilometers:");
			scanf("%lf" , &juli);
			printf("Enter fuel consumed in liters:");
			scanf("%lf" , &youliang);
			break ;
		case 1 :
			printf("Enter distance traveled in miles:");
			scanf("%lf" , &juli);
			printf("Enter fuel consumed in gallons:");
			scanf("%lf" , &youliang);
			break ;
	}
}

void show_info(void)
{
	double youhao = 0 ;

	switch (mode)
	{
		case 0 :
			youhao = youliang / (juli / 100) ;	
			printf("Fuel consumption is %.2lf liters per 100 km.\n" , youhao);
			break ;
		case 1 :
			youhao = juli / youliang ;
			printf("Fuel consumption is %.2lf miles per gallon.\n" , youhao);
			break ;
	}
}

/*pel2-2a.h*/
/*同文件夹下包含此头文件*/
void set_mode(int mode);
void get_info(void);
void show_info(void);

no3.c

//重新设计编程练习2,要求只使用自动变量.该程序提供的用户界面不变,即提示用户输入模式等.
//但是函数调用要作相应变化
# include <stdio.h>
# include "pel3-3a.h"

int main(void)
{
	int mode ;
	int t_mode ;
	double juli ;
	d
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值