CH12Ex03解读

CH12Ex03解读

标签(空格分隔): C# 泛型


Program.cs

namespace Ch12Ex03
{
    class Program
    {
        static void Main(string[] args)
        {
            Vectors route = new Vectors();
            route.Add(new Vector(2.0, 90.0));
            route.Add(new Vector(1.0, 180.0));
            route.Add(new Vector(0.5, 45.0));
            route.Add(new Vector(2.5, 315.0));
            Console.WriteLine(route.Sum());

            //创建第一个委托sortor,这个委托属于Comparison<Vector>类型
            Comparison<Vector> sorter = new Comparison<Vector>(VectorDelegates.Compare);
            //Compare()方法就是赋予委托的方法
            route.Sort(sorter);//经过委托的方法进行排序
            //简化写法
            //route.Sort(VectorDelegates.Compare);
            //重新排序之后的结果等于第一个结果
            Console.WriteLine(route.Sum());

            //搜索,同样创建一个Predication<Vector>类型的委托
            Predicate<Vector> searcher =
                new Predicate<Vector>(VectorDelegates.TopRightQuadrant);
            //FindAll()返回一个List<Vector>实例
            //存在一个Vectors(IEnumerable<Vector> initialItems)构造函数,根据List<Vector>实例进行新的Vectors实例
            Vectors topRighQuadrantRouts = new Vectors(route.FindAll(searcher));
            Console.WriteLine(topRighQuadrantRouts.Sum());
            Console.ReadKey();
        }
    }
}

Vector.cs

 public class Vector
    {
        public double? R = null;
        public double? Theta = null;
        public double? ThetaRadians
        {
            //Couvert degree to radians
            get { return (Theta * Math.PI / 180.0); }
        }
        public Vector(double? r, double? theta)
        {
            //Normalize
            if (r < 0)
            {
                r = -r;
                theta += 180;
            }
            theta = theta % 360;
            //Assign fiellds
            R = r;
            Theta = theta;
        }
        public static Vector operator +(Vector op1, Vector op2)
        {
            try
            {
                double newX = op1.R.Value * Math.Sin(op1.ThetaRadians.Value)
                    + op2.R.Value * Math.Sin(op2.ThetaRadians.Value);
                double newY = op1.R.Value * Math.Cos(op1.ThetaRadians.Value)
                    + op2.R.Value * Math.Cos(op2.ThetaRadians.Value);
                double newR = Math.Sqrt(newX * newX + newY * newY);
                double newTheta = Math.Atan2(newX, newY) * 180 / Math.PI;
                return new Vector(newR, newTheta);
            }
            catch
            {
                return new Vector(null, null);
            }
        }
        public static Vector operator -(Vector op1) => new Vector(-op1.R, op1.Theta);
        public static Vector operator -(Vector op1, Vector op2) => op1 + (-op2);
        public override string ToString()
        {
            string rString = R.HasValue ? R.ToString() : "null";
            string thetaString = Theta.HasValue ? Theta.ToString() : "null";
            return string.Format($"({rString}, {thetaString})");
        }
    }

Vectors.cs

    class Vectors : List<Vector>
    {
        public Vectors()
        { }
        public Vectors(IEnumerable<Vector> initialItems)
        {
            foreach (Vector vector in initialItems)
            {
                Add(vector);
            }
        }
        public string Sum()
        {
            StringBuilder sb = new StringBuilder();
            Vector currentPoint = new Vector(0.0, 0.0);
            sb.Append("orgin");
            foreach(Vector vector in this)
            {
                sb.AppendFormat($" + {vector}");
                currentPoint += vector;
            }
            sb.AppendFormat($" = {currentPoint}");
            return sb.ToString();
        }
    }
    ```

    VectorDelegates.cs
    ```
    class VectorDelegates
    {
        public static int Compare(Vector x, Vector y)
        {
            if(x.R > y.R)
            {
                return 1;
            }
            else if(x.R < y.R)
            {
                return -1;
            }
            return 0;
        }
        public static bool TopRightQuadrant(Vector target)
        {
            if(target.Theta >= 0.0 && target.Theta <= 90.0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

VectorDelegates.cs

    class VectorDelegates
    {
        public static int Compare(Vector x, Vector y)
        {
            if(x.R > y.R)
            {
                return 1;
            }
            else if(x.R < y.R)
            {
                return -1;
            }
            return 0;
        }
        public static bool TopRightQuadrant(Vector target)
        {
            if(target.Theta >= 0.0 && target.Theta <= 90.0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_34332733/article/details/79392237