重新设计练习2中的程序,使它仅使用自动变量。程序提供相同的用户界面,也就是说,要提示用户输入模式等等。然而,您还必须给出一组不同的函数调用。
练习2中的程序:
//*********************************************************************** //pe12-2a.c #include <stdio.h> #include "pe12-2a.h" static int present_mode = METRIC; static double distance; static double fuel; void set_mode(int mode) { if (mode==METRIC || mode==US) present_mode = mode; else printf("Invalid mode specified. Mode %s used.\n", present_mode == METRIC? "0(METRIC)" : "1(US)"); } void get_info(void) { if (present_mode==METRIC) { printf ("Enter distance traveled in kilometers:"); scanf ("%lf",&distance); printf ("Enter fuel consumed in liters:"); scanf ("%lf",&fuel); } else { printf ("Enter distance traveled in miles:"); scanf ("%lf",&distance); printf ("Enter fuel consumed in gallons:"); scanf ("%lf",&fuel); } } void show_info(void) { if (present_mode==METRIC) printf ("Fuel consumption is %g liters per 100 km.\n", fuel / (distance/100) ); else printf ("Fuel consumption is %g miles per gallon.\n", distance / fuel ); } //*********************************************************************** //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; } //*********************************************************************** //pe12-2a.h #define METRIC 0 #define US 1 void set_mode(int); void get_info(void); void show_info(void);