计算体指数(3分)
题目内容:
从键盘输入某人的身高(以厘米为单位,如174cm)和体重(以公斤为单位,如70公斤),将身高(以米为单位,如1.74m)和体重(以斤为单位,如140斤)输出在屏幕上,并按照以下公式计算并输出体指数,要求结果保留到小数点后2位。程序中所有浮点数的数据类型均为float。
假设体重为w公斤,身高为h米,则体指数的计算公式为:
t=w/h*h
以下是程序的运行结果示例:
Input weight, height:
70,174↙
weight=140
height=1.74
t=23.12
输入格式: "%d,%d"
输出格式:
输入提示信息:"Input weight, height:\n" (注意:在height和逗号之间有一个空格)
体重输出格式:"weight=%d\n"
身高输出格式:"height=%.2f\n"
体指数输出格式:"t=%.2f\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
我的程序代码:
#include
int main()
{ int weight,height;
float t;
printf("Input weight, height:\n");
scanf("%d,%d",&weight,&height);
height=(float)height;
height=height/100.0;
printf("weight=%d\n",weight);
printf("height=%.2f\n",height);
t=weight/(height*height);
printf("t=%.2f\n",t);
return 0;
}
为什么一编译就停止运行了
int main()
10 {
11 int weight,height;
12 float t,fheight;
13 printf("Input weight, height:\n");
14 scanf("%d,%d",&weight,&height);
15
16 fheight=(float)height/100.0;
17
18 printf("weight=%d\n",weight*2);
19 printf("height=%.2f\n",fheight);
20 t=(float)weight/(fheight*fheight);
21 printf("t=%.2f\n",t);
22 return 0;
23 }
Input weight, height:
70,174
weight=140
height=1.74
t=23.12
因为当你输入的height < 100的时候 height / 100 = 0 然后你又把新的height当成了除数 所以就宕机了
你没有贴公式 我就不算了 反正就是说
这样 int a =9;
flota aa = 9/100.0; //结果为0.09
float aaa = 9/100; //结果为0
a = (float)a/100.0 //你这样把一个Int型变量 以一个float类型计算 又 用int型变量去接 是不正确的
(int) a 这样只是用int型变量的方式去读取这个变量 并没有改变这个变量原本类型
[root@localhost test]# cat height.c
#include
#include
#include
int main()
{
int weight,height;
float t,fheight;
printf("Input weight, height:\n");
scanf("%d,%d",&weight,&height);
fheight = (float) height / 100.0;
printf("weight=%d\n",weight * 2);//体重由公斤转化斤 *2
t = (float)weight / (fheight * fheight);
printf("height=%0.2f\n",fheight);
printf("t=%.2f\n",t);
return 0;
}
[root@localhost test]# gcc height.c -o height
[root@localhost test]# ./height
Input weight, height:
70,174
weight=140
height=1.74
t=23.12
[root@localhost test]#
#include
int main()
{
int weight,height;
float t,h;
printf("Input weight, height:\n");
scanf("%d,%d",&weight,&height);//输入数据
h=(float)height/100.0;
printf("weight=%d\n",weight*2);//公斤*2
printf("height=%.2f\n",h);
t=weight/(h*h);
printf("t=%.2f\n",t);
return 0;
}
强制转换之后把它赋值给另一个变量。