• 运算符重载 简单样例: 版本大小比较


    
    namespace Test
    {
        using System;
        using Microshaoft;
        public class Class1
        {
            static void Main(string[] args)
            {
                Console.WriteLine("第一个\".\"和第二个\".\"之间的数字按小数比较,其他按整数比较");
                StringVersion v1 = new StringVersion("12.0390.001");
                StringVersion v2 = new StringVersion("21.039000.1");
                Console.WriteLine("版本: [{0}][{1}] ", v1.ToString(), v2.ToString());
                Console.WriteLine("GetHashCode: [{0}][{1}] ", v1.GetHashCode(), v2.GetHashCode());
                Console.WriteLine("Equals , {0} ", v1.Equals(v2));
                Console.WriteLine("== , {0} ", v1 == v2);
                Console.WriteLine("!= , {0} ", v1 != v2);
                Console.WriteLine("> , {0} ", v1 > v2);
                Console.WriteLine("< , {0} ", v1 < v2);
                Console.WriteLine(">= , {0} ", v1 >= v2);
                Console.WriteLine("<= , {0} ", v1 <= v2);
                Console.WriteLine(Environment.Version.ToString());
            }
        }
    }
    namespace Microshaoft
    {
        using System;
        public class StringVersion
        {
            private string[] _versions;
            private string _version;
            protected string[] Versions
            {
                get
                {
                    return _versions;
                }
            }
            public StringVersion(string version)
            {
                _version = version;
                char[] c = new char[]{'.'};
                _versions = version.Split(c);
            }
            public override string ToString()
            {
                return _version;
            }
            public override bool Equals(object o)
            {
                StringVersion x = this;
                StringVersion y = o as StringVersion;
                int xl = x.Versions.Length;
                int yl = y.Versions.Length;
                for (int i = 0; i < xl || i < yl; i ++)
                {
                    uint xi = 0;
                    uint yi = 0;
                    double xd = 0.0d;
                    double yd = 0.0d;
                    if (i < xl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(x.Versions[i], out xi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", x.Versions[i])
                                            , out xd
                                        );
                        }
                    }
                    if (i < yl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(y.Versions[i], out yi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", y.Versions[i])
                                            , out yd
                                        );
                        }
                    }
                    if (i == 1)
                    {
                        if (xd != yd)
                        {
                            return false;
                        }
                    }
                    else
                    {
                        if (xi != yi)
                        {
                            return false;
                        }
                    }
                }
                return true;
            }
            public override int GetHashCode()
            {
                string[] a = new string[_versions.Length];
                Array.Copy(_versions, 0, a, 0, a.Length);
                for(int i = 0; i < _versions.Length; i ++)
                {
                    if (i == 1)
                    {
                        a[i] = _versions[i].TrimEnd('0');
                    }
                    else
                    {
                        a[i] = _versions[i].TrimStart('0');
                    }
                }
                string s = string.Join(".", a);
                return s.GetHashCode();
            }
            public static bool operator == (StringVersion x, StringVersion y)
            {
                return x.Equals(y);
            }
            public static bool operator != (StringVersion x, StringVersion y)
            {
                int xl = x.Versions.Length;
                int yl = y.Versions.Length;
                for (int i = 0; i < xl || i < yl; i ++)
                {
                    uint xi = 0;
                    uint yi = 0;
                    double xd = 0.0d;
                    double yd = 0.0d;
                    if (i < xl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(x.Versions[i], out xi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", x.Versions[i])
                                            , out xd
                                        );
                        }
                    }
                    if (i < yl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(y.Versions[i], out yi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", y.Versions[i])
                                            , out yd
                                        );
                        }
                    }
                    if (i == 1)
                    {
                        if (xd != yd)
                        {
                            return true;
                        }
                    }
                    else
                    {
                        if (xi != yi)
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
            public static bool operator > (StringVersion x, StringVersion y)
            {
                int xl = x.Versions.Length;
                int yl = y.Versions.Length;
                for (int i = 0; i < xl || i < yl; i ++)
                {
                    uint xi = 0;
                    uint yi = 0;
                    double xd = 0.0d;
                    double yd = 0.0d;
                    if (i < xl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(x.Versions[i], out xi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", x.Versions[i])
                                            , out xd
                                        );
                        }
                    }
                    if (i < yl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(y.Versions[i], out yi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", y.Versions[i])
                                            , out yd
                                        );
                        }
                    }
                    if (i == 1)
                    {
                        if (xd > yd)
                        {
                            return true;
                        }
                        else if (xd < yd)
                        {
                            return false;
                        }
                    }
                    else
                    {
                        if (xi > yi)
                        {
                            return true;
                        }
                        else if (xi < yi)
                        {
                            return false;
                        }
                    }
                }
                return false;
            }
            public static bool operator < (StringVersion x, StringVersion y)
            {
                int xl = x.Versions.Length;
                int yl = y.Versions.Length;
                for (int i = 0; i < xl || i < yl; i ++)
                {
                    uint xi = 0;
                    uint yi = 0;
                    double xd = 0.0d;
                    double yd = 0.0d;
                    if (i < xl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(x.Versions[i], out xi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", x.Versions[i])
                                            , out xd
                                        );
                        }
                    }
                    if (i < yl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(y.Versions[i], out yi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", y.Versions[i])
                                            , out yd
                                        );
                        }
                    }
                    if (i == 1)
                    {
                        if (xd < yd)
                        {
                            return true;
                        }
                        else if (xd > yd)
                        {
                            return false;
                        }
                    }
                    else
                    {
                        if (xi < yi)
                        {
                            return true;
                        }
                        else if (xi > yi)
                        {
                            return false;
                        }
                    }
                }
                return false;
            }
            public static bool operator >= (StringVersion x, StringVersion y)
            {
                int xl = x.Versions.Length;
                int yl = y.Versions.Length;
                bool equal = true;
                for (int i = 0; i < xl || i < yl; i ++)
                {
                    uint xi = 0;
                    uint yi = 0;
                    double xd = 0.0d;
                    double yd = 0.0d;
                    if (i < xl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(x.Versions[i], out xi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", x.Versions[i])
                                            , out xd
                                        );
                        }
                    }
                    if (i < yl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(y.Versions[i], out yi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", y.Versions[i])
                                            , out yd
                                        );
                        }
                    }
                    if (i == 1)
                    {
                        if (xd > yd)
                        {
                            return true;
                        }
                        else if (xd < yd)
                        {
                            return false;
                        }
                        equal = equal && (xd == yd);
                    }
                    else
                    {
                        if (xi > yi)
                        {
                            return true;
                        }
                        else if (xi < yi)
                        {
                            return false;
                        }
                        equal = equal && (xi == yi);
                    }
                }
                return equal;
            }
            public static bool operator <= (StringVersion x, StringVersion y)
            {
                int xl = x.Versions.Length;
                int yl = y.Versions.Length;
                bool equal = true;
                for (int i = 0; i < xl || i < yl; i ++)
                {
                    uint xi = 0;
                    uint yi = 0;
                    double xd = 0.0d;
                    double yd = 0.0d;
                    if (i < xl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(x.Versions[i], out xi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", x.Versions[i])
                                            , out xd
                                        );
                        }
                    }
                    if (i < yl)
                    {
                        if (i != 1)
                        {
                            uint.TryParse(y.Versions[i], out yi);
                        }
                        else
                        {
                            double.TryParse
                                        (
                                            string.Format("0.{0}", y.Versions[i])
                                            , out yd
                                        );
                        }
                    }
                    if (i == 1)
                    {
                        if (xd < yd)
                        {
                            return true;
                        }
                        else if (xd > yd)
                        {
                            return false;
                        }
                        equal = equal && (xd == yd);
                    }
                    else
                    {
                        if (xi < yi)
                        {
                            return true;
                        }
                        else if (xi > yi)
                        {
                            return false;
                        }
                        equal = equal && (xi == yi);
                    }
                }
                return equal;
            }
        }
    }
    
    
  • 相关阅读:
    C#与Java的几点区别
    用VB读取记事本的内容,控制计算器的按钮(VB6)
    通过实例说明Java中的多态
    [导入]析构函数(内存泄漏,三法则)
    [导入]const限制符
    [导入]类的一些特殊限制成员
    [导入]基类的复制控制函数
    [导入]容器适配器
    [导入]派生类到基类转换的可访问性
    [导入](复制、默认)构造函数
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/1539486.html
Copyright © 2020-2023  润新知