Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO.Pipes;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- OrderedItemsHandler orderedItemsHandler = new OrderedItemsHandler(1000);
- orderedItemsHandler.Sort();
- Person[] cancerFreeTeam = orderedItemsHandler.Filter(IsCancerFreePerson);
- Console.Read();
- }
- private static bool IsCancerFreePerson(Person person)
- {
- return !person.IsCancer();
- }
- }
- }
- ---------------------------------------------------------------------------------
- Osztályak
- ---------------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- internal class Person : IComparable
- {
- private static Random random = new Random();
- private int age;
- private bool isCancer;
- private StateOfCancer stateOfCancer;
- public event Action<Person> CancerStateChanged;
- public event Action<Person> TreatmentOfCancer;
- public enum StateOfCancer
- {
- None, One, Two, Three, Four
- }
- public Person(int age)
- {
- this.age = age;
- }
- public int GetAge()
- { return this.age; }
- public bool IsCancer()
- { return this.isCancer; }
- public StateOfCancer GetStateOfCancer()
- { return this.stateOfCancer; }
- public void Diagnose()
- {
- int rng = random.Next(1, 101);
- if (this.isCancer && rng <= 45)
- {
- int newState = random.Next(1, 4);
- this.stateOfCancer = (StateOfCancer)newState;
- this.CancerStateChanged?.Invoke(this);
- }
- if (!this.isCancer && rng <= 90)
- {
- this.isCancer = true;
- int newState = random.Next(1, 4);
- this.stateOfCancer = (StateOfCancer)newState;
- this.CancerStateChanged?.Invoke(this);
- }
- }
- public void GetTreatment()
- {
- int treatmentPower = random.Next(1, 11);
- if (this.isCancer && treatmentPower < 9)
- {
- this.stateOfCancer = StateOfCancer.None;
- this.isCancer = false;
- this.TreatmentOfCancer?.Invoke(this);
- }
- if (this.isCancer && treatmentPower > 9)
- {
- this.Diagnose();
- }
- }
- public int CompareTo(object obj)
- {
- if (obj is Person)
- {
- Person otherPerson = obj as Person;
- if (this.age < otherPerson.age) return -1;
- if (this.age > otherPerson.age) return 1;
- }
- return 0;
- }
- }
- }
- ---------------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- internal class OrderedItemsHandler
- {
- private static Random random = new Random();
- private Person[] people;
- public enum SortingMethod
- {
- Selection, Bubble, Insertion
- }
- public enum Sorting
- {
- ASC, DESC
- }
- public OrderedItemsHandler(int n)
- {
- this.people = new Person[n];
- for (int i = 0; i < n; ++i)
- {
- this.people[i] = new Person(random.Next(20, 96));
- this.people[i].CancerStateChanged += Notify;
- this.people[i].Diagnose();
- }
- }
- public Person[] Filter(Predicate<Person> p)
- {
- if (p == null)
- {
- return this.people;
- }
- Person[] filtered = new Person[0];
- foreach (Person person in this.people)
- {
- if (p.Invoke(person))
- {
- filtered = filtered.Append(person).ToArray();
- }
- }
- return filtered;
- }
- public void Sort(SortingMethod sortingMethod = SortingMethod.Bubble, Sorting sorting = Sorting.ASC)
- {
- switch (sortingMethod)
- {
- case SortingMethod.Selection:
- SelectionSort();
- break;
- case SortingMethod.Bubble:
- BubbleSort();
- break;
- case SortingMethod.Insertion:
- InsertionSort();
- break;
- }
- }
- private void BubbleSort()
- {
- int i = this.people.Length;
- while (i >= 1)
- {
- int idx = -1;
- for (int j = 0; j < i - 1; ++j)
- {
- if (this.people[j].CompareTo(this.people[j + 1]) == 1)
- {
- Person helper = this.people[j];
- this.people[j] = this.people[j + 1];
- this.people[j + 1] = helper;
- idx = j;
- }
- }
- i = idx;
- }
- }
- private void SelectionSort()
- {
- for (int i = 0; i < this.people.Length - 1; ++i)
- {
- int min = i;
- for (int j = i + 1; j < this.people.Length; ++j)
- {
- if (this.people[min].CompareTo(this.people[j]) == -1)
- {
- min = j;
- }
- }
- Person helper = this.people[min];
- this.people[min] = this.people[i];
- this.people[i] = helper;
- }
- }
- private void InsertionSort()
- {
- for (int i = 1; i < people.Length; ++i)
- {
- int j = i - 1;
- while (j > -1 && this.people[j].CompareTo(this.people[j + 1]) == 1)
- {
- Person helper = this.people[j];
- this.people[j] = this.people[j + 1];
- this.people[j + 1] = helper;
- i = j - 1;
- }
- }
- }
- public int BinarySearchByAge(int left, int right, int age)
- {
- if (left > right) return -1;
- int center = (left + right) / 2;
- if (this.people[center].GetAge() == age)
- {
- return center;
- }
- if (this.people[center].GetAge() > age)
- {
- return BinarySearchByAge(left, center - 1, age);
- }
- return BinarySearchByAge(center + 1, right, age);
- }
- private void Notify(Person person)
- {
- Console.WriteLine($"Potential money bag: {person.GetAge()} {person.GetStateOfCancer()}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement