C++ Primer Plus 第六版编程练习——第3章

★★★★★备注★★★★★

使用的编译环境为 Visual Studio 2017   

默认省略了如下内容:                           

     #include "stdafx.h"                           
     #include <iostream>                               

     using namespace std;

1、How could you use C++ to find out which character the code 88 represents? 

int main(int argc, const char*argv[])
{
	char c = 88;
	cout << c << endl;
	cout.put(char(88)) << endl;
	cout << char(88) << endl;
	cout << (char)88 << endl;

	return 0;
}
2、 Write a short program that asks for your height in integer inches and then converts your height to feet and inches. Have the program use the underscore character to indicate where to type the response.Also use a const symbolic constant to represent the conversion factor.
const int feet_to_inch = 12;

int main(int argc, const char*argv[])
{
	int height;
	cout << "Please enter your height: ___\b\b\b";
	cin >> height;
	int feet = height / feet_to_inch;
	int inch = height % feet_to_inch;
	cout << "Your height is " << feet << " feet " << inch << " inch." << endl;

	return 0;
}
3、 Write a short program that asks for your height in feet and inches and your weight in pounds. (Use three variables to store the information.) Have the program report your body mass index (BMI).To calculate the BMI, first convert your height in feet and inches to your height in inches (1 foot = 12 inches).Then convert your height in inches to your height in meters by multiplying by 0.0254.Then convert your weight in pounds into your mass in kilograms by dividing by 2.2. Finally, compute your BMI by dividing your mass in kilograms by the square of your height in meters. Use symbolic constants to represent the various conversion factors.
const int feet_to_inch = 12;
const double inch_to_meter = 0.0254;
const double kg_to_pound = 2.2;

int main(int argc, const char*argv[])
{
	int ht_ft;
	int ht_in;
	int weight;
	cout << "First, please enter your height of feet part:_\b";
	cin >> ht_ft;
	cout << "Second, please enter your height of inch part:__\b\b";
	cin >> ht_in;
	cout << "Now, please enter your weight in pound: ___\b\b\b";
	cin >> weight;

	ht_in = ht_ft * feet_to_inch + ht_in;
	double ht_meter = ht_in * inch_to_meter;
	double wt_kg = weight / kg_to_pound;
	double bmi = wt_kg / (ht_meter * ht_meter);
	cout << "Your height is " << ht_in << " inches, and " << ht_meter << " meter." << endl;
	cout << "Your weight is " << weight << " pounds, and " << wt_kg << " kg." << endl;
	cout << "Your BMI is: " << bmi << endl;

	return 0;
}
4、 Write a program that asks the user to enter a latitude in degrees, minutes, and seconds and that then displays the latitude in decimal format.There are 60 seconds of arc to a minute and 60 minutes of arc to a degree; represent these values with symbolic constants.You should use a separate variable for each input value.A sample run should look like this:
    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
const int degree_to_min = 60;
const int min_to_sec = 60;

int main(int argc, const char*argv[])
{
	int degree;
	int min;
	int sec;
	double ret_degree;
	cout << "Enter a latitude in degrees, minutes, and seconds: " << endl;
	cout << "First, enter the degrees: ";
	cin >> degree;
	cout << "Next, enter the minutes of arc: ";
	cin >> min;
	cout << "Finally, enter the seconds of arc: ";
	cin >> sec;
	ret_degree = degree + (double)min / degree_to_min + (double)sec / (degree_to_min * min_to_sec);
	cout << degree << " degrees, " << min << " minutes, " << sec << " seconds = " << ret_degree << " degrees." << endl;

	return 0;
}
5、 Write a program that asks the user to enter the number of seconds as an integer value (use type long , or, if available, long long ) and that then displays the equivalent time in days, hours, minutes, and seconds. Use symbolic constants to represent the number of hours in the day, the number of minutes in an hour, and the number of seconds in a minute.The output should look like this:
    Enter the number of seconds: 31600000
    31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds
const int day_to_hour = 24;
const int hour_to_min = 60;
const int min_to_sec = 60;

int main(int argc, const char*argv[])
{
	long long sec_num;
	int day;
	int hour;
	int min;
	int sec;

	cout << "Enter the number of seconds: ";
	cin >> sec_num;
	sec = sec_num % min_to_sec;
	min = sec_num / min_to_sec;
	hour = min / hour_to_min;
	min %= hour_to_min;
	day = hour / day_to_hour;
	hour %= day_to_hour;
	cout << sec_num << " seconds = " << day << " days, " << hour << " hours, " << min << " minutes, " << sec << " seconds" << endl;

	return 0;
}
6、 Write a program that requests the user to enter the current world population and the current population of the U.S. (or of some other nation of your choice). Store the information in variables of type long long . Have the program display the percent that the U.S. (or other nation’s) population is of the world’s population.The output should look something like this:
    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.
You can use the Internet to get more recent figures.
int main(int argc, const char*argv[])
{
	long long World_population;
	long long US_population;
	double percent;
	
	cout << "Enter the world's population: ";
	cin >> World_population;
	cout << "Enter the population of the US: ";
	cin >> US_population;
	percent = (double)US_population / World_population * 100;
	cout << "The population of the US is " << percent << "% of the world population." << endl;

	return 0;
}







猜你喜欢

转载自blog.csdn.net/wenfei11471/article/details/80616520
今日推荐