C Primer Plus(第六版)12.9 编程练习 第2题

//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

void set_mode(int mode);

void get_info(void);

void show_info(void);

//pe12-2a.c

#include "pe12-2a.h"

#include <stdio.h>

static float distance=0.0;

static float fuel_consumed=0.0;

static int Mode;

void set_mode(int mode)

{

if(mode == 1 || mode == 0)//只有输入正确才保存到Mode

Mode=mode;

}

void get_info(void)

{

if(Mode==0)

{

printf("Enter distance traveled in kilometers:");

scanf("%f",&distance);

printf("Enter fuel consumed in liters: ");

scanf("%f",&fuel_consumed);

}

else if(Mode==1)

{

printf("Enter distance traveled in miles:");

scanf("%f",&distance);

printf("Enter fuel consumed in gallons: ");

scanf("%f",&fuel_consumed);

}

}

void show_info(void)

{

if(Mode==0)

printf("Fuel consumption is %.2f liters per 100 km.\n",fuel_consumed/distance*100);

else if(Mode==1)

printf("Fuel consumption is %.1f miles per gallon.\n",distance/fuel_consumed);

}

相关推荐
晓蛋1 天前
c语言指的是什么意思
c语言·编译器·编程开发·集成开发环境·程序实例
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
傲世仙尊1 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
牵猫散步的鱼儿1 天前
重载、重写(覆盖)、重定义区别
c语言
伞伞悦读1 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
phltxy1 天前
C 语言指针:从内存地址到灵活的数据访问
c语言
C语言小火车1 天前
C/C++ 为什么需要编译器?
开发语言·c++
phltxy1 天前
C 语言中的数据存储:从类型到二进制位
c语言
霍霍的袁1 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
孙启超1 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust