用C语言研究表明,人的身高与遗传因素有一定的关系。可以按照下面的公式,预测遗传身高(不考虑后天因素)。请据此设计一套身高预测系统,要求配有系统界面,可以支持用户选择性别。 男性成人时身高=(fa_Height + mo_Height)×0.54 cm 女性成人时身高=(fa_Height×0.923 + mo_Height) / 2 cm 这里fa_Height和mo_Height分别表示父亲和母亲的身高,单位均为cm。 以下为程序的运行结果示例1: ************************************* * Welcome to Use Height Prediction System * ************************************* Please choose the sex: 1.Female 2.Male Your choice is: 1↙ May I know your Surname? Ma↙ Hi, Miss Ma! Could you please input your mother’s height(cm): 153↙ Could you please input your father’s height(cm): 169↙ Your predicted height is 154cm Thank you for use our “Height Prediction System”!
时间: 2023-07-05 19:30:20 浏览: 100
关于C语言的书,这里有遗传算法的程序可参考
以下是身高预测系统的代码实现:
```c
#include <stdio.h>
int main() {
int sex, fa_Height, mo_Height, height;
printf("*************************************\n");
printf("* Welcome to Use Height Prediction System *\n");
printf("*************************************\n");
printf("Please choose the sex:\n");
printf("1. Female\n");
printf("2. Male\n");
printf("Your choice is: ");
scanf("%d", &sex);
if (sex == 1) {
char surname[20];
printf("May I know your Surname? ");
scanf("%s", surname);
printf("Hi, Miss %s! Could you please input your mother's height(cm): ", surname);
scanf("%d", &mo_Height);
printf("Could you please input your father's height(cm): ");
scanf("%d", &fa_Height);
height = (int)((fa_Height * 0.54 + mo_Height * 0.923) / 2);
printf("Your predicted height is %dcm\n", height);
} else if (sex == 2) {
char surname[20];
printf("May I know your Surname? ");
scanf("%s", surname);
printf("Hi, Mr. %s! Could you please input your mother's height(cm): ", surname);
scanf("%d", &mo_Height);
printf("Could you please input your father's height(cm): ");
scanf("%d", &fa_Height);
height = (int)((fa_Height + mo_Height) * 0.54);
printf("Your predicted height is %dcm\n", height);
} else {
printf("Invalid choice!\n");
}
printf("Thank you for use our \"Height Prediction System\"!\n");
return 0;
}
```
程序解释:
- 首先,打印欢迎信息和菜单,让用户选择性别。
- 然后,根据用户的选择,提示用户输入母亲和父亲的身高,以及姓氏(为了更友好的交互体验)。
- 接着,根据公式计算出预测身高,并输出预测结果。
- 最后,输出感谢信息。
需要注意的是,此程序假设用户输入的身高值都是合法的正整数。如果用户输入了非法值,可能会导致程序出错。此外,这个公式只是一个大致的预测,实际身高还受到许多其他因素的影响。
阅读全文