题目:
身高预测。
每个做父母的都关心自己孩子成人后的身高,据有关生理卫生知识与数理统计分析表明,影响小孩成人后的身高的因素包括遗传、饮食习惯与体育锻炼等。小孩成人后的身高与其父母的身高和自身的性别密切相关。
设faHeight为其父身高,moHeight为其母身高,为float类型,身高预测公式为
男性成人时身高 = (faHeight + moHeight) × 0.54 cm
女性成人时身高 = (faHeight × 0.923 + moHeight) / 2 cm
此外,如果喜爱体育锻炼,那么可增加身高2%;如果有良好的卫生饮食习惯,那么可增加身高1.5%。
请编程从键盘输入用户的性别(用字符型变量sex存储,输入字符F表示女性,输入字符M表示男性)、父母身高(用实型变量存储,faHeight为其父身高,moHeight为其母身高)、是否喜爱体育锻炼(用字符型变量sports存储,输入字符Y表示喜爱,输入字符N表示不喜爱)、是否有良好的饮食习惯等条件(用字符型变量diet存储,输入字符Y表示良好,输入字符N表示不好),利用给定公式和身高预测方法对身高进行预测。
运行示例:
Are you a boy(M) or a girl(F)?F↙
Please input your father’s height(cm):182↙
Please input your mother’s height(cm):162↙
Do you like sports(Y/N)?N↙
Do you have a good habit of diet(Y/N)?Y↙
Your future height will be 167(cm)
程序:
#include <stdio.h>
int main()
{
float faHeight,moHeight,x;
char sex,sports,diet;
printf("Are you a boy(M) or a girl(F)?");
scanf("%c",&sex);
printf("Please input your father's height(cm):");
scanf("%f",&faHeight);
fflush(stdin);
printf("Please input your mother's height(cm):");
scanf("%f",&moHeight);
fflush(stdin);
if (sex=='M')
x=(faHeight + moHeight) * 0.54;
else if (sex=='F')
x=(faHeight * 0.923 + moHeight) / 2;
printf("Do you like sports(Y/N)?");
scanf("%c",&sports);
fflush(stdin);
if (sports=='Y')
x=x*(1+0.02);
else if (sports=='N')
x=x;
printf("Do you have a good habit of diet(Y/N)?");
scanf("%c",&diet);
fflush(stdin);
if (diet=='Y')
x=x*(1+0.015);
else if (diet=='N')
x=x;
printf("Your future height will be %d(cm)",(int)x);
return 0;
}
不用fflush也🉑
" %c"
#include <stdio.h>
int main()
{
float faHeight,moHeight,x;
char sex,sports,diet;
printf("Are you a boy(M) or a girl(F)?");
scanf("%c",&sex);
printf("Please input your father's height(cm):");
scanf("%f",&faHeight);
printf("Please input your mother's height(cm):");
scanf("%f",&moHeight);
if (sex=='M')
x=(faHeight + moHeight) * 0.54;
else if (sex=='F')
x=(faHeight * 0.923 + moHeight) / 2;
printf("Do you like sports(Y/N)?");
scanf(" %c",&sports);
if (sports=='Y')
x=x*(1+0.02);
else if (sports=='N')
x=x;
printf("Do you have a good habit of diet(Y/N)?");
scanf(" %c",&diet);
if (diet=='Y')
x=x*(1+0.015);
else if (diet=='N')
x=x;
printf("Your future height will be %d(cm)",(int)x);
return 0;
}
fflush or " %c"