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);

}

相关推荐
移幻漂流几秒前
Lua开发实践指南:从宿主差异到精通之路
开发语言·junit·lua
进阶小白猿2 分钟前
Java技术八股Day19
java·开发语言
啟明起鸣2 分钟前
【C++ 面向对象编程】补档:线程池和 MySQL 连接池的设计模式分析
开发语言·c++·mysql
七夜zippoe3 分钟前
Python内存管理深潜:从引用计数到GC机制的全面优化实战
开发语言·python·gc·分代回收·内存池
jghhh019 分钟前
三维热传导方程和泊松方程的有限元方法MATLAB实现
开发语言·算法·matlab
一叶之秋14129 分钟前
Qt常用控件(三)
开发语言·qt
百***074511 分钟前
GPT-5.2:重构通用智能边界,迈向生产级AI新纪元
开发语言·python·gpt
froginwe1111 分钟前
RDF 规则:构建语义网的基础
开发语言
_OP_CHEN13 分钟前
【从零开始的Qt开发指南】(二十三)Qt 界面优化之绘图实战:解锁自定义界面的无限可能
开发语言·qt
郝学胜-神的一滴19 分钟前
使用QVideoWidget实现高效视频播放:从基础到高级应用
开发语言·c++·qt·程序人生·音视频