微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有功能指针的BMI计算器

如何解决具有功能指针的BMI计算器

#include <stdio.h>
float bmindex(int *weight,float *height){
    float *bmi;

    //BMI = (mass or weight)/(height*height)
    *bmi=(*weight)/(*height**height);

    return *bmi;
}
int main() {
    
    int weight;
    float height;
    printf("Enter your body mass(in kg):"); scanf("%f",&weight);
    printf("Enter your height( in meter,e.g. 1.85):"); scanf("%f",&height);
    
    if (bmindex(&weight,&height) <=18.5) {
        printf("Your BMI is :%.f,you are underweight and not healthy!",bmindex(&weight,&height));
    } else if (18.5 <bmindex(&weight,&height)<=25){
        printf("Your bmi is :%.f,you have normal weight!",&height));
    } else{
        printf("Your bmi is over %25,you are overweight and unhealthy!");
    }
    return 0;
}

我对C编程还是比较陌生的,所以对指针也是如此。我尝试过在Google上寻找答案,然后再在此处提交问题,但无法解决我的问题。另外,我知道我无需使用指针和函数就可以轻松解决此问题,但是我不想这样做,因为我想练习使用指针函数并了解其背后的逻辑。

该程序相当简单。我希望用户输入体重,身高并计算体重指数。问题是,我得到了地址作为输出。我猜问题出在函数中,并且与返回的变量“ bmi”有关。

提前感谢所有建议。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。