c++ primer plus(第6版)中文版 第三章编程练习答案

本文提供了一系列编程练习题的答案,包括身高英寸到英尺英寸的转换、BMI计算、时间单位转换等,通过这些练习帮助读者更好地理解和掌握编程基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第三章编程练习答案

3.1编写一个程序,要求输入身高(__英寸),转换为英尺英寸

//3.1编写一个程序,要求输入身高(__英寸),转换为英尺英寸
#include <iostream>
using namespace std;

int main () 
{     
	const int	incun = 12;	// 1英尺 = 12英寸
	int	inches;
	cout << "输入身高(__英寸):";
	cin >> inches;
	cout << "你的身高为:" << inches / incun << "英尺又" << inches % incun << "英寸";	
	cout << endl;
} 

3.2编写程序,输入身高英尺英寸和体重,计算BMI

//3.2编写程序,输入身高英尺英寸和体重,计算BMI
#include <iostream>
using namespace std;

int main () 
{     
	double	height, weight, BMI;
	int feet, inches;
	cout << "输入身高,__英尺又__英寸:";
	cin >> feet >> inches;
	// 1英尺 = 12英寸,1英寸*0.0254
	height = feet * 12+ inches;	
	height = height * 0.0254;
	int	pounds;
	cout << "输入以磅为单位的体重:";
	cin >> pounds;
	// 1磅 = 1/2.2千克 
	weight = pounds * 1/2.3;
	BMI = weight / (height * height);
	cout << "BMI:" << BMI<< endl;
} 


3.3输入一个纬度(度,分,秒),输出度

//3.3输入一个纬度(度,分,秒),输出度
#include <iostream>
using namespace std;

int main ()
{     
	int	degrees, minutes, seconds;
	cout << "Enter a latitude in degrees, minutes, and seconds." << endl;
	cout << "First, enter the degrees: ";
	cin >> degrees;
	cout << "Next, enter the minutes of arc: ";
	cin >> minutes;
	cout << "Finally, enter the seconds of arc: ";
	cin >> seconds;
	cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = "
		   << degrees + 1.0*minutes/60 + 1.0*seconds/60/60<< endl;
} 

3.4输入秒,输出天,时,分,秒

//3.4输入秒,输出天,时,分,秒
#include <iostream>
using namespace std;

int main () 
{ 
	cout << "Enter the number of seconds: ";
	long	secondsTotal;
	cin >> secondsTotal;
	long	seconds = secondsTotal % 60;
	long	minutesTotal = secondsTotal /60;
	long	minutes = minutesTotal % 60;
	long	hoursTotal = minutesTotal / 60;
	long	hours = hoursTotal % 24;
	long	days = hoursTotal / 24;
	cout << secondsTotal <<  " seconds = " << days << " days, " << hours << " hours, " 
			<< minutes << " minutes, " << seconds << " seconds" << endl;
} 

3.5输入世界和美国人口,计算美国人口百分比

//3.5输入世界和美国人口,计算美国人口百分比
#include <iostream>
using namespace std;

int main () 
{     
	long long	worldPopulation, usaPopulation;
	cout << "Enter the world's population: ";
	cin >> worldPopulation;
	cout << "Enter the population of the US: ";
	cin >> usaPopulation;
	cout << "The population of the US is " << 100.0 * usaPopulation / worldPopulation << "% of the world population.";
	cout << endl;
} 

3.6输入行驶公里数(公里)和耗油量(升),输出油耗(升/100公里)

//3.6输入行驶公里数(公里)和耗油量(升),输出油耗(升/100公里)
#include <iostream>
using namespace std;

int main () 
{     
	cout << "输入行驶公里数(公里)和耗油量(升):";
	double	distance, petrol;
	cin >> distance >> petrol;
	cout << "油耗为" << 100 * petrol / distance << "(升/100公里)";
	cout << endl;
} 

3.7转换欧洲耗油量为美式表示

//3.7转换欧洲耗油量为美式表示
#include <iostream>
using namespace std;

int main () 
{     
	cout << "输入欧洲标准的油耗(升/100公里):";
	double	gasConsumptionInEuropean;
	cin >> gasConsumptionInEuropean;
	const double	k_factorKmToMile = 0.6214;
	const double	k_factorLiterToGallon = 1/3.875;
	cout << gasConsumptionInEuropean << "(升/100公里)=" << 100 * k_factorKmToMile / (gasConsumptionInEuropean * k_factorLiterToGallon) << "(英里/加仑)";
	cout << endl;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值