using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace learn_list_directory
{
    using static System.Math;
    internal 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(2.5, 315.0));
            route.Add(new Vector(0.5, 45.0));
            Console.WriteLine(route.Sum());
            // 创建一个委托,对矢量进行排序
            Comparison<Vector> sorter = new Comparison<Vector>(VectorDelegates.Compare);
            route.Sort(sorter);
            Console.WriteLine(route.Sum());
            // 创建一个委托, 搜索角度0<theta<90的元素
            Predicate<Vector> predicate = new Predicate<Vector>(VectorDelegates.TopRightQuadrant);
            Vectors topRightQuadrantRoute = new Vectors(route.FindAll(predicate));
            Console.WriteLine(topRightQuadrantRoute.Sum());

            Console.ReadKey();
        }
    }
    // 定义一个集合类,限定集合的类型为Vectors
    public class Vectors : List<Vector>
    {
        public Vectors()
        {

        }
        public Vectors(IEnumerable<Vector> initialItems)
        {
            foreach (Vector item in initialItems)
            {
                Add(item);
            }
        }
        // sum方法用来输出列表中的所有元素,每个元素都是一个有方向和长度的向量,所以可以看成时一个有端点的路径
        public string Sum()
        {
            // StringBuilder是一个方便的字符串处理方法,性能比串联各个字符串要高
            StringBuilder sb = new StringBuilder();
            Vector currectPoint = new Vector(0.0, 0.0);
            sb.Append("origin");
            foreach(Vector vector in this)
            {
                sb.AppendFormat($" + {vector}");
                currectPoint += vector;
            }
            sb.AppendFormat($" = {currectPoint}");
            return sb.ToString();
        }
    }
    // 定义两个用作委托的方法
    public static class VectorDelegates
    {
        // Compare用来比较(排序)
        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;
        }
        // TopRightQuadrant用来搜索
        public static bool TopRightQuadrant(Vector target)
        {
            if (target.Theta >= 0 && target.Theta <= 90.0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    // 一个处理极坐标系的类
    public class Vector
    {
        // double? 表示一个可空的double变量,下同
        public double? R = null;
        public double? Theta = null;
        // 表示把度数制转换为极坐标坐标
        public double? ThetaRadians
        {
            get { return (Theta * PI / 180.0); }
        }

        // 构造函数
        public Vector(double? r, double? theta)
        {
            // 表示如果给的长度是负值,那么把值变成正数,然后把度数加180度
            if (r < 0)
            {
                r = -r;
                theta += 180;
            }

            // 处理度数,大于360度时获取实际的度数
            theta = theta % 360;

            R = r;
            Theta = theta;
        }
        // 重写x+y方法
        public static Vector operator +(Vector op1, Vector op2)
        {
            try
            {
                double newX = op1.R.Value * Sin(op1.ThetaRadians.Value) + op2.R.Value * Sin(op2.ThetaRadians.Value);
                double newY = op1.R.Value * Cos(op1.ThetaRadians.Value) + op2.R.Value * Sin(op2.ThetaRadians.Value);
                double newR = Sqrt(newX * newX + newY * newY);
                double newTheta = Atan2(newX, newY) * 180.0 / PI;
                return new Vector(newR, newTheta);
            }
            catch
            {
                return new Vector(null, null);
            }
        }
        // 重写-x方法
        public static Vector operator -(Vector op1) => new Vector(-op1.R, op1.Theta);
        // 重写x-y方法
        public static Vector operator -(Vector op1, Vector op2) => op1 + (-op2);
        // 重写tostring方法
        public override string ToString()
        {
            string rString = R.HasValue ? R.ToString() : "null";
            string thetaString = Theta.HasValue ? Theta.ToString() : "null";
            return string.Format($"({rString}, {thetaString})");
        }

    }
}

执行结果

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace learn_list_directory
{
    using static System.Math;
    internal 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(2.5, 315.0));
            route.Add(new Vector(0.5, 45.0));
            Console.WriteLine(route.Sum());
            // 创建一个委托,对矢量进行排序
            Comparison<Vector> sorter = new Comparison<Vector>(VectorDelegates.Compare);
            route.Sort(sorter);
            Console.WriteLine(route.Sum());
            // 创建一个委托, 搜索角度0<theta<90的元素
            Predicate<Vector> predicate = new Predicate<Vector>(VectorDelegates.TopRightQuadrant);
            Vectors topRightQuadrantRoute = new Vectors(route.FindAll(predicate));
            Console.WriteLine(topRightQuadrantRoute.Sum());

            Console.ReadKey();
        }
    }
    // 定义一个集合类,限定集合的类型为Vectors
    public class Vectors : List<Vector>
    {
        public Vectors()
        {

        }
        public Vectors(IEnumerable<Vector> initialItems)
        {
            foreach (Vector item in initialItems)
            {
                Add(item);
            }
        }
        // sum方法用来输出列表中的所有元素,每个元素都是一个有方向和长度的向量,所以可以看成时一个有端点的路径
        public string Sum()
        {
            // StringBuilder是一个方便的字符串处理方法,性能比串联各个字符串要高
            StringBuilder sb = new StringBuilder();
            Vector currectPoint = new Vector(0.0, 0.0);
            sb.Append("origin");
            foreach(Vector vector in this)
            {
                sb.AppendFormat($" + {vector}");
                currectPoint += vector;
            }
            sb.AppendFormat($" = {currectPoint}");
            return sb.ToString();
        }
    }
    // 定义两个用作委托的方法
    public static class VectorDelegates
    {
        // Compare用来比较(排序)
        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;
        }
        // TopRightQuadrant用来搜索
        public static bool TopRightQuadrant(Vector target)
        {
            if (target.Theta >= 0 && target.Theta <= 90.0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    // 一个处理极坐标系的类
    public class Vector
    {
        // double? 表示一个可空的double变量,下同
        public double? R = null;
        public double? Theta = null;
        // 表示把度数制转换为极坐标坐标
        public double? ThetaRadians
        {
            get { return (Theta * PI / 180.0); }
        }

        // 构造函数
        public Vector(double? r, double? theta)
        {
            // 表示如果给的长度是负值,那么把值变成正数,然后把度数加180度
            if (r < 0)
            {
                r = -r;
                theta += 180;
            }

            // 处理度数,大于360度时获取实际的度数
            theta = theta % 360;

            R = r;
            Theta = theta;
        }
        // 重写x+y方法
        public static Vector operator +(Vector op1, Vector op2)
        {
            try
            {
                double newX = op1.R.Value * Sin(op1.ThetaRadians.Value) + op2.R.Value * Sin(op2.ThetaRadians.Value);
                double newY = op1.R.Value * Cos(op1.ThetaRadians.Value) + op2.R.Value * Sin(op2.ThetaRadians.Value);
                double newR = Sqrt(newX * newX + newY * newY);
                double newTheta = Atan2(newX, newY) * 180.0 / PI;
                return new Vector(newR, newTheta);
            }
            catch
            {
                return new Vector(null, null);
            }
        }
        // 重写-x方法
        public static Vector operator -(Vector op1) => new Vector(-op1.R, op1.Theta);
        // 重写x-y方法
        public static Vector operator -(Vector op1, Vector op2) => op1 + (-op2);
        // 重写tostring方法
        public override string ToString()
        {
            string rString = R.HasValue ? R.ToString() : "null";
            string thetaString = Theta.HasValue ? Theta.ToString() : "null";
            return string.Format($"({rString}, {thetaString})");
        }

    }
}

posted on 2023-01-22 23:31  盈盈的月儿  阅读(63)  评论(0编辑  收藏  举报