C++ Primer Plus 第六版 第三章练习答案
-
变量名要求;
-
sizeof() 和 头文件climits;
–> SHRT_MAX
–> USHRT_MAX -
数据溢出 (轮回);
-
多进制使用与表示;
-
const
限定符; -
float表示格式:+5.4E+16;
-
强制转换的格式:
–> (typename) value
–> typename (value)
–> static_cast<typename> (value) -
auto
使用;
其中
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;
}