C++ Primer Plus 第6版编程练习 - 第三章答案
持续学习C++ Primer Plus,编程练习,题目和原创答案详见以下code
第一题:
/**
* 编写一个小程序,要求用户使用一个整数指出自己的身高(单位是英寸),
* 然后将身高转换为英尺和英寸。
* 该程序使用下换线字符来指示输入位置。
* 另外,使用一个const符号常量来表示转换因子。
*/
#include <iostream>
using namespace std;
int main(){
const int inchesOfFeet =12;
cout << "Enter your height in inches:____\b\b\b\b";
int heightInInches = 0;
cin >> heightInInches;
cout << "Your height is ";
cout << heightInInches / inchesOfFeet << " feet ";
cout << heightInInches % inchesOfFeet << " inches." << endl;
return 0;
}
第二题:
/**
* 编写一个小程序,要求以几英尺几英寸的方式输入其身高,
* 并以磅为单位输入其体重(使用3个变量来存储这些信息。)
* 该程序报告其BMI(Body Mass Index,体重指数)。
* 为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),
* 并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。
* 然后将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。
* 最后,计算相应的BMI——体重(千克)除以身高(米)的平方。
* 用符号常量表示各种转换因子。
*/
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int feet = 0;
int inches = 0;
int pound = 0;
const int inchesOfFeet = 12;
const