64,637
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHILDEN_BLOOD 5
struct _pair{
const char* father;
const char* mother;
};
struct _m{
struct _pair k;
const char* c[MAX_CHILDEN_BLOOD];
} m[] = {
{{"O","O"}, {"O", NULL}},
{{"A","O"}, {"A","O", NULL}},
{{"A","A"}, {"A", "O", NULL}},
{{"A","B"}, {"A","AB","B","O", NULL}},
{{"A", "AB"}, {"A","AB","B", NULL}},
{{"B","O"}, {"B", "O", NULL}},
{{"B","B"}, {"B","O", NULL}},
{{"B", "AB"},{"A","AB","B", NULL}},
{{"AB", "O"}, {"A", "B", NULL}},
{{"AB","AB"}, {"A","AB","B", NULL}}
};
struct _m2 {
const char* pair;
const char* c[MAX_CHILDEN_BLOOD];
} m2[] = {
{"OO", {"O", NULL}},
{"AO", {"A","O", NULL}},
{"AA", {"A", "O", NULL}},
{"AB", {"A","AB","B","O", NULL}},
{"AAB", {"A","AB","B", NULL}},
{"BO", {"B", "O", NULL}},
{"BB", {"B","O", NULL}},
{"BAB",{"A","AB","B", NULL}},
{"ABO", {"A", "B", NULL}},
{"ABAB", {"A","AB","B", NULL}}
};
const char** m_check_blood_type(const char* father, const char* mother) {
int i = 0;
for (i; i < sizeof(m)/sizeof(m[0]); ++i) {
struct _m* p = m+i;
if (0 == strcmp(p->k.father, father) &&
0 == strcmp(p->k.mother, mother) ||
0 == strcmp(p->k.father, mother) &&
0 == strcmp(p->k.mother, father)
) {
return p->c;
}
}
return NULL;
}
const char** m2_check_blood_type(const char* father, const char* mother) {
char key[16] = { 0 };
char key2[16] = { 0 };
int i ;
strcpy(key, father);
strcat(key, mother);
strcpy(key, mother);
strcat(key, father);
for (i = 0; i < sizeof(m2)/sizeof(m2[0]); ++i) {
struct _m2* p = m2+i;
if (0 == strcmp(key, p->pair) || 0 == strcmp(key2, p->pair)) {
return p->c;
}
}
return NULL;
}
void print_children_types( const char** p ) {
while (p && *p) {
printf("%s,", *p);
++p;
}
printf("\n");
}
int main() {
const char** result2, **p2;
const char** result,**p;
result = m_check_blood_type("O","B");
p = result;
printf("f=O, m=B:\n");
print_children_types(p);
result2 = m2_check_blood_type("O", "B");
p2 = result2;
print_children_types(p2);
return 0;
}