• 多种方法求最大值(委托方法,重载)


      1 namespace ConsoleAppLearningCeshi
      2 {
      3     /// <summary>
      4     /// 不同打招呼
      5     /// </summary>
      6     /// <param name="name"></param>
      7     public delegate int deleMaxCompara<T>(T one, T two);//泛型委托
      8     public delegate int deleMaxCompara(object one, object two);
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13 
     14         }
     15       
     16         #region 泛型委托终极版
     17         //int[] intarray = { 1,33,11,34,54,2,45,55};
     18         //   string[] strarray = { "adfasdf","adfasdfasdfas","afsdfasdfasdfasddfas","fnvncvncvbn"};
     19         //   int intmax= MaxCompara<int>(intarray,comparaIntOrStr);
     20         //   string strmax = MaxCompara<string>(strarray, comparaIntOrStr);
     21         //   Console.WriteLine("最大int值"+intmax);
     22         //   Console.WriteLine("最长string值"+strmax);
     23         //   Console.ReadKey();
     24         public static T MaxCompara<T>(T[] obj, deleMaxCompara<T> delemaxcompara)
     25         {
     26             T maxobj = obj[0];//初始化初值
     27             for (int i = 0; i < obj.Length; i++)
     28             {
     29                 if ((int)(delemaxcompara(maxobj, obj[i])) < 0)
     30                 {
     31                     maxobj = obj[i];
     32                 }
     33             }
     34             return maxobj;
     35         }
     36         public static int comparaIntOrStr(int one, int two)
     37         {
     38             return one - two;
     39         }
     40         //委托指向string的函数
     41         public static int comparaIntOrStr(string one, string two)
     42         {
     43             return one.ToString().Length - two.ToString().Length;
     44         }
     45 
     46         #endregion
     47         #region 普通委托方法实现
     48         //object[] intarray = { 14,37,32,32,11};
     49         //  object[] strarray = { "ssssss","aaaaasdsd","adadadadadad","asdafsgsvsfgfdhfgkgkk"};
     50         //  int intmax= (int)MaxCompara(intarray, comparaOneInt);
     51         //  string strmax = MaxCompara(strarray, comparaOneStr).ToString();
     52         //  Console.WriteLine("int:"+intmax);
     53         //  Console.WriteLine("string.length:"+strmax);
     54         //  Console.ReadKey();
     55         public static object MaxCompara(object[] obj, deleMaxCompara delemaxcompara)
     56         {
     57             object maxobj = obj[0];//初始化初值
     58             for (int i = 0; i < obj.Length; i++)
     59             {
     60                 if ((int)(delemaxcompara(maxobj, obj[i])) < 0)
     61                 {
     62                     maxobj = obj[i];
     63                 }
     64             }
     65             return maxobj;
     66         }
     67         //委托指向int的函数
     68         public static int comparaOneInt(int one, int two)
     69         {
     70             return (one) - two;
     71         }
     72         public static int comparaOneInt(string one, string two)
     73         {
     74             return one.Length - two.Length;
     75         }
     76         //委托指向string的函数
     77         public static int comparaOneStr2(object one, object two)
     78         {
     79             return one.ToString().Length - two.ToString().Length;
     80         }
     81         #endregion
     82         #region 重载的方法
     83 
     84         //object str = "dfdsaf";
     85         //   int[] intarray = { 1,44,55,67,33,6,7};
     86         //   string[] strarray = { "ddfddfd", "dsfasfasfd", "sdfasdfsadfasfasf", "sdfasfasfasdfasfsafasfasdfas" };
     87         //   int maxint;
     88         //    bool maxinbool=int.TryParse( GetMax(intarray).ToString(),out maxint);
     89         //    string maxstr = GetMax(strarray).ToString();
     90         //    if (maxinbool==true)
     91         //    {
     92         //        Console.WriteLine(maxint);
     93 
     94         //    }
     95         //    Console.WriteLine(maxstr);
     96         //    Console.ReadKey();
     97         public static object GetMax(int[] intarray)
     98         {
     99             int intmax = intarray[0];//赋初值
    100             //循环遍历获得最大值
    101             for (int i = 0; i < intarray.Length; i++)
    102             {
    103                 if (intmax < intarray[i])
    104                 {
    105                     intmax = intarray[i];
    106                 }
    107 
    108             }
    109             return intmax;
    110 
    111         }
    112         public static object GetMax(string[] strarray)
    113         {
    114             string strmax = strarray[0];
    115             for (int i = 0; i < strarray.Length; i++)
    116             {
    117                 if (strmax.Length < strarray[i].Length)
    118                 {
    119                     strmax = strarray[i];
    120 
    121                 }
    122 
    123             }
    124             return strmax;
    125         }
    126         #endregion
    127         #region 普通方法实现方法
    128         //int[] intarray = { 1, 23, 4, 55, 57, 622 };
    129         //  string[] strarray = { "xiaobing", "dddddd", "sssssssssss", "ddddddddd", "sssssssssss", "eeeeeeeeee", "gggggggggggggggggggg" };
    130         //  int maxint = GetIntMax(intarray);
    131         //  string maxstr = GetStringMax(strarray);
    132         //  Console.WriteLine("最大值int:" + maxint);
    133         //  Console.WriteLine("最小值string:" + maxstr);
    134         //  Console.ReadKey();
    135         /// <summary>
    136         /// 获得int最大值
    137         /// </summary>
    138         /// <param name="intarray"></param>
    139         /// <returns></returns>
    140         public static object GetIntMax(int[] intarray)
    141         {
    142             int intmax = intarray[0];//赋初值
    143             //循环遍历获得最大值
    144             for (int i = 0; i < intarray.Length; i++)
    145             {
    146                 if (intmax < intarray[i])
    147                 {
    148                     intmax = intarray[i];
    149                 }
    150             }
    151             return intmax;
    152 
    153         }
    154         /// <summary>
    155         /// 获得字符串最大值
    156         /// </summary>
    157         /// <param name="strarray"></param>
    158         /// <returns></returns>
    159         public static object GetStringMax(string[] strarray)
    160         {
    161             string strmax = strarray[0];
    162             for (int i = 0; i < strarray.Length; i++)
    163             {
    164                 if (strmax.Length < strarray[i].Length)
    165                 {
    166                     strmax = strarray[i];
    167 
    168                 }
    169 
    170             }
    171             return strmax;
    172         }
    173         #endregion
    174     }
    175 
    176 
    177 }
  • 相关阅读:
    Linux Centos7之由Python2升级到Python3教程
    10张图带你深入理解Docker容器和镜像
    Centos7 添加用户及设置权限
    OpenCV 和 Dlib 人脸识别基础
    python unittest 源码学习
    Markdown 模板
    python Django ORM相关
    java的介绍
    java反射
    代理模式
  • 原文地址:https://www.cnblogs.com/xiaobing1/p/11263351.html
Copyright © 2020-2023  润新知