From 32d34dcf9cd50dea8135cd3567fff2dff6981f38 Mon Sep 17 00:00:00 2001 From: Dmytro Kyba Date: Sun, 28 Apr 2024 06:01:12 +0300 Subject: [PATCH 1/2] done my block and added some minor comments --- src/main.cs | 31 ++++++++++++++++++++----------- src/methods.cs | 38 +++++++++++++++++++++++++++++++++----- src/student.cs | 23 +++++++++++++++-------- 3 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/main.cs b/src/main.cs index 34a50c0..4112aaa 100644 --- a/src/main.cs +++ b/src/main.cs @@ -1,9 +1,13 @@ using System.Text; -namespace struct_lab_student { - public partial class Program { - static List ReadData(string fileName) { +namespace struct_lab_student +{ + public partial class Program + { + static List ReadData(string fileName) + { List students = []; - try { + try + { StreamReader reader = new(fileName); string? line; while ((line = reader.ReadLine()) != null) @@ -13,13 +17,17 @@ static List ReadData(string fileName) { return students; } - static void RunMenu(List students) { + static void RunMenu(List students) + { int match; - do { + do + { Console.Write("\n<- Вихід [0]\nВиконати варіант 9 студента Попов Антон [1]\nВиконати варіант 10 студента Дмитро Киба [2]\nВиконати варіант 24 студента Волощук Влад [3]\nНомер > "); string? s = Console.ReadLine(); - if (int.TryParse(s, out match)) { - switch (match) { + if (int.TryParse(s, out match)) + { + switch (match) + { case 1: Var9(students); break; @@ -40,10 +48,11 @@ static void RunMenu(List students) { } - static void Main() { - Console.OutputEncoding = UTF8Encoding.UTF8; + static void Main() + { + Console.OutputEncoding = UTF8Encoding.UTF8; List students = ReadData("../input.txt"); RunMenu(students); } } -} \ No newline at end of file +} diff --git a/src/methods.cs b/src/methods.cs index b2f8ab8..176eb53 100644 --- a/src/methods.cs +++ b/src/methods.cs @@ -1,7 +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 void Var10(List students) + { + static double CountAverageScore(Student student) + { + char[] marks = student.Marks; + double sum = 0; + double n = marks.Length; + foreach (var c in marks) + { + if (c == '-') + { + sum += 2; + } + else { sum += double.Parse(c.ToString()); } + + } + + return sum / n; + } + foreach (var student in students) + { + if (student.Marks[1] == '5') + { + Console.WriteLine($"{student.surName} {student.firstName} {student.patronymic} {CountAverageScore(student)} {student.scholarship}"); + } + }; + } static void Var24(List students) { } - } -} \ No newline at end of file + } +} diff --git a/src/student.cs b/src/student.cs index 2c66353..acfc147 100644 --- a/src/student.cs +++ b/src/student.cs @@ -1,5 +1,7 @@ -namespace struct_lab_student { - public struct Student { +namespace struct_lab_student +{ + public struct Student + { public string surName; public string firstName; public string patronymic; @@ -9,22 +11,27 @@ public struct Student { public int scholarship; // debug - public override readonly string ToString() { + public override readonly string ToString() + { return surName + " " + firstName + " " + patronymic + " " + dateOfBirth; } - public Student(string line) { + public Student(string line) + { string[] fields = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); surName = fields[0]; firstName = fields[1]; patronymic = fields[2]; sex = char.Parse(fields[3]); dateOfBirth = fields[4]; - Marks = [ - char.Parse(fields[5]), + Marks = [ + //math + char.Parse(fields[5]), + //physics char.Parse(fields[6]), + // computer science char.Parse(fields[7]) - ]; + ]; scholarship = int.Parse(fields[8]); } } -} \ No newline at end of file +} From e790fb41beb9136caea6cd2bb29e8ae1932cba41 Mon Sep 17 00:00:00 2001 From: Dmytro Kyba Date: Mon, 29 Apr 2024 23:19:58 +0300 Subject: [PATCH 2/2] fixed the issues and rewrote some code kinda idk --- src/methods.cs | 51 ++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/methods.cs b/src/methods.cs index 020c7f0..575fce9 100644 --- a/src/methods.cs +++ b/src/methods.cs @@ -10,56 +10,59 @@ static double CountAverageScore(Student student) { char[] marks = student.Marks; double sum = 0; - double n = marks.Length; foreach (var c in marks) { - if (c == '-') - { - sum += 2; - } - else { sum += double.Parse(c.ToString()); } - + sum += (c == '-') ? 2 : double.Parse(c.ToString()); } - return sum / n; + return Math.Round(sum / 3, 1); } - foreach (var student in students) + + static IEnumerable FilterTalentedPhysicians(List students) { - if (student.Marks[1] == '5') - { - Console.WriteLine($"{student.surName} {student.firstName} {student.patronymic} {CountAverageScore(student)} {student.scholarship}"); - } - }; + 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 Var24(List students) { + + 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}"); } - public static IEnumerable FilterSummerKids(List students) { - return students.Where(student => { + 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 List EvalAverages(IEnumerable? students) { + public static List EvalAverages(IEnumerable? students) + { List averages = []; - if (students != null) { - foreach (var dude in students) { + 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; } } } -