Advertisement
despores

Plant

Jan 20th, 2021
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace SeminarModule3
  5. {
  6.     class Plant
  7.     {
  8.         private double growth;
  9.         private double photosensitivity;
  10.         private double frostresistance;
  11.  
  12.         public double Growth { get { return growth; } }
  13.         public double Photosensitivity { get { return photosensitivity; } }
  14.         public double Frostresistance { get { return frostresistance; } }
  15.         public Plant(double growth, double photosensitivity, double frostresistance)
  16.         {
  17.             if(photosensitivity < 0 || photosensitivity > 100 ||
  18.                 frostresistance < 0 || frostresistance > 100)
  19.             {
  20.                 throw new ArgumentException("Параметры светочувствительности и морозостойкости должны быть в диапазоне от 0 до 100!");
  21.             }
  22.             this.growth = growth;
  23.             this.photosensitivity = photosensitivity;
  24.             this.frostresistance = frostresistance;
  25.         }
  26.         public override string ToString() => $"Рост: {growth:f3}, светочувствительность: {photosensitivity:f3}, морозостойкость: {frostresistance:f3}.";
  27.     }
  28.     class Program
  29.     {
  30.         static Random rand = new Random();
  31.         public static void Main()
  32.         {
  33.             int n = int.Parse(Console.ReadLine());
  34.             Plant[] plants = new Plant[n];
  35.             for (int i = 0; i < n; i++)
  36.             {
  37.                 double growth = rand.NextDouble() * 75 + 25;
  38.                 double photosensitivity = rand.NextDouble() * 100;
  39.                 double frostresistance = rand.NextDouble() * 80;
  40.                 plants[i] = new Plant(growth, photosensitivity, frostresistance);
  41.             }
  42.             Array.ForEach(plants, (x) => Console.WriteLine(x));
  43.             Array.Sort(plants, delegate (Plant a, Plant b)
  44.             {
  45.                 if (a.Growth < b.Growth) return 1;
  46.                 if (a.Growth == b.Growth) return 0;
  47.                 else return -1;
  48.             });
  49.             //Array.ForEach(plants, (x) => Console.WriteLine(x));
  50.             Array.Sort(plants, (Plant a, Plant b) => a.Frostresistance > b.Frostresistance ? 1 : (a.Frostresistance == b.Frostresistance ? 0 : -1));
  51.             //Array.ForEach(plants, (x) => Console.WriteLine(x));
  52.             Array.Sort(plants, new Comparison<Plant>(PlantsComp));
  53.             plants = Array.ConvertAll(plants, new Converter<Plant, Plant>(newPlant));
  54.             Array.ForEach(plants, (x) => Console.WriteLine(x));
  55.         }
  56.  
  57.         static Plant newPlant(Plant a)
  58.         {
  59.             if ((int)a.Frostresistance % 2 == 0) return new Plant(a.Growth, a.Photosensitivity, (a.Frostresistance) / 3.0);
  60.             return new Plant(a.Growth, a.Photosensitivity, (a.Frostresistance) / 2.0);
  61.         }
  62.  
  63.         static int PlantsComp(Plant a, Plant b)
  64.         {
  65.             if((int)a.Photosensitivity % 2 == 0 && (int)b.Photosensitivity % 2 != 0)
  66.             {
  67.                 return 1;
  68.             }
  69.             if((int)a.Photosensitivity % 2 == 0 && (int)b.Photosensitivity % 2 == 0)
  70.             {
  71.                 return 0;
  72.             }
  73.             return -1;
  74.         }
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement