BMI指数测量——c++ 1.0.0
BMI
身体质量指数(BMI,Body Mass Index)是国际上常用的衡量人体肥胖程度和是否健康的重要标准,主要用于统计分析。肥胖程度的判断不能采用体重的绝对值,它天然与身高有关。因此,BMI 通过人体体重和身高两个数值获得相对客观的参数,并用这个参数所处范围衡量身体质量。
计算公式
体重指数BMI=体重/身高的平方(国际单位kg/㎡)
理想BMI(18.5~23.9) = 体重(单位Kg) ÷ 身高的平方 (单位m)根据世界卫生组织定下的标准,亚洲人的BMI(体重指标Body Mass Index)若高于22.9便属于过重。亚洲人和欧美人属于不同人种,WHO的标准不是非常适合中国人的情况,为此制定了中国参考标准:
来源:百度百科
程序可以这么写,先创建两个个文本文档:AldBMI.txt
里面写:
low 0 18.4
right 18.5 23.9
big 24.0 27.9
large 28.0 100.0
和StuBMI.txt
low 10.0 14.6
right 14.7 21.8
big 21.9 24.5
large 24.8 100.0
代表学生版和成人版BMI数据
我们把下限设为0,上限设为100,这样以免麻烦
代码:
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include <iomanip>
#include "windows.h"
using namespace std;
void OUTlevel(string s)
{
if(s == "low")
cout << "您的BMI等级为:" << "偏瘦" << endl;
else if(s == "big")
cout << "您的BMI等级为:" << "超重" << endl;
else if(s == "large")
cout << "您的BMI等级为:" << "肥胖" << endl;
}
void CheckAndOut(double AnsBMI, double high, double weight)//最后检查输出
{
const double rightWeightB = 21.8 * (high * high);//常量定义,正常重量边界
const double rightWeightS = 14.7 * (high * high);//常量定义,正常重量边界
if(rightWeightS > AnsBMI)
cout << "您还需要加" << rightWeightS - AnsBMI << "Kg" << endl;
else if(rightWeightB < AnsBMI)
cout << "您还需要减" << AnsBMI - rightWeightB << "Kg" << endl;
}
struct BMI_ALL{//结构体定义,
string lv;//等级
double score_small;//小成绩
double score_big;//大成绩
};
void BMI_STUDENT();
void BMI_ALDULT();//提前声明
void BMI_m()//menu主界面
{
while(true){
cout << "--------BMI test--------" << endl;
cout << "aldult [1]" << endl;
cout << "student [2]" << endl;
cout << "exit [0]" << endl;
cout << "------------------------" << endl;
cout << ">>> ";
int Choose;
cin >> Choose;//无等待输入-conio.h
switch (Choose)
{
case 1:
system("cls");
BMI_ALDULT();
break;
case 2:
system("cls");
BMI_STUDENT();
break;
case 0:
goto loop;
}
}
loop:
return;
}
void BMI_STUDENT()
{
ifstream fin("StuBMI.txt");
double high, weight;
cout << "BMI for aldult:" << endl;
cout << "tips:输入时如无小数点加.0" << endl;
cout << "input your high(M)" << endl;
cin >> high;
cout << "input your weight(kg)" << endl;
cin >> weight;
double BMIn = weight / (high * high);//19.65
BMI_ALL student[4];
for(int i = 0; i < 3; i++){
fin >> student[i].lv >> student[i].score_small >> student[i].score_big;//录入等级
}/*
for(int i = 0; i < 3; i++){
cout << student[i].lv << student[i].score_small << student[i].score_big << endl;
}*/
for(int i = 0; i < 3; i++)
if(BMIn >= student[i].score_small && BMIn <= student[i].score_big){
cout << "BMI值=" << BMIn << endl;
OUTlevel(student[i].lv);
if(i == 1)
cout << "恭喜!达到正常水平!" << endl;//是否达到正常水平
else
CheckAndOut(BMIn, high, weight);//否则不正常-二阶查看
//cout << "check loop" << endl;
}
//cout << "check loop" << endl;
system("pause");
system("cls");
return;
}
void BMI_ALDULT()
{
ifstream fin("AldBMI.txt");
double high, weight;
cout << "BMI for aldult:" << endl;
cout << "tips:输入时如无小数点加.0" << endl;
cout << "input your high(M)" << endl;
cin >> high;
cout << "input your weight(kg)" << endl;
cin >> weight;
double BMIn = weight / (high * high);//19.65
BMI_ALL student[4];
for(int i = 0; i < 3; i++){
fin >> student[i].lv >> student[i].score_small >> student[i].score_big;//录入等级
}/*
for(int i = 0; i < 3; i++){
cout << student[i].lv << student[i].score_small << student[i].score_big << endl;
}*/
for(int i = 0; i < 3; i++)
if(BMIn >= student[i].score_small && BMIn <= student[i].score_big){
cout << "BMI值=" << BMIn << endl;
OUTlevel(student[i].lv);
if(i == 1)
cout << "恭喜!达到正常水平!" << endl;//是否达到正常水平
else
CheckAndOut(BMIn, high, weight);//否则不正常-二阶查看
//cout << "check loop" << endl;
}
//cout << "check loop" << endl;
system("pause");
system("cls");
return;
}
int main()
{
BMI_m();
return 0;
}
详细文件、代码、exe文件链接:
https://download.csdn.net/download/cool99781/12149691
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 想让你多爱自己一些的开源计时器
· Cursor预测程序员行业倒计时:CTO应做好50%裁员计划
· 大模型 Token 究竟是啥:图解大模型Token
· 如何在 .NET 中 使用 ANTLR4
· 用99元买的服务器搭一套CI/CD系统