Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9.  
  10. namespace HW
  11. {
  12. abstract class Shape : IComparable<Shape>
  13. {
  14. private string name;
  15. public string Name { get { return name; } }
  16. public Shape(string name)
  17. {
  18. this.name = name;
  19. }
  20. public abstract double Area();
  21. public abstract double Perimeter();
  22. public int CompareTo(Shape s)
  23. {
  24. Shape shape = s as Shape;
  25. if (shape != null)
  26. return this.Area().CompareTo(shape.Area());
  27. else
  28. throw new Exception("���������� �������� ��� �������");
  29. }
  30. }
  31.  
  32. class Circle : Shape
  33. {
  34. private double radius;
  35. public double Radius { get { return radius; } }
  36. public Circle(string name, double radius) : base(name)
  37. {
  38. this.radius = radius;
  39. }
  40. public override double Area()
  41. {
  42. return Math.PI * (this.radius * this.radius);
  43. }
  44. public override double Perimeter()
  45. {
  46. return this.radius * (2 * Math.PI);
  47. }
  48. }
  49. class Square : Shape
  50. {
  51. private double a;
  52. public double A { get { return a; } }
  53. public Square(string name, double a) : base(name)
  54. {
  55. this.a = a;
  56. }
  57. public override double Area()
  58. {
  59. return a * this.a;
  60. }
  61. public override double Perimeter()
  62. {
  63. return 4 * this.a;
  64. }
  65. }
  66.  
  67.  
  68. class Program
  69. {
  70.  
  71. static void Main()
  72. {
  73. List<Shape> shapes = new List<Shape>();
  74.  
  75. for (int i = 0; i < 4; i++)
  76. {
  77. Console.WriteLine("What shape should it be? \n" +
  78. "S - Square : C - Circle\n" +
  79. "If you want to stop entering shapes press e");
  80. string tempShape = Console.ReadLine();
  81. if (tempShape == "s" || tempShape == "S")
  82. {
  83. Console.Write("Enter length : ");
  84. double tempShapePar = Convert.ToDouble(Console.ReadLine());
  85. shapes.Add(new Square($"Square {i}", tempShapePar));
  86. }
  87. else if (tempShape == "c" || tempShape == "C")
  88. {
  89. Console.Write("Enter radius : ");
  90. double tempShapePar = Convert.ToDouble(Console.ReadLine());
  91. shapes.Add(new Circle($"Circle {i}", tempShapePar));
  92. }
  93. else if (tempShape == "e")
  94. {
  95. break;
  96.  
  97. }
  98. else
  99. {
  100. i--;
  101. Console.WriteLine("You have entered wrong char. Try again");
  102.  
  103. }
  104. }
  105. Console.WriteLine("Do you want to find largest perimiter?\n" +
  106. "Y - yes : N - no");
  107. string TempPer = Console.ReadLine(); ;
  108. bool ans = false;
  109. while (!ans)
  110. {
  111. if ((TempPer == "y" || TempPer == "Y") && shapes.Count > 0)
  112. {
  113. ans = true;
  114. double tempPerimeter = 0;
  115. int tempCounterTrue = 0;
  116. int tempCounter = 0;
  117. foreach (var i in shapes)
  118. {
  119. if (tempPerimeter < i.Perimeter())
  120. {
  121. tempPerimeter = i.Perimeter();
  122. tempCounterTrue = tempCounter;
  123. }
  124. tempCounter++;
  125. }
  126. Console.WriteLine(shapes[tempCounterTrue].Name + " has the biggest perimeter");
  127. }
  128. else if ((TempPer == "y" || TempPer == "Y") && shapes.Count <= 0)
  129. {
  130. Console.WriteLine("Not enough items in list");
  131. ans = true;
  132. }
  133. else if (TempPer == "n" || TempPer == "N")
  134. {
  135. ans = true;
  136. }
  137. else
  138. {
  139. Console.WriteLine("Wrong answer. Try again.");
  140. }
  141. }
  142.  
  143. Console.WriteLine("Do you want to sort list and print it?\n" +
  144. "Y - yes : N - no");
  145. bool istempSort = false;
  146. string SortAns = Console.ReadLine();
  147. while (!istempSort)
  148. if ((SortAns == "y" || SortAns == "Y") && shapes.Count > 0)
  149. {
  150. istempSort = true;
  151. shapes.Sort();
  152. foreach (var i in shapes)
  153. {
  154. Console.WriteLine($"{i.Name}, Area = {i.Area()}, Perimeter = {i.Perimeter()}");
  155. }
  156. }
  157. else if ((SortAns == "y" || SortAns == "Y") && shapes.Count <= 0)
  158. {
  159. Console.WriteLine("Not enough items in list");
  160. istempSort = true;
  161. }
  162. else if (SortAns == "n" || SortAns == "N")
  163. {
  164. istempSort = true;
  165. Console.WriteLine("Not enough items in list");
  166. }
  167. else
  168. {
  169. Console.WriteLine("Wrong answer. Try again.");
  170. }
  171.  
  172. Console.ReadKey();
  173. }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement