//主函数
#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;
}
//pe12-2a.h 头文件
void set_mode(int number); //模式选择函数
void get_info(void); //输入距离/油耗函数
void show_info(void); //显示结果函数
//pe12-2a.c文件
#include<stdio.h>
#include “pe12-2a.h”
static int mode; //静态储存,文件内部链接(模式)
static double distance; //同上 (距离)
static double oil_consumption; //同上 (总油耗)
void set_mode(int number)
{
if(number==0||number==1)
mode=number;
if((number!=0&&number!=1)&&(mode==0||mode==1))
{
if(mode==0)
printf("Invalid mode specified.Mode 0(metric) used.\n");
if(mode==1)
printf("Invalid mode specified.Mode 1(US) used.\n");
}
}
void get_info(void)
{
if(mode == 0)
{
printf(“Enter distance traveled in kilometers:”);
scanf("%lf",&distance);
printf(“Enter fuel consumed in liters:”);
scanf("%lf",&oil_consumption);
}
if(mode==1)
{
printf(“Enter distance traveled in miles:”);
scanf("%lf",&distance);
printf(“Enter fuel consumed in gallons:”);
scanf("%lf",&oil_consumption);
}
}
void show_info(void)
{
if(mode == 0)
printf(“Fuel consumptionis %.2lf liters per 100 km.\n”,oil_consumption/distance*100);
if(mode==1)
printf(“Fuel consumption is %.1lf miles per gallon.\n”,distance/oil_consumption);
}