这是代码:
// 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
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
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 = malloc(sizeof(person));
if (person == NULL)
{
return NULL;
}
// If there are still generations left to create
if (generations > 1)
{
// Create two new parents for current person by recursively calling create_family
person *parent0 = create_family(generations - 1);
person *parent1 = create_family(generations - 1);
// TODO: Set parent pointers for current person
person->parent[0] = parent0;
person->parent[1] = parent1;
// TODO: Randomly assign current person's alleles based on the alleles of their parents
if (rand() % 2 == 0)
{
person->alleles[0] = person->parent[0]->alleles[0];
}
else
{
person->alleles[0] = person->parent[0]->alleles[1];
}
if (rand() % 2 == 0)
{
person->alleles[1] = person->parent[1]->alleles[0];
}
else
{
person->alleles[1] = person->parent[1]->alleles[1];
}
}
// If there are no generations left to create
else
{
// TODO: Set parent pointers to NULL
person->parent[0] = NULL;
person->parent[1] = NULL;
// TODO: Randomly assign alleles
person->alleles[0] = random_allele();
person->alleles[1] = random_allele();
}
// TODO: Return newly created person
return person;
return NULL;
}
// Free `p` and all ancestors of `p`.
void free_family(person *p)
{
// TODO: Handle base case
if (p->parent[0] == NULL)
{
free(p);
return;
}
// TODO: Free parents recursively
free_family(p->parent[0]);
free_family(p->parent[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
if (generation == 0)
{
printf("Child (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
}
else if (generation == 1)
{
printf("Parent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
}
else
{
for (int i = 0; i < generation - 2; i++)
{
printf("Great-");
}
printf("Grandparent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
}
// Print parents of current generation
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';
}
}
显然错误出在 create_family 函数的 if 子句中:
if (generations > 1)
{
// Create two new parents for current person by recursively calling create_family
person *parent0 = create_family(generations - 1);
person *parent1 = create_family(generations - 1);
}
编译时出现的错误:
inheritance.c:52:17: error: use of undeclared identifier 'parent0'
person *parent0 = create_family(generations - 1);
^
我尝试将指针的名称更改为与“person0”不同的名称,但仍然无法解决任何问题。我不明白为什么错误是“未声明的标识符”,我认为从这个意义上说,代码是正确的,并且创建指向这样的人的指针应该不是问题。在整个代码中没有像这样调用其他指针/元素。
您在以
person
开头的行上定义了一个名为 person *person
的变量。因此,您有一个名称完全相同的变量和类型,并且编译器对您编写 person *parent0
时的含义感到困惑:它认为您想要将 person
变量parent0
乘。
我建议您的类型名称以大写字母开头以避免这种情况。
person *person
不是最好的主意。之后 person
被解释为变量 person
,而不是类型。