Advertisement
Levi0227

2. félév 10. hét

Apr 16th, 2024 (edited)
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.41 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Pipes;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp1
  9. {
  10.     internal class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             OrderedItemsHandler orderedItemsHandler = new OrderedItemsHandler(1000);
  15.             orderedItemsHandler.Sort();
  16.             Person[] cancerFreeTeam = orderedItemsHandler.Filter(IsCancerFreePerson);
  17.  
  18.             Console.Read();
  19.         }
  20.  
  21.         private static bool IsCancerFreePerson(Person person)
  22.         {
  23.             return !person.IsCancer();
  24.         }
  25.     }
  26. }
  27.  
  28. ---------------------------------------------------------------------------------
  29. Osztályak
  30. ---------------------------------------------------------------------------------
  31.  
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Linq;
  35. using System.Text;
  36. using System.Threading.Tasks;
  37.  
  38. using System;
  39. using System.Collections.Generic;
  40. using System.Linq;
  41. using System.Text;
  42. using System.Threading.Tasks;
  43.  
  44. namespace ConsoleApp1
  45. {
  46.     internal class Person : IComparable
  47.     {
  48.         private static Random random = new Random();
  49.         private int age;
  50.         private bool isCancer;
  51.         private StateOfCancer stateOfCancer;
  52.  
  53.         public event Action<Person> CancerStateChanged;
  54.         public event Action<Person> TreatmentOfCancer;
  55.  
  56.         public enum StateOfCancer
  57.         {
  58.             None, One, Two, Three, Four
  59.         }
  60.         public Person(int age)
  61.         {
  62.             this.age = age;
  63.         }
  64.  
  65.         public int GetAge()
  66.         { return this.age; }
  67.  
  68.         public bool IsCancer()
  69.         { return this.isCancer; }
  70.  
  71.         public StateOfCancer GetStateOfCancer()
  72.         { return this.stateOfCancer; }
  73.  
  74.         public void Diagnose()
  75.         {
  76.             int rng = random.Next(1, 101);
  77.             if (this.isCancer && rng <= 45)
  78.             {
  79.                 int newState = random.Next(1, 4);
  80.                 this.stateOfCancer = (StateOfCancer)newState;
  81.                 this.CancerStateChanged?.Invoke(this);
  82.             }
  83.  
  84.             if (!this.isCancer && rng <= 90)
  85.             {
  86.                 this.isCancer = true;
  87.                 int newState = random.Next(1, 4);
  88.                 this.stateOfCancer = (StateOfCancer)newState;
  89.                 this.CancerStateChanged?.Invoke(this);
  90.             }
  91.         }
  92.  
  93.         public void GetTreatment()
  94.         {
  95.             int treatmentPower = random.Next(1, 11);
  96.             if (this.isCancer && treatmentPower < 9)
  97.             {
  98.                 this.stateOfCancer = StateOfCancer.None;
  99.                 this.isCancer = false;
  100.                 this.TreatmentOfCancer?.Invoke(this);
  101.             }
  102.  
  103.             if (this.isCancer && treatmentPower > 9)
  104.             {
  105.                 this.Diagnose();
  106.             }
  107.         }
  108.  
  109.         public int CompareTo(object obj)
  110.         {
  111.             if (obj is Person)
  112.             {
  113.                 Person otherPerson = obj as Person;
  114.                 if (this.age < otherPerson.age) return -1;
  115.                 if (this.age > otherPerson.age) return 1;
  116.             }
  117.  
  118.             return 0;
  119.         }
  120.     }
  121. }
  122.  
  123. ---------------------------------------------------------------------------------
  124.  
  125. using System;
  126. using System.Collections.Generic;
  127. using System.Linq;
  128. using System.Text;
  129. using System.Threading.Tasks;
  130.  
  131. namespace ConsoleApp1
  132. {
  133.     internal class OrderedItemsHandler
  134.     {
  135.         private static Random random = new Random();
  136.         private Person[] people;
  137.         public enum SortingMethod
  138.         {
  139.             Selection, Bubble, Insertion
  140.         }
  141.         public enum Sorting
  142.         {
  143.             ASC, DESC
  144.         }
  145.  
  146.         public OrderedItemsHandler(int n)
  147.         {
  148.             this.people = new Person[n];
  149.             for (int i = 0; i < n; ++i)
  150.             {
  151.                 this.people[i] = new Person(random.Next(20, 96));
  152.                 this.people[i].CancerStateChanged += Notify;
  153.                 this.people[i].Diagnose();
  154.             }
  155.         }
  156.  
  157.         public Person[] Filter(Predicate<Person> p)
  158.         {
  159.             if (p == null)
  160.             {
  161.                 return this.people;
  162.             }
  163.  
  164.             Person[] filtered = new Person[0];
  165.             foreach (Person person in this.people)
  166.             {
  167.                 if (p.Invoke(person))
  168.                 {
  169.                     filtered = filtered.Append(person).ToArray();
  170.                 }
  171.             }
  172.  
  173.             return filtered;
  174.         }
  175.  
  176.         public void Sort(SortingMethod sortingMethod = SortingMethod.Bubble, Sorting sorting = Sorting.ASC)
  177.         {
  178.             switch (sortingMethod)
  179.             {
  180.                 case SortingMethod.Selection:
  181.                     SelectionSort();
  182.                     break;
  183.                 case SortingMethod.Bubble:
  184.                     BubbleSort();
  185.                     break;
  186.                 case SortingMethod.Insertion:
  187.                     InsertionSort();
  188.                     break;
  189.             }
  190.         }
  191.  
  192.         private void BubbleSort()
  193.         {
  194.             int i = this.people.Length;
  195.             while (i >= 1)
  196.             {
  197.                 int idx = -1;
  198.                 for (int j = 0; j < i - 1; ++j)
  199.                 {
  200.                     if (this.people[j].CompareTo(this.people[j + 1]) == 1)
  201.                     {
  202.                         Person helper = this.people[j];
  203.                         this.people[j] = this.people[j + 1];
  204.                         this.people[j + 1] = helper;
  205.                         idx = j;
  206.                     }
  207.                 }
  208.                 i = idx;
  209.             }
  210.         }
  211.  
  212.         private void SelectionSort()
  213.         {
  214.             for (int i = 0; i < this.people.Length - 1; ++i)
  215.             {
  216.                 int min = i;
  217.                 for (int j = i + 1; j < this.people.Length; ++j)
  218.                 {
  219.                     if (this.people[min].CompareTo(this.people[j]) == -1)
  220.                     {
  221.                         min = j;
  222.                     }
  223.                 }
  224.                 Person helper = this.people[min];
  225.                 this.people[min] = this.people[i];
  226.                 this.people[i] = helper;
  227.             }
  228.         }
  229.  
  230.         private void InsertionSort()
  231.         {
  232.             for (int i = 1; i < people.Length; ++i)
  233.             {
  234.                 int j = i - 1;
  235.                 while (j > -1 && this.people[j].CompareTo(this.people[j + 1]) == 1)
  236.                 {
  237.                     Person helper = this.people[j];
  238.                     this.people[j] = this.people[j + 1];
  239.                     this.people[j + 1] = helper;
  240.                     i = j - 1;
  241.                 }
  242.             }
  243.         }
  244.  
  245.         public int BinarySearchByAge(int left, int right, int age)
  246.         {
  247.             if (left > right) return -1;
  248.  
  249.             int center = (left + right) / 2;
  250.  
  251.             if (this.people[center].GetAge() == age)
  252.             {
  253.                 return center;
  254.             }
  255.  
  256.             if (this.people[center].GetAge() > age)
  257.             {
  258.                 return BinarySearchByAge(left, center - 1, age);
  259.             }
  260.  
  261.             return BinarySearchByAge(center + 1, right, age);
  262.         }
  263.  
  264.         private void Notify(Person person)
  265.         {
  266.             Console.WriteLine($"Potential money bag: {person.GetAge()} {person.GetStateOfCancer()}");
  267.         }
  268.     }
  269. }
  270.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement