[关闭]
@sharif 2018-02-26T11:49:19.000000Z 字数 2802 阅读 871

CH12Ex03解读

C# 泛型


Program.cs

  1. namespace Ch12Ex03
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. Vectors route = new Vectors();
  8. route.Add(new Vector(2.0, 90.0));
  9. route.Add(new Vector(1.0, 180.0));
  10. route.Add(new Vector(0.5, 45.0));
  11. route.Add(new Vector(2.5, 315.0));
  12. Console.WriteLine(route.Sum());
  13. //创建第一个委托sortor,这个委托属于Comparison<Vector>类型
  14. Comparison<Vector> sorter = new Comparison<Vector>(VectorDelegates.Compare);
  15. //Compare()方法就是赋予委托的方法
  16. route.Sort(sorter);//经过委托的方法进行排序
  17. //简化写法
  18. //route.Sort(VectorDelegates.Compare);
  19. //重新排序之后的结果等于第一个结果
  20. Console.WriteLine(route.Sum());
  21. //搜索,同样创建一个Predication<Vector>类型的委托
  22. Predicate<Vector> searcher =
  23. new Predicate<Vector>(VectorDelegates.TopRightQuadrant);
  24. //FindAll()返回一个List<Vector>实例
  25. //存在一个Vectors(IEnumerable<Vector> initialItems)构造函数,根据List<Vector>实例进行新的Vectors实例
  26. Vectors topRighQuadrantRouts = new Vectors(route.FindAll(searcher));
  27. Console.WriteLine(topRighQuadrantRouts.Sum());
  28. Console.ReadKey();
  29. }
  30. }
  31. }

Vector.cs

  1. public class Vector
  2. {
  3. public double? R = null;
  4. public double? Theta = null;
  5. public double? ThetaRadians
  6. {
  7. //Couvert degree to radians
  8. get { return (Theta * Math.PI / 180.0); }
  9. }
  10. public Vector(double? r, double? theta)
  11. {
  12. //Normalize
  13. if (r < 0)
  14. {
  15. r = -r;
  16. theta += 180;
  17. }
  18. theta = theta % 360;
  19. //Assign fiellds
  20. R = r;
  21. Theta = theta;
  22. }
  23. public static Vector operator +(Vector op1, Vector op2)
  24. {
  25. try
  26. {
  27. double newX = op1.R.Value * Math.Sin(op1.ThetaRadians.Value)
  28. + op2.R.Value * Math.Sin(op2.ThetaRadians.Value);
  29. double newY = op1.R.Value * Math.Cos(op1.ThetaRadians.Value)
  30. + op2.R.Value * Math.Cos(op2.ThetaRadians.Value);
  31. double newR = Math.Sqrt(newX * newX + newY * newY);
  32. double newTheta = Math.Atan2(newX, newY) * 180 / Math.PI;
  33. return new Vector(newR, newTheta);
  34. }
  35. catch
  36. {
  37. return new Vector(null, null);
  38. }
  39. }
  40. public static Vector operator -(Vector op1) => new Vector(-op1.R, op1.Theta);
  41. public static Vector operator -(Vector op1, Vector op2) => op1 + (-op2);
  42. public override string ToString()
  43. {
  44. string rString = R.HasValue ? R.ToString() : "null";
  45. string thetaString = Theta.HasValue ? Theta.ToString() : "null";
  46. return string.Format($"({rString}, {thetaString})");
  47. }
  48. }

Vectors.cs

  1. class Vectors : List<Vector>
  2. {
  3. public Vectors()
  4. { }
  5. public Vectors(IEnumerable<Vector> initialItems)
  6. {
  7. foreach (Vector vector in initialItems)
  8. {
  9. Add(vector);
  10. }
  11. }
  12. public string Sum()
  13. {
  14. StringBuilder sb = new StringBuilder();
  15. Vector currentPoint = new Vector(0.0, 0.0);
  16. sb.Append("orgin");
  17. foreach(Vector vector in this)
  18. {
  19. sb.AppendFormat($" + {vector}");
  20. currentPoint += vector;
  21. }
  22. sb.AppendFormat($" = {currentPoint}");
  23. return sb.ToString();
  24. }
  25. }
  26. ```
  27. VectorDelegates.cs
  28. ```
  29. class VectorDelegates
  30. {
  31. public static int Compare(Vector x, Vector y)
  32. {
  33. if(x.R > y.R)
  34. {
  35. return 1;
  36. }
  37. else if(x.R < y.R)
  38. {
  39. return -1;
  40. }
  41. return 0;
  42. }
  43. public static bool TopRightQuadrant(Vector target)
  44. {
  45. if(target.Theta >= 0.0 && target.Theta <= 90.0)
  46. {
  47. return true;
  48. }
  49. else
  50. {
  51. return false;
  52. }
  53. }
  54. }

VectorDelegates.cs

  1. class VectorDelegates
  2. {
  3. public static int Compare(Vector x, Vector y)
  4. {
  5. if(x.R > y.R)
  6. {
  7. return 1;
  8. }
  9. else if(x.R < y.R)
  10. {
  11. return -1;
  12. }
  13. return 0;
  14. }
  15. public static bool TopRightQuadrant(Vector target)
  16. {
  17. if(target.Theta >= 0.0 && target.Theta <= 90.0)
  18. {
  19. return true;
  20. }
  21. else
  22. {
  23. return false;
  24. }
  25. }
  26. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注