From 38e3c8b6fe78f6c604b397dd2d408e8b8db40cc6 Mon Sep 17 00:00:00 2001 From: flovnes Date: Tue, 30 Apr 2024 00:38:12 +0300 Subject: [PATCH 1/3] =?UTF-8?q?cut=20cut=20cut=20=F0=9F=94=AA=F0=9F=94=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/methods.cs | 81 +++++++++++++++----------------------------------- 1 file changed, 24 insertions(+), 57 deletions(-) diff --git a/src/methods.cs b/src/methods.cs index 575fce9..fbed360 100644 --- a/src/methods.cs +++ b/src/methods.cs @@ -1,68 +1,35 @@ -namespace struct_lab_student -{ - public partial class Program - { +namespace struct_lab_student { + public partial class Program { static void Var9(List students) { } - static void Var10(List students) - { - static double CountAverageScore(Student student) - { - char[] marks = student.Marks; - double sum = 0; - foreach (var c in marks) - { - sum += (c == '-') ? 2 : double.Parse(c.ToString()); - } - - return Math.Round(sum / 3, 1); - } - - static IEnumerable FilterTalentedPhysicians(List students) - { - return students.Where(student => student.Marks[1] == '5'); - } - - foreach (var student in FilterTalentedPhysicians(students)) - { - Console.WriteLine($"{student.surName} {student.firstName} {student.patronymic} {CountAverageScore(student):0.0} {student.scholarship}"); - } + static void Var10(List students) { + 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 = FilterSummerKids(students); - foreach (var (dude, avg) in summer_kids.Zip(EvalAverages(summer_kids))) - Console.WriteLine($"{dude.surName} {dude.firstName} {avg:0.0}"); + 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)}"); } - public static IEnumerable FilterSummerKids(List students) - { - 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; - }); + public static IEnumerable Filter(List students, Func Condition) { + return students.Where(Condition); } - public static List EvalAverages(IEnumerable? students) - { - 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; + private static bool IsSummer(Student student) { + int month = int.Parse(student.dateOfBirth[3..4]); + return month >= 6 && month <= 8; + } + + private static bool IsTalentedPhysician(Student student) { + 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); } } } From 408814285ff3b0ac5db7491c6279560db9ea663c Mon Sep 17 00:00:00 2001 From: flovnes Date: Tue, 30 Apr 2024 00:46:16 +0300 Subject: [PATCH 2/3] up --- src/methods.cs | 97 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 79 insertions(+), 18 deletions(-) diff --git a/src/methods.cs b/src/methods.cs index fbed360..4fde281 100644 --- a/src/methods.cs +++ b/src/methods.cs @@ -1,35 +1,96 @@ -namespace struct_lab_student { - public partial class Program { - static void Var9(List students) { } +namespace struct_lab_student +{ + public partial class Program + { + static void Var9(List students) + { + WriteData(ProcessData(students), "data_new.txt"); + Console.WriteLine("Дані були успішно записані у файл data_new.txt"); + } - static void Var10(List students) { - foreach (var student in Filter(students, IsTalentedPhysician)) + static void Var10(List students) + { + 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) { + 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)}"); } - public static IEnumerable Filter(List students, Func Condition) { + static List ProcessData(List students) + { + List processedStudents = []; + + foreach (var student in students) + { + char sex = student.sex; + if (sex == 'M' || sex == 'м' || sex == 'Ч') + sex = 'Ч'; + else if (sex == 'F' || sex == 'ж' || sex == 'Ж') + sex = 'Ж'; + + 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); + } + + return processedStudents; + } + + static void WriteData(List students, string filename) + { + try + { + 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}"); + } + } + } + catch (IOException e) + { + Console.WriteLine($"Помилка запису у файл: {e.Message}"); + } + } + + public static IEnumerable Filter(List students, Func Condition) + { return students.Where(Condition); } - private static bool IsSummer(Student student) { - int month = int.Parse(student.dateOfBirth[3..4]); - return month >= 6 && month <= 8; - } + private static bool IsSummer(Student student) + { + int month = int.Parse(student.dateOfBirth[3..4]); + return month >= 6 && month <= 8; + } - private static bool IsTalentedPhysician(Student student) { - return student.Marks[1] == '5'; - } + private static bool IsTalentedPhysician(Student student) + { + 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); + 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); } } } From fe272eb4fe0bf6afd7cf899397a70488dfb20c05 Mon Sep 17 00:00:00 2001 From: flovnes Date: Tue, 30 Apr 2024 01:46:41 +0300 Subject: [PATCH 3/3] minor fixes --- data_new.txt | 24 ++++++++++++++++++++++ gen.py | 10 +++++----- input.txt | 44 +++++++++++++++++++++------------------- src/main.cs | 1 - src/methods.cs | 54 ++++++++++++++++---------------------------------- 5 files changed, 70 insertions(+), 63 deletions(-) create mode 100644 data_new.txt diff --git a/data_new.txt b/data_new.txt new file mode 100644 index 0000000..c0fbad3 --- /dev/null +++ b/data_new.txt @@ -0,0 +1,24 @@ +Атаманенко Бодя Степанович Ж 02.01.1996 5 3 5 4319 +Атаманенко Саша Степанович F 23.06.1996 2 1 - 1285 +Атаманенко Софія Степанович Ж 26.11.2004 4 5 4 1630 +Конопленко Бодя Степанович Ж 04.01.1998 2 1 1 3078 +Атамась Софія Сергійович Ж 02.08.2000 3 2 2 2506 +Katleho Ömür Jensen F 07.05.2004 2 3 3 2103 +Narcisse Céleste Albani F 24.08.1999 4 5 3 4139 +Атаманенко Софія Степанович Ж 06.03.2001 4 1 - 1918 +Єрмоленко Софія Степанович Ж 21.03.1997 4 5 3 1365 +Єрмоленко Ніка Сергійович F 02.01.2004 - 1 3 3514 +Атамась Саша Сергійович F 01.11.1996 2 5 3 3451 +Конопленко Саша Георгійович F 11.08.2003 1 1 5 2374 +Конопленко Бодя Романович Ч 12.06.1998 4 4 5 3538 +Атамась Бодя Сергійович M 21.07.2001 2 1 3 2401 +Aytaç Nzinga Nevin F 12.11.1999 4 5 4 3392 +Атаманенко Ніка Сергійович F 04.02.2004 4 1 3 3593 +Єрмоленко Софія Степанович F 14.07.1996 4 5 4 2592 +Атаманенко Софія Сергійович Ч 01.02.1996 2 4 1 4157 +Claude Ange-Giusi Leoni F 02.03.2004 - 1 _ 1514 +Єрмоленко Ніка Георгійович F 19.01.1999 3 5 2 2346 +Атаманенко Софія Степанович M 28.01.2003 - 4 1 1473 +Атамась Саша Георгійович Ч 20.08.1998 - 5 1 2504 +Єрмоленко Саша Георгійович Ч 19.09.1995 1 4 2 1599 +Єрмоленко Ніка Степанович Ч 26.11.2001 1 - 4 2074 diff --git a/gen.py b/gen.py index 4faf935..188060e 100644 --- a/gen.py +++ b/gen.py @@ -1,9 +1,9 @@ import random -surnames = ['surname1', 'surname2', 'surname3', 'surname4'] -firstnames = ['firstname1', 'firstname2', 'firstname3', 'firstname4'] -patronymics = ['patronymic1', 'patronymic2', 'patronymic3', 'patronymic4'] -sexes = ['M', 'F'] +surnames = ['Атаманенко', 'Конопленко', 'Атамась', 'Єрмоленко'] +firstnames = ['Бодя', 'Саша', 'Софія', 'Ніка'] +patronymics = ['Сергійович', 'Романович', 'Степанович', 'Георгійович'] +sexes = ['M', 'F', 'Ж', 'Ч'] marks = [1, 2, 3, 4, 5, '-'] scholarships = list(range(1234, 4322)) @@ -22,6 +22,6 @@ line = f"{surname} {firstname} {patronymic} {sex} {date_of_birth} {mark1} {mark2} {mark3} {scholarship}" lines.append(line) -with open('input.txt', 'w') as file: +with open('input.txt', 'w', encoding='utf-8') as file: for line in lines: file.write(line + '\n') diff --git a/input.txt b/input.txt index 75cea48..a29138c 100644 --- a/input.txt +++ b/input.txt @@ -1,20 +1,24 @@ -surname2 firstname3 patronymic3 F 06.12.1995 - 3 5 4756 -surname4 firstname2 patronymic3 F 10.09.1995 2 - 1 3291 -surname2 firstname4 patronymic4 F 07.05.2004 2 3 3 2103 -surname1 firstname1 patronymic2 F 05.05.1998 1 - - 2766 -surname3 firstname2 patronymic3 M 03.02.2001 - 3 1 2260 -surname1 firstname1 patronymic2 F 12.04.2002 2 3 - 4495 -surname4 firstname1 patronymic3 F 04.09.2005 3 2 4 4183 -surname3 firstname1 patronymic2 F 08.07.2004 5 5 2 4345 -surname2 firstname2 patronymic1 M 22.01.1999 2 1 5 3870 -surname1 firstname1 patronymic1 M 04.05.2005 4 3 4 3302 -surname2 firstname3 patronymic3 F 16.12.1995 1 3 5 2801 -surname3 firstname2 patronymic3 F 24.08.1999 4 5 3 4139 -surname4 firstname3 patronymic4 M 26.07.1998 2 3 4 2548 -surname1 firstname3 patronymic1 M 23.06.1998 1 4 - 1092 -surname1 firstname3 patronymic2 F 15.10.2000 5 3 3 1577 -surname2 firstname2 patronymic2 M 08.12.2004 3 5 2 4394 -surname4 firstname1 patronymic3 M 05.08.1998 2 - 2 1404 -surname3 firstname4 patronymic3 F 15.12.1998 1 3 4 1133 -surname3 firstname3 patronymic2 M 06.09.2000 2 3 3 1118 -surname4 firstname2 patronymic3 F 22.12.2003 3 4 3 2677 +Атаманенко Бодя Степанович Ж 02.01.1996 5 3 5 4319 +Атаманенко Саша Степанович F 23.06.1996 2 1 - 1285 +Атаманенко Софія Степанович Ж 26.11.2004 4 5 4 1630 +Конопленко Бодя Степанович Ж 04.01.1998 2 1 1 3078 +Атамась Софія Сергійович Ж 02.08.2000 3 2 2 2506 +Katleho Ömür Jensen F 07.05.2004 2 3 3 2103 +Narcisse Céleste Albani F 24.08.1999 4 5 3 4139 +Атаманенко Софія Степанович Ж 06.03.2001 4 1 - 1918 +Єрмоленко Софія Степанович Ж 21.03.1997 4 5 3 1365 +Єрмоленко Ніка Сергійович F 02.01.2004 - 1 3 3514 +Атамась Саша Сергійович F 01.11.1996 2 5 3 3451 +Конопленко Саша Георгійович F 11.08.2003 1 1 5 2374 +Конопленко Бодя Романович Ч 12.06.1998 4 4 5 3538 +Атамась Бодя Сергійович M 21.07.2001 2 1 3 2401 +Aytaç Nzinga Nevin F 12.11.1999 4 5 4 3392 +Атаманенко Ніка Сергійович F 04.02.2004 4 1 3 3593 +Єрмоленко Софія Степанович F 14.07.1996 4 5 4 2592 +Атаманенко Софія Сергійович Ч 01.02.1996 2 4 1 4157 +Claude Ange-Giusi Leoni F 02.03.2004 - 1 _ 1514 +Єрмоленко Ніка Георгійович F 19.01.1999 3 5 2 2346 +Атаманенко Софія Степанович M 28.01.2003 - 4 1 1473 +Атамась Саша Георгійович Ч 20.08.1998 - 5 1 2504 +Єрмоленко Саша Георгійович Ч 19.09.1995 1 4 2 1599 +Єрмоленко Ніка Степанович Ч 26.11.2001 1 - 4 2074 diff --git a/src/main.cs b/src/main.cs index 63f6196..264bc0b 100644 --- a/src/main.cs +++ b/src/main.cs @@ -47,7 +47,6 @@ 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 4fde281..90b7a9b 100644 --- a/src/methods.cs +++ b/src/methods.cs @@ -4,21 +4,23 @@ public partial class Program { static void Var9(List students) { - WriteData(ProcessData(students), "data_new.txt"); - Console.WriteLine("Дані були успішно записані у файл data_new.txt"); + WriteData(ProcessData(students), "../data_new.txt"); + Console.WriteLine("\nДані збережено у data_new.txt"); } static void Var10(List students) { + Console.WriteLine("\nСписок студентів з відмінними оцінками з фізики:"); 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) { + Console.WriteLine("\nСписок студентів, які народились влітку:"); var summer_kids = Filter(students, IsSummer); foreach (var dude in summer_kids) - Console.WriteLine($"{dude.surName} {dude.firstName} {EvalAverage(dude)}"); + Console.WriteLine($"{dude.surName} {dude.firstName} {EvalAverage(dude):0.0}"); } static List ProcessData(List students) @@ -27,49 +29,27 @@ static List ProcessData(List students) foreach (var student in students) { - char sex = student.sex; - if (sex == 'M' || sex == 'м' || sex == 'Ч') - sex = 'Ч'; - else if (sex == 'F' || sex == 'ж' || sex == 'Ж') - sex = 'Ж'; - - 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); + var newStudent = student; + processedStudents.Add(newStudent); + newStudent.sex = (student.sex == 'M' || student.sex == 'Ч') ? 'Ч' : (student.sex == 'F' || student.sex == 'Ж') ? 'Ж' : '?'; } - + return processedStudents; } static void WriteData(List students, string filename) { - try - { - StreamWriter writer = new(filename); + var max_len = students.Max(s=>s.surName.Length); + using (StreamWriter writer = new(filename)) { + foreach (var student in students) { - 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}"); - } + writer.WriteLine($"{student.surName.PadRight(max_len+2)} {student.firstName.PadRight(max_len+2)} {student.patronymic.PadRight(max_len+2)} " + + $"{student.sex,-3} {student.dateOfBirth,-12} {student.Marks[0],-3} {student.Marks[1],-3} {student.Marks[2],-3} " + + $"{student.scholarship}"); } } - catch (IOException e) - { - Console.WriteLine($"Помилка запису у файл: {e.Message}"); - } } - + public static IEnumerable Filter(List students, Func Condition) { return students.Where(Condition); @@ -77,7 +57,7 @@ public static IEnumerable Filter(List students, Func= 6 && month <= 8; }