血型遗传关系c语言编程,根据血型遗传关系,编程实现:○1.输入
时间: 2024-02-17 07:59:45 浏览: 23
十个人的血型(A、B、AB、O)和他们的父母的血型,输出每个人的基因型和表型。假设父母的血型确定,则子女的基因型随机。
以下是一个简单的C语言程序实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char bloodType[4][3] = {"A", "B", "AB", "O"}; // 血型数组
char parent1[3], parent2[3], child[3]; // 父母和孩子的血型
int i;
srand((unsigned)time(NULL)); // 初始化随机数种子
for (i = 1; i <= 10; i++) {
printf("请输入第%d个人的血型和父母的血型(如:A B O):", i);
scanf("%s %s %s", child, parent1, parent2);
// 根据父母的血型随机生成孩子的基因型
if (parent1[0] == parent2[0]) { // 父母同种血型
if (parent1[0] == 'A') {
if (rand() % 2 == 0) {
strcpy(child, "AA");
} else {
strcpy(child, "AO");
}
} else if (parent1[0] == 'B') {
if (rand() % 2 == 0) {
strcpy(child, "BB");
} else {
strcpy(child, "BO");
}
} else if (parent1[0] == 'AB') {
strcpy(child, "AB");
} else {
strcpy(child, "OO");
}
} else { // 父母不同种血型
if (rand() % 2 == 0) {
strcpy(child, "A");
if (parent1[0] == 'B' || parent2[0] == 'B') {
strcat(child, "B");
} else {
strcat(child, "O");
}
} else {
strcpy(child, "B");
if (parent1[0] == 'A' || parent2[0] == 'A') {
strcat(child, "A");
} else {
strcat(child, "O");
}
}
}
// 根据基因型计算表型
if (strcmp(child, "AA") == 0 || strcmp(child, "AO") == 0) {
printf("第%d个人的基因型为%s,表型为%s\n", i, child, bloodType[0]);
} else if (strcmp(child, "BB") == 0 || strcmp(child, "BO") == 0) {
printf("第%d个人的基因型为%s,表型为%s\n", i, child, bloodType[1]);
} else if (strcmp(child, "AB") == 0) {
printf("第%d个人的基因型为%s,表型为%s\n", i, child, bloodType[2]);
} else {
printf("第%d个人的基因型为%s,表型为%s\n", i, child, bloodType[3]);
}
}
return 0;
}
```
这个程序会提示用户依次输入十个人的血型和他们的父母的血型,并根据血型遗传规律随机生成每个人的基因型,最后输出每个人的基因型和表型。