C++ Primer Plus 第六版 第三章练习答案

C++ Primer Plus 第六版 第三章练习答案

  1. 变量名要求;

  2. sizeof() 和 头文件climits;
    –> SHRT_MAX
    –> USHRT_MAX

  3. 数据溢出 (轮回);

  4. 多进制使用与表示;

  5. const限定符;

  6. float表示格式:+5.4E+16;

  7. 强制转换的格式:
    –> (typename) value
    –> typename (value)
    –> static_cast<typename> (value)

  8. auto使用;

  9. cout.setf(ios_base::fixed, ios_base::floatfield) 以及 setw

       其中setw(int n)是指在输出时分配了n个字符的输出宽度,然后默认的是在n个字符宽度中右对齐输出。

int s = 10;
cout << setw(5) << s << "\n";

                                                 在这里插入图片描述


#include <iostream>
using namespace std;

//practice 3.1
const int Foot2Inch = 12;
void p3_1(void){
	int height_inch = 0;
	int height_foot = 0;
	cout << "Please input you height in inches:___\b\b\b";
	cin >> height_inch;
	height_foot = height_inch / Foot2Inch;

	cout << "Your height in foot and inch is: " << height_foot << " feet " << height_inch % Foot2Inch << " inches!" << endl;
	return;
}


// practice 3.2
#include<math.h>
const double Inch2Meter = 0.0254;
const double Kilo2Pound = 2.2;
//const int Foot2Inch = 12;    //上面已经定义过
void p3_2(void){
	double height_foot = 0.0;
	double height_inch = 0.0;
	double weight_pound = 0.0;
	double BMI = 0.0;

	cout << "Enter your height in foot and inch" << endl;
	cout << "First enter the foot: ";
	cin >> height_foot;
	cout << "Then enter the inch: ";
	cin >> height_inch;
	cout << "Enter you weight in pound: ";
	cin >> weight_pound;

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

	BMI = weight_kilo / pow(height_meter, 2);
	cout << "Your BIM is " << BMI << endl;
}


// practice 3.3
const double trans = 60.0;
void p3_3(void){
	int degrees = 0;
	int minutes = 0;
	int seconds = 0;
	double latitude = 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;

	latitude = degrees + minutes / trans + seconds / pow(trans, 2);
	cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << latitude << " degrees." << endl;
}


// practice 3.4
void p3_4(void){
	long long total_seconds = 0;
	cout << "Enter the number of seconds: ";
	cin >> total_seconds;
	int seconds = (total_seconds % 60);
	int minutes = (total_seconds - seconds) % (60 * 60) / 60;
	int hours = (total_seconds % (24 * 60 * 60)) / (60 * 60);
	int days = total_seconds / (24 * 60 * 60);
	
	cout << total_seconds << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << endl;
}


// practice 3.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;  // 此处必须要强制转换,不然rate就是0.0
	cout << "The population of the US is " << rate * 100 << "% of the world population." << endl;
}


// practice 3.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: ";
	cin >> mile;
	cout << "Enter the consumption of oil: ";
	cin >> gallon;

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


// practice 3.7
void p3_7(void){
	double fuel_eu = 0.0;
	double fuel_us = 0.0;

	cout << "Enter the fuel consumption in Europ standard(/100km): ";
	cin >> fuel_eu;

	fuel_us = 62.14 * 3.875 / fuel_eu;
	cout << "the fuel consumption in US standard is " << fuel_us << "mpg" << endl;
}


int main(int argc, char **argv) {
	cout << "=====================================\n"
		<< "============  Chapter3:  ============\n"
		<< "=====================================\n\n";
	cout << "==>> Practice 3_1:\n";
	p3_1();
	cout << endl;

	cout << "==>>Practice 3_2:\n";
	p3_2();
	cout << endl;

	cout << "==>>Practice 3_3:\n";
	p3_3();
	cout << endl;

	cout << "==>>Practice 3_4:\n";
	p3_4();
	cout << endl;

	cout << "==>>Practice 3_5:\n";
	p3_5();
	cout << endl;

	cout << "==>>Practice 3_6:\n";
	p3_6();
	cout << endl;

	cout << "==>>Practice 3_7:\n";
	p3_7();
	cout << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值