Ejercicio
Estructuras anidadas
Objetivo
Cree una estructura para almacenar dos datos para una persona:
nombre y fecha de nacimiento.
La fecha de nacimiento debe ser otra estructura que consista en día, mes y año.
Finalmente, cree una matriz de personas, pida al usuario el dato de dos personas y muéstrelas.
Código de Ejemplo
using System;
class Program
{
struct DateOfBirth
{
public int Day;
public int Month;
public int Year;
}
struct Person
{
public string Name;
public DateOfBirth BirthDate;
}
static void Main()
{
Person[] persons = new Person[2];
for (int i = 0; i < persons.Length; i++)
{
Console.Write($"Enter the name of person {i + 1}: ");
persons[i].Name = Console.ReadLine();
Console.Write($"Enter the birth day of person {i + 1}: ");
persons[i].BirthDate.Day = int.Parse(Console.ReadLine());
Console.Write($"Enter the birth month of person {i + 1}: ");
persons[i].BirthDate.Month = int.Parse(Console.ReadLine());
Console.Write($"Enter the birth year of person {i + 1}: ");
persons[i].BirthDate.Year = int.Parse(Console.ReadLine());
}
for (int i = 0; i < persons.Length; i++)
{
Console.WriteLine($"Person {i + 1}:");
Console.WriteLine($"Name: {persons[i].Name}");
Console.WriteLine($"Birth Date: {persons[i].BirthDate.Day}/{persons[i].BirthDate.Month}/{persons[i].BirthDate.Year}");
}
}
}