Guest User

Untitled

a guest
May 27th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Help1
  6. {
  7. public class Region
  8. {
  9. /// <summary>
  10. /// Название области
  11. /// </summary>
  12. public string Name { get; set; }
  13.  
  14. /// <summary>
  15. /// Площадь
  16. /// </summary>
  17. public double Square { get; set; }
  18. /// <summary>
  19. /// Население
  20. /// </summary>
  21. public int Population { get; set; }
  22.  
  23. /// <summary>
  24. /// Метод для расчета плотности населения
  25. /// </summary>
  26. /// <returns>Плотность населения</returns>
  27. public double GetPopulationDensity()
  28. {
  29. return this.Square / this.Population;
  30. }
  31.  
  32. public Region(string name, double square, int population)
  33. {
  34. Name = name;
  35. Square = square;
  36. Population = population;
  37. }
  38.  
  39. public Region()
  40. {
  41.  
  42. }
  43.  
  44. /// <summary>
  45. /// метод выводит данные в удобном виде
  46. /// </summary>
  47. /// <returns></returns>
  48. public override string ToString()
  49. {
  50. return string.Format("Название: {0}t Площадь: {1:N2}t Кол-во населения: {2}t Плотность населения: {3:N2}", Name, Square, Population, this.GetPopulationDensity());
  51. }
  52. }
  53. class Program
  54. {
  55. static void Main(string[] args)
  56. {
  57. List<Region> list = new List<Region>()
  58. {
  59. new Region("Область 1", 100d, 588),
  60. new Region("Область 2", 200d, 288),
  61. new Region("Область 3", 300d, 338),
  62. };
  63.  
  64. //выывод списка областей
  65. foreach (var region in list)
  66. {
  67. Console.WriteLine(region.ToString());
  68. }
  69.  
  70. //выводим плотность первой
  71. Console.WriteLine("Плотность первой");
  72. Console.WriteLine(list[0].GetPopulationDensity().ToString());
  73.  
  74. //Ввести данные
  75. Region newRegion = new Region();
  76. newRegion.Name = "Область 4";
  77. newRegion.Square = 123d;
  78. newRegion.Population = 222;
  79. list.Add(newRegion);
  80.  
  81. //Максимум
  82. Console.WriteLine("Максимальная плотность");
  83. var max = list.Max(x => x.GetPopulationDensity());
  84. Console.WriteLine(max.ToString("N2"));
  85.  
  86. Console.ReadLine();
  87. }
  88. }
Add Comment
Please, Sign In to add comment