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

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