Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C# by Andrei ( 7 years ago )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp6
{
    public class Person
    {
        public string name;
        public string mother;
        public string father;
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> people = new List<Person>()
            {
                new Person { name = "Dasha"},
                new Person { name = "Alex"},
                new Person { name = "Alysa", mother = "Dasha", father = "Alex"},
                new Person { name = "Ivan", mother = "Dasha", father = "Alex"},
                new Person { name = "Olya"},
                new Person { name = "Marya"},
                new Person { name = "Oleg"},
                new Person { name = "Liza"},
                new Person { name = "Max", mother = "Olya", father = "Ivan"},
                new Person { name = "Petya", mother = "Marya", father = "Ivan"},
                new Person { name = "Vasya", mother = "Marya", father = "Ivan"},
                new Person { name = "Vova", mother = "Marya", father = "Oleg"},
                new Person { name = "Oleg2", mother = "Liza", father = "Oleg"}
            };
            string name = Console.ReadLine();
            var person = checkPerson(name, people);

            Console.Write("родители:");
            var parents = checkParents(person, people);
            printNames(parents);
            //parents = parents.ToList();

            var father = parents.Where(b => b.name == person.father).FirstOrDefault();
            var mother = parents.Where(b => b.name == person.mother).FirstOrDefault();

            var childs = checkChilds(person, people);
            Console.Write("\nдети: ");
            printNames(childs);

            var grandMothers = people.Where(x => parents.Any(s => s.mother == x.name));
            var grandFathers = people.Where(x => parents.Any(s => s.father == x.name));
            Console.Write("\nбабушки: ");
            printNames(grandMothers);
            Console.Write("\nдедушки: ");
            printNames(grandFathers);


            var brothers = people.Where(x => parents.Any(s => s.name == x.mother)
                                            && parents.Any(s => s.name == x.father) && x != person);
            Console.Write("\nбратья: ");
            printNames(brothers);

            var oneFather = people.Where(x => parents.Any(s => s.name == x.father) && x != person);
            Console.Write("\nединокровные: ");
            printNames(oneFather);

            var oneMother = people.Where(x => parents.Any(s => s.name == x.mother) && x != person);
            Console.Write("\nединоутробные: ");
            printNames(oneMother);

            var aunts = people.Where(x => (grandMothers.Any(s => s.name == x.mother)
                                        || grandFathers.Any(s => s.name == x.father))
                                        && parents.All(s => s != x));
            Console.Write("\nтети: ");
            printNames(aunts);

            //Console.WriteLine("\nвведите предка:");
            //var ancestorName = Console.ReadLine();
            //var ancestor = checkPerson(ancestorName, people);
            //var isAncestor = checkAncestor(person, ancestor, people);
            //Console.Write(isAncestor ? "является предком" : "не является предком");

            //Console.WriteLine("\nвведите прародителя:");
            //var primogenitorName = Console.ReadLine();
            //var primogenitor = checkPerson(primogenitorName, people);
            //var isPrimogenitor = checkPrimogenitor(person, primogenitor, people);
            //Console.Write(isPrimogenitor ? "является пращуром" : "не является пращуром");

            //Console.WriteLine("\nвведите родственника:");
            //var relativeName = Console.ReadLine();
            //var relative = checkPerson(relativeName, people);
            //var isRelative = checkRelationship(person, relative, people);
            //Console.Write(isRelative ? "является родственником с общими предками" : "не является родственником");

            //Console.WriteLine("\nвведите сводного брата в общем смысле:");
            //var relativeName = Console.ReadLine();
            //var stepBro = checkPerson(relativeName, people);
            //var isBro = checkBrothership(person, stepBro, people);
            //Console.Write(isBro ? "является сводным братом" : "не является братом");

            //Console.Write("\nродственники: ");
            //printNames(getRelatives(person, people));

            //Console.WriteLine("\nвведите свояка:");
            //var relativeName = Console.ReadLine();
            //var relative = checkPerson(relativeName, people);
            //var isSvoyak = checkSvoyak(person, relative, people, new HashSet<Person>());
            //Console.Write(isSvoyak ? "является свояком" : "не является свояком");

            Console.WriteLine("\nвведите человека из поколения:");
            var genName = Console.ReadLine();
            var genPerson = checkPerson(genName, people);
            var isOneGeneration = checkGeneration(person, genPerson, people, new HashSet<Person>());
            Console.Write(isOneGeneration ? "из одного поколения" : "не из одного поколения");


            Console.ReadKey();
        }

        static public bool checkGeneration(Person person, Person genPerson, IEnumerable<Person> people, HashSet<Person> checkedPeople)
        {
            var childs = checkParents(person, people);
            var genChilds = checkParents(genPerson, people);
            if ()
            return false;
        }

        static public bool checkSvoyak(Person person, Person relative, IEnumerable<Person> people, HashSet<Person> checkedPeople)
        {
            var rlship = checkRelationship(person, relative, people);
            if (rlship && checkedPeople.Count == 0)
            {
                return false;
            }
            else if (rlship)
            {
                return true;
            }

            var spouses = getSpouses(person, people).Where(x => checkedPeople.All(f => f != x));
            checkedPeople.Add(person);

            if(spouses.Any(x=> x == relative))
            {
                return true;
            }

            foreach(var item in spouses)
            {
                var spousesSpouses = getSpouses(item, people).Where(x => checkedPeople.All(f=> f != x));
                foreach(var item2 in spousesSpouses)
                {
                    if(checkSvoyak(item2, relative, people, checkedPeople))
                    {
                        return true;
                    }
                }
                if (checkSvoyak(item, relative, people, checkedPeople))
                {
                    return true;
                }
            }

            var relatives = getRelatives(person, people).Where(x => checkedPeople.All(f => f != x));
            foreach (var item in relatives)
            {
                if (checkSvoyak(item, relative, people, checkedPeople))
                {
                    return true;
                }
            }

            return false;
        }

        static public IEnumerable<Person> getSpouses(Person person, IEnumerable<Person> people)
        {
            return people.Where(x => checkChilds(person, people).Any(f => f.father == x.name || f.mother == x.name) && x != person);
        }

        static public bool checkBrothership(Person person, Person stepBro, IEnumerable<Person> people)
        {
            var parents = checkParents(person, people);
            if (parents.Any(x => x.name == stepBro.mother || x.name == stepBro.father))
            {
                return true;
            }
            foreach (var item in parents)
            {
                var brothersList = checkChilds(item, people).Where(x => x != person);
                foreach(var brother in brothersList)
                {
                    var notBothParent = people.Where(x => (brother.father == x.name || brother.mother == x.name) && parents.All(g => g != x));
                    foreach (var item2 in notBothParent)
                    {
                        var newBrothers = (checkChilds(item2, people).Where(x => x != brother));
                        foreach (var item3 in newBrothers)
                        {
                            if(checkBrothership(item3, stepBro, people))
                            {
                                return true;
                            }
                        }
                    }
                }
            }

            return false;
        }

        static public bool checkRelationship(Person person, Person relative, IEnumerable<Person> people)
        {
            if (checkAncestor(person, relative, people) || checkAncestor(relative, person, people))
            {
                return true;
            }

            var ancestors = getAncestors(person, people);

            foreach(var item in ancestors)
            {
                if(checkAncestor(relative, item, people))
                {
                    return true;
                }
            }

            return false;
        }

        static public bool checkPrimogenitor(Person person, Person primogenitor, IEnumerable<Person> people)
        {
            var noParents = checkParents(primogenitor, people).All(s => s == null);
            if (!noParents)
            {
                return false;
            }
            if (noParents && checkChilds(primogenitor, people).Any(s => s.name == person.name))
            {
                return true;
            }
            var parents = checkParents(person, people);
            foreach (var item in parents)
            {
                if (checkPrimogenitor(item, primogenitor, people))
                {
                    return true;
                }
            }
            return false;
        }

        static public bool checkAncestor(Person person, Person ancestor, IEnumerable<Person> people)
        {
            var isAncestorParent = checkChilds(ancestor, people).Any(s => s.name == person.name);
            if (isAncestorParent)
            {
                return true;
            }
            var parents = checkParents(person, people);
            foreach (var item in parents)
            {
                if (checkAncestor(item, ancestor, people))
                {
                    return true;
                }
            }
            return false;
        }

        static public IEnumerable<Person> getRelatives(Person person, IEnumerable<Person> people)
        {
            var ancestors = people.Where(x => checkRelationship(person, x, people) && x != person);
            return ancestors;
        }

        static public IEnumerable<Person> getAncestors(Person person, IEnumerable<Person> people)
        {
            var ancestors = people.Where(x => checkAncestor(person, x, people));
            return ancestors;
        }

        static public IEnumerable<Person> checkChilds(Person person, IEnumerable<Person> people)
        {
            return people.Where(x => x.mother == person.name || x.father == person.name);
        }

        static public Person checkPerson(string name, IEnumerable<Person> people)
        {
            return people.Where(x => x.name == name).FirstOrDefault();
        }

        static public IEnumerable<Person> checkParents(Person person, IEnumerable<Person> people)
        {
            return people.Where(x => x.name == person.father || x.name == person.mother);
        }

        static public void printNames(IEnumerable<Person> people)
        {
            if (people != null && people.Any())
            {
                foreach (var item in people)
                {
                    Console.Write($"{item.name} ");
                }
            }
            else
                Console.Write("нет");
        }
    }
}

 

Revise this Paste

Your Name: Code Language: