BMI计算器C $ C $Ç [英] BMI calculator C code

查看:192
本文介绍了BMI计算器C $ C $Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个简单的BMI计算器,但由于某些原因,当我尝试175高度(其配方使1.75)和70大众应该给予22.8,这是在正常范围内,但它给我减持。我知道这可能是一个简单的错误,但我不能看到它。

 浮动主要(无效){    浮动高度;
    的printf(中输入厘米你的身高:\\ n);
    scanf函数(%F,&安培;高度);    浮重;
    的printf(公斤输入您的体重:\\ n);
    scanf函数(%F,&安培;重量);    浮BMI;
    BMI =(重量/(身高/ 100)*(高度/ 100));    如果(BMI< = 16){
        的printf(体重严重不足\\ n);
    }
    否则,如果(16< BMI< = 18.5){
        的printf(减持\\ n);
    }
    否则,如果(18.5< BMI< = 25){
        的printf(健康\\ n);
    }
    否则如果(25℃; BMI&下; = 30){
        的printf(增持\\ n);
    }
    其他{
        的printf(严重超重\\ n);
    }
}


解决方案

所有这些

 否则如果(16< BMI< = 18.5){

是错误的。他们不这样做,你的意思是他们做的事。以实现所期望的结果,可以使用

 否则如果(16< BMI&放大器;&安培; BMI< = 18.5){

原因是,你的前pressions被评为

 否则如果((16 LT; BMI)LT = 18.5){

其中,(16< BMI)计算结果为真正这又等于 1 0 ,然后被相比,第二恒定。它之所以像这样评价,是因为比较运营商的左结合,因而是从左向右的。

编辑2

强制性SO链接:<一个href=\"http://stackoverflow.com/questions/8889522/is-4-y-1-a-valid-statement-in-c-how-do-you-evaluate-it-if-so\">Is (4> Y> 1)有效的在C ++中声明?你如何如果是这样评价它?

修改

我怀疑这一个,但不知道该公式。现在@MOehm已确认(和维基百科似乎证实了这一点为好):

  BMI =(体重/(身高/ 100)*(高度/ 100));

应成为

  BMI =(体重/((身高/ 100)*(高度/ 100)));

原因这里是pretty大同小异:运营商precedence和前$ P $用C pssion评价规则++。 OP,要注意这些方面,并把括号酌情!

修改3 这是我怎么会去这个使用STL(这种方法也有明显的前pressing算法背后的想法的利益,没有掩埋它的实现细节下面):

 的#include&LT;&iostream的GT;
#包括LT&;串GT;
#包括LT&;矢量&GT;
#包括LT&;实用&GT;
#包括LT&;限制和GT;
#包括LT&;&算法GT;诠释的main()
{
    的std ::矢量&lt;的std ::对&LT;浮动,标准::字符串&GT; &GT; bmi_table = {
        {16,严重不足},
        {18.5,减持},
        {25,健康},
        {30,增持},
        {的std :: numeric_limits&LT;浮动&GT; :: MAX(),严重超重}
    };
    浮身高,体重;
    给std :: cin&GT;&GT;高度&GT;&GT;重量;
    常量浮BMI =(重量/((高度/ 100.f)*(高度/ 100.f)));
    常量汽车IDX =
        的std :: find_if(bmi_table.begin()
                     bmi_table.end(),
                     [&放大器;(decltype(bmi_table):: VALUE_TYPE&安培; P) - &GT;布尔{返回p.first&GT; BMI; });
    性病::法院LT&;&LT; idx-&gt;第二个&LT;&LT;的'\\ n';
    返回0;
}

I'm trying to write a simple BMI calculator, but for some reason when I try 175 for height (which the formula makes 1.75) and 70 for mass it should give 22.8, which is in the healthy range, but it gives me underweight. I know it's probably a simple mistake, but I can't see it.

float main(void) { 

    float height;   
    printf("Enter your height in cm:\n");
    scanf("%f",&height);    

    float weight; 
    printf("Enter your weight in kg:\n");
    scanf("%f",&weight);

    float bmi;
    bmi = (weight/(height/100)*(height/100));

    if (bmi <= 16) {
        printf("Severely Underweight\n");
    }
    else if (16 < bmi <= 18.5) {
        printf("Underweight\n");
    }
    else if (18.5 < bmi <= 25) {
        printf("Healthy\n");
    }
    else if (25 < bmi <= 30) {
        printf("Overweight\n");
    }
    else {
        printf("Severely Overweight\n");
    }
}

解决方案

All of these

else if (16 < bmi <= 18.5) {

are wrong. They don't do what you mean them to do. To achieve the desired result, use

else if (16 < bmi && bmi <= 18.5) {

The reason is, your expressions are evaluated as

else if ((16 < bmi) <= 18.5) {

where (16 < bmi) evaluates to true or false which in turn equals 1 or 0, and is then compared to the second constant. Reason why it is evaluated like so, is because the comparison operators are left-associative, thus are evaluated left-to-right.

Edit 2

Obligatory SO link: Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?

Edit

I suspected this one, but didn't know the formula. Now @MOehm has confirmed it (and wikipedia seems to confirm this as well):

bmi = (weight/(height/100)*(height/100));

should become

bmi = (weight/((height/100)*(height/100)));

The reason here is pretty much the same: operator precedence and expression evaluation rules in C++. OP, pay attention to these aspects and put parentheses where appropriate!

Edit 3 Here's how I'd go about this using STL (this approach has the benefit of clearly expressing the idea behind the algorithm, without burying it beneath the implementations details):

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <limits>
#include <algorithm>

int main()
{
    std::vector<std::pair<float, std::string> > bmi_table = {
        { 16, "Severely Underweight" },
        { 18.5, "Underweight" },
        { 25, "Healthy" },
        { 30, "Overweight" },
        { std::numeric_limits<float>::max(), "Severely Overweight" }
    };
    float height, weight;
    std::cin >>  height >> weight;
    const float bmi = (weight/((height/100.f)*(height/100.f)));
    const auto idx =
        std::find_if(bmi_table.begin(),
                     bmi_table.end(),
                     [&](decltype(bmi_table)::value_type& p) -> bool { return p.first > bmi; });
    std::cout << idx->second << '\n';
    return 0;
}

这篇关于BMI计算器C $ C $Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