#include #include #define NAME_LEN 40 #define BIRTHDATE_LEN 40 #define MAX_PEOPLE_COUNT 1000 struct person { char birthdate[BIRTHDATE_LEN]; char name[NAME_LEN]; struct person * mother; struct person * father; }; struct person people[MAX_PEOPLE_COUNT]; struct person * find_person(const char * name, struct person * begin, struct person * end) { for(; begin != end; ++begin) if (strcmp(name, begin->name) == 0) return begin; return 0; } int read_people_file(FILE * input) { char buf[MAX_PEOPLE_COUNT]; int line; int count = 0; for(line = 1; count < MAX_PEOPLE_COUNT && fgets(buf, MAX_PEOPLE_COUNT, input) != 0; ++line) { char * name; char * birthdate; char * mother; char * father; static const char * separators = ",\n"; if (!(birthdate = strtok(buf, separators)) || !(name = strtok(0, separators)) || !(mother = strtok(0, separators)) || !(father = strtok(0, separators))) { fprintf(stderr, "bad format on line %d\n", line); continue; } struct person * p = people + count; strncpy(p->name, name, NAME_LEN); strncpy(p->birthdate, birthdate, BIRTHDATE_LEN); p->mother = find_person(mother, people, people + count); p->father = find_person(father, people, people + count); ++count; if (!p->mother && count < MAX_PEOPLE_COUNT) { p->mother = people + count; strncpy(p->mother->name, mother, NAME_LEN); p->mother->birthdate[0] = 0; p->mother->father = 0; p->mother->mother = 0; ++count; } if (!p->father && count < MAX_PEOPLE_COUNT) { p->father = people + count; strncpy(p->father->name, father, NAME_LEN); p->father->birthdate[0] = 0; p->father->father = 0; p->father->mother = 0; ++count; } } return count; } void print_genealogy(const struct person * p, int level) { if (!p) return; const char * g; if (level < 0) { g = "father"; level = -level; } else { g = "mother"; } int l; for(l = level; l > 2; --l) printf("great-"); if (l > 1) { printf("grand"); --l; } if (l > 0) printf("%s ", g); printf("%s\n", p->name); ++level; print_genealogy(p->mother, level); print_genealogy(p->father, -level); } int main(int argc, const char * argv[]) { int n, i; n = read_people_file(stdin); for(i = 1; i < argc; ++i) print_genealogy(find_person(argv[i], people, people + n), 0); }