05-类与对象-
题目描述
评估成年人身体健康有多个指标,包括BMI、体脂率等
设计一个身体健康类,包含私有成员:姓名、身高(米)、体重(公斤),腰围(厘米),实现两个公有方法如下:
BMI方法,返回BMI数值(整数),计算公式BMI= 体重 / 身高的平方
体脂率方法,返回体脂率数值(浮点数),计算过程如下:
1)参数a=腰围(cm)×0.74
2)参数b=体重(kg)×0.082+34.89
3)体脂肪重量(kg)=a-b
4)体脂率 = 体脂肪重量÷体重
其它方法根据需要自行定义
输入
第一行输入t表示有t个测试实例
第二行起,每行输入四个参数:姓名、身高、体重,腰围
依次输入t行
输出
输出t行,每行输入一个实例的BMI和体脂率,小数数值精确到小数点后2位,用空格隔开
输入样例
2
张高 1.85 78.5 85.2
李圆 1.55 67.6 77.3
张高的BMI指数为23–体脂率为0.28
李圆的BMI指数为28–体脂率为0.25
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class body
{
string name;
double height,weight,waist;
double bodyfat,bmi;
public:
void getname(string n);
void geth(double h);
void getw(double we);
void getwa(double wa);
double bf();
double BMI();
string getn();
};
void body::geth(double h)
{height=h;}
void body::getname(string n)
{name=n;}
void body::getw(double w)
{weight=w;}
void body::getwa(double wa)
{waist=wa;}
double body::bf()
{
double a,b;
a=waist*0.74;
b=weight*0.082+34.89;
return (a-b)/weight;
}
double body::BMI()
{
return weight/(height*height);
}
string body::getn()
{return name;}
int main()
{
int t;
cin>>t;
while(t--)
{
string name;
double height,weight,waist;
body a;
cin>>name>>height>>weight>>waist;
a.geth(height);
a.getname(name);
a.getw(weight);
a.getwa(waist);
cout<<a.getn()<<"的BMI指数为"<<fixed<<setprecision(0)<<a.BMI()<<"--体脂率为";
cout<<fixed<<setprecision(2)<<a.bf()<<endl;
}
return 0;
}