C++ Primer plus第六版 3.7编程练习

1.

#include <iostream>

int main() {
	using namespace std;
	//1英寸(in)=2.54厘米(cm),12英寸为1英尺
	const float Convert_in = 2.54;
	const int Convert_foot = 12;
	int high = 0;
	int inch = 0;
	int foot = 0;
	cout << "请输入您的身高(cm):____\b\b\b\b";
	cin >> high;
	inch = high / Convert_in;
	foot = inch / Convert_foot;
	inch = inch % Convert_foot;
	cout << "您的身高转换结果为: " << foot << " 英尺 " << inch << " 英寸" << endl;
 	return 0;
}

2.

#include <iostream>

int main() {
	using namespace std;
	//1英寸(in)=2.54厘米(cm),12英寸为1英尺,1kg约为2.2磅,BMI的正常值在18.5-23.9之间
	const float Convert_in = 2.54;
	const int Convert_foot = 12;
	const float Convert_pound = 2.2;
	int high_foot = 0;
	int high_inch = 0;
	float high_metre = 0.0;
	float weight = 0.0;
	float bml = 0.0;
	cout << "请输入您的身高英尺数部分(foot):____\b\b\b\b";
	cin >> high_foot;
	cout << endl;
	cout << "请输入您的身高英寸数部分(inch):____\b\b\b\b";
	cin >> high_inch;
	cout << endl;
	cout << "请输入您的体重的磅数(pound):____\b\b\b\b";
	cin >> weight;
	cout << endl;
	high_metre = (((high_foot * Convert_foot) + high_inch) * Convert_in) / 100;
	weight = weight / Convert_pound;
	bml = weight / (high_metre * high_metre);
	cout << "您的BML指数为: " << bml << endl;
	if (bml < 18.5 || bml > 23.9) {
		cout << "您的BML指数不在正常范围内";
	}
	else {
		cout << "您的BML指数正常";
	}
 	return 0;
}

3.

#include <iostream>

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

4.

#include <iostream>

int main() {
	using namespace std;
	const int OneDayTime = 24;
	const int OneHourTime = 60;
	const int OneMinuteTime = 60;
	long long seconds = 0;
	long long seconds_temp = 0;
	int inDays = 0;
	int inMinutes = 0;
	int inHours = 0;
	int inSeconds = 0;
	cout << "Enter the number of seconds: ";
	cin >> seconds;
	seconds_temp = seconds;
	inDays = seconds_temp / (OneMinuteTime * OneHourTime * OneDayTime);
	seconds_temp -= static_cast<long long>(inDays) * (OneMinuteTime * OneHourTime * OneDayTime);
	inHours = seconds_temp / (OneMinuteTime * OneHourTime);
	seconds_temp -= static_cast<long long>(inHours) * (OneMinuteTime * OneHourTime);
	inMinutes = seconds_temp / OneMinuteTime;
	inSeconds = seconds % OneMinuteTime;
	cout << seconds << " seconds " << " = " << inDays << " days, "
		<< inHours << " hours, " << inMinutes << " minutes, " 
		<< inSeconds << "seconds.";

 	return 0;
}

5.

#include <iostream>

int main() {
	using namespace std;
	long long ChinaPopulation = 0;
	long long allPopulation = 0;
	float per = 0.0;
	cout << "请输入世界人口总数: ";
	cin >> allPopulation;
	cout << "请输入中国目前总人口数量: ";
	cin >> ChinaPopulation;
	per = static_cast<long double>(ChinaPopulation) / allPopulation;
	cout << "中国人口占世界人口的百分比为: " << per * 100 << "% " << "of the world population.";
 	return 0;
}

6.

#include <iostream>

int main() {
	using namespace std;
	float dis = 0.0;
	float oilVolume = 0.0;
	float result = 0.0;
	cout << "请输入您的驱车里程(km): ";
	cin >> dis;
	cout << "请输入您使用的汽油量(L): ";
	cin >> oilVolume;
	result = oilVolume / (dis / 100);
	cout << "每100公里的耗油量(升): " << result << "(L/100km)";
 	return 0;
}

7.

#include <iostream>

int main() {
	using namespace std;
	float dis = 0.0;
	float oilVolume = 0.0;
	float result1 = 0.0;
	float result2 = 0.0;
	cout << "请输入您的驱车里程(km): ";
	cin >> dis;
	cout << "请输入您使用的汽油量(L): ";
	cin >> oilVolume;
	result1 = oilVolume / (dis / 100);
	result2 = dis / oilVolume;
	cout << "(欧洲风格)" << "每100公里的耗油量: " << result1 << "(L/100km)";
	cout << "(美国风格)" << "每升燃油行驶公里数: " << result2 << "(km/L)";
 	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值