第5章实验1:身高预测。
每个做父母的都关心自己孩子成人后的身高,据有关生理卫生知识与数理统计分析表明,影响小孩成人后的身高的因素包括遗传、饮食习惯与体育锻炼等。小孩成人后的身高与其父母的身高和自身的性别密切相关。
设faHeight为其父身高,moHeight为其母身高,身高预测公式为
男性成人时身高 = (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)?”
输入格式:" %c"
输入提示:“Please input your father’s height(cm):”
输入格式:“%f”
输入提示:“Please input your mother’s height(cm):”
输入格式:“%f”
输入提示:“Do you like sports(Y/N)?”
输入格式:" %c",
输入提示:“Do you have a good habit of diet(Y/N)?”
输入格式:" %c"
输出格式:“Your future height will be %.0f(cm)\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>
main()
{
float fh, mh, h;
char sex, sports, diet;
printf("Are you a boy(M) or a girl(F)?");
sex = getchar();
getchar();
printf("Please input your father's height(cm):");
scanf("%f", &fh);
getchar();
printf("Please input your mother's height(cm):");
scanf("%f", &mh);
getchar();
printf("Do you like sports(Y/N)?");
sports = getchar();
getchar();
printf("Do you have a good habit of diet(Y/N)?");
diet = getchar();
if (sex == 'M')
h = (fh + mh) * 0.54;
else if (sex == 'F')
h = (fh * 0.923 + mh) / 2;
else
{
printf("Error!\n");
goto R;
}
if (sports == 'Y')
h = h * 1.02;
else if (sports == 'N');
else
{
printf("Error!\n");
goto R;
}
if (diet == 'Y')
h = h * 1.015;
else if (diet == 'N');
else
{
printf("Error!\n");
goto R;
}
printf("Your future height will be %.0f(cm)\n", h);
R:
return 0;
}