C++ Primer Plus(第六版) 第三章 编程练习

编程环境:Visual Studio 2017

1:

//practice 1
#include<iostream>
using namespace std;

const int InchToFoot = 12;

int main()
{
	int inch = 0;
	int foot = 0;
	cout << "Please enter your height in inch:___\b\b\b";
	cin >> inch;
	foot = inch / InchToFoot;
	inch = inch % InchToFoot;
	cout << "Your height in foot and inch is " << foot << " foot " << inch << " inch." << endl;
	cin.get();
	cin.get();
	return 0;
}

 2:

//practice 2
#include<iostream>
using namespace std;

const double FootToInch = 12;
const double KgToPound = 2.2;
const double InchToMeter = 0.0254;

int main()
{
	double foot = 0.0;
	double inch = 0.0;
	double meter = 0.0;
	double pound = 0.0;
	double kg = 0.0;
	double BMI = 0.0;

	cout << "Please enter your height in foot and inch" << endl;
	cout << "The foot:";
	cin >> foot;
	cout << "The inch:";
	cin >> inch;
	cout << "Then please enter your weight in pound:";
	cin >> pound;
	
	meter = (foot*FootToInch + inch)*InchToMeter;
	kg = pound / KgToPound;

	BMI = kg / (meter*meter);

	cout << "Your BMI is " << BMI << endl;
	system("pause");
	return 0;
}

3: 

//practice 3
#include<iostream>
using namespace std;

const double DegreeToMinute = 60.0;
const double MinuteToSecond = 60.0;

int main()
{
	int degree = 0;
	int minute = 0;
	int second = 0;
	double total = 0.0;

	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 >> minute;
	cout << "Finally,enter the seconds of arc:";
	cin >> second;

	total = degree + minute / DegreeToMinute + second / (DegreeToMinute * MinuteToSecond);
	
	cout << degree << " degrees, " << minute << " minutes, " << second << " seconds = " << total << " degrees." << endl;
	system("pause");
	return 0;
}

4: 

//practice 4
#include<iostream>
using namespace std;

const int DayToHour = 24;
const int HourToMinute = 60;
const int MinuteToSecond = 60;

int main()
{
	long input_seconds = 0;
	int days = 0;
	int hours = 0;
	int minutes = 0;
	int seconds = 0;

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

	days = input_seconds / (DayToHour * HourToMinute * MinuteToSecond);
	hours = (input_seconds % (DayToHour * HourToMinute * MinuteToSecond)) / (HourToMinute * MinuteToSecond);
	minutes = (input_seconds % (HourToMinute * MinuteToSecond)) / MinuteToSecond;
	seconds = input_seconds % MinuteToSecond;

	cout << input_seconds << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << endl;
	system("pause");
	return 0;
}

5: 

//practice 5
#include<iostream>
using namespace std;

int main()
{
	long long world_pop = 0;
	long long US_pop = 0;
	double rate = 0.0;

	cout << "Enter the world's population :";
	cin >> world_pop;
	cout << "Enter the population of the US:";
	cin >> US_pop;

	rate = double(US_pop) / double(world_pop);

	cout << "The population of the US is " << rate * 100 << "% of the world population." << endl;
	system("pause");
	return 0;
}

 6:

//practice 6
#include<iostream>
using namespace std;

int main()
{
	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 in gallon: ";
	cin >> gallon;

	mile_per_gallon = mile / gallon;
	cout << "Average fuel comsuption: " << mile_per_gallon << " mile/gallon" << endl;

	system("pause");
	return 0;
}

7: 

//practice 7
#include<iostream>
using namespace std;

int main()
{
	double eu = 0.0;
	double us = 0.0;

	cout << "Enter the fuel comsuption in Europ standard: ";
	cin >> eu;

	us = eu / 19 * 12.41;
	cout << "the fuel comsuption in US standard is " << us << "/100KM" << endl;
	system("pause");
	return 0;
}

才疏学浅,如有错误或者改进的建议还请大牛多多指出。

继续学习,慢慢更新。

猜你喜欢

转载自blog.csdn.net/qq_42212893/article/details/82288370
今日推荐