C++,输入体重,身高,算bmi

学习 时间:2026-03-31 17:28:55 阅读:6687
C++,输入体重,身高,算bmi1.Ask for the user for his first name.输入你的姓名\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x092.Ask the user whether he wants to use metric or standard system.选择哪种方式来计算\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x093.Based on the choice of the system,prompt to ask weight and height.Use the answer\x09\x09\x09\x09\x09and the appropriate formula to calculate the user’s bmi.根据选择的方式输入体重和身高(磅和英尺,或千克和米)\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x094.Prints out to the user (addressed with his name) what his BMI is.输出,用户的姓名和bmi的值\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x09\x095.Prints out which category of weight he belongs to depending on his BMI.输出,用户的姓名,和更具bmi的值用户的体重属于哪一类的.BMI = weight (kg) /( (height (m))(height(m))BMI = weight (lb) / ((height (in)(height(in))*703 \x09\x09\x09Underweight if BMI < 18.5Normal if 18.5 ≤ BMI < 25.0\x09\x09\x09Overweight if 25.0 ≤ BMI < 30.0Obese if 30.0 ≤ BMI \x09\x09Here are two sample output \x09\x09\x09v:\cmpsc101Type your first name (with no space):MichaelType metric for metric system,type standard for standard system:standardType in your weight in pound and your height in inch:18070.5Michael,your BMI is 25.4595Michael,you are considered to be Overweight.\x09\x09\x09v:\cmpsc101Type your first name (with no space):JohnType metric for metric system,type standard for standard system:metricType in your weight in kilogram and your height in meter:251John,your BMI is 25John,you are considered to be Overweight.\x09\x09\x09v:\cmpsc101Type your first name (with no space):DoraType metric for metric system,type standard for standard system:unknownType in your weight in pound and your height in inch:10065.2Dora,your BMI is 16.5371Dora,you are considered to be Underweight.谁知道这个c++的code要怎么写,code中需要用到system和category.

最佳回答

懵懂的玫瑰

仁爱的中心

2026-03-31 17:28:55

#include <iostream>
#include <string>
#include <stdlib。h>
using namespace std;
class person {//新建人物类
public:
    person();
    ~person();

    void setname(string inputname){
        name=inputname;
    }

    string getname(){
        return name;
    }

    void setbmi(double bmi){
        person::bmi=bmi;
    }

    double getbmi(){
        return bmi;
    }

private:
    string name;//姓名
    double bmi;//BMI
};

person::person(){

}

int main(){
    person* p1=new person();//人物对象
    string text1 = "Type your first name (with no space):";
    string text2 = "Type metric for metric system, type standard for standard system:";
    string input;
    string weight,height;
    double bmi;
    int choice;

    bmi=0。0;
    cout<<text1<<endl;
    cin>>input;
    p1->setname(input);
    cout<<text2<<endl;
    cin>>input;
    if(input=="standard")//判断属于那种system
        choice=0;
    else if (input=="metric")
        choice=1;
    else if (input=="unknow")
        choice=2;

    switch(choice){
    case 0:
        cout<<"Type in your weight in pound and your height in inch:"<<endl;
        cin>>weight>>height;
        bmi=((atof((weight。c_str())))/((atof(height。c_str()))*(atof(height。c_str()))))*703;//计算bmi
        p1->setbmi(bmi);
        break;
    case 1:
        cout<<"Type in your weight in kilogram and your height in meter:"<<endl;
        cin>>weight>>height;
        bmi=(atof(weight。c_str()))/((atof(height。c_str()))*(atof(height。c_str())));
        p1->setbmi(bmi);
        break;
    case 2:
        cout<<"Type in your weight in pound and your height in inch:"<<endl;
        cin>>weight>>height;
        bmi=((atof((weight。c_str())))/((atof(height。c_str()))*(atof(height。c_str()))))*703;
        p1->setbmi(bmi);
        break;
    }
    cout<<p1->getname()<<", your BMI is "<<p1->getbmi()<<endl;
    cout<<p1->getname()<<", you are considered to be ";
    if(p1->getbmi()<18。5){//判断category
        cout<<"Underweight"<<endl;
    }
    else if(p1->getbmi()<25。0){
        cout<<"Normal"<<endl;
    }
    else if(p1->getbmi()<30。0){
        cout<<"Overweight"<<endl;
    }
    else {
        cout<<"Obese"<<endl;
    }
}基本上行了,你试试,至于system和category,system是关键字,不过用不上,category可以弄成一个变量,我这里没用,也没必要。

最新回答共有2条回答

  • 超级的石头
    回复
    2026-03-31 17:28:55

    #include <iostream>#include <string>#include <stdlib。h>using namespace std;class person {//新建人物类public:    person();    ~person();    void setname(string inputname){        name=inputname;    }    string getname(){        return name;    }    void setbmi(double bmi){        person::bmi=bmi;    }    double getbmi(){        return bmi;    }private:    string name;//姓名    double bmi;//BMI};person::person(){}int main(){    person* p1=new person();//人物对象    string text1 = "Type your first name (with no space):";    string text2 = "Type metric for metric system, type standard for standard system:";    string input;    string weight,height;    double bmi;    int choice;    bmi=0。0;    cout<<text1<<endl;    cin>>input;    p1->setname(input);    cout<<text2<<endl;    cin>>input;    if(input=="standard")//判断属于那种system        choice=0;    else if (input=="metric")        choice=1;    else if (input=="unknow")        choice=2;    switch(choice){    case 0:        cout<<"Type in your weight in pound and your height in inch:"<<endl;        cin>>weight>>height;        bmi=((atof((weight。c_str())))/((atof(height。c_str()))*(atof(height。c_str()))))*703;//计算bmi        p1->setbmi(bmi);        break;    case 1:        cout<<"Type in your weight in kilogram and your height in meter:"<<endl;        cin>>weight>>height;        bmi=(atof(weight。c_str()))/((atof(height。c_str()))*(atof(height。c_str())));        p1->setbmi(bmi);        break;    case 2:        cout<<"Type in your weight in pound and your height in inch:"<<endl;        cin>>weight>>height;        bmi=((atof((weight。c_str())))/((atof(height。c_str()))*(atof(height。c_str()))))*703;        p1->setbmi(bmi);        break;    }    cout<<p1->getname()<<", your BMI is "<<p1->getbmi()<<endl;    cout<<p1->getname()<<", you are considered to be ";    if(p1->getbmi()<18。5){//判断category        cout<<"Underweight"<<endl;    }    else if(p1->getbmi()<25。0){        cout<<"Normal"<<endl;    }    else if(p1->getbmi()<30。0){        cout<<"Overweight"<<endl;    }    else {        cout<<"Obese"<<endl;    }}基本上行了,你试试,至于system和category,system是关键字,不过用不上,category可以弄成一个变量,我这里没用,也没必要。

上一篇 用多少多大的力量可以改变木卫二轨道?使它脱离木星?

下一篇 某街心花园,花园中间有一个正方形花坛,在四周有1米宽的水泥路,如果水泥路的总面积是12平方米,中间花坛的面积是多少?