Advertisement
Levi0227

2. félév 1. óra (otthon)

Mar 4th, 2024
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.62 KB | Source Code | 0 0
  1. namespace ZooApp
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             //Cage[] cages = new Cage[5];
  8.            
  9.             //cages[0] = new Cage(5);
  10.             //cages[0].Add(new Animal("Kormos", true, 20, Species.Dog));
  11.             //cages[0].Add(new Animal("Mici", false, 320, Species.Panda));
  12.             //cages[0].Add(new Animal("Kajás", true, 40, Species.Dog));
  13.             //cages[0].Add(new Animal("Killer", true, 3, Species.Dog));
  14.             //cages[0].Add(new Animal("Kaller", true, 5, Species.Dog));
  15.            
  16.             //cages[1] = new Cage(3);
  17.             //cages[1].Add(new Animal("Nóri", false, 4, Species.Rabbit));
  18.             //cages[1].Add(new Animal("Marcsi", false, 320, Species.Panda));
  19.             //cages[1].Add(new Animal("Nándi", true, 4, Species.Rabbit));
  20.            
  21.             //cages[2] = new Cage(5);
  22.             //cages[2].Add(new Animal("Kolbász", true, 40, Species.Dog));
  23.             //cages[2].Add(new Animal("Kati", false, 13, Species.Dog));
  24.             //cages[2].Add(new Animal("Karesz", true, 15, Species.Dog));
  25.            
  26.             //cages[3] = new Cage(5);
  27.             //cages[3].Add(new Animal("Nindzsa", true, 2, Species.Rabbit));
  28.             //cages[3].Add(new Animal("Nyami", false, 12, Species.Rabbit));
  29.             //cages[3].Add(new Animal("Kicsi", false, 10, Species.Dog));
  30.             //cages[3].Add(new Animal("Norbi", true, 5, Species.Rabbit));
  31.             //cages[3].Add(new Animal("Krumpli", true, 10, Species.Dog));
  32.  
  33.             //Console.WriteLine("4.ben nyulak db: " + cages[3].Count(Species.Rabbit));
  34.             //Console.WriteLine("2.ban hím panda: " + (cages[1].DoesExist(Species.Panda, true) ? "van" : "nincs"));
  35.  
  36.             //Animal[] dogsInFirstCage = cages[0].CollectAll(Species.Dog);
  37.             //foreach (Animal currentAnimal in dogsInFirstCage)
  38.             //{
  39.             //    if (currentAnimal == null)
  40.             //        break;
  41.  
  42.             //    Console.WriteLine(currentAnimal.Name);
  43.             //}
  44.  
  45.             //Console.WriteLine("1.ben kutyák avg tömege: " + cages[0].AverageWeight(Species.Dog));
  46.  
  47.             //Console.WriteLine("4.ben azonos faj, különböző nem: " + (cages[3].DoesExistSameSpeciesOppositeSex() ? "van" : "nincs"));
  48.  
  49.             ////Cage mostDogsCage = MaxCageWith(cages, Species.Dog);
  50.             ////Console.WriteLine(mostDogsCage.ToString());
  51.  
  52.             //Animal newAnimal = Animal.Parse("Kormos,male,20,Dog");
  53.  
  54.  
  55.             //Cage newCage = Cage.CreateFrom("./files/cage1.txt");
  56.             //Console.WriteLine(newCage.ToString());
  57.  
  58.             Cage[] cages = LoadCages(args[0]);
  59.         }
  60.  
  61.         static Cage[] LoadCages(string path)
  62.         {
  63.             string[] fileNames = Directory.GetFiles(path, "*.txt");
  64.  
  65.             Cage[] result = new Cage[fileNames.Length];
  66.             for (int i = 0; i < fileNames.Length; i++)
  67.             {
  68.                 result[i] = Cage.CreateFrom(fileNames[i]);
  69.             }
  70.  
  71.             return result;
  72.         }
  73.  
  74.         static int Count(Animal[] animals)
  75.         {
  76.             int count = 0;
  77.  
  78.             foreach (Animal currentAnimal in animals)
  79.             {
  80.                 if (currentAnimal == null) break;
  81.  
  82.                 count++;
  83.             }
  84.  
  85.             return count;
  86.         }
  87.  
  88.         static Cage MaxCageWith(Cage[] cages, Species species)
  89.         {
  90.             if (cages.Length == 0) return null;
  91.  
  92.             int maxIndex = 0;
  93.             int maxValue = Count(cages[0].CollectAll(species));
  94.  
  95.             for (int i = 1; i < cages.Length; i++)
  96.             {
  97.                 int currentValue = Count(cages[i].CollectAll(species));
  98.                 if (currentValue > maxValue)
  99.                 {
  100.                     maxIndex = i;
  101.                     maxValue = currentValue;
  102.                 }
  103.             }
  104.  
  105.             return cages[maxIndex];
  106.         }
  107.     }
  108. }
  109.  
  110. ---------------------------------------
  111. Osztályok
  112. ---------------------------------------
  113.  
  114. using System;
  115. using System.Collections.Generic;
  116. using System.Linq;
  117. using System.Text;
  118. using System.Threading.Tasks;
  119.  
  120. namespace ZooApp
  121. {
  122.     enum Species
  123.     {
  124.         Dog,
  125.         Panda,
  126.         Rabbit
  127.     }
  128.  
  129.     internal class Animal
  130.     {
  131.         string name;
  132.         bool isMale;
  133.         int weight;
  134.         Species species;
  135.  
  136.         public string Name => name;
  137.         public bool IsMale => isMale;
  138.        
  139.         public int Weight => weight;
  140.         public Species Species => species;
  141.  
  142.         Animal() {}
  143.  
  144.         public Animal(string name, bool isMale, int weight, Species species)
  145.         {
  146.             this.name = name;
  147.             this.isMale = isMale;
  148.             this.weight = weight;
  149.             this.species = species;
  150.         }
  151.  
  152.         private string SpeciesToString()
  153.         {
  154.             switch (species)
  155.             {
  156.                 case Species.Dog: return "kutya";
  157.                 case Species.Panda: return "panda";
  158.                 case Species.Rabbit: return "nyúl";
  159.                 default: return string.Empty;
  160.             }
  161.         }
  162.         public override string ToString()
  163.         {
  164.             return $"{name} ({(isMale ? "hím" : "nőstény")}, {weight} kg, {SpeciesToString()})";
  165.         }
  166.  
  167.         public static Animal Parse(string input)
  168.         {
  169.             Animal result = new Animal();
  170.  
  171.             string[] items = input.Split(',');
  172.             result.name = items[0];
  173.             result.isMale = items[1] == "male";
  174.             result.weight = int.Parse(items[2]);
  175.             result.species = (Species)Enum.Parse(typeof(Species), items[3]);
  176.            
  177.             return result;
  178.         }
  179.     }
  180. }
  181.  
  182. ---------------------------------------
  183.  
  184. using System;
  185. using System.Collections.Generic;
  186. using System.Linq;
  187. using System.Text;
  188. using System.Threading.Tasks;
  189.  
  190. namespace ZooApp
  191. {
  192.     internal class Cage
  193.     {
  194.         static int  maxSize = 10;
  195.         Animal[] animals;
  196.         int count = 0;
  197.  
  198.         public Cage(int size = 10)
  199.         {
  200.             animals = new Animal[size <= 10 ? size : maxSize]; //ternary operator
  201.  
  202.             //if (size <= 10)
  203.             //{
  204.             //    animals = new Animal[size];
  205.             //}
  206.             //else
  207.             //{
  208.             //    animals = new Animal[maxSize];
  209.             //}
  210.         }
  211.  
  212.         public bool Add(Animal newAnimal)
  213.         {
  214.             if (count == animals.Length) return false; //early exit
  215.  
  216.             animals[count] = newAnimal;
  217.             count++;
  218.             return true;
  219.  
  220.             //if (count != animals.Length)
  221.             //{
  222.             //    animals[count] = newAnimal;
  223.             //    count++;
  224.             //    return true;
  225.             //}
  226.             //return false;
  227.         }
  228.  
  229.         private int Search(string name)
  230.         {
  231.             for (int i = 0; i < count; i++)
  232.             {
  233.                 if (animals[i].Name == name)
  234.                 {
  235.                     return i;
  236.                 }
  237.             }
  238.  
  239.             return animals.Length;
  240.         }
  241.  
  242.         public bool Remove(Animal animal)
  243.         {
  244.             int index = Search(animal.Name);
  245.  
  246.             if (index == animals.Length) return false;
  247.  
  248.             animals[index] = null;
  249.  
  250.             for (int i = index+1; i < count; i++)
  251.             {
  252.                 animals[i - 1] = animals[i];
  253.             }
  254.  
  255.             animals[count - 1] = null;
  256.             count--;
  257.  
  258.             return true;
  259.         }
  260.  
  261.         public int Count(Species species)
  262.         {
  263.             int speciesCount = 0;
  264.  
  265.             for (int i = 0; i < count; i++)
  266.             {
  267.                 if (animals[i].Species == species)
  268.                 {
  269.                     speciesCount++;
  270.                 }
  271.             }
  272.  
  273.             return speciesCount;
  274.         }
  275.  
  276.         public bool DoesExist(Species species, bool isMale)
  277.         {
  278.             for (int i = 0; i < count; i++)
  279.             {
  280.                 if (animals[i].Species == species && animals[i].IsMale == isMale)
  281.                 {
  282.                     return true;
  283.                 }
  284.             }
  285.             return false;
  286.         }
  287.  
  288.         public Animal[] CollectAll(Species species)
  289.         {
  290.             Animal[] result = new Animal[count];
  291.             int speciesCount = 0;
  292.  
  293.             for (int i = 0; i < count; i++)
  294.             {
  295.                 if (animals[i].Species == species)
  296.                 {
  297.                     result[speciesCount] = animals[i];
  298.                     speciesCount++;
  299.                 }
  300.             }
  301.  
  302.             return result;
  303.         }
  304.  
  305.         public double AverageWeight(Species species)
  306.         {
  307.             double sum = 0;
  308.             int speciesCount = 0;
  309.  
  310.             Animal[] animalsWithSpecifiedSpecies = CollectAll(species);
  311.  
  312.             foreach (Animal currentAnimal in animalsWithSpecifiedSpecies)
  313.             {
  314.                 if (currentAnimal == null) break;
  315.  
  316.                 sum += currentAnimal.Weight;
  317.                 speciesCount++;
  318.             }
  319.  
  320.             return speciesCount == 0 ? 0.0 : sum/speciesCount;
  321.         }
  322.  
  323.         public bool DoesExistSameSpeciesOppositeSex()
  324.         {
  325.             foreach (Species currentSpecies in Enum.GetValues(typeof(Species)))
  326.             {
  327.                 Animal[] sameSpeciesAnimals = CollectAll(currentSpecies);
  328.  
  329.                 bool isMale = false;
  330.                 bool isFemale = false;
  331.  
  332.                 foreach (Animal currentAnimal in sameSpeciesAnimals)
  333.                 {
  334.                     if (currentAnimal == null) break;
  335.  
  336.                     if (currentAnimal.IsMale) isMale = true;
  337.                     else isFemale = true;
  338.  
  339.                     if (isMale && isFemale) return true;
  340.                 }
  341.             }
  342.  
  343.             return false;
  344.         }
  345.  
  346.         public override string ToString()
  347.         {
  348.             StringBuilder stringBuilder = new StringBuilder();
  349.  
  350.             foreach (Animal currentAnimal in animals)
  351.             {
  352.                 if (currentAnimal == null) break;
  353.                
  354.                 stringBuilder.AppendLine(currentAnimal.ToString());
  355.             }
  356.  
  357.             return stringBuilder.ToString();
  358.         }
  359.  
  360.         public static Cage CreateFrom(string fileName)
  361.         {
  362.             if (!File.Exists(fileName)) return null;
  363.  
  364.             Cage result = new Cage();
  365.  
  366.             using (StreamReader sr = new StreamReader(fileName))
  367.             {
  368.                 while (!sr.EndOfStream)
  369.                 {
  370.                     result.Add(Animal.Parse(sr.ReadLine()));
  371.                 }
  372.             }
  373.  
  374.             return result;
  375.         }
  376.     }
  377. }
  378.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement