roberthsu2003 / cAndC-

51 stars 18 forks source link

計算bmi值,請使用物件導向的寫法 #46

Closed roberthsu2003 closed 1 month ago

roberthsu2003 commented 2 months ago

建立class BMI

請輸入姓名:xxxx
請輸入身高:xxx
請輸入體重:xxx
xxxx的bmi是:xxx
xxx的體重:正常
截圖 2024-06-25 晚上9 55 17
fatpao commented 2 months ago
#include <iostream>
using namespace std;

class PersonData {
public:
  //建立3個欄位,姓名、身高、體重
  string name;
  float height;
  float weight;

  PersonData(string n, float h, float w) { //建構式
    name = n;
    height = h;
    weight = w;
  }

  //建立實體方法(instance method)
  float get_bmi() { 
    return weight / ((height / 100) * (height / 100)); 
  }

  string status() {
    if (get_bmi() < 18.5)
      return "體重過輕";
    else if (get_bmi() < 24)
      return "正常範圍";
    else if (get_bmi() < 27)
      return "過重";
    else if (get_bmi() < 30)
      return "輕度肥胖";
    else if (get_bmi() < 35)
      return "中度肥胖";
    else
      return "重度肥胖";
  }

};

int main() {
  string name;
  float height, weight;
  cout << "請輸入姓名:";
  cin >> name;
  cout << "請輸入身高(cm):";
  cin >> height;
  cout <<"請輸入體重(kg):";
  cin >> weight;

  PersonData p1(name, height, weight);
  cout << p1.name << "的BMI是:" << p1.get_bmi() << endl;
  cout << p1.name << "的體重:" << p1.status() << endl;

}

image

Norri2 commented 2 months ago
#include <iostream>
#include <math.h>
using namespace std;

class health_info_c
{
public:
  string name;
  double height_cm;
  double weight_kg;

  health_info_c(string name, double height_cm, double weight_kg)
  {
    this->name      = name;
    this->height_cm = height_cm;
    this->weight_kg = weight_kg;
  }

  double get_bmi(void)
  {
    return weight_kg / pow( (height_cm / 100), 2);
  }

  string get_bmi_status(void)
  {
    double bmi = get_bmi();
    return (bmi < 18.5)                ? "\33[31m過輕(BMI < 18.5)\33[0m"         : 
           (bmi >= 18.5) && (bmi < 24) ? "\33[32m正常(18.5 <= BMI < 24)\33[0m"   : 
           (bmi >= 24)   && (bmi < 27) ? "\33[31m過重(24 <= BMI < 27)\33[0m"     :
           (bmi >= 27)   && (bmi < 30) ? "\33[31m輕度肥胖(27 <= BMI < 30)\33[0m"  :
           (bmi >= 30)   && (bmi < 35) ? "\33[31m中度肥胖(30 <= BMI < 35)\33[0m"  :
           "\33[31m重度肥胖(BMI >= 35)";
  }
};

int main(void) 
{
  string name;
  double height = 0;
  double weight = 0;

  cout << "輸入你的名字, 身高(公分)與體重(公斤): ";
  cin >> name >> height >> weight;

  health_info_c person(name, height, weight);
  cout << "你的BMI是" << person.get_bmi() << ",體重" << person.get_bmi_status() << "。" << endl;

  return 0;
}

image

charlywang11 commented 2 months ago

計算bmi值,請使用物件導向的寫法

建立class BMI 欄位->name,height,weight 建立自訂的建構式 建立實體方法 get_bmi() 建立實體方法 status()

#include <iostream>
using namespace std;

class BMI{
public:
  string name;
  float height,weight;

  BMI(string n,float h,float w){
    name = n;
    height = h;
    weight = w;
  }

  float get_bmi(){
    float bmi = weight / ((height/100)*(height/100));
    return bmi;
  }

  string status(){
    float bmi = get_bmi();
    string status;
    if(bmi<18.5){
      status = "體重過輕";
    }else if(bmi<24){
      status = "體重正常";
    }else if(bmi<27){
      status = "體重過重";
    }else if(bmi<30){
      status = "輕度肥胖";
    }else if(bmi<35){
      status = "中度肥胖";
    }else
      status = "重度肥胖";
    return status;
  }
};

int main() {
  string name;
  float height,weight;
  cout << "請輸入姓名:";
  cin >> name;
  cout << "請輸入身高(cm):";
  cin >> height;
  cout << "請輸入體重(kg):";
  cin >> weight;

  BMI p1(name,height,weight);

  cout << p1.name << "的bmi是:"; printf("%.2f\n", p1.get_bmi());
  cout << p1.name << "的體重:" << p1.status() << endl;

  return 0;
}

image

createcube commented 2 months ago
#include <iostream>
using namespace std;

class Data{
  public :
    string name;
    double weight;
    double height;
  Data(string n, double w, double h){
    name = n;
    weight = w;
    height = h;
  }
  double get_BMI(){
    return weight / ((height/100) * (height/100));
  }
  string status(){
    if(get_BMI() < 18.5){
      return "體重過輕";
    }else if(get_BMI() < 24){
      return "正常範圍";
    }else if(get_BMI() < 27){
      return "體重過重";
    }else if(get_BMI() < 30){
      return "輕度肥胖";
    }else if(get_BMI() < 35){
      return "中度肥胖";
    }else{
      return "重度肥胖";
    }
  }
};

int main() {
  string name;
  double weight, height;
  cout << "請輸入姓名:";
  cin >> name;
  cout << "請輸入體重(公斤):";
  cin >> weight;
  cout << "請輸入身高(公分):";
  cin >> height;

  Data p1(name, weight, height);
  cout << name << "的BMI為" << p1.get_BMI() << endl;
  cout << name << "的體重為" << p1.status() << endl;

  return 0;
}

image