C++ premier plus 第六版 编程练习解答(第三章)

1.编写一个小程序,要求用户使用一个整数指出自己的身高,然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。

#include<iostream>
int main()
{
	using namespace std;
	const float Rate = 12.0;
	int inch;
	float feet;
	cout << "您的身高为____英寸\b\b\b\b\b\b\b\b";
	cin >> inch;
	feet=(float) inch / Rate;
	cout << "您的身高为 " << feet << " 英尺" << endl;
	return 0;
}

2.编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。

#include<iostream>
int main()
{
	using namespace std;

	const float RateA = 12.0;
	const float RateB = 0.0254;
	const float RateC = 2.2;
	float feet,inch,meter,pound,kg,bmi;

	//输入部分
	cout << "请您按英尺、英寸、磅的顺序输入身高体重:" << endl;
	cout << "英尺: ";
	cin >> feet ;
	cout << "英寸: ";
	cin >> inch;
	cout << "磅: ";
	cin >> pound;

	//计算部分
	inch = inch + feet * RateA;
	meter = inch * RateB;
	kg = pound * RateC;
	bmi = meter * meter / kg;

	//显示部分
	cout << "你的BMI指数为: " << bmi << endl;

	return 0;
}

3.编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。

#include<iostream>
int main()
{
	using namespace std;
	const float Rate = 60.0;
	float degree,minute,second,final;

	//输入部分
	cout << "Enter a latitude in degrees, minutes, and seconds :" << endl << "First, enter the degrees: ";
	cin >> degree;
	cout << "Next, enter the minutes of arc: ";
	cin >> minute;
	cout << "Finally, enter the seconds of arc: ";
	cin >> second;

	//计算部分
	final = (float) degree + minute / Rate + second /Rate / Rate;

	//输出部分
	cout << degree << " degrees, " << minute << " minutes, " << second << " second = " << final << " degrees" << endl;

	return 0;
}

4.编写一个程序,要求用户以整数方式输入秒数,然后以天、小时、分钟和秒的方式显示这段时间。

#include<iostream>
int main()
{
	using namespace std;
	long second,seconds;
	int days,hours,minutes;
	cout << "Enter the number of seconds: ";
	cin >> second;
	seconds = second;
	days = seconds / ( 60*60*24 );
	seconds = seconds % ( 60*60*24 );
	hours = seconds / 3600;
	seconds = seconds % 3600;
	minutes = seconds / 60;
	seconds = seconds % 60;
	cout << second << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << endl;
	return 0;
}

5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口。将这些信息储存在long long 变量中,并让程序显示美国的人口占全球人口的百分比。

#include<iostream>
int main()
{
	using namespace std;
	long long world,us;
	float rate;
	cout << "Enter the world's population: ";
	cin >> world;
	cout << "Enter the population of the US: ";
	cin >> us;
	rate = (float) us / world * 100;
	cout << "The population of the US is " << rate << "% of  the world population" << endl;
	return 0;
}

6.编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。

#include<iostream>
int main()
{
	using namespace std;
	float distance,gasoline,result;
	cout << "Enter the distance: ";
	cin >> distance;
	cout << "Enter the gasoline consumption: ";
	cin >> gasoline;
	result = (float) distance / gasoline;
	cout << "The car can run " << result << " miles with 1 gallon gasoline." << endl;
	return 0;
}

7.编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量(每加仑多少英里)。

#include<iostream>
int main()
{
	using namespace std;
	float us,eu;
	cout << "Please input the fuel consumption according to the European style:" << endl << "Fuel consumption is:____/100km\b\b\b\b\b\b\b\b\b\b";
	cin >> eu;
	us = (float) 62.4 / eu * 3.875;
	cout << "The fuel consumption converted to American style is: " << us << "mpg" << endl;
	return 0;
}
发布了17 篇原创文章 · 获赞 10 · 访问量 419

猜你喜欢

转载自blog.csdn.net/acslsr/article/details/104012485