1.编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为 英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换 因子。
#include <iostream>
int main()
{
using namespace std;
const int F_PER_I = 12; // 一英尺等于12英寸
int height;
cout << "Enter the height with inch: ____";
cout << "\b\b\b\b\b";
cin >> height;
cout << "The height is " << height / F_PER_I << " feet ";
cout << "and " << height % F_PER_I << " inches." << endl;
return 0;
}
2.编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用 3个变量来存储这些信息。) 该程序报告其BMI(Body Mass Index,体重指数)。为了计算 BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为 以米为单位的身高(1 英寸= 0. 0254 米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1 千克= 2. 2 磅)。最后,计算相应的 BMI— 体重(千克)除以身高(米)的平方。用符号 常量表示各种转换因子。
#include <iostream>
int main()
{
using namespace std;
float feet, inch, pound;
float BMI;
const int F_PER_I = 12; // 1英尺等于12英寸
const float M_PER_I = 0.0254f; // 1英尺等于0.0254米
const float P_PER_K = 2.2f; // 1千克等于2.2磅
cout << "Enter the height with feet and inches:\n";
cin >> feet >> inch;
cout << "Enter the weight with pound:\n"