Advertisement
double_trouble

predicate

Jun 9th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace WindowsFormsApplication18
  8. {
  9.     class Student
  10.     {
  11.         public String First { get; set; }
  12.         public String Last { get; set; }
  13.         public Int32 ID { get; set; }
  14.         int[] scores = new int[4];
  15.         public int[] Scores
  16.         {
  17.             get { return scores; }
  18.             set { scores = value; }
  19.         }
  20.         public override string ToString()
  21.         {
  22.             return First + " " + Last + " " + ID + " " + Scores.Average();
  23.         }
  24.     }
  25.     class Students<T>
  26.     {
  27.         public T[] GetList()
  28.         {
  29.             return M;
  30.         }
  31.         private T[] M;
  32.         public Students(IEnumerable<T> students)
  33.         {
  34.             M = (T[])students;
  35.         }
  36.         public IEnumerator<T> GetEnumerator()
  37.         {
  38.             //for (int i = 0; i < M.Length; i++)
  39.             //{
  40.             //  yield return M[i];
  41.             //}
  42.             return (M as IEnumerable<T>).GetEnumerator();
  43.         }
  44.         public Students<T> FindAll(System.Predicate<T> match)
  45.         {
  46.             int leng = 0;
  47.             foreach (T k in M)
  48.             {
  49.                 if (match(k)) leng++;
  50.             }
  51.             T[] a = new T[leng];
  52.             leng = 0;
  53.             foreach (T k in M)
  54.             {
  55.                 if (match(k)) a[leng++] = k;
  56.             }
  57.             return new Students<T>(a);
  58.         }
  59.     }
  60.     class Program
  61.     {
  62.         static void Main(string[] args)
  63.         {
  64.             Student a = new Student();
  65.             a.First = "Daniil";
  66.             a.Last = "Dranga";
  67.             a.ID = 3;
  68.             a.Scores = new int[4] { 3, 4, 5, 5 };
  69.             Student z = new Student();
  70.             z.First = "Ayuka";
  71.             z.Last = "Erdneev";
  72.             z.ID = 4;
  73.             z.Scores = new int[4] { 3, 4, 5, 5 };
  74.             Student[] b = new Student[2];
  75.             b[0] = a;
  76.             b[1] = z;
  77.             Students<Student> mass = new Students<Student>(b);
  78.             Students<Student> ans = mass.FindAll(x => x.ID > 3);
  79.             Student[] l = ans.GetList();
  80.             foreach(Student p in ans)
  81.             {
  82.                 Console.WriteLine(p.ToString());
  83.             }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement