C#——《C#语言程序设计》实验报告——综合练习——委托、Lambda表达式、LINQ、接口

问题描述

1、使用委托、Lambda表达式、LINQ等知识完成以下内容:(本题35分)

(1)要求定义Employee类,带有Name属性(string类型),带有Birthday属性(DateTime类型),带有Salary属性(double类型);重载ToString()方法,将信息以适当格式输出。[8分]

(2)定义一个委托如下 :

delegate bool EmployeePredicate(Employee emp);

写一个静态方法FindEmployee,根据filter参数在数组中查找符合条件的Person实例,方法声明为:[6分]

static List<Employee> FindEmployee(Employee[] emps, EmployeePredicate t)

(3)写一个静态方法FilterBySalary,使之符合委托EmployeePredicate定义,当Salary超过7000时返回true。[3分]

(4)在主方法中添加代码,结合(2)和(3),将工资超过7000的员工找出来并打印。[3分]

(5)编写一个静态类MyExtensions,其中有一个静态方法Eighties,扩展DateTime类型的功能。如日期符合“80后”定义则返回true,否则返回false。其中年份数据可通过 DateTime的Year属性访问到。[5分]

private static bool IsEighties(DateTime dt)

(6)利用(5)中扩展方法写一个lambda表达式,调用(2)中方法,把80后员工找出来并打印。[5分]

(7)利用LINQ技术,把80后员工找出来,按工资从高到低排序,并打印。[5分]

主方法中已提供以下代码和数据:

    public static void Main(string[] argv)
    {
        Employee[] employees = new Employee[] {
          new Employee { Name = "Damon", Birthday = new DateTime(1988, 5, 1), Salary = 4000 },
          new Employee { Name = "Niki", Birthday = new DateTime(1995, 10, 4), Salary = 7500 },
          new Employee { Name= "Ayrton", Birthday = new DateTime(1982, 6, 23), Salary = 9200 },
          new Employee { Name= "Graham", Birthday = new DateTime(1994, 9, 15), Salary = 6800 }
        };
    }

2、Array类的静态Sort方法要求数组中的元素是可比较的。[16分]

1)扩展第1题中Employee类的功能,使之实现泛型IComparable<T>接口(其中定义了方法int CompareTo(T p)),按Salary进行比较。(第1题中已答出的代码不需要重复写。)[4分]

2)如果需要以其它方式对Employee对象进行排序,就需要自己创建一个类EmployeeComparer,实现泛型IComparer<T>接口(其中定义了方法int Compare(T a, T b)),它独立于要比较的类,因此需要两个参数进行比较。

写个一EmployeeComparer类,继承IComparer<T>,内部包含一个整型Sort属性。当Sort为0时按Salary进行比较,为1时按Name排序,为2时按DateTime排序。其中string类和DateTime类均已实现IComparable接口。[8分]

3)在主方法中实现按Salary和Name的两种排序。[4分]

解决方案

using System;
using System.Collections.Generic;
using System.Linq;

namespace Homework20
{

    /**
     1、使用委托、Lambda表达式、LINQ等知识完成以下内容:(本题35分)
    (1)要求定义Employee类,带有Name属性(string类型),带有Birthday属性(DateTime类型),带有Salary属性(double类型);重载ToString()方法,将信息以适当格式输出。[8分]
    (2)定义一个委托如下 :
    delegate bool EmployeePredicate(Employee emp);
    写一个静态方法FindEmployee,根据filter参数在数组中查找符合条件的Person实例,方法声明为:[6分]
         static List<Employee> FindEmployee(Employee[] emps, EmployeePredicate t)
    (3)写一个静态方法FilterBySalary,使之符合委托EmployeePredicate定义,当Salary超过7000时返回true。[3分]
    (4)在主方法中添加代码,结合(2)和(3),将工资超过7000的员工找出来并打印。[3分]
    (5)编写一个静态类MyExtensions,其中有一个静态方法Eighties,扩展DateTime类型的功能。如日期符合“80后”定义则返回true,否则返回false。其中年份数据可通过 DateTime的Year属性访问到。[5分]
         private static bool IsEighties(DateTime dt)
    (6)利用(5)中扩展方法写一个lambda表达式,调用(2)中方法,把80后员工找出来并打印。[5分]
    (7)利用LINQ技术,把80后员工找出来,按工资从高到低排序,并打印。[5分]

    主方法中已提供以下代码和数据:
    public static void Main(string[] argv)
    {
        Employee[] employees = new Employee[] {
          new Employee { Name = "Damon", Birthday = new DateTime(1988, 5, 1), Salary = 4000 },
          new Employee { Name = "Niki", Birthday = new DateTime(1995, 10, 4), Salary = 7500 },
          new Employee { Name= "Ayrton", Birthday = new DateTime(1982, 6, 23), Salary = 9200 },
          new Employee { Name= "Graham", Birthday = new DateTime(1994, 9, 15), Salary = 6800 }
        };
    }
    */
    /**
    2、Array类的静态Sort方法要求数组中的元素是可比较的。[16分]
    1)扩展第1题中Employee类的功能,使之实现泛型IComparable<T>接口(其中定义了方法int CompareTo(T p)),按Salary进行比较。(第1题中已答出的代码不需要重复写。)[4分]
    2)如果需要以其它方式对Employee对象进行排序,就需要自己创建一个类EmployeeComparer,实现泛型IComparer<T>接口(其中定义了方法int Compare(T a, T b)),它独立于要比较的类,因此需要两个参数进行比较。
    写个一EmployeeComparer类,继承IComparer<T>,内部包含一个整型Sort属性。当Sort为0时按Salary进行比较,为1时按Name排序,为2时按DateTime排序。其中string类和DateTime类均已实现IComparable接口。[8分]
    3)在主方法中实现按Salary和Name的两种排序。[4分]
    */
    class Employee : IComparable<Employee>
    {
        public string Name;
        public DateTime Birthday;
        public double Salary;
        public override string ToString()
        {
            return string.Format("{0}: Salary={1}, Birthday={2}", Name, Salary, Birthday);
        }
        public int CompareTo(Employee emp)
        {
            return (int)(Salary - emp.Salary);
        }
    }
    public static class MyExtensions
    {
        public static bool IsEighties(this DateTime d)
        {
            return d.Year > 1979 && d.Year < 1990;
        }
    }

