Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace SeminarModule3
- {
- class Plant
- {
- private double growth;
- private double photosensitivity;
- private double frostresistance;
- public double Growth { get { return growth; } }
- public double Photosensitivity { get { return photosensitivity; } }
- public double Frostresistance { get { return frostresistance; } }
- public Plant(double growth, double photosensitivity, double frostresistance)
- {
- if(photosensitivity < 0 || photosensitivity > 100 ||
- frostresistance < 0 || frostresistance > 100)
- {
- throw new ArgumentException("Параметры светочувствительности и морозостойкости должны быть в диапазоне от 0 до 100!");
- }
- this.growth = growth;
- this.photosensitivity = photosensitivity;
- this.frostresistance = frostresistance;
- }
- public override string ToString() => $"Рост: {growth:f3}, светочувствительность: {photosensitivity:f3}, морозостойкость: {frostresistance:f3}.";
- }
- class Program
- {
- static Random rand = new Random();
- public static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- Plant[] plants = new Plant[n];
- for (int i = 0; i < n; i++)
- {
- double growth = rand.NextDouble() * 75 + 25;
- double photosensitivity = rand.NextDouble() * 100;
- double frostresistance = rand.NextDouble() * 80;
- plants[i] = new Plant(growth, photosensitivity, frostresistance);
- }
- Array.ForEach(plants, (x) => Console.WriteLine(x));
- Array.Sort(plants, delegate (Plant a, Plant b)
- {
- if (a.Growth < b.Growth) return 1;
- if (a.Growth == b.Growth) return 0;
- else return -1;
- });
- //Array.ForEach(plants, (x) => Console.WriteLine(x));
- Array.Sort(plants, (Plant a, Plant b) => a.Frostresistance > b.Frostresistance ? 1 : (a.Frostresistance == b.Frostresistance ? 0 : -1));
- //Array.ForEach(plants, (x) => Console.WriteLine(x));
- Array.Sort(plants, new Comparison<Plant>(PlantsComp));
- plants = Array.ConvertAll(plants, new Converter<Plant, Plant>(newPlant));
- Array.ForEach(plants, (x) => Console.WriteLine(x));
- }
- static Plant newPlant(Plant a)
- {
- if ((int)a.Frostresistance % 2 == 0) return new Plant(a.Growth, a.Photosensitivity, (a.Frostresistance) / 3.0);
- return new Plant(a.Growth, a.Photosensitivity, (a.Frostresistance) / 2.0);
- }
- static int PlantsComp(Plant a, Plant b)
- {
- if((int)a.Photosensitivity % 2 == 0 && (int)b.Photosensitivity % 2 != 0)
- {
- return 1;
- }
- if((int)a.Photosensitivity % 2 == 0 && (int)b.Photosensitivity % 2 == 0)
- {
- return 0;
- }
- return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement