题目
(健康记录信息化)在这个练习题中, 要为一个人设计一个HealthPr0file 类。这个类的属性包括人的姓名、性别、出生日期(由年、月、日单独的属性组成)、身高( 以英寸为单位)、体重( 以磅为单位) 。你的类应包括一个构造函数,用来接收这些信息。对每个属性,都提供一个set 函数和get 函数。这个类也应当有计算和返回用户年龄、最大心率、目标心率范围( 见练习题3 .16 )、身体质量指数(BMI ;见练习题2 .30)的函数。编写一个应用程序, 它提示输人用户信息, 为这个用户初始化一个HealthProfile 类的对象, 并用这个对象打印信息, 包括该用户的姓、名、性别、出生日期、身高、体重等信息, 然后计算并返回该用户的年龄、BMI 、最大心率和目标心率范围。它也应该显示练习题2 . 30 中"BMI 值” 的图表。使用与练习题3 .16 相同的方法来计算用户的年龄。
实现代码
#include <iostream>
using namespace std;
class HealthProfile
{
public:
HealthProfile(string LastName, string FirstName, string Sex, int yy,
int mm, int dd, int Height, int Weight)
{
lastName = LastName;
firstName = FirstName;
sex = Sex;
birth.YY = yy;
birth.MM = mm;
birth.DD = dd;
height = Height;
weight = Weight;
}
string getLastName()
{
return lastName;
}
void setLastName(string LastName)
{
lastName = LastName;
}
string getFirstName()
{
return firstName;
}
void setFirstName(string FirstName)
{
firstName = FirstName;
}
string getSex()
{
return sex;
}
void setSex(string Sex)
{
sex = Sex;
}
int getYY()
{
return birth.YY;
}
void setYY(int yy)
{
birth.YY = yy;
}
int getMM()
{
return birth.MM;
}
void setMM(int mm)
{
birth.MM = mm;
}
int getDD()
{
return birth.DD;
}
void setDD(int dd)
{
birth.DD = dd;
}
int getHeight()
{
return height;
}
void setHeight(int Height)
{
height = Height;
}
int getWeight()
{
return weight;
}
void setWeight(int Weight)
{
weight = Weight;
}
int getAge()
{
struct tm t; //tm结构指针
time_t rawTime; //声明time_t类型变量
time(&rawTime); //获取系统日期和时间
localtime_s(&t, &rawTime); //获取当地日期和时间
/*printf("年:%d\n", t.tm_year + 1900);
printf("月:%d\n", t.tm_mon + 1);
printf("日:%d\n", t.tm_mday);*/
int age = t.tm_year + 1900 - birth.YY;
if (t.tm_mon + 1 < birth.MM)
{
--age;
}
else if (birth.MM == t.tm_mon + 1)
{
if (t.tm_mday < birth.DD)
{
--age;
}
}
return age;
}
int getMaxHeartRate()
{
return 220 - getAge();
}
struct Bound {
double lower;
double upper;
}bound;
Bound getTargetHeartRate()
{
bound.lower = 0.5 * getMaxHeartRate();;
bound.upper = 0.85 * getMaxHeartRate();;
return bound;
}
double getBMI()
{
return weight / pow(height, 2) * 703;
}
private:
string lastName;
string firstName;
string sex;
struct Birth
{
int YY;
int MM;
int DD;
}birth;
int height;
int weight;
};
int main()
{
string lastName, firstName, sex;
int YY, MM, DD, height, weight;
cout << "Input your first name: ";
cin >> firstName;
cout << "Input your last name: ";
cin >> lastName;
cout << "Input your sex: ";
cin >> sex;
cout << "Input your year of birth: ";
cin >> YY;
cout << "Input your month of birth: ";
cin >> MM;
cout << "Input your date of birth: ";
cin >> DD;
cout << "Input your height: ";
cin >> height;
cout << "Input your weight: ";
cin >> weight;
HealthProfile healthProfile(lastName, firstName, sex, YY, MM, DD, height,
weight);
cout << "Your age: " << healthProfile.getAge() << endl;
cout << "Your BMI: " << healthProfile.getBMI() << endl;
cout << "your max heart rate: " << healthProfile.getMaxHeartRate() << endl;
cout << "Your target heart rate: "
<< healthProfile.getTargetHeartRate().lower << " - "
<< healthProfile.getTargetHeartRate().upper << endl;
}