예제 #1
0
        static void Main(string[] args)
        {
            var wantedPersonInfo = Console.ReadLine();
            var inputs           = new List <string>();
            var people           = new List <Person>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                if (input.Contains("-"))
                {
                    inputs.Add(input);
                }
                else
                {
                    string[] personInfo = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                    string   name       = $"{personInfo[0]} {personInfo[1]}";
                    string   birthday   = personInfo[2];
                    Person   person     = new Person(name, birthday);
                    people.Add(person);
                }
            }

            foreach (string tie in inputs)
            {
                string[] tieInfo    = tie.Split("-", StringSplitOptions.RemoveEmptyEntries);
                string   parentInfo = tieInfo[0].Trim();
                string   childInfo  = tieInfo[1].Trim();
                Person   parent     = GetPerson(people, parentInfo);
                Person   child      = GetPerson(people, childInfo);
                parent.AddChild(child);
                child.AddParent(parent);
            }

            Person wantedPerson = GetPerson(people, wantedPersonInfo);

            Console.WriteLine(wantedPerson);
            Console.WriteLine("Parents:");

            if (wantedPerson.Parents.Count > 0)
            {
                Console.WriteLine(string.Join(Environment.NewLine, wantedPerson.Parents));
            }

            Console.WriteLine("Children:");

            if (wantedPerson.Children.Count > 0)
            {
                Console.WriteLine(string.Join(Environment.NewLine, wantedPerson.Children));
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            List <Person> tree      = new List <Person>();
            string        searchFor = Console.ReadLine();
            string        input     = Console.ReadLine();

            while (input != "End")
            {
                string[] tokens = input.Split(" - ", StringSplitOptions.RemoveEmptyEntries);
                if (tokens.Length == 1)
                {
                    string[] newTokens = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                    string   firstname = newTokens[0];
                    string   lastname  = newTokens[1];
                    string   birthday  = newTokens[2];
                    Person   person    = new Person(firstname, lastname, birthday);
                    tree.Add(person);
                }
                else if (tokens[0].Contains(" "))
                {
                    string[] newTokens       = tokens[0].Split(" ", StringSplitOptions.RemoveEmptyEntries);
                    string   firstnameParent = newTokens[0];
                    string   lastnameParent  = newTokens[1];
                    Person   parent          = new Person(firstnameParent, lastnameParent);
                    if (tokens[1].Contains(" "))
                    {
                        newTokens = tokens[1].Split(" ", StringSplitOptions.RemoveEmptyEntries);
                        string firstnameChild = newTokens[0];
                        string lastnameChild  = newTokens[1];
                        Person person         = new Person(firstnameChild, lastnameChild);
                        person.AddParent(parent);
                        parent.AddChild(person);
                    }
                    else
                    {
                        Person person = new Person(tokens[1]);
                        person.AddParent(parent);
                        parent.AddChild(person);
                    }
                }
                else if (tokens[1].Contains(" "))
                {
                    string   parentBirthday = tokens[0];
                    Person   parent         = new Person(parentBirthday);
                    string[] newTokens      = tokens[1].Split(" ", StringSplitOptions.RemoveEmptyEntries);
                    string   firstnameChild = newTokens[0];
                    string   lastnameChild  = newTokens[1];
                    Person   person         = new Person(firstnameChild, lastnameChild);
                    person.AddParent(parent);
                    parent.AddChild(person);
                }
                else
                {
                    Person parent = new Person(tokens[0]);
                    Person person = new Person(tokens[0]);
                    person.AddParent(parent);
                    parent.AddChild(person);
                }
                input = Console.ReadLine();
            }

            if (searchFor.Contains(" "))
            {
                string[] tokens = searchFor.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                Person   person = tree.First(x => x.FirstName == tokens[0] && x.Lastname == tokens[1]);
                Console.WriteLine(person);
            }
            else
            {
                Person person = tree.First(X => X.Birthday == searchFor);
                Console.WriteLine(person);
            }
        }