cpp primer plus chapter 3 programming exercise

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
bool PE[7] = {0,0,0,0,0,0,1};
//PE is the switch to the programming exercise.
void underscore_back() {
	int underscore_number = 10;
	for (int i = 0; i < underscore_number; i++) {
		cout << "_";
	}
	for (int i = 0; i < underscore_number; i++) {
		cout << "\b";
	}
}
double input(string& s, double&d)
{
	cout << "Enter the " << s << ": ";
	underscore_back();
	cin >> d;
	return d;
}
int main()
{
	{
		enum valn {
			height_inch, height_feet, height_meter,
			weight_pound, weight_kg
		};
		string val[]{ "height_inch","height_feet","height_meter",
		"weight_pound","weight_kg" };
		double d[7]{};
		if (PE[0])
		{
			input(val[height_inch], d[height_inch]);
			cout << "Your height is " << int(d[height_inch]) / 12 << " feet and "
				<< int(d[height_inch]) % 12 << " inches.\n";
		}
		if (PE[1])
		{
			input(val[height_feet], d[height_feet]);
			d[height_inch] =
				input(val[height_inch], d[height_inch])
				+ d[height_feet] * 12.;
			d[height_meter] =
				0.0254 * d[height_inch];

			d[weight_kg]=
				input(val[weight_pound], d[weight_pound]) / 2.2;

			double BMI =
				d[weight_kg] / d[height_meter] / d[height_meter];

			cout << "The BMI is " << BMI << ".\n" << endl;


		}
	}
	if (PE[2])
	{
		enum { degrees, minutes, seconds };
		double d[3]{};
		cout << "Enter a latitude in degrees, minutes, and seconds:\n"
			<< "First, enter the degrees : ";
		cin >> d[degrees];
			
		cout << "Next, enter the minutes of arc : ";
		cin >> d[minutes];

		cout << "Finally, enter the seconds of arc : ";
		cin >> d[seconds];

		cout << d[degrees] << " degrees, "
			<< d[minutes] << " minutes, "
			<< d[seconds] << " seconds = " <<
			d[degrees] + d[minutes] / 60. + d[seconds] / 3600
			<< " degrees";
	}
	if (PE[3])
	{
		string prompt{ " number of seconds" };
		enum{day,hour,min,sec};
		double d[4]{0};
		string s[]{ " days, ", " hours, "," minutes, "," seconds"};
		
		input(prompt, d[sec]);
		
		cout <<long(d[sec]) << " seconds = ";

		enum{sec_day,sec_hour,sec_min};
		long l_convert[]{ (60 * 60 * 24),(60*60),60 };

		for (int i = 0; i < 3; i++)
		{

			d[i] = long(d[sec] / l_convert[i]);
			d[sec] = long(d[sec]) % l_convert[i];
			cout <<long( d[i]) << s[i];
		}
		cout<< long(d[sec]) << s[sec];

	}
	if (PE[4])
	{
		string p[]{ "world's population","population of the US" };
		double d[2]{};
		
		for (int i = 0; i < 2; i++)
		{
			input(p[i], d[i]);
		}
		cout << "The population of the US is "
			<< d[1] / d[0] * 100. <<
			"% of the world population.";
			
	}
	{
		string s[]
		{
		"miles you have driven",
		"gallons of gasoline you have used",
		"the miles per gallon",
		"liters per 100 kilometers"
		};
		double d[4]{};

		if (PE[5])
		{
			for (int i = 0; i < 2; i++)
			{
				input(s[i], d[i]);
			
			}
			cout << s[2] << ": " << d[0] / d[1];
		}
		if (PE[6])
		{
			input(s[3], d[3]);
			cout << s[2] << ": "
				<<long(62.14 / (d[3] / 3.875));
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值