fork download
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. enum ShapeColor { 红色, 绿色, 蓝色, 青色, 品红, 黄色, 黑色 }
  8. interface IDrawable
  9. {
  10. void Draw();
  11. }
  12.  
  13. public class ShapeComparer : IComparer
  14. {
  15. int Type;
  16. public ShapeComparer(int Type)
  17. {
  18. this.Type = Type;
  19. }
  20. public int Compare(object a, object b)
  21. {
  22. if(Type==0) //根据x排序
  23. {
  24. if (((Shape)a).CenterX > ((Shape)b).CenterX)
  25. return 1;
  26. else if (((Shape)a).CenterX < ((Shape)b).CenterX)
  27. {
  28. return -1;
  29. }
  30. else
  31. return 0;
  32. }
  33. else if(Type==1) //根据y排序
  34. {
  35. if (((Shape)a).CenterY > ((Shape)b).CenterY)
  36. return 1;
  37. else if (((Shape)a).CenterY < ((Shape)b).CenterY)
  38. {
  39. return -1;
  40. }
  41. else
  42. return 0;
  43. }
  44. else if(Type==2) //根据面积
  45. {
  46. if (((Shape)a).Area > ((Shape)b).Area)
  47. return 1;
  48. else if (((Shape)a).Area < ((Shape)b).Area)
  49. {
  50. return -1;
  51. }
  52. else
  53. return 0;
  54. }
  55. else //根据周长
  56. {
  57. if (((Shape)a).Perimeter > ((Shape)b).Perimeter)
  58. return 1;
  59. else if (((Shape)a).Perimeter < ((Shape)b).Perimeter)
  60. {
  61. return -1;
  62. }
  63. else
  64. return 0;
  65. }
  66. }
  67. }
  68.  
  69.  
  70. abstract class Shape:IDrawable, IComparable
  71. {
  72. public double CenterX, CenterY, RadiusLong, RadiusShort;
  73. public int CompareTo(object o)
  74. {
  75. if (RadiusLong > ((Shape)o).RadiusLong)
  76. return 1;
  77. else if (RadiusLong == ((Shape)o).RadiusLong)
  78. return 0;
  79. else
  80. return -1;
  81. }
  82. public int Type;
  83. public void Draw()
  84. {
  85. if (Type == 0)
  86. System.Console.WriteLine($"用{LineColor}画圆形:圆心在({CenterX}, {CenterY}),半径为{RadiusLong}");
  87. else
  88. System.Console.WriteLine($"用{LineColor}画椭圆:圆心在({CenterX}, {CenterY}),长短半轴为({RadiusLong}, {RadiusShort})");
  89.  
  90. }
  91. public Shape(ShapeColor color, double CenterX, double CenterY, double RadiusLong, double RadiusShort, int Type)
  92. {
  93. LineColor = color;
  94. this.CenterX = CenterX;
  95. this.CenterY = CenterY;
  96. this.Type = Type;
  97. if (Type == 0)
  98. this.RadiusLong = this.RadiusShort = RadiusShort;
  99. else
  100. this.RadiusLong = Math.Max(RadiusShort, RadiusLong); this.RadiusShort = Math.Min(RadiusLong, RadiusShort);
  101. }
  102. public double Area
  103. {
  104. get { return Math.PI * RadiusLong * RadiusShort; }
  105. }
  106. public double Perimeter
  107. {
  108. get { return 2 * Math.PI * RadiusShort + 4 * (RadiusLong - RadiusShort); }
  109. }
  110. public ShapeColor LineColor { get; set; }
  111. }
  112.  
  113.  
  114. class Ellipse :Shape
  115. {
  116. public Ellipse(ShapeColor color, double x, double y, double RL, double RS, int t) : base(color, x, y, RL, RS, t) { }
  117. }
  118.  
  119. class Program
  120. {
  121. static void Main(string[] args)
  122. {
  123. Random rd = new Random();
  124. Shape[] arrs = new Shape[10];
  125. for(int i=0;i<10;i++)
  126. {
  127. arrs[i] = new Ellipse((ShapeColor)rd.Next(0, 7), rd.NextDouble() * 10, rd.NextDouble() * 10, rd.NextDouble() * 10, rd.NextDouble() * 10, rd.Next(0, 2));
  128. }
  129. Array.Sort(arrs);
  130. for(int i=0;i<10;i++)
  131. {
  132. arrs[i].Draw();
  133. }
  134. Array.Sort(arrs, new ShapeComparer(1));//按Y坐标进行排序
  135. System.Console.ReadKey();
  136. }
  137. }
  138.  
Success #stdin #stdout 0.01s 131520KB
stdin
Standard input is empty
stdout
用红色画椭圆:圆心在(6.12592910236024, 9.74948341015237),长短半轴为(2.61966875876285, 0.356117491776178)
用品红画椭圆:圆心在(8.12488428695355, 1.72670178195774),长短半轴为(4.4058599809212, 4.40212688613782)
用绿色画圆形:圆心在(9.58758796080369, 4.7557863848078),半径为4.64485788468498
用黑色画圆形:圆心在(9.71539222622076, 8.69980688611968),半径为5.14293387306059
用品红画椭圆:圆心在(2.94806713375639, 0.448584347240899),长短半轴为(5.54093919021121, 3.213630273572)
用蓝色画椭圆:圆心在(9.5345923814618, 9.00718993461094),长短半轴为(6.93360322012268, 0.722116395236047)
用黄色画圆形:圆心在(9.05100854069507, 5.87829523993577),半径为7.28133565153989
用蓝色画椭圆:圆心在(7.72991999878079, 9.75711855560407),长短半轴为(7.72606820693522, 6.50920364377517)
用绿色画椭圆:圆心在(1.49293672362945, 8.03865692021263),长短半轴为(7.97429923805143, 1.66086334346834)
用绿色画椭圆:圆心在(5.69395975009257, 3.2331349063819),长短半轴为(9.24725451471622, 2.10577183501132)