用委托处理排序 讨论怎样用委托处理排序

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

用委托处理排序 讨论怎样用委托处理排序

  2021-03-18 我要评论
想了解讨论怎样用委托处理排序的相关内容吗,在本文为您仔细讲解用委托处理排序的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:委托,处理,排序,下面大家一起来学习吧。

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class 冒泡排序
    {
        //首先要了解冒泡排序,其实很简单就是索引前面的跟后面的比较,如果比后面的大2个值的位置就进行调换
        static void Main()
        {
            int[] str ={ 0, 14, 3, 6, 1, 30, 10, 9, 28 };
            for (int i = 0; i < str.Length; i++)
            {
                for (int j = i + 1; j < str.Length; j++)
                {
                    if (str[j] < str[i])
                    {
                        int index = str[i];
                        str[i] = str[j];
                        str[j] = index;
                    }
                }
            }
            for (int m = 0; m < str.Length; m++)
            {
                Console.WriteLine(str[m]);
            }
        }
    }
}


复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    public delegate bool DelegateTest(object obj1, object obj2);
    class Class1
    {
        static void Main()
        {
            Employee[] Employees =
              {
                  new Employee("huguo",1000000),
                  new Employee("lili",20000),
                  new Employee("lulu",30000),
                  new Employee("xixi",50000),
                  new Employee("jianjian",10000),
                  new Employee("yoyo",9000)
              };
            //委托DelegateTest代理的方法是Greate
            DelegateTest MyTest = new DelegateTest(Employee.Greate);
            Sorter MySort = new Sorter();
            //冒泡算法中第一个参数是对应Employees数组信息,第二个参数是委托
            MySort.Sort(Employees, MyTest);
            for (int m = 0; m < Employees.Length; m++)
            {
                Console.WriteLine(Employees[m].ToString());
            }
        }
    }
    class Employee
    {
        public string Name;
        public int Salary;
        public Employee(string Name, int Salary)
        {
            this.Name = Name;
            this.Salary = Salary;
        }
        //用override重写string方法
        public override string ToString()
        {
            return string.Format(Name + ",{0:C},", Salary);
        }
        //定义一个方法,如果obj2传过来的 Salary大于obj1就返回true;
        public static bool Greate(object obj1, object obj2)
        {
            Employee Employee1 = (Employee)obj1;
            Employee Employee2 = (Employee)obj2;
            return (Employee2.Salary > Employee1.Salary) ? true : false;
        }
    }
    class Sorter
    {
        public void Sort(object[] ArrayObj, DelegateTest Test)
        {
            //下面就是冒泡算法啦
            for (int i = 0; i < ArrayObj.Length; i++)
            {
                for (int j = i + 1; j < ArrayObj.Length; j++)
                {
                    if (Test(ArrayObj[j], ArrayObj[i]))
                    {
                        object Temp = ArrayObj[i];
                        ArrayObj[i] = ArrayObj[j];
                        ArrayObj[j] = Temp;
                    }
                }
            }
        }
    }

}


Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们