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

}

相关推荐
祈安_5 小时前
C语言内存函数
c语言·后端
郑州光合科技余经理2 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1232 天前
matlab画图工具
开发语言·matlab
dustcell.2 天前
haproxy七层代理
java·开发语言·前端
norlan_jame2 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054962 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
czy87874752 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
遥遥江上月2 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237172 天前
C语言-数组练习进阶
c语言·开发语言·算法