Closed roberthsu2003 closed 4 months ago
main.cpp
#include <iostream>
#include "people.h"
using namespace std;
int main() {
People p1;
p1.name = "正常人阿民";
p1.height = 171;
p1.weight = 66;
p1.profile();
People p2("過瘦阿賢", 180, 50);
p2.profile();
People *p4 = new People("一個大胖子", 170, 170);
p4->profile();
}
people.h
#include <iostream>
#include <math.h>
using namespace std;
/*
依據lesson5_2,請多輸出一行
您必需最少減重xx公斤,才可以至標準,或加重多少,才可以至標準
算式:
// 正常值的BMI * (身高2)公尺 可得此身高正常值的體重
*/
class People{
public:
string name;
double weight;
double height;
People(){}
People(string name, double height, double weight){
this -> name = name;
this -> height = height;
this -> weight = weight;
}
void profile(){
cout << "姓名:" << name << endl;
cout << "身高:" << height << endl;
cout << "體重:" << weight << endl;
cout << "BMI:" << bmi() << endl;
cout << "狀況:" << status(bmi()) << endl;
cout << remind(status(bmi()),weight,height) << endl;
cout << "================" << endl;
}
string remind(string text,double weight,double height){
if(text == "正常範圍"){
return "很棒,繼續保持!";
}else{
string text2 = "降低 ";
if(text == "體重過輕") text2 = "增加 "; // 依據情況將文本改為增加、降低
return "你需要再" + text2 +
to_string(normalbmimax(weight,height)) +
"~" +
to_string(normalbmimin(weight,height)) +
" 公斤,BMI才可達正常範圍";
}
}
//計算該身高BMI為24時,時的正常體重
double normalbmimax(double weight,double height){
double bmimax = 24;
double max = bmimax * pow(height / 100,2);
return abs(weight - max); //將體重-正常體重 得到差值,使用abs取絕對值,以處理體重過低時的負值
}
//計算該身高BMI為18.5時,時的正常體重
double normalbmimin(double weight,double height){
double bmimin = 18.5;
double min = bmimin * pow(height / 100,2);
return abs(weight - min);
}
double bmi(){
return weight / pow(height / 100, 2);
}
string status(double bmi){
if(bmi < 18.5){
return "體重過輕";
}else if(bmi < 24){
return "正常範圍";
}else if(bmi < 27){
return "過重";
}else if(bmi < 30){
return "輕度肥胖";
}else if(bmi < 35){
return "中度肥胖";
}else{
return "重度肥胖";
}
}
};
#include <iostream>
#include <math.h>
using namespace std;
class People {
public:
string name;
int weight;
int height;
People() {
// cout << "目前執行此建構式" << endl;
}
People(string n, int w, int h) {
name = n;
weight = w;
height = h;
}
void profile() {
cout << "姓名:" << name << endl;
cout << "身高:" << height << "cm" << endl;
cout << "體重:" << weight << "kg" << endl;
cout << "BMI:" << bmi() << endl;
cout << "您的狀況:" << status(bmi()) << endl;
cout << fun(bmi()) << endl;
}
double bmi() { return weight / pow(height / 100.0, 2); }
string status(double bmi) {
if (bmi < 18.5) {
return "體重過輕";
} else if (bmi < 24) {
return "正常範圍";
} else if (bmi < 27) {
return "過重";
} else if (bmi < 30) {
return "輕度肥胖";
} else if (bmi < 35) {
return "中度肥胖";
} else {
return "重度肥胖";
}
}
string fun(double bmi) {
if (bmi < 18.5) {
return "至少增加" + to_string(18.5-weight) + "公斤才可以至標準";
} else if (bmi > 24 && bmi < 35) {
return "至少減重" + to_string(weight- 24) + "公斤才可以至標準";
}else{
return "";
}
};
int main() {
People p1;
p1.name = "徐國堂";
p1.height = 183;
p1.weight = 81;
p1.profile();
cout << "===================\n";
People p2("張xx", 69, 170);
p2.profile();
cout << "===================\n";
People p3("王xx", 65, 150);
p3.profile();
cout << "===================\n";
// new,傳出的是指標變數
People *p4 = new People("陳xx", 92, 175);
p4->profile();