C++ Primer Plus 第6版 第3章 编程练习答案

例题记录:

1. 程序清单3.8  floatnum.cpp

// floatnum.cpp -- floating-point types
#include <iostream>

int main()
{
	using namespace std;
	cout.setf(ios_base::fixed, ios_base::floatfield); 
	//此调用迫使输出使用定点表示法,以便更好地了解精度,它防止程序把较大的值切换为E表示法;
	//并使程序显示到小数点后6位
	//通常cout会删除结尾的零
	float tub = 10.0 / 3.0;
	double mint = 10.0 / 3.0;
	const float million = 1.0e6;

	cout << "tub = " << tub;
	cout << ", a million tubs = " << million * tub;
	cout << ",\nand ten million tubs = ";
	cout << 10 * million * tub << endl;

	cout << "mint = " << mint << " and a million mints = ";
	cout << million * mint << endl;
	return 0;
}

课后编程题:

1. 编写一个程序,用户使用一个整数指出自己身高(单位为英寸),将升高转换为英尺和英寸。该程序使用下划线字符来指示用户输入的位置。且使用一个const符号常量来表示转换因子。

注:1英尺=12英寸

// 3-1
#include <iostream>

int main()
{
	using namespace std;
	const int conFactor = 12;   //foot2Inch
	cout << "请输入您的身高:___\b\b\b";  //注意回退
	int height = 0;
	cin >> height;
	int heightFoot = height / conFactor;
	int heightInch = height % conFactor;
	cout << "您的身高为: " << heightFoot << " 英尺," << heightInch << " 英寸。";

	return 0;
}

2.  编写程序,要求以几英尺几英寸的方式输入身高,并以磅为单位输入体重(使用3个变量来存储这些信息),该程序报告其BMI(Body Mass Index,体重指数)。为计算BMI,该程序以英寸的方式指出用户身高,并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米),然后将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后计算相应的BMI——体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。

// 3-2
#include <iostream>

int main()
{
	using namespace std;
	const int foot2Inch = 12;  //1英尺 = 12英寸
	const float inch2Meter = 0.0254;  //1英寸 = 0.0254米
	const float kg2Pound = 2.2;  //1千克 = 2.2磅

	int heightCinFoot = 0, heightCinInch = 0, weightCinPound = 0;
	int heightInch = 0;
	double heightMeter = 0.0, weightKg = 0.0, dBMI = 0.0;

	cout << "请输入身高(英尺):___\b\b\b";
	cin >> heightCinFoot;
	cout << "请输入身高(英寸):___\b\b\b";
	cin >> heightCinInch;
	cout << "请输入体重(磅):___\b\b\b";
	cin >> weightCinPound;

	heightInch = heightCinFoot * foot2Inch + heightCinInch;
	heightMeter = heightInch * inch2Meter;
	weightKg = weightCinPound / kg2Pound;
	dBMI = weightKg / (heightMeter * heightMeter);
	cout << "您的BMI指数为: " << dBMI << endl;

	return 0;
}

3. 编写一个程序,要求用户以度、分、秒输入一个纬度,然后以度为单位显示该维度。1度=60分,1分=60秒,以常量字符的形式表示这些值。对于每个输入值,应使用一个独立的变量存储它。

// 3-3
#include <iostream>

int main()
{
	using namespace std;
	const int degrees2Minutes = 60, minutes2Seconds = 60;

	int degreesCin = 0, minutesCin = 0, secondsCin = 0;
	double degreesCout = 0.0;

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

	degreesCout = degreesCin + double(minutesCin) / degrees2Minutes + double(secondsCin) / minutes2Seconds / degrees2Minutes;

	cout << degreesCin << " degrees, " << minutesCin << " minutes, " << secondsCin << " seconds = " << degreesCout << " degrees." << endl;

	return 0;
}

4. 要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示转换常量。

// 3-4
#include <iostream>

int main()
{
	using namespace std;
	const int day2Hour = 24, hour2Minute = 60, minute2Second = 60;

	long long secondCin = 0;
	cout << "Enter the number of seconds: ";
	cin >> secondCin;

	int dayCout = 0, hourCout = 0, minuteCout = 0, secondCout = 0;
	dayCout = secondCin / (day2Hour * hour2Minute * minute2Second);
	int secondLast = secondCin % (day2Hour * hour2Minute * minute2Second);
	hourCout = secondLast / (hour2Minute * minute2Second);
	secondLast = secondLast % (hour2Minute * minute2Second);
	minuteCout = secondLast / minute2Second;
	secondCout = secondLast % minute2Second;

	cout << secondCin << " seconds = " << dayCout << " days, " << hourCout << " hours, " << minuteCout << " minutes, " << secondCout << " seconds." << endl;

	return 0;
}

5. 要求用户输入全球当前的人口和美国当前的人口,将这些信息存储在long long变量中,并让程序显示美国的人口占全球人口的百分比。

// 3-5
#include <iostream>

int main()
{
	using namespace std;
	long long populationOfWorld = 0, populationOfAmerica = 0;
	cout << "Enter the world's population: ";
	cin >> populationOfWorld;
	cout << "Enter the population of the US: ";
	cin >> populationOfAmerica;

	double proportion = double(populationOfAmerica) / double(populationOfWorld) * 100;
	cout << "The population of the US is " << proportion << "% of the world population.";

	return 0;
}

6. 编写程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加里的里程。其次,让用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果——即每100公里的耗油量(升)。

// 3-6
#include <iostream>

int main()
{
	using namespace std;
	int mileageOfMile = 0, petrolOfGallon = 0;
	cout << "Enter the mileage in mile: ";
	cin >> mileageOfMile;
	cout << "Enter the amount of gasoline used in gallon: ";
	cin >> petrolOfGallon;
	double mileageOfGallon = double(mileageOfMile) / petrolOfGallon;
	cout << "The mileage of a car with a fuel consumption of one gallon is: " << mileageOfGallon;

	return 0;
}

7. 编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量——每加仑多少公里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里=62.14英里,1加仑=3.875升。因此,19mpg大约合12.41/100km,127mpg大约合8.71/100km。

// 3-7
#include <iostream>

int main()
{
	using namespace std;
	double consumptionOfEurope = 0.0;
	cout << "Enter the fuel consumption in European style: ";
	cin >> consumptionOfEurope;
	double mile = 62.14;
	double gallon = consumptionOfEurope / 3.875;
	double consumptionOfAmerica = mile / gallon;
	cout << "The fuel consumption in America style is " << consumptionOfAmerica << "mpg." << endl;

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值