    class EmployeeComparer : IComparer<Employee>
    {
        public int Sort { get; set; }
        public EmployeeComparer(int sort)
        {
            Sort = sort;
        }
        public int Compare(Employee e1, Employee e2)
        {
            switch (Sort)
            {
                case 0:
                    return (int)(e1.Salary - e1.Salary);
                case 1:
                    return e1.Name.CompareTo(e2.Name);
                default:
                    return e1.Birthday.CompareTo(e2.Birthday);
            }
        }
    }
    class Program
    {
        delegate bool EmployeePredicate(Employee emp);

        static List<Employee> FindEmployee(Employee[] emps, EmployeePredicate t) {

            List<Employee> results = new List<Employee>();
            foreach (Employee p in emps)
            {
                if (t(p))
                    results.Add(p);
            }
            return results;

        }
        private static bool FilterBySalary(Employee emp) {
            return emp.Salary > 7000;
        }
        public static void Main(string[] args)
        {
            Employee[] employees = new Employee[] {
              new Employee { Name = "Damon", Birthday = new DateTime(1988, 5, 1), Salary = 4000 },
              new Employee { Name = "Niki", Birthday = new DateTime(1995, 10, 4), Salary = 7500 },
              new Employee { Name= "Ayrton", Birthday = new DateTime(1982, 6, 23), Salary = 9200 },
              new Employee { Name= "Graham", Birthday = new DateTime(1994, 9, 15), Salary = 6800 }
            };
            List<Employee> high = FindEmployee(employees, FilterBySalary);
            foreach (Employee emp in high)
                Console.WriteLine(emp);
            List<Employee> eighties = FindEmployee(employees, p => p.Birthday.IsEighties());
            foreach (Employee emp in eighties)
                Console.WriteLine(emp);
            var eighties2 = from emp in employees
                            where emp.Birthday.IsEighties() == true
                            orderby emp.Salary descending
                            select emp;
            foreach (Employee emp in eighties2)
                Console.WriteLine(emp);
            Array.Sort(employees);
            foreach (Employee emp in employees)
                Console.WriteLine(emp);
            Array.Sort(employees,new EmployeeComparer(1));
            foreach (Employee emp in employees)
                Console.WriteLine(emp);
            Console.WriteLine("Hello World!");
        }
    }
}

参考答案

//1、
//(1)
    class Employee
    {
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
        public double Salary { get; set; }
        public override string ToString()
        {
            return string.Format("{0}: Salary={1}, Birthday={2}", Name, Salary, Birthday);
        }
    }
//(2)
        private static List<Employee> FindEmployee(Employee[] ps, EmployeePredicate t)
        {
            List<Employee> results = new List<Employee>();
            foreach (Employee p in ps)
            {
                if (t(p))
                    results.Add(p);
            }
            return results;
        }
//(3)
        private static bool FilterBySalary(Employee emp)
        {
            return emp.Salary >= 7000;
        }
//(4)
            List<Employee> high = FindEmployee(employees, FilterBySalary);
            foreach(Employee emp in high)
                Console.WriteLine(emp);
//(5)
    public static class MyExtensions
    {
        public static bool IsEighties(this DateTime d)
        {
            return d.Year > 1979 && d.Year < 1980;
        }
    }
//(6)
            List<Employee> eighties = FindEmployee(employees, p => p.Birthday.IsEighties());
            foreach(Employee emp in eighties)
                Console.WriteLine(emp);
//(7)
            var eighties2 = from emp in employees
                            where emp.Birthday.IsEighties() == true
                            orderby emp.Salary descending
                            select emp;
            foreach (Employee emp in eighties2)
                Console.WriteLine(emp);
//2、
    class Employee : IComparable<Employee>
    {
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
        public double Salary { get; set; }
        public override string ToString()
        {
            return string.Format("{0}: Salary={1}, Birthday={2}", Name, Salary, Birthday);
        }

        public int CompareTo(Employee emp)
        {
            return (int)(Salary - emp.Salary);
        }
    }

    class EmployeeComparer : IComparer<Employee>
    {
        public int Sort { get; set; }
        public EmployeeComparer(int sort)
        {
            Sort = sort;
        }
        public int Compare(Employee e1, Employee e2)
        {
            switch(Sort)
            {
                case 0:
                    return (int)(e1.Salary - e1.Salary);
                case 1:
                    return e1.Name.CompareTo(e2.Name);
                default:
                    return e1.Birthday.CompareTo(e2.Birthday);
            }
        }
    }

 

运行结果 

参考文章

https://www.cnblogs.com/skm-blog/p/4229487.html

https://www.cnblogs.com/forever-Ys/p/10315830.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章