simeonniko

Untitled

Mar 25th, 2022
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Triangle : IComparable<Triangle>
  5. {
  6. public Triangle(double side, double h)
  7. {
  8. this.SideA = side;
  9. this.H = h;
  10. this.Surface = this.FindSurfaceBySideAndH();
  11. }
  12.  
  13. public Triangle(double sideA, double sideB, double sideC)
  14. {
  15. this.SideA = sideA;
  16. this.SideB = sideB;
  17. this.SideC = sideC;
  18. this.Surface = this.FindSurfaceByThreeSides();
  19. }
  20.  
  21. public double SideA { get; protected set; }
  22. public double SideB { get; protected set; }
  23. public double SideC { get; protected set; }
  24. public double H { get; protected set; }
  25. public double Surface { get; protected set; }
  26.  
  27. private double FindSurfaceBySideAndH()
  28. {
  29. return (SideA * H) / 2;
  30. }
  31.  
  32. protected double FindSurfaceByThreeSides()
  33. {
  34. double p = (SideA + SideB + SideC) / 2;
  35.  
  36. return Math.Sqrt(p * (p - SideA) * (p - SideB) * (p - SideC));
  37. }
  38.  
  39. public int CompareTo(Triangle other)
  40. {
  41. return other.Surface.CompareTo(Surface);
  42. }
  43.  
  44. public override string ToString()
  45. {
  46. if (SideA > 0 && SideB > 0 && SideC > 0)
  47. {
  48. return String.Format("a = {0}, b = {1}, c = {2}, S = {3}",
  49. SideA, SideB, SideC, Surface);
  50. }
  51.  
  52. return String.Format("a = {0}, h = {1}, S = {2}",
  53. SideA, H, Surface);
  54. }
  55. }
  56.  
  57. class Pyramid : Triangle
  58. {
  59. public Pyramid(double sideA, double sideB, double sideC, double h)
  60. : base(sideA, sideB, sideC)
  61. {
  62. this.FindSurfaceByThreeSides();
  63. this.H = h;
  64.  
  65. this.Volume = this.FindVolume();
  66. }
  67.  
  68. public double Volume { get; private set; }
  69.  
  70. private double FindVolume()
  71. {
  72. return this.Surface * H / 3;
  73. }
  74.  
  75. public int CompareTo(Pyramid other)
  76. {
  77. return other.Volume.CompareTo(this.Volume);
  78. }
  79.  
  80. public override string ToString()
  81. {
  82. return String.Format("a = {0}, b = {1}, c = {2}, h = {3}, V = {4}",
  83. SideA, SideB, SideC, H, this.Volume);
  84. }
  85. }
  86.  
  87. public class Program
  88. {
  89. public static void Main(string[] args)
  90. {
  91. // List<Triangle> list = new List<Triangle>();
  92. // list.Add(new Triangle(7, 3));
  93. // list.Add(new Triangle(3, 4, 5));
  94. // list.Add(new Triangle(8, 5));
  95. //
  96. // list.Sort();
  97. //
  98. // Console.WriteLine(String.Join("\n\n", list));
  99. //
  100. // Console.WriteLine();
  101.  
  102. List<Pyramid> listOfPyramids = new List<Pyramid>();
  103. listOfPyramids.Add(new Pyramid(2, 3, 5, 9));
  104. listOfPyramids.Add(new Pyramid(3, 4, 5, 8));
  105. listOfPyramids.Add(new Pyramid(1, 3, 5, 10));
  106.  
  107. listOfPyramids.Sort();
  108.  
  109. Console.WriteLine(String.Join("\n\n", listOfPyramids));
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment