diff --git a/src/data_new.txt b/src/data_new.txt new file mode 100644 index 0000000..a09fcb9 --- /dev/null +++ b/src/data_new.txt @@ -0,0 +1,20 @@ +surname2 firstname3 patronymic3 Ж 06.12.1995 - 3 5 4756 +surname4 firstname2 patronymic3 Ж 10.09.1995 2 - 1 3291 +surname2 firstname4 patronymic4 Ж 07.05.2004 2 3 3 2103 +surname1 firstname1 patronymic2 Ж 05.05.1998 1 - - 2766 +surname3 firstname2 patronymic3 Ч 03.02.2001 - 3 1 2260 +surname1 firstname1 patronymic2 Ж 12.04.2002 2 3 - 4495 +surname4 firstname1 patronymic3 Ж 04.09.2005 3 2 4 4183 +surname3 firstname1 patronymic2 Ж 08.07.2004 5 5 2 4345 +surname2 firstname2 patronymic1 Ч 22.01.1999 2 1 5 3870 +surname1 firstname1 patronymic1 Ч 04.05.2005 4 3 4 3302 +surname2 firstname3 patronymic3 Ж 16.12.1995 1 3 5 2801 +surname3 firstname2 patronymic3 Ж 24.08.1999 4 5 3 4139 +surname4 firstname3 patronymic4 Ч 26.07.1998 2 3 4 2548 +surname1 firstname3 patronymic1 Ч 23.06.1998 1 4 - 1092 +surname1 firstname3 patronymic2 Ж 15.10.2000 5 3 3 1577 +surname2 firstname2 patronymic2 Ч 08.12.2004 3 5 2 4394 +surname4 firstname1 patronymic3 Ч 05.08.1998 2 - 2 1404 +surname3 firstname4 patronymic3 Ж 15.12.1998 1 3 4 1133 +surname3 firstname3 patronymic2 Ч 06.09.2000 2 3 3 1118 +surname4 firstname2 patronymic3 Ж 22.12.2003 3 4 3 2677 diff --git a/src/main.cs b/src/main.cs index 63f6196..802bb6d 100644 --- a/src/main.cs +++ b/src/main.cs @@ -1,4 +1,4 @@ -using System.Text; +using System.Text; namespace struct_lab_student { public partial class Program @@ -46,8 +46,7 @@ static void RunMenu(List students) } } while (match != 0); } - - + static void Main() { Console.OutputEncoding = UTF8Encoding.UTF8; diff --git a/src/methods.cs b/src/methods.cs index 575fce9..4fde281 100644 --- a/src/methods.cs +++ b/src/methods.cs @@ -2,67 +2,95 @@ namespace struct_lab_student { public partial class Program { - static void Var9(List students) { } + static void Var9(List students) + { + WriteData(ProcessData(students), "data_new.txt"); + Console.WriteLine("Дані були успішно записані у файл data_new.txt"); + } static void Var10(List students) { - static double CountAverageScore(Student student) + foreach (var student in Filter(students, IsTalentedPhysician)) + Console.WriteLine($"{student.surName} {student.firstName} {student.patronymic} {EvalAverage(student):0.0} {student.scholarship}"); + } + + static void Var24(List students) + { + var summer_kids = Filter(students, IsSummer); + foreach (var dude in summer_kids) + Console.WriteLine($"{dude.surName} {dude.firstName} {EvalAverage(dude)}"); + } + + static List ProcessData(List students) + { + List processedStudents = []; + + foreach (var student in students) { - char[] marks = student.Marks; - double sum = 0; - foreach (var c in marks) - { - sum += (c == '-') ? 2 : double.Parse(c.ToString()); - } + char sex = student.sex; + if (sex == 'M' || sex == 'м' || sex == 'Ч') + sex = 'Ч'; + else if (sex == 'F' || sex == 'ж' || sex == 'Ж') + sex = 'Ж'; - return Math.Round(sum / 3, 1); + Student processedStudent = new() + { + surName = student.surName, + firstName = student.firstName, + patronymic = student.patronymic, + sex = sex, + dateOfBirth = student.dateOfBirth, + Marks = student.Marks, + scholarship = student.scholarship + }; + + processedStudents.Add(processedStudent); } - static IEnumerable FilterTalentedPhysicians(List students) + return processedStudents; + } + + static void WriteData(List students, string filename) + { + try { - return students.Where(student => student.Marks[1] == '5'); + StreamWriter writer = new(filename); + { + foreach (var student in students) + { + writer.WriteLine($"{student.surName,-20} {student.firstName,-20} {student.patronymic,-20} " + + $"{student.sex,-5} {student.dateOfBirth,-12} {student.Marks[0],-5} {student.Marks[1],-5} {student.Marks[2],-5} " + + $"{student.scholarship}"); + } + } } - - foreach (var student in FilterTalentedPhysicians(students)) + catch (IOException e) { - Console.WriteLine($"{student.surName} {student.firstName} {student.patronymic} {CountAverageScore(student):0.0} {student.scholarship}"); + Console.WriteLine($"Помилка запису у файл: {e.Message}"); } } - static void Var24(List students) + public static IEnumerable Filter(List students, Func Condition) { - var summer_kids = FilterSummerKids(students); - foreach (var (dude, avg) in summer_kids.Zip(EvalAverages(summer_kids))) - Console.WriteLine($"{dude.surName} {dude.firstName} {avg:0.0}"); + return students.Where(Condition); } - public static IEnumerable FilterSummerKids(List students) + private static bool IsSummer(Student student) { - return students.Where(student => - { - if (DateTime.TryParseExact(student.dateOfBirth, "dd.MM.yyyy", null, System.Globalization.DateTimeStyles.None, - out DateTime date) - ) - { - return date.Month >= 6 && date.Month <= 8; - } - return false; - }); + int month = int.Parse(student.dateOfBirth[3..4]); + return month >= 6 && month <= 8; } - public static List EvalAverages(IEnumerable? students) + private static bool IsTalentedPhysician(Student student) { - List averages = []; - if (students != null) - { - foreach (var dude in students) - { - int sum = 0; - for (int i = 0; i < 3; i++) sum += (dude.Marks[i] == '-') ? 2 : dude.Marks[i] - '0'; - averages.Add(sum / 3.0); - } - } - return averages; + return student.Marks[1] == '5'; + } + + public static double EvalAverage(Student student) + { + double sum = 0; + foreach (var c in student.Marks) sum += (c == '-') ? 2 : double.Parse(c.ToString()); + return Math.Round(sum / 3, 1); } } }