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

题目
1.Ask for the user for his first name. 输入你的姓名

2.Ask the user whether he wants to use metric or standard system. 选择哪种方式来计算

3.Based on the choice of the system, prompt to ask weight and height. Use the answer

and the appropriate formula to calculate the user’s bmi. 根据选择的方式输入体重和身高(磅和英尺,或千克和米)

4.Prints out to the user (addressed with his name) what his BMI is. 输出,用户的姓名和bmi的值

5.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

Underweight if BMI < 18.5
Normal if 18.5 ≤ BMI < 25.0

Overweight if 25.0 ≤ BMI < 30.0
Obese if 30.0 ≤ BMI

Here are two sample output

v:\cmpsc101
Type your first name (with no space):
Michael
Type metric for metric system, type standard for standard system:
standard
Type in your weight in pound and your height in inch:
180
70.5
Michael, your BMI is 25.4595
Michael, you are considered to be Overweight.

v:\cmpsc101
Type your first name (with no space):
John
Type metric for metric system, type standard for standard system:
metric
Type in your weight in kilogram and your height in meter:
25
1
John, your BMI is 25
John, you are considered to be Overweight.

v:\cmpsc101
Type your first name (with no space):
Dora
Type metric for metric system, type standard for standard system:
unknown
Type in your weight in pound and your height in inch:
100
65.2
Dora, your BMI is 16.5371
Dora, you are considered to be Underweight.

谁知道这个c++的code要怎么写,code中需要用到system和category。谢谢

#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个回答  2013-09-14
BMI =体重 (kg) / 身高 (m2)
第2个回答  2013-09-13
既然要求用c++,就应该使用面向对象的思维进行思考。
第3个回答  2020-12-23

如何判断一个人的胖瘦,用公式BMI算一算就知道了

求一个c++程序 要求输入身高和体重,输出BMI值(身体质量指数)
const double KILOGRAMS_PER_POUND = 0.45359237;double bmi = weightInKilongrams \/ (heightInMeters*heightInMeters);cout << "BMI is" << bmi << endl;if (bmi < 18.5)cout << "Overweight" << endl;else 起源 C++程序源于C语言,还记得很久以前学习C语言的时光(那是一段快乐而充实的...

C++,输入体重,身高,算bmi
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:...

C++,输入体重,身高,算bmi
bmi = weight \/ (height * height);} person.setBMI(bmi);std::cout << person.getName() << ", your BMI is " << person.getBMI() << std::endl;std::cout << person.getName() << ", you are considered to be ";if (person.getBMI() < 18.5) { std::cout << "und...

C++英文题目。要求用低级语言编写!不允许用高级语言!
printf("BMI = %.4f Category = Normalweight\\n",BMI);else if (BMI>=25&&BMI<30)printf("BMI = %.4f Category = Overweight\\n",BMI);else printf("BMI = %.4f Category = Obese\\n",BMI);} float bmi(int h,float w){ return w*703\/(h*h);} 是一个比较简单的输入计算问...

C++ 问题 unsafe use of type 'bool' in operation
cout<<"你的身高是 "<<setprecision(2)<<setiosflags(ios::fixed)<<d<<"米"<<endl;cout<<"请输入体重(磅)";cin>>e;f=e\/x;cout<<"你的体重是"<<f+f<<"斤"<<endl;BMI=f\/(d*d);cout<<"BMI= "<<BMI<<endl;cout<<"你是男的么(Y\/N)?"<<endl;cin>>y;y=y;if (y=='...

python身高体重程序代码
```python while True:weight = input("请输入你的体重(单位:Kg):")tall = input("请输入你的身高(单位:m):")try:weight = float(weight)tall = float(tall)break except ValueError:print("输入错误,请输入数字形式的体重和身高。")```2. 计算体质指数(BMI)并判断类别。```python...

python身高体重程序代码
whileTrue:weight=input(“请输入你的体重:(单位:Kg)”)tall=input(“请输入你的身高:(单位:m)”)weight=float(weight)tall=float(tall)BMI=weight\/tall**2#计算公式BMI=round(BMI,3)ifBMI=18.5andBMI=24andBMI=28andBMIelifBMI>=30andBMIPython提供了高效的高级数据结构,还能简单有效地...

需要一个c++的源程序,关于男性和女性的身高体重bmi指数,男女要分开来...
这个需要200行?50行应该够了吧。男女怎么分开,好像bmi指数不分性别的

C++中pBInfo->bmiHeader.biSize是什么意思
pBInfo表示一个类对象,类型为指针类型 bmiHeader 表示该指针对象的一个成员 .biSize 是bmiHeader成员的一个变量。-》和.都是访问对象成员的。一个使用于指针类型,一个是非指针类型而已。

python里缩进多少个空格(2023年最新分享)
python一般怎样缩进在python中,强制要求缩进,一般使用Tab或空格来进行缩进,且缩进必须要保持一致,否则可能会出缩进的错误。官方规定是缩进四个空格,而Tab键不一定等于四个空格,所以需要设置一个tab等于四个空格。要求严格的代码缩进是Python语法的一大特色,好比C语言中的花括号一样重要,在大多数场合还...

相似回答