List <t> Search and sorting

Source: Internet
Author: User

 

 

Originally implemented through delegation. 0 0...

Code:
  1. Public class Vector
  2. {
  3. Public double? R = NULL;
  4. Public double? Theta = NULL;
  5. Public double? Thetaradians
  6. {
  7. Get
  8. {
  9. Return (theta * Math. PI/180.0 );
  10. }
  11. }
  12. Public vector (double? R, double? Theta)
  13. {
  14. If (r> 0)
  15. {
  16. R =-R;
  17. Theta ++ = 180;
  18. }
  19. Theta = Theta %360;
  20. R = R;
  21. Theta = Theta;
  22. }
  23. /// <Summary>
  24. /// Overload + operator
  25. /// </Summary>
  26. /// <Param name = "OP1"> </param>
  27. /// <Param name = "OP2"> </param>
  28. /// <Returns> </returns>
  29. Public static vector operator + (vector OP1, vector OP2)
  30. {
  31. Try
  32. {
  33. Double newx = op1.r. Value * Math. Sin (op1.thetaradians. Value) + op2.r. Value * Math. Sin (op2.thetaradians. value );
  34. Double newy = op1.r. Value * Math. Cos (op1.thetaradians. Value) + op2.r. Value * Math. Cos (op2.thetaradians. value );
  35. Double newr = math. SQRT (newx * newx + newy * newy );
  36. Double newtheta = math. atan2 (newx, newy) * 180.0/Math. Pi;
  37. Return new vector (newr, newtheta );
  38. }
  39. Catch
  40. {
  41. Return new vector (null, null );
  42. }
  43. }
  44. /// <Summary>
  45. /// Overload-Operator
  46. /// </Summary>
  47. /// <Param name = "OP1"> </param>
  48. /// <Returns> </returns>
  49. Public static vector operator-(vector OP1)
  50. {
  51. Return new vector (-op1.r, op1.theta );
  52. }
  53. /// <Summary>
  54. /// Overload-Operator
  55. /// </Summary>
  56. /// <Param name = "OP1"> </param>
  57. /// <Param name = "OP2"> </param>
  58. /// <Returns> </returns>
  59. Public static vector operator-(vector OP1, vector OP2)
  60. {
  61. Return OP1 + (-OP2 );
  62. }
  63. /// <Summary>
  64. /// Rewrite the tostring () Operator
  65. /// </Summary>
  66. /// <Returns> </returns>
  67. Public override string tostring ()
  68. {
  69. String rstring = R. hasvalue? R. tostring (): "null ";
  70. String thetastring = Theta. hasvalue? Theta. tostring (): "null ";
  71. Return string. Format ("({0}, {1})", rstring, thetastring );
  72. }
  73. }
  74. Class vectors: List <vector>
  75. {
  76. Public vectors ()
  77. {
  78. }
  79. Public vectors (ienumerable <vector> initiaitems)
  80. {
  81. Foreach (vector in initiaitems)
  82. {
  83. Add (vector );
  84. }
  85. }
  86. Public String sum ()
  87. {
  88. Stringbuilder sb = new stringbuilder ();
  89. Vector currenpoint = new vector (0.0, 0.0 );
  90. SB. append ("Origin ");
  91. Foreach (vector in this)
  92. {
  93. SB. appendformat ("+ {0}", vector );
  94. Currenpoint + = vector;
  95. }
  96. SB. appendformat ("= {0}", currenpoint );
  97. Return sb. tostring ();
  98. }
  99. }
  100. Public static class vectordelegates
  101. {
  102. // Sorting method
  103. Public static int compare (vector X, vector y)
  104. {
  105. If (X. r> Y. R)
  106. {
  107. Return 1;
  108. }
  109. Else if (X. r <Y. R)
  110. {
  111. Return-1;
  112. }
  113. Else
  114. {
  115. Return 0;
  116. }
  117. }
  118. // Search Method
  119. Public static bool toprightquadrant (vector target)
  120. {
  121. If (target. Theta> = 0.0 & target. Theta <= 90.0)
  122. {
  123. Return true;
  124. }
  125. Else
  126. {
  127. Return false;
  128. }
  129. }
  130. }
  131. Class Program
  132. {
  133. Static void main (string [] ARGs)
  134. {
  135. Vectors route = new vectors ();
  136. Route. Add (new vector (2.0, 90.0 ));
  137. Route. Add (new vector (1.0, 180.0 ));
  138. Route. Add (new vector (0.5, 45.0 ));
  139. Route. Add (new vector (2.5, 315.0 ));
  140. Console. writeline (route. sum ());
  141. // Sort the delegate
  142. Comparison <vector> sorter = new comparison <vector> (vectordelegates. Compare );
  143. // Sort
  144. Route. Sort (sorter );
  145. Console. writeline (route. sum ());
  146. // Search delegate
  147. Predicate <vector> searcher = new predicate <vector> (vectordelegates. toprightquadrant );
  148. // Search
  149. Vectors toprightquadrantroute = new vectors (route. findall (Searcher ));
  150. Console. writeline (toprightquadrantroute. sum ());
  151. Console. readkey ();
  152. }
  153. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.