C++ Prime Plus 编程练习 第三章

2022/3/20 20:34:29

本文主要是介绍C++ Prime Plus 编程练习 第三章,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1. 整数输入身高英寸,转为英尺英寸

#include <iostream>

int main()
{
	using namespace std;
	int inch;
	const int inch2foot = 12;
	cout << "Enter your height of inch:_\b";
	cin >> inch;
	cout << "your inch: " << inch % inch2foot << endl;
	cout << "your foot: " << inch / inch2foot << endl;

	return 0; 
}

2. 计算BMI

#include <iostream>

int main()
{
	using namespace std;
	int inch;
	int foot;
	int bang;
	const int inch2foot = 12;
	const double inch2m = 0.0254;
	const double bang2k = 2.2;
	cout << "Enter your height of inch:_\b";
	cin >> inch;
	cout << "Enter your height of foot:_\b";
	cin >> foot;
	cout << "Enter your height of bang:_\b";
	cin >> bang;
	double result = (bang / bang2k) / (foot * inch2foot / inch2m);
	cout << "your result: " << result << endl;

	return 0; 
}

3. 计算纬度

#include <iostream>

int main()
{
	using namespace std;
	int degrees{};
	int minutes{};
	int seconds{};
	const int cov = 60;
	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;
	double all_degrees = degrees + double(minutes) / cov +
		double(seconds) / cov / cov;
	cout << degrees << " degrees, "
		<< minutes << " minutes, "
		<< seconds << " seconds = "
		<< all_degrees << " degreses";

	return 0; 
}

4. 将秒转为天数,小时,分钟,秒

#include <iostream>

int main()
{
	using namespace std;
	int days{};
	int hours{};
	int minutes{};
	int seconds{};
	const int time_cov = 60;
	const int hours2day = 24;
	long long all_minutes{};

	cout << "enter the number of seconds: ";
	cin >> all_minutes;
	days = all_minutes / (hours2day * time_cov * time_cov);
	long dayofminutes_r = all_minutes - days * hours2day * time_cov * time_cov;
	hours = dayofminutes_r / (time_cov * time_cov);
	long hourofminutes_r = dayofminutes_r - hours * time_cov * time_cov;
	minutes = hourofminutes_r / time_cov;
	seconds = hourofminutes_r - minutes * time_cov;

	cout << all_minutes << " seconds = "
		<< days << " days, "
		<< hours << " hours, "
		<< minutes << " minutes, "
		<< seconds << " seconds";
	return 0; 
}

5. 人口占比

#include <iostream>

int main()
{
	using namespace std;
	long long world_pop{};
	long long us_pop{};
	cout << "Enter the world's population: ";
	cin >> world_pop;
	cout << "Enter the population of the US: ";
	cin >> us_pop;
	double ratio = double(us_pop) / world_pop;
	cout << "The population of the US is "
		<< ratio * 100 << "% of the world population.";
	return 0; 
}

6. 一加仑的里程

#include <iostream>

int main()
{
	using namespace std;
	double mile{};
	double gallon{};
	cout << "Enter the mile: ";
	cin >> mile;
	cout << "Enter the gallon: ";
	cin >> gallon;
	cout << "The car cost: " << mile / gallon << " mpg.";
	return 0; 
}

7. 欧洲风格的耗油量和美国风格的转换

#include <iostream>

int main()
{
	using namespace std;
	double mile{};
	double gallon{};
	const double km2mile = 62.14;
	const double gallon2 = 3.875;
	double sheng{};
	cout << "Enter the p100km: ";
	cin >> sheng;
	mile = km2mile;
	gallon = sheng / gallon2;
	cout << "The car cost: " << mile / gallon << " mpg.";
	return 0; 
}



这篇关于C++ Prime Plus 编程练习 第三章的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程