c++ primer 学习之路 (10) 3.7课后编程习题 英寸英尺转换 BMI

3.7 编程练习

1 英寸英尺转换

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

#include<iostream>
#include<climits>
using namespace std;
int main()
{
 const  int tran = 12;//转换因子1英尺=12英寸
 cout << "Please enter your height:____\b\b\b\b";//\b是退格符
 int hight;
 cin >> hight;
 cout <<"Your hight is "<< hight<<" inches " << endl;
 int feet = hight / tran;
 int inches = hight%tran;
 cout<<"Your hight is " << feet << " feet "<< inches<<" inches." << endl;
 system("pause");
 return 0;
}

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

#include<iostream>
#include<climits>
using namespace std;
int main()
{
 cout << "Please enter your height for feet:____\b\b\b\b";//\b是退格符
 int feet;
 cin >> feet;
 cout << "Please enter your height for inches:____\b\b\b\b";
 int inches;
 cin >> inches;
 cout << "Please enter your weight for pounds:____\b\b\b\b";
 double pounds;
 cin >> pounds;
 int inch = feet * 12 + inches;
 double mi = inch*0.0254;
 cout <<"Your hight is "<< mi<<" m " << endl;
 double KG = pounds / 2.2;
 cout << "Your weight is " << KG << " kg " << endl;
 double BMI = KG /( mi*mi);
 cout << "Your BMI is " << BMI  << endl;
 
 
 system("pause");
 return 0;
}



猜你喜欢

转载自blog.csdn.net/zhangfengfanglzb/article/details/80511586
今日推荐