《C++ Primer Plus 第六版》编程练习参考答案(第三章)

本文提供了一系列C++编程实例,专注于不同单位之间的转换,包括长度、重量、角度、时间和百分比等。通过具体代码示例,展示了如何将英寸转换为英尺和英寸、计算BMI指数、转换经纬度、秒转换为天时分秒、人口百分比计算以及汽车油耗计算。

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

第三章

第一题:

#include <iostream>
using namespace std;
const int TransFactor=12;

int main()
{
    int height=0;
    int inch=0;
	int foot=0;
	
	cout<<"Enter the height of you in inch: ___\b\b\b";
    cin>>height;
    foot=height/TransFactor;
    inch=height%TransFactor;
    cout<<"Your height is "<<foot<<" foot and "<<inch<<" inch";
}

第二题:

#include <iostream>
using namespace std;
const double HeightTrans=0.0254;
const double WeightTrans=2.2;
const int InchFootTrans=12;

int main()
{
	int inch=0;
	int foot=0;
	double pound=0;
	double meter=0;
	double kilogram=0;
	double BMI;
	
	cout<<"Enter the height of you in foot and inch: ";
	cin>>foot>>inch;
	meter=HeightTrans*(foot*InchFootTrans+inch);
	cout<<"Enter the weight of you in pound: ";
	cin>>pound;
	kilogram=pound/WeightTrans;
	
	BMI=kilogram/(meter*meter);
	cout<<"The BMI index of you is: "<<BMI;
}

第三题:

#include <iostream>
using namespace std;
const double TransFactor=60;

int main()
{
	double degrees=0;
	double minutes=0;
	double seconds=0;
	double degrees_trans=0;
	
	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;
	
	degrees_trans=degrees+minutes/TransFactor+seconds/(TransFactor*TransFactor);
	cout<<degrees<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds= "
	<<degrees_trans<<" degrees."; 
}

第四题:

#include <iostream>
using namespace std;
const long TransFactorMinutes=60;
const long TransFactorHours=60*60;
const long TransFactorDays=60*60*24;

int main()
{
	long seconds_full=0;
	long days=0;
	long hours=0;
	long minutes=0;
	long seconds=0;
	
	cout<<"Enter the number of seconds: ";
	cin>>seconds_full;
	days=seconds_full/TransFactorDays;
	hours=(seconds_full%TransFactorDays)/TransFactorHours;
	minutes=((seconds_full%TransFactorDays)%TransFactorHours)/TransFactorMinutes;
	seconds=((seconds_full%TransFactorDays)%TransFactorHours)%TransFactorMinutes;
	
	cout<<seconds_full<<" seconds= "<<days<<" days, "<<hours<<" hours, "
	<<minutes<<" minutes, "<<seconds<<" seconds.";
}

第五题:

#include <iostream>
using namespace std;

int main()
{
	long long USA=0;
	long long world=0;
	double percent=0;
	
	cout<<"Enter the world's population: ";
	cin>>world;
	cout<<"Enter the population of the US: ";
	cin>>USA;
	percent=((double)USA/world)*100;
	cout<<"The population of the US is "
	<<percent<<"% of the world population.";	
}

第六题:

#include <iostream>
using namespace std;

int main()
{
	double distance;
	double gasoline;
	double consumption;
	
	cout<<"Enter the distance in kilometer: ";
	cin>>distance;
	cout<<"Enter the gasoline in litre: ";
	cin>>gasoline;
	consumption=(gasoline/distance)*100;
	cout<<"The gasoline consumption of your car is "
	<<consumption<<" litre per 100 kilometer";	
}

第七题:

#include <iostream>
using namespace std;
const double DistanceTrans=62.14;
const double GasolineTrans=3.875;

int main()
{
	double litre=0;
	double kilometer=0;
	double EUconsumption=0;
	double gallon=0;
	double mile=0;
	double USconsumption=0;
	
	cout<<"Enter the distance in kilometer: ";
	cin>>kilometer;
	cout<<"Enter the gasoline in litre: ";
	cin>>litre;
	EUconsumption=(litre/kilometer)*100;
	cout<<"In EU, The gasoline consumption of your car is "
	<<EUconsumption<<" litre per 100 kilometer"<<endl;
	
	gallon=litre/GasolineTrans;
	mile=(kilometer/100)*DistanceTrans;
	USconsumption=mile/gallon;
	cout<<"In the US, The gasoline consumption of your car is "
	<<USconsumption<<" mile per gallon";
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值