public TestCollections(int n, GenerateElement <TKey, TValue> generator) { plist = new List <TKey>(); slist = new List <string>(); dperson = new Dictionary <TKey, TValue>(); dstring = new Dictionary <string, TValue>(); KeyValuePair <TKey, TValue> res; for (int i = 0; i <= n; i++) { res = generator(i); StringBuilder str_build = new StringBuilder(); Random random = new Random(); str_build.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(25 * random.NextDouble())) + 65)); Person randPerson = new Person(str_build.ToString(), str_build.ToString(), new DateTime()); Student randStudent = new Student(randPerson, Education.Bachelor, 1); StringBuilder str_build2 = new StringBuilder(); Random random2 = new Random(); str_build2.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(25 * random2.NextDouble())) + 65)); Person randPerson2 = new Person(str_build2.ToString(), str_build2.ToString(), new DateTime()); Student randStudent2 = new Student(randPerson2, Education.Bachelor, 1); plist.Add(res.Key); slist.Add(str_build.ToString()); dperson.Add(res.Key, res.Value); dstring.Add(str_build.ToString(), res.Value); } }
public TestCollection(int capacity, GenerateElement <TKey, TValue> func) { _TKeys = new List <TKey>(capacity); _Strings = new List <string>(capacity); _TKeysTValue = new Dictionary <TKey, TValue>(capacity); _GenElTKeyTValue = func; }
public TestCollections(int size, GenerateElement <TKey, TValue> generator) { KeyValuePair <TKey, TValue> current; for (int i = 0; i < size; i++) { current = generator(i); list.Add(current.Key); stringList.Add(current.Key.ToString()); dictionary.Add(current.Key, current.Value); stringDictionary.Add(current.Key.ToString(), current.Value); } }
public TestCollections(int num, GenerateElement <TKey, TValue> f) { m_f = f; m_num = num; for (int i = 0; i < num; ++i) { var el = f(i + 1); m_dict1.Add(el.Key, el.Value); m_dict2.Add("" + i, el.Value); m_keys.Add(el.Key); m_strings.Add("" + i); } }
public TestCollections(int count, GenerateElement <TKey, TValue> j) { if (count <= 0) { throw new ArgumentException(); } generateElement = j; for (int i = 0; i < count; i++) { var element = generateElement(i); dictionaryTKey.Add(element.Key, element.Value); dictionaryString.Add(element.Key.ToString(), element.Value); testTKeyList.Add(element.Key); testStringList.Add(element.Key.ToString()); } }
internal TestCollections(int count, GenerateElement <Edition, Magazine> GenerateKeyValuePair) { keys = new List <Edition>(count); strings = new List <string>(count); generateElement = GenerateKeyValuePair; keyDict = new Dictionary <Edition, Magazine>(count); stringDict = new Dictionary <string, Magazine>(count); KeyValuePair <Edition, Magazine> pair; for (int i = 0; i < count; i++) { keys.Add(new Edition(i.ToString(), DateTime.MinValue, 0)); strings.Add(i.ToString()); pair = generateElement(i); keyDict.Add(pair.Key, pair.Value); stringDict.Add(i.ToString(), pair.Value); } }
/* Constuctor to create collections with specified number of items */ public GenericTestCollections(int length, GenerateElement <TKey, TValue> kvp_generator) { list_of_keys = new List <TKey>(); list_of_strings = new List <string>(); key_value_dict = new Dictionary <TKey, TValue>(); value_dict = new Dictionary <string, TValue>(); this.generate_element_method = kvp_generator; for (int i = 0; i < length; i++) { /* Gnerating equal but different KVP objects */ KeyValuePair <TKey, TValue> kvp1 = generate_element_method(i); KeyValuePair <TKey, TValue> kvp2 = generate_element_method(i); list_of_keys.Add(kvp1.Key); list_of_strings.Add(kvp1.Key.ToString()); key_value_dict.Add(kvp2.Key, kvp2.Value); value_dict.Add(kvp1.Key.ToString(), kvp1.Value); } }
/// <summary> /// constructor /// </summary> /// <param name="n">count of objects in collection</param> /// <param name="generator">function-generator for elements in collection</param> public TestCollection(int n, GenerateElement <TKey, TValue> generator) { listTKey = new List <TKey>(); listString = new List <string>(); dictTKey = new Dictionary <TKey, TValue>(); dictString = new Dictionary <string, TValue>(); if (n <= 0 || n >= Int32.MaxValue) { throw new Exception("n in [0; Int32.MaxValue]"); } else { generateElement = generator; for (int i = 0; i < n; i++) { var obj = generateElement(i); listTKey.Add(obj.Key); listString.Add(obj.Key.ToString()); dictTKey.Add(obj.Key, obj.Value); dictString.Add(obj.Key.ToString(), obj.Value); } } }
static void Main(string[] args) { Person p1 = new Person("Fyodr", "Uncle", new DateTime(2000, 11, 4)); Person p2 = new Person(); Student s1 = new Student(p1, Education.Bachelor, 1); Exam cs = new Exam(); Exam math = new Exam("math", 4, new DateTime(2021 - 5 - 6)); Exam physics = new Exam("physics", 4, new DateTime(2021 - 5 - 7)); Exam[] exams = new Exam[3]; exams[0] = cs; exams[1] = math; exams[2] = physics; List <Exam> examsList = new List <Exam>(); examsList.AddRange(exams); s1.AddExams(examsList); Test t = new Test(); Test[] tests = new Test[1]; tests[0] = t; List <Test> testsList = new List <Test>(); testsList.AddRange(tests); s1.AddTests(testsList); s1.SortBySubject(); foreach (var e in s1.exams) { Console.WriteLine(e); } s1.SortByGrade(); foreach (var e in s1.exams) { Console.WriteLine(e); } s1.SortByDate(); foreach (var e in s1.exams) { Console.WriteLine(e); } Student s2 = new Student(); Exam ex1 = new Exam(); Exam[] exams2 = new Exam[1]; exams2[0] = ex1; List <Exam> examsList2 = new List <Exam>(); examsList2.AddRange(exams); s1.AddExams(examsList2); StudentCollection <Student> stcl = new StudentCollection <Student>(); Student[] st = new Student[2]; st[0] = s1; st[1] = s2; stcl.AddStudents(st); Console.WriteLine(stcl.ToString()); Console.WriteLine("max mean value: " + stcl.maxMean); Console.WriteLine("specialists: " + stcl.specialists.ToString()); var grouped = stcl.AverageMarkGroup(4); Console.WriteLine("grouped: ", grouped.ToString()); GenerateElement <Person, Student> generator = x => { Person p = new Person("Yellow", "White", new DateTime(2000, 11, 4)); Student s = new Student(p, Education.Bachelor, 1); return(new KeyValuePair <Person, Student>(p, s)); }; TestCollections <Person, Student> tc = new TestCollections <Person, Student>(3, generator); }
public static void Run() { //task 1 // Создать объект ResearchTeam и вызвать методы, выполняющие //сортировку списка публикаций List<Paper> по разным критериям, после //каждой сортировки вывести данные объекта. Выполнить сортировку // по дате выхода публикации; // по названию публикации; // по фамилии автора. ResearchTeam researchTeam1 = new ResearchTeam("R1", new Team("team1", 220020), TimeFrame.Long); Person person1 = new Person("N1", "S1", new DateTime(2000, 1, 1)); Person person2 = new Person("N2", "S2", new DateTime(2000, 2, 1)); Person person3 = new Person("N1", "S2", new DateTime(2000, 1, 1)); researchTeam1.AddPersons(person1, person2, person3); researchTeam1.AddPapers(new Paper("T1", person1, new DateTime(2020, 7, 20)), new Paper("T2", person3, new DateTime(2019, 7, 20))); researchTeam1.SortByDate(); Console.WriteLine(researchTeam1.ToString()); researchTeam1.SortByName(); Console.WriteLine(researchTeam1.ToString()); researchTeam1.SortByAuthorSnm(); Console.WriteLine(researchTeam1.ToString()); //task 2 //Создать объект ResearchTeamCollection<string>. Добавить в коллекцию //несколько разных элементов ResearchTeam и вывести объект //ResearchTeamCollection<string> KeySelector <string> keySelector = (rt) => rt.GetHashCode().ToString(); ResearchTeamCollection <string> researchTeamCollection = new ResearchTeamCollection <string>(keySelector); ResearchTeam researchTeam2 = new ResearchTeam("R2", new Team("team2", 220021), TimeFrame.Long); researchTeam2.AddPersons(person1, person3); researchTeam2.AddPapers(new Paper("T3", person3, new DateTime(2018, 7, 20))); researchTeamCollection.AddDefault(); researchTeamCollection.AddResearchTeams(researchTeam1, researchTeam2); Console.WriteLine(researchTeamCollection.ToString()); //task 3 // Вызвать методы класса ResearchTeamCollection<string>, выполняющие //операции с коллекцией - словарем Dictionary<TKey, ResearchTeam>, после //каждой операции вывести результат операции. Выполнить // поиск даты последней по времени выхода публикации среди всех //элементов коллекции; // вызвать метод TimeFrameGroup для выбора объектов ResearchTeam //с заданным значением продолжительности исследований; // вызвать свойство класса, выполняющее группировку элементов //коллекции по значениию продолжительности исследований; //вывести все группы элементов из списка. Console.WriteLine(researchTeamCollection.LastPublication.ToShortDateString()); foreach (var obj in researchTeamCollection.TimeFrameGroup(TimeFrame.Long)) { Console.WriteLine(obj.Key + "\n" + obj.Value.ToString() + "\n"); } foreach (var group in researchTeamCollection.GroupByTimeFrame) { Console.WriteLine(group.Key + "::"); foreach (var obj in group) { Console.WriteLine(obj.Key); Console.WriteLine(obj.Value); } Console.WriteLine(); } //task 4 //Создать объект типа TestCollection<Team, ResearchTeam>. Ввести число //элементов в коллекциях и вызвать метод для поиска первого, //центрального, последнего и элемента, не входящего в коллекции. //Вывести значения времени поиска для всех четырех случаев. GenerateElement <Team, ResearchTeam> generator = delegate(int j) { Team el_key = new Team(); el_key.Name += j; ResearchTeam el_value = new ResearchTeam(); return(new KeyValuePair <Team, ResearchTeam>(el_key, el_value)); }; string ch; while (true) { ch = Console.ReadLine(); try { var testColl = new TestCollection <Team, ResearchTeam>(Convert.ToInt32(ch), generator); testColl.SearchListTKey(); testColl.SearchListString(); testColl.SearchDictTkey(); testColl.SearchDictString(); testColl.SearchDictValue(); break; } catch (Exception e) { Console.WriteLine(e.Message + "\nTry again"); } } }