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