VC++ 5.题目:根据血型遗传关系,编程实现:①输入父亲或母亲一个人的血型时,输 出子女可能的血型,②输入子女的血型时,输出其父母可能的血型,同时输入父亲和 母亲的血型时,输出子女可能的血型, 4同时输入父母和子女血型时能判断是否具有血 型血缘关系。 题目难度,易 程序设计要求 (1程序运行时有友好的操作提示界面; )可进行多人次输入,将血型关系总表输入文件保持,在主菜单中能选择查看血型关 系总表; B.有输入错误检查,并限制出错次数,如:应输入字母时输入其它内容,能提示错误 开要求重新输入: 4..程序中有详细的注程说明
时间: 2024-03-06 18:48:44 浏览: 21
这是一道比较复杂的编程题目,需要综合运用多个知识点,包括输入输出、条件语句、文件操作等等。
下面是一个初步的解决方案:
1. 首先需要定义血型的类型,可以使用枚举类型来表示不同的血型:
```
enum BloodType {
A, B, AB, O
};
```
2. 然后需要定义一个函数来根据父母血型计算子女可能的血型,可以根据血型遗传关系来判断:
```
void calcChildBloodType(BloodType father, BloodType mother, BloodType& child1, BloodType& child2) {
if (father == A && mother == A) {
child1 = A;
child2 = O;
} else if (father == A && mother == B) {
child1 = A;
child2 = B;
} else if (father == A && mother == AB) {
child1 = A;
child2 = B;
} else if (father == A && mother == O) {
child1 = A;
child2 = O;
} else if (father == B && mother == A) {
child1 = A;
child2 = B;
} else if (father == B && mother == B) {
child1 = B;
child2 = O;
} else if (father == B && mother == AB) {
child1 = A;
child2 = B;
} else if (father == B && mother == O) {
child1 = B;
child2 = O;
} else if (father == AB && mother == A) {
child1 = A;
child2 = B;
} else if (father == AB && mother == B) {
child1 = A;
child2 = B;
} else if (father == AB && mother == AB) {
child1 = A;
child2 = B;
} else if (father == AB && mother == O) {
child1 = A;
child2 = B;
} else if (father == O && mother == A) {
child1 = A;
child2 = O;
} else if (father == O && mother == B) {
child1 = B;
child2 = O;
} else if (father == O && mother == AB) {
child1 = A;
child2 = B;
} else if (father == O && mother == O) {
child1 = O;
child2 = O;
}
}
```
这个函数需要传入父亲和母亲的血型,以及两个引用类型的变量,用来保存可能的子女血型。在函数内部,根据血型遗传关系来计算子女的血型。
3. 接下来需要定义一个函数来根据子女血型计算父母可能的血型,也可以根据血型遗传关系来判断:
```
void calcParentBloodType(BloodType child, BloodType& father1, BloodType& father2, BloodType& mother1, BloodType& mother2) {
if (child == A) {
father1 = A;
father2 = O;
mother1 = A;
mother2 = O;
} else if (child == B) {
father1 = B;
father2 = O;
mother1 = B;
mother2 = O;
} else if (child == AB) {
father1 = A;
father2 = B;
mother1 = A;
mother2 = B;
} else if (child == O) {
father1 = O;
father2 = O;
mother1 = O;
mother2 = O;
}
}
```
这个函数需要传入子女的血型,以及四个引用类型的变量,用来保存可能的父母血型。在函数内部,根据血型遗传关系来计算父母的血型。
4. 在主函数中,需要使用循环来进行多次输入输出,并且需要添加输入错误的检查和提示:
```
int main() {
// 读取血型关系总表
// ...
while (true) {
// 显示操作提示
cout << "请选择操作:" << endl;
cout << "1. 父母血型计算子女可能的血型" << endl;
cout << "2. 子女血型计算父母可能的血型" << endl;
cout << "3. 判断是否具有血型血缘关系" << endl;
cout << "4. 查看血型关系总表" << endl;
cout << "5. 退出程序" << endl;
// 读取用户选择
int choice;
cin >> choice;
if (choice == 1) {
// 输入父母血型
BloodType father, mother;
cout << "请输入父亲血型(A/B/AB/O):";
cin >> father;
cout << "请输入母亲血型(A/B/AB/O):";
cin >> mother;
// 计算子女可能的血型
BloodType child1, child2;
calcChildBloodType(father, mother, child1, child2);
// 输出结果
cout << "可能的子女血型为:" << child1 << " " << child2 << endl;
} else if (choice == 2) {
// 输入子女血型
BloodType child;
cout << "请输入子女血型(A/B/AB/O):";
cin >> child;
// 计算父母可能的血型
BloodType father1, father2, mother1, mother2;
calcParentBloodType(child, father1, father2, mother1, mother2);
// 输出结果
cout << "可能的父亲血型为:" << father1 << " " << father2 << endl;
cout << "可能的母亲血型为:" << mother1 << " " << mother2 << endl;
} else if (choice == 3) {
// 输入父母和子女血型
BloodType father, mother, child;
cout << "请输入父亲血型(A/B/AB/O):";
cin >> father;
cout << "请输入母亲血型(A/B/AB/O):";
cin >> mother;
cout << "请输入子女血型(A/B/AB/O):";
cin >> child;
// 判断是否具有血型血缘关系
BloodType father1, father2, mother1, mother2, child1, child2;
calcParentBloodType(child, father1, father2, mother1, mother2);
calcChildBloodType(father, mother, child1, child2);
bool hasRelation = (child1 == child || child2 == child) && (father1 == father || father2 == father) && (mother1 == mother || mother2 == mother);
// 输出结果
if (hasRelation) {
cout << "具有血型血缘关系" << endl;
} else {
cout << "不具有血型血缘关系" << endl;
}
} else if (choice == 4) {
// 显示血型关系总表
// ...
} else if (choice == 5) {
// 退出程序
break;
} else {
// 输入错误,提示重新输入
cout << "输入错误,请重新输入(1-5)!" << endl;
}
}
return 0;
}
```
在主函数中,使用一个无限循环来进行多次输入输出,直到用户选择退出程序。每次循环先显示操作提示,然后根据用户的选择执行相应的操作。如果用户输入错误,会有相应的错误提示。同时,还需要读取和保存血型关系总表,以及添加注释说明。