c++ primer plus第三章programming exercises答案

c++ primer plus第三章programming exercises答案

3.1

//programming exercise 3.1
#include 
int main()
{
	using namespace std;
	const int Feet_to_inch = 12;
	cout << "Please enter your height in intteger inches: ___\b\b\b";
	int input_inch;
	cin >> input_inch;
	int foot = input_inch / Feet_to_inch;
	int output_inch = input_inch % Feet_to_inch;
	cout << "Your height in inches was: " << input_inch << "inches." << endl;
	cout << "Your height in foot and inch was : " << foot << " foot " << output_inch << " inches!" << endl;

	cin.get();
	cin.get();
	return 0;
}

3.2

//programming exercise 3.2
#include 
int main()
{
	using namespace std;
	const int Feet_to_inch = 12;
	const double Inch_to_meter = 0.0254;
	const double Kilo_to_poun = 2.2;

	cout << "Please enter your height in foot and inches" << endl;
	cout << "First enter the foot: ";
	double height_foot;
	cin >> height_foot;
	cout << "Then enter the inches: ";
	double height_inch;
	cin >> height_inch;

	cout << "Please enter you weight in pound: ";
	double weight_pound;
	cin >> weight_pound;

	double height_meter = (height_foot * Feet_to_inch + height_inch) * Inch_to_meter;
	double weight_kilo = weight_pound / Kilo_to_poun;

	double BMI = weight_kilo / (height_meter * height_meter);

	cout << "Your BMI is " << BMI << endl;
	cin.get();
	cin.get();
	return 0;
}

3.3

//programming exercise 3.3
#include 
int main()
{
	using namespace std;
	const double Min_to_sec = 60;
	const double Deg_to_min = 60;

	cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
	cout << "First, enter the degrees: ";
	int degrees;
	cin >> degrees;
	cout << "Next, enter the minutes of arc: ";
	int minutes;
	cin >> minutes;
	cout << "Finally, enter the seconds of arc: ";
	int seconds;
	cin >> seconds;

	double total = degrees + minutes / Deg_to_min + seconds / Deg_to_min / Min_to_sec;
	cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << total << " degrees" << endl;

	cin.get();
	cin.get();
	return 0;
}

3.4

//programming exercise 3.4
#include 
int main()
{
	using namespace std;
	const int Hours_in_day = 24;
	const int Mins_in_hour = 60;
	const int Secs_in_min = 60;

	cout << "Enter the number of seconds: ";
	long long totseconds;
	cin >> totseconds;

	int days = totseconds / (Hours_in_day * Mins_in_hour * Secs_in_min);
	int hours = (totseconds % (Hours_in_day * Mins_in_hour * Secs_in_min)) / (Mins_in_hour * Secs_in_min);
	int minutes = (totseconds % (Mins_in_hour * Secs_in_min)) / Secs_in_min;
	int seconds = totseconds % Secs_in_min;

	cout << totseconds << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << endl;
	cin.get();
	cin.get();
	return 0;
}

3.5

//programming exercise 3.5
#include 
int main()
{
	using namespace std;
	cout << "Enter the world's population: ";
	long long world_pop;
	cin >> world_pop;

	cout << "Enter the population of the US: ";
	long long US_pop;
	cin >> US_pop;

	double rate = double(US_pop) / world_pop;
	cout << "The population of the US is " << rate * 100 << "% of the world population." << endl;

	cin.get();
	cin.get();
	return 0;
}

3.6

//programming exercise 3.6
#include 
int main()
{
	using namespace std;
	cout << "Enter the distance in mile you drive: ";
	double mile;
	cin >> mile;
	cout << "Enter the comsumption of oil: ";
	double gallon = 0.0;
	cin >> gallon;
	double mile_per_gallon = mile / gallon;
	cout << "Average fuel comsuption: " << mile_per_gallon << " mile/gallon " << endl;

	cin.get();
	cin.get();
	return 0;
}

3.7

//programming exercise 3.7
#include 
int main()
{
	using namespace std;
	cout << "Enter the fuel comsuption in Europ standard: ";
	double fuel_comsuption_eu;
	cin >> fuel_comsuption_eu;
	double fuel_comsuption_us = 3.875 * 62.14 / fuel_comsuption_eu;
	cout << "The fuel comsuption in US standard is " << fuel_comsuption_us << "/100km" << endl;

	cin.get();
	cin.get();
	return 0;
}

你可能感兴趣的:(c++编程练习)