CS50 lab5 継承
CS50 lab5
血液型の継承
3世代の家系図
構成員にそれぞれメモリを割り当てる
最長老にはランダムに血液型を設定
→親から子へ、片方の親から1つずつ対立遺伝子を継承
乱数
srand()で乱数のパターンを変えて、rand()で乱数を返す。
srand()に与える引数はシード(種)と呼ばれ、基本はtime()で時刻を使うのがセオリー。
rand()だけだと、複数回使うと同一パターンの乱数を出力してしまう
srand()でパターンを変えてやらないとランダムな結果にならない
time()
1970年1月1日からの経過秒数が取得できる関数→シードに使える
time()の引数は時刻の出力先。NULLや0を引数とした場合は出力されないので、シードとして使うだけならNULLや0を引数にする
▼乱数範囲の取得公式
[取得目標範囲の最小値] + rand()% [乱数で取得したい数の個数]
▼inheritance.c
// Simulate genetic inheritance of blood type
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Each person has two parents and two alleles
// alleles = 対立遺伝子
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)
{
// Seed random number generator
//乱数にシードを与えてパターンを変える
srand(time(0));
// Create a new family with three generations
//create_family関数の戻り値は、最も若いpersonへのポインタ
person *p = create_family(GENERATIONS);
// Print family tree of blood types
print_family(p, 0);
// Free memory
free_family(p);
}
// Create a new individual with `generations`
person *create_family(int generations)
{
// TODO: Allocate memory for new person
// 新しいpersonのためにメモリを取得
person *new = malloc(sizeof(person));
// Generation with parent data
// 親がいる場合
if (generations > 1)
{
// TODO: Recursively create blood type histories for parents
// generationesが1より大きいということは、更に親がいるということ。
// 再帰的にcreate_person()を呼び出す。引数はgenerations-1。
generations = generations -1;
person *parent1 = create_family(generations);
new ->parents[0] = parent1;
person *parent2 = create_family(generations);
new ->parents[1] = parent2;
// TODO: Randomly assign child alleles based on parents
// 両方の親から対立遺伝子をランダムに1つずつ継承する
new ->alleles[0] = parent1 ->alleles[rand()%2];
new ->alleles[1] = parent2 ->alleles[rand()%2];
}
// Generation without parent data
// 親がいない場合
else
{
// TODO: Set parent pointers to NULL
new ->parents[0] = NULL;
new ->parents[1] = NULL;
// TODO: Randomly assign alleles
new ->alleles[0] = random_allele();
new ->alleles[1] = random_allele();
}
// TODO: Return newly created person
return new;
}
// Free `p` and all ancestors of `p`.
void free_family(person *p)
{
// TODO: Handle base case
if(p == NULL)
{
return;
}
// TODO: Free parents
free_family(p ->parents[0]);
free_family(p ->parents[1]);
// TODO: Free child
free(p);
}
// Print each family member and their alleles.
void print_family(person *p, int generation)
{
// Handle base case
if (p == NULL)
{
return;
}
// Print indentation
// 字下げ(段落 読みやすくするための空白を入れる)
for (int i = 0; i < generation * INDENT_LENGTH; i++)
{
printf(" ");
}
// Print person
printf("Generation %i, blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
print_family(p->parents[0], generation + 1);
print_family(p->parents[1], generation + 1);
}
// Randomly chooses a blood type allele.
// 最長老の血液型をランダムに決めてくれる関数
char random_allele()
{
int r = rand() % 3;
if (r == 0)
{
return 'A';
}
else if (r == 1)
{
return 'B';
}
else
{
return 'O';
}
}