c++ primer 学习之路 (10) 3.7课后编程习题 英寸英尺转换 BMI
2018-06-01 10:52
295 查看
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;
}
相关文章推荐
- c++ primer(第五版)学习笔记及习题答案代码版(第十四章)重载运算与类型转换
- bootstrap 学习之路 10 (javascript DOM 编程)
- python学习之路-10 网络编程之进阶
- C++ Primer 学习笔记与思考_10 类型转换易错处大总结
- 编程小白的学习之路(一)
- C++ Primer 学习笔记:类型转换
- python核心编程学习笔记-2016-08-03-01-习题9-9
- c++ primer(第五版)学习笔记及习题答案代码版(第一章)
- [置顶] UNIX学习之路 一步一个脚印之socket编程/Socket的参数
- Java多线程编程--(10)学习Java5.0 并发编程包--线程工具类
- C++ Primer 学习笔记_63_满载操作符与转换 -转换与类类型【上】
- 讲座:编程学习之路
- 我的网络编程学习之路——getsockname 和 getpeername 函数
- windows编程学习之路--双缓冲绘图
- Java学习笔记10 网络编程
- Linux下的C语言编程——10进制转换成任意进制的字符串
- [C++ 学习] C++ primer 第4版 习题4.19
- c++ primer 学习之路 (11) 4.1数组
- C++ Primer学习系列(5):复制控制/重载操作与转换/面向对象编程
- 《linux c 编程一站式学习》课后部分习题解答