-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-CS50-CÓDIGO - HERANCA
95 lines (76 loc) · 1.69 KB
/
05-CS50-CÓDIGO - HERANCA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Dois pais e dois alelos
typedef struct person
{
struct person *parents[2];
char alleles[2];
} person;
const int GENERATIONS = 3;
const int INDENT_LENGTH = 4;
person *create_family(int generations);
void print_family(person *p, int generation);
void free_family(person *p);
char random_allele();
int main(void)
{
srand(time(0));
// Nova familia 3 gerações
person *p = create_family(GENERATIONS);
print_family(p, 0);
free_family(p);
}
// Familia individual
person *create_family(int generations)
{
// Alocar meoria
person *p = malloc(sizeof(person));
// Parentes
if (generations > 1)
{
// Historia com parentes
p->parents[0] = create_family(generations - 1);
p->parents[1] = create_family(generations - 1);
p->alleles[0] = p->parents[0]->alleles[rand() % 2];
p->alleles[1] = p->parents[1]->alleles[rand() % 2];
}
else
{
p->parents[0] = NULL;
p->parents[1] = NULL;
p->alleles[0] = random_allele();
p->alleles[1] = random_allele();
}
// Nova pessoa
return p;
}
void free_family(person *p)
{
if (p == NULL)
return;
free_family(p->parents[0]);
free_family(p->parents[1]);
free(p);
}
void print_family(person *p, int generation)
{
if (p == NULL)
return;
for (int i = 0; i < generation * INDENT_LENGTH; i++)
printf(" ");
printf("Geração %i, tipo sanguineo %c%c\n", generation, p->alleles[0], p->alleles[1]);
print_family(p->parents[0], generation + 1);
print_family(p->parents[1], generation + 1);
}
char random_allele()
{
int r = rand() % 3;
if (r == 0)
return 'A';
else if (r == 1)
return 'B';
else
return 'O';
}