gpt4 book ai didi

c - 如何从 char 函数返回字符串

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:01 25 4
gpt4 key购买 nike

我希望函数 getCategory() 返回“invalid”,而不是在函数输入无效时打印“invalid”一词(即不使用 printf )(即当高度或体重低于零时) .请帮助:

#include<stdio.h>
#include<conio.h>
char getCategory(float height,float weight)
{
char invalid = '\0';
float bmirange;
if(height<=0 || weight<=0)
return invalid;
else
{
height=height*0.01;
bmirange=[weight/(height*height)];
if(bmirange< 15 )
return starvation;
}
}

/* return the following to category
If bmi range < 15 then category is "starvation"
If bmi range >=15 && bmi range < 18.5 then category is "underweight"
If bmi range >=18.5 && bmi range < 25 then category is "normal"
If bmi range >= 25 && bmi range < 30 then category is "overweight"
If bmi range >=30 && bmi range < 40 then category is "obese"
If bmi range >=40 then category is "morbidly obese
*/


int main()
{
char Category;
float height,weight;
printf("enter height");
scanf("%f",&height);
printf("enter weight");
scanf("%f",&weight);
Category=getCategory(height,weight);
if(Category == 0)
printf("invalid");
else
printf("%c", Category);
}

最佳答案

将返回类型更改为 char *,然后返回指向各种类别字符串的指针。您还需要填写各种 bmi 水平的检查。

看起来很像作业;如果是这样,请将其标记为这样。

这是我的解决方案...但我已将行的顺序随机化,因为您确实需要自己做作业。不过,这里应该有一些提示,例如 structreturn 行。

    scanf("%f",&height);
struct bmicategory {
}
{18.5, "normal"},
return categories[i-1].name;
float bmi = weight / (height * height);
struct bmicategory categories[] = {
printf("enter weight (in kg): ");

};
char *name;

return 0;

{40, "morbidly obese"},
{15, "underweight"},
}
scanf("%f",&weight);
for(i=1; i<sizeof(categories)/sizeof(struct bmicategory); i++) {
#include<stdio.h>
if(bmi < categories[i].value) {
break;
printf("%s\n", category);
height = height * 0.01;
printf("enter height (in cm): ");
{25, "overweight"},

category=getBmiCategory(height,weight);

int main() {
float value;
int i;
/* printf("BMI = %f\n", bmi); */
}
};
float height;
{30, "obese"},
}
char *category;

char *name = NULL;
{0, "starvation"},
char *getBmiCategory(float height, float weight) {
float weight;

关于c - 如何从 char 函数返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1848394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com