n\
return 0; }
下面是一些输出示例:
Enter 0 for metric mode, 1 for US mode: 0 Enter distance traveled in kilometers: 600 Enter fuel consumed in liters:
Fuel consumption is liters per 100 km.
Enter 0 for metric mode, 1 for US mode (-1 to quit): 1 Enter distance traveled in miles: 434 Enter fuel consumed in gallons: Fuel consumption is miles per gallon.
Enter 0 for metric mode, 1 for US mode (-1 to quit): 3 Invalid mode specified. Mode 1(US) used. Enter distance traveled in miles: 388 Enter fuel consumed in gallons: Fuel consumption is miles per gallon.
Enter 0 for metric mode, 1 for US mode (-1 to quit): -1 Done.
如果用户键入了不正确的模式,程序向用户给出提示信息并选取最接近的模式。请提供一个头文件和源代码文件,来使程序可以运行。源代码文件应定义3个具有文件作用域、内部链接的变量。一个代表模式,一个代表距离,还有一个代表消耗的燃料。函数get_info()根据模式设置提示输入相应的数据,并将用户的回答存入文件作用域变量。函数show_info()根据所选的模式计算并显示燃料消耗值。
Mode %s used.\\n\ }
void get_info(void) {
if (present_mode==METRIC) {
printf (\ scanf (\
printf (\
scanf (\ } else {
printf (\ scanf (\
printf (\ scanf (\ } }
void show_info(void) {
if (present_mode==METRIC)
printf (\ else
printf (\ } n\
return 0; }
Mode %s used.\\n\ }
void get_info(int mode, int present_mode, double *p_disance, double *p_fuel) {
if (present_mode==METRIC) {
printf (\ scanf (\
printf (\
scanf (\ } else {
printf (\ scanf (\
printf (\ scanf (\ } }
void show_info(int present_mode, double distance, double fuel) {
if (present_mode==METRIC)
printf (\ else
printf (\ } n\
return 0; } 18
How many sides and how many dice? 6 3
Here are 18 sets of 3 6-sided throws.
12 10 6 9 8 14 8 15 9 14 12 17 11 7 10 13 8 14
How many sets? Enter q to stop. q
#include <> #include <> #include <>
int main(void) {
char set,side,dice,i,sum;
puts(\ while(scanf(\ {
srand( time(0) );
puts(\ while(scanf(\ {
scanf(\ while (set--) {
for (i=0,sum=0; i puts(\ } return 0; } 8.下面是某程序的一部分: n\ return 0; } 给出函数make_array()和show_array()的定义以使程序完整。函数make_array()接受两个参数。第一个是int数组的元素个数,第二个是要赋给每个元素的值。函数使用malloc()来创建一个适当大小的数组,把每个元素设定为指定的值,并返回一个数组指针。函数show_array()以8个数一行的格式显示数组内容。 #include <> #include <> int * make_array (int elem, int val); void show_array (const int ar[], int n); int main(void) { int * pa; int size; int value; printf (\ scanf (\ while (size > 0) { printf (\ scanf (\ pa = make_array (size, value); if (pa) { show_array (pa,size); free(pa); } printf(\ scanf(\ } printf(\ return 0; } int * make_array (int elem, int val) { int *p; p = (int *)malloc( elem * sizeof(int) ); while(elem--) *(p + elem) = val; return p; } void show_array (const int ar[], int n) { int i; for (i=0;i printf(\ if(i%8==7) printf(\ } }