【C++】C++ primer plus 编程练习—第3章

这些C++程序实现了不同度量单位之间的转换,包括身高(英寸到英尺)、体重(磅到千克,计算BMI)、角度(度、分、秒到度)、时间和日期(秒到天、小时、分钟)、以及汽车燃料消耗(升/100公里到英里/加仑)。
摘要由CSDN通过智能技术生成

//3-1

#include <iostream>

int main()
{
	using namespace std;
	const int conver=12;
	int height;
	
	cout<<"Enter your height in inches:___\b\b\b";
	cin>>height;
	cout<<"Your height is "<<height/conver<<" feet "<<height%conver<<" inches."<<endl;
	
	return 0;
}

//3-2

#include <iostream>

int main()
{
	using namespace std;
	const int inch_per_feet=12;
	const float pound_per_kg=2.2;
	const float meter_per_inch=0.0254;
	
	float height_f;
	float height_i;
	float height_m;
	float weight;
	
	cout<<"Enter your height in X feet and y inches,plesae."<<endl;
	cout<<"feet:";
	cin>>height_f;
	cout<<"inches:";
	cin>>height_i;
	cout<<"Enter your mass in pound:";
	cin>>weight;
	
	height_m=(height_i+height_f*inch_per_feet)*meter_per_inch;
	weight/=pound_per_kg;
	cout<<"Your BMI is "<<weight/(height_m*height_m)<<endl;
	
	return 0;
}

//3-3

#include <iostream>

int main()
{
	using namespace std;
	const int conver=60;
	float mdegree;
	float mmin;
	float msec;
	
	cout<<"Enter a latitude in degrees,minutes,and seconds:"<<endl;
	cout<<"First,enter the degrees:"; cin>>mdegree;
	cout<<"Next,enter the minutes of arc:"; cin>>mmin;
	cout<<"Finally,enter the seconds of arc:"; cin>>msec;
	
	cout<<mdegree<<" degrees, "<<mmin<<" minutes, "<<msec<<" seconds = ";
	cout<<mdegree+mmin/conver+msec/(conver*conver)<<" degrees"<<endl;
	return 0;
}

//3-4

#include <iostream>

int main()
{
	using namespace std;
	const int hour_per_day=24;
	const int min_per_hour=60;
	const int sec_per_min=60;
	long long msec;
	
	cout<<"Enter the number of seconds:";
	cin>>msec;
	
	cout<<msec<<" seconds = "<<msec/(hour_per_day*min_per_hour*sec_per_min)<<" days, ";
	msec%=(hour_per_day*min_per_hour*sec_per_min);
	
	cout<<msec/(min_per_hour*sec_per_min)<<" hours, ";
	msec%=(min_per_hour*sec_per_min);
	
	cout<<msec/sec_per_min<<" minutes, ";
	msec%=sec_per_min;
	cout<<msec<<" seconds"<<endl;

	return 0;
}

//3-5

#include <iostream>

int main()
{
	using namespace std;
	long long popul_w;//世界人口 
	long long popul_a;//美国人口 
	
	cout<<"Enter the world's population:";
	cin>>popul_w;
	cout<<"Enter the population of the US:";
	cin>>popul_a;
	cout<<"The population of the US is "<<float(popul_a)/float(popul_w)*100;
	cout<<"% of the world population."<<endl;

	return 0;
}

//3-6

#include <iostream>

int main()
{
	using namespace std;
	float dist;
	float fuel;
	
	cout<<"Enter your distanse in km:";
	cin>>dist;
	cout<<"Enter the consumption of fuel in litre:";
	cin>>fuel;
	cout<<"The fuel consumption per 100km is "<<fuel/dist*100<<endl;

	return 0;
}

//3-7

#include <iostream>

int main()
{
	using namespace std;
	const float mile_per_100km=62.14;
	const float liter_per_gal=3.875;
	float dist_g;
	float fuel_g;
	float consum_e;//欧洲单位 (litre per 100km); 
	
	cout<<"Enter your car's fuel consumpion(litre per 100km):";
	cin>>consum_e;
	dist_g=mile_per_100km;
	fuel_g=consum_e/liter_per_gal;
	cout<<"The US stanfrad fuel consumpton is "<<dist_g/fuel_g<<endl;

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值