C++ Primer Plus (第6版)习题之处理数据

习题3.1

#include<iostream>
using namespace std;

int main()
{
	const int FeetTransInches = 12;
	int height_inches, height_feet, remain_inches;
	cout << "Please enter height(inches):__\b\b ";
	cin >> height_inches;
	height_feet = height_inches / FeetTransInches;
	remain_inches = height_inches % FeetTransInches;
	cout << "My height is " << height_feet << " feet, and " << remain_inches << " inches.\n";

	return 0;
}

执行结果

Please enter height (inches): 68
My height is 5 feet, and 8 inches.

习题3.2

#include<iostream>
using namespace std;

int main()
{
	const int FeetTransInches = 12;
	const double InchesTransMeter = 0.0254;
	const double KiloTransPound = 2.2;
	int height_feet, height_inches;
	double weight_pound, weight_kilo;
	double height_meter;
	double BMI;
	cout << "Enter your height of feet part: ";
	cin >> height_feet;
	cout << "Enter your height of inches part: ";
	cin >> height_inches;
	cout << "Enter your weight (Pound):";
	cin >> weight_pound;
	height_meter = (height_feet * FeetTransInches + height_inches) * InchesTransMeter;
	weight_kilo = weight_pound / KiloTransPound;
	BMI = weight_kilo / (height_meter * height_meter);
	cout << "Your height is " << height_meter << " (meter)\n";
	cout << "Your weight is " << weight_kilo << " (kilogram)\n";
	cout << "Your body mass index (BMI) is " << BMI << endl;

	return 0;
}

执行结果

Enter your height of feet part: 5
Enter your height of inches part: 12
Enter your weight (Pound): 140
Your height is 1.8288 (meter)
Your weight is 63.6364 (kilogram)
Your body mass index (IBM) is 19.0271

习题3.3

#include<iostream>
using namespace std;

int main()
{
	const int deg_of_min = 60;
	const int min_of_sec = 60;
	int degress, minutes, seconds;
	double degr;
	cout << "Enter a latitude in degrees, minutes, and seconds:\n";
	cout << "First, enter the degrees: ";
	cin >> degress;
	cout << "Next, enter the minutes of arc: ";
	cin >> minutes;
	cout << "Finally, enter the seconds of arc: ";
	cin >> seconds;
	degr = degress + double(minutes) / deg_of_min + double(seconds) / (deg_of_min * min_of_sec);
	cout << degress << " degrees, " << minutes << " minutes, " << seconds << " seconds = " <<  degr << " degrees";
	return 0;
}

执行结果

Enter a latitude in degrees, minutes, and seconds:
First, enter the degrees: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19 seconds = 37.8553 degrees

习题3.4

#include<iostream>
using namespace std;

int main()
{
	const int day_of_hours = 24, hour_of_minutes = 60, minute_of_seconds = 60;
	long seconds;
	int Days, Hours, Minutes, Seconds;
	cout << "Enter the number of seconds: ";
	cin >> seconds;
	Days = seconds / (day_of_hours * hour_of_minutes * minute_of_seconds);
	Hours = (seconds % (day_of_hours * hour_of_minutes * minute_of_seconds)) / (hour_of_minutes * minute_of_seconds);
	Minutes = ((seconds % (day_of_hours * hour_of_minutes * minute_of_seconds)) % (hour_of_minutes * minute_of_seconds)) / (minute_of_seconds);
	Seconds = ((seconds % (day_of_hours * hour_of_minutes * minute_of_seconds)) % (hour_of_minutes * minute_of_seconds)) % (minute_of_seconds);
	cout << seconds << " seconds = " << Days << " days, " << Hours << " hours, " << Minutes << " minutes, " << Seconds << " seconds\n";

	return 0;
}

执行结果

Enter the number of seconds: 31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds

习题3.5

#include<iostream>
using namespace std;

int main()
{
	long long World_Popula, US_Popula;
	double rate;
	cout << "Enter the world's population: ";
	cin >> World_Popula;
	cout << "Enter the population of the US: ";
	cin >> US_Popula;
	rate = double(US_Popula) / World_Popula * 100;
	cout << "The population of the US is " << rate << "% of the world population.\n";
	return 0;
}

执行结果

Enter the world's population: 6898758899
Enter the population of the US: 310783781
The population of the US is 4.50492% of the world population
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值