C++ Primer Pluse 课后习题答案 第三章

1. 例程3.1 limits.cpp

#include <iostream>
#include<climits>

int main()
{
    using namespace std;

    int n_int = INT_MAX;
    short n_short = SHRT_MAX;
    long n_long = LONG_MAX;
    long long n_llong = LLONG_MAX;

    cout << "int is " << sizeof(int) << " bytes." << endl;
    cout << "short is " << sizeof(short) << " bytes." << endl;
    cout << "long is " << sizeof(long) << " bytes." << endl;
    cout << "long long is " << sizeof(long long) << " bytes." << endl;

    cout << "Maximum values: "  << endl;
    cout << "int: " << n_int << endl;
    cout << "short: " << n_short << endl;
    cout << "long: " << n_long << endl;
    cout << "long long: " << n_llong << endl;

    cout << "Minimum values: " << endl;
    cout << "Bits per byte = " << CHAR_BIT << endl;

    return 0;
}

2. 习题

#include <iostream>
using namespace std;
//--------------Practice 3.1--------------------
const int Foot2Inch = 12;
void p3_1(void)
{
	int inch_input = 0;
	int inch_output = 0;
	int foot_output = 0;

	cout << "Please input your height in inches: __\b\b";
	cin >> inch_input;
	inch_output = inch_input % Foot2Inch;
	foot_output = inch_input / Foot2Inch;
	cout << "Your height is " << foot_output << " foots and " << inch_output << " inches." << endl;
}
//--------------Practice 3.2--------------------
const double Inch2Meter = 0.0254;
const double Kilo2Pound = 2.2;
void p3_2(void)
{
	double height_foot = 0.0;
	double height_inch = 0.0;
	double height_meter = 0.0;
	double weight_pound = 0.0;
	double weight_kilo = 0.0;
	double BMI = 0.0;

	cout << "Please input your height in foot and height: " << endl;
	cout << "The foot: ";
	cin >> height_foot;
	cout << "The inch: ";
	cin >> height_inch;
	cout << endl << "Then please input your weight in pound: ";
	cin >> weight_pound;

	weight_kilo = weight_pound / Kilo2Pound;
	height_meter = (height_foot * Foot2Inch + height_inch) * Inch2Meter;
	BMI = weight_kilo / (height_meter * height_meter);

	cout << "Your BMI is : " << BMI << endl;
}
//--------------Practice 3.3--------------------
void p3_3(void)
{
	int degrees = 0;
	int minutes = 0;
	int seconds = 0;
	double total = 0.0;

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

	total = degrees + minutes / 60.0 + seconds / 3600.0;
	cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << total << " degrees." << endl;
}

// practice 4
void p3_4(void)
{
	long long total_seconds = 0;
	int days = 0;
	int hours = 0;
	int minutes = 0;
	int seconds = 0;

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

	days = total_seconds / (24 * 60 * 60);
	hours = ((total_seconds % (24 * 60 * 60)) / (60 * 60));
	minutes = ((total_seconds % (60 * 60)) / 60);
	seconds = (total_seconds % 60);

	cout << total_seconds << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << endl;
}
// practice 5
void p3_5(void)
{
	long long world_population = 0;
	long long US_population = 0;
	double rate = 0.0;

	cout << "Enter the world's population :";
	cin >> world_population;

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

	rate = double(US_population) / (world_population);
	cout << "The population of the US is " << rate * 100 << "% of the world population." << endl;
}
// practice 6
void p3_6(void)
{
	double mile = 0.0;
	double gallon = 0.0;
	double mile_per_gallon = 0.0;

	cout << "Enter the distance in mile you drive: ";
	cin >> mile;
	cout << "Enter the comsumption of oil: ";
	cin >> gallon;

	mile_per_gallon = mile / gallon;
	cout << "Average fuel comsuption: " << mile_per_gallon << " mile/gallon" << endl;
}
// practice 7
const double kmtomile = 62.14;
const double ltogallon = 3.875;
void p3_7(void)
{
	double Europe, US;
	cout << "Enter oil consumption rate in Europe: ";
	cin >> Europe;
	US = kmtomile /(Europe / ltogallon);
	cout << "The oil consunption rate in US: " << US << " mile/gallon" << endl;
}

int main()
{
	p3_7();

	return 0;
}

  

posted @ 2019-10-05 20:21  咸鲸鱼  阅读(174)  评论(0)    收藏  举报