每个做父母的都关心自己的孩子成人后的身高,据有关生理卫生知识与数理统计分析表明,影响小孩成人后身高的因素包括遗传、饮食习惯与锻炼。小孩成人后身高与其父母的身高和自己的性别密切相关。
设faHeight为其父身高,moHeight为母身高,身高预测公式为
男性成人时身高=(faHeight+moHeight)x0.54cm
女性成人时身高=(faHeight x 0.923+moHeight)/2cm
此外,若喜爱体育锻炼,则可增加身高2%;若有良好的卫生因饮食习惯,则可增加身高1.5%。
请编程从键盘输入用户的性别(用字符变量sex存储,输入字符F表示女性,输入字符M表示男性)、父母身高(用实型变量存储,faHeight为其父身高,moHeight为其母身高)、是否喜欢体育锻炼(用字符型变量sports存储,输入字符Y表示喜爱,输入字符N表示不喜爱)、是否有良好的饮食习惯(用字符型变量diet存储,输入字符Y表示良好,输入字符N表示不好)等条件,利用给定公式和身高预测方法堆身高进行预测。
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {int result = 0;float faHeight, moHeight, manHeight, womanHeight; //变量的声明,父母身高,用户男女身高char sex, sports, diet; //性别,运动,饮食习惯printf("Please enter your gender:F or M\n");scanf("%c", &sex); //输入性别 //输入用户性别while (sex != 'M' && sex != 'F') { //都性别输入进行判断,是否符合格式printf("输入错误,请重新输入:\n");fflush(stdin); //不符合,清楚缓存中的数据scanf("%c", &sex); //重新输入}fflush(stdin); //清楚缓存中的数据printf("Please enter the height of the father and mother\n");result = scanf("%f %f", &faHeight, &moHeight); //输入父母的身高while (result != 2) { //根据scanf()的返回值判断输入是否正确printf("输入错误请重新输入:\n");fflush(stdin);result = scanf("%f %f", &faHeight, &moHeight);}fflush(stdin);printf("Do you like physical exercise:Y or N\n");scanf("%c", &sports); //是否喜欢运动while (sports != 'Y' && sports != 'N') { //格式判断printf("输入错误,请重新输入:\n");fflush(stdin);scanf("%c", &sports);}fflush(stdin);printf("Do you have good eating habits:Y or N\n");scanf("%c", &diet); //是否有良好的饮食习惯while (diet != 'Y' && diet != 'N') { //格式判断printf("输入错误,请重新输入:\n");fflush(stdin);scanf("%c", &diet);}fflush(stdin);manHeight = (faHeight + moHeight) * (0.54); //计算男性身高womanHeight = (faHeight * (0.923) + moHeight) / 2; //计算女性身高if (sports == 'Y') { //是否喜欢运动manHeight = manHeight * (1 + 0.02);womanHeight = womanHeight * (1 + 0.02);if (diet == 'Y') { //是否有良好的饮食习惯manHeight = manHeight * (1 + 0.015);womanHeight = womanHeight * (1 + 0.015);}}if (sex == 'F') { //输入判断时男性或者女性用户printf("Your height is %f cm", manHeight);} else {printf("Your height is %f cm", womanHeight);}return 0;}