资源描述:
《《C Primer Plus》第六版 第十二章编程练习答案.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1.#includeintcritic(void);intmain(intargc,char*argv[]){intnum=56;intunits;printf("Howmanypoundstoafirkinofbutter?");scanf("%d",&units);while(units!=num){units=critic();}getchar();return0;}intcritic(){intn;printf("Noluck,myfriend.Tryagain.");scanf("%d",&n);returnn;}2.//******
2、*****pe12-2a.h*******//#includevoidset_mode(intmode);voidget_info();voidshow_info();//**********pe12-2a.c*************//#include#include"pe12-2a.h"intmode;floatdistance,fuel;voidset_mode(intm){if(m!=0&&m!=1){printf("Invalidmodespecified.Mode1(US)used.");m=1;}mode=m;}v
3、oidget_info(){if(0==mode){printf("Enterdistancetraveledinkilometers:");scanf("%f",&distance);printf("Enterfuelconsumedinliters:");scanf("%f",&fuel);}else{printf("Enterdistancetraveledinmiles:");scanf("%f",&distance);printf("Enterfuelconsumedingallons:");scanf("%f",&fuel);}}voidshow_info(
4、){floatunits;if(0==mode){units=100*(fuel/distance);printf("Fuelconsumedinliters:%.1fper100km",units);}else{units=distance/fuel;printf("Fuelconsumedis%.1fmilespergallon",units);}}3.//***********pe12-2a.h*******//#includevoidset_mode(intmode);voidget_info(intmode);voidshow_inf
5、o(intmode,floatdistance,floatfuel);//***********pe12-2a.c*******//#include#include"pe12-2a.h"voidset_mode(intmode){if(mode!=0&&mode!=1){printf("Invalidmodespecified.Mode1(US)used.");mode=1;}get_info(mode);}voidget_info(intmode){floatdistance,fuel;if(0==mode){printf("Enterdista
6、ncetraveledinkilometers:");scanf("%f",&distance);printf("Enterfuelconsumedinliters:");scanf("%f",&fuel);}else{printf("Enterdistancetraveledinmiles:");scanf("%f",&distance);printf("Enterfuelconsumedingallons:");scanf("%f",&fuel);}show_info(mode,distance,fuel);}voidshow_info(intmode,floatd
7、istance,floatfuel){floatunits;if(0==mode){units=100*(fuel/distance);printf("Fuelconsumedinliters:%.1fper100km",units);}else{units=distance/fuel;printf("Fuelconsumedis%.1fmilespergallon",units);}}//***********pe12-2b.c*******//#include#include"pe12-2a.h"intmai