可空类型

 

今天在书上看到了段对于可空类型运用的代码 对于数学白痴的我 实在汗颜.....

被这段代码吸引不是对于可空类型的运用 而是里面有运算符重载 对 ToString() 的重写

觉得挺综合的 就打了下看看 唉 发现作为数学白痴的我 实在情何以堪.....

 

Code:
  1. class Vector   
  2.    {   
  3.        public double? R = null;   
  4.        public double? Theta = null;   
  5.   
  6.        public double? ThetaRadians   
  7.        {   
  8.            get  
  9.            {   
  10.                return (Theta * Math.PI / 180.0);   
  11.            }   
  12.        }   
  13.   
  14.        public Vector(double? r, double? theta)   
  15.        {   
  16.   
  17.            if (r > 0)   
  18.            {   
  19.                r = -r;   
  20.                theta += 180;   
  21.            }   
  22.            theta = theta % 360;   
  23.   
  24.            R = r;   
  25.            Theta = theta;   
  26.        }   
  27.   
  28.        /// <summary>   
  29.        /// 重载+运算符   
  30.        /// </summary>   
  31.        /// <param name="op1"></param>   
  32.        /// <param name="op2"></param>   
  33.        /// <returns></returns>   
  34.        public static Vector operator +(Vector op1, Vector op2)   
  35.        {   
  36.            try  
  37.            {   
  38.                double newX = op1.R.Value * Math.Sin(op1.ThetaRadians.Value) + op2.R.Value * Math.Sin(op2.ThetaRadians.Value);   
  39.                double newY = op1.R.Value * Math.Cos(op1.ThetaRadians.Value) + op2.R.Value * Math.Cos(op2.ThetaRadians.Value);   
  40.   
  41.                double newR = Math.Sqrt(newX * newX + newY * newY);   
  42.                double newTheta = Math.Atan2(newX, newY) * 180.0 / Math.PI;   
  43.   
  44.                return new Vector(newR, newTheta);   
  45.            }   
  46.            catch  
  47.            {   
  48.                return new Vector(null,null);   
  49.            }   
  50.        }   
  51.   
  52.        /// <summary>   
  53.        /// 重载-运算符   
  54.        /// </summary>   
  55.        /// <param name="op1"></param>   
  56.        /// <returns></returns>   
  57.        public static Vector operator -(Vector op1)   
  58.        {   
  59.            return new Vector(-op1.R, op1.Theta);   
  60.        }   
  61.   
  62.        /// <summary>   
  63.        /// 重载-运算符   
  64.        /// </summary>   
  65.        /// <param name="op1"></param>   
  66.        /// <param name="op2"></param>   
  67.        /// <returns></returns>   
  68.        public static Vector operator -(Vector op1, Vector op2)   
  69.        {   
  70.            return op1 + (-op2);   
  71.        }   
  72.   
  73.        /// <summary>   
  74.        /// 重写ToString()运算符   
  75.        /// </summary>   
  76.        /// <returns></returns>   
  77.        public override string ToString()   
  78.        {   
  79.            string rString = R.HasValue ? R.ToString() : "null";   
  80.            string thetaString = Theta.HasValue ? Theta.ToString() : "null";   
  81.   
  82.            return string.Format("({0},{1})", rString, thetaString);   
  83.        }   
  84.   
  85.    }   
  86.   
  87.    class Program   
  88.    {   
  89.        static void Main(string[] args)   
  90.        {   
  91.            Vector v1 = GetVector("vector1");   
  92.            Vector v2 = GetVector("vector2");   
  93.            Console.WriteLine("{0} + {1} = {2}", v1, v2, v1 + v2);   
  94.            Console.WriteLine("{0} + {1} = {2}", v1, v2, v1 - v2);   
  95.            Console.ReadKey();   
  96.        }   
  97.   
  98.        /// <summary>   
  99.        /// 输入值   
  100.        /// </summary>   
  101.        /// <param name="name"></param>   
  102.        /// <returns></returns>   
  103.        public static Vector GetVector(string name)   
  104.        {   
  105.            Console.WriteLine("输入{0}大小:", name);   
  106.            double? r = GetNullableDouble();   
  107.            Console.WriteLine("输入{0}角", name);   
  108.            double? theta = GetNullableDouble();   
  109.            return new Vector(r, theta);   
  110.        }   
  111.   
  112.        /// <summary>   
  113.        /// 取值 如果无法转换为double型 则给空值   
  114.        /// 这里我对它用异常处理的方式感到不是特别好. 想用Double.TryParse取代 发现result定义的是可空类型    
  115.        /// 有什么办法可以用我的思路实现么 可是要是在TryParse之前强转下 把它设为可空类型就完全没有意义了   
  116.        /// </summary>   
  117.        /// <returns></returns>   
  118.        public static double? GetNullableDouble()   
  119.        {   
  120.            double? result;   
  121.            string userInput = Console.ReadLine();   
  122.            try  
  123.            {   
  124.                result = double.Parse(userInput);   
  125.            }   
  126.            catch  
  127.            {   
  128.                result = null;   
  129.            }   
  130.            return result;   
  131.        }   
  132.    }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值