diff --git a/Recursion/K1/EL.csv b/Recursion/K1/EL.csv new file mode 100644 index 0000000..a2cf9d1 --- /dev/null +++ b/Recursion/K1/EL.csv @@ -0,0 +1,3 @@ +Elektronikos fakultetas, 10 +Charlie, Charleson, EL-1/1, 1, 3, 1, 1, 1, 5 +David, Davidson, EL-1/1, 1, 1, 1, 1, 2, 3, 1 diff --git a/Recursion/K1/IF.csv b/Recursion/K1/IF.csv new file mode 100644 index 0000000..d5b4adb --- /dev/null +++ b/Recursion/K1/IF.csv @@ -0,0 +1,5 @@ +Informatikos fakultetas, 20 +Puzonas, Rokas, IF-1/1, 10, 8, 6, 4 +Klaidas, Tadaravicius, IF-1/1, 8, 6, 4 +Bob, Bobby, IF-1/2, 10, 8, 6 +Alice, Alison, IF-1/1, 10 diff --git a/Recursion/K1/K1.csproj b/Recursion/K1/K1.csproj new file mode 100644 index 0000000..18561f6 --- /dev/null +++ b/Recursion/K1/K1.csproj @@ -0,0 +1,17 @@ + + + + Exe + netcoreapp3.1 + + + + + Always + + + Always + + + + diff --git a/Recursion/K1/Program.cs b/Recursion/K1/Program.cs new file mode 100644 index 0000000..70be27e --- /dev/null +++ b/Recursion/K1/Program.cs @@ -0,0 +1,218 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace K1 +{ + class Student + { + public string Name { get; set; } + public string Surname { get; set; } + public string Group { get; set; } + public List Credits { get; set; } + + public Student(string name, string surname, string group, List credits) + { + Name = name; + Surname = surname; + Group = group; + Credits = new List(credits); + } + + public int Sum(int ii) + { + return Credits.Count > ii ? Credits[ii] + Sum(ii+1) : 0; + } + + public static bool operator >(Student a, Student b) + { + int groupCompare = a.Group.CompareTo(b.Group); + if (groupCompare == 0) + { + return a.Surname.CompareTo(b.Surname) > 0; + } else + { + return groupCompare > 0; + } + } + public static bool operator <(Student a, Student b) + { + return !(a > b && a == b); + } + + public static bool operator ==(Student a, Student b) + { + return a.Group.Equals(b.Group) && a.Surname.Equals(b.Surname); + } + public static bool operator !=(Student a, Student b) + { + return !(a == b); + } + } + + class Faculty + { + public string Name { get; set; } + public int CreditLimit { get; set; } + public List Students { get; set; } + + public Faculty(string name, int creditLimit, List students) + { + Name = name; + CreditLimit = creditLimit; + Students = new List(students); + } + + public void Sort() + { + for (int i = 0; i < Students.Count-1; i++) + { + for (int j = i+1; j < Students.Count; j++) + { + if (Students[i] > Students[j]) + { + Student tmp = Students[i]; + Students[i] = Students[j]; + Students[j] = tmp; + } + } + } + } + + public static bool operator >(Faculty a, Faculty b) + { + return a.Students.Count > b.Students.Count; + } + public static bool operator <(Faculty a, Faculty b) + { + return a.Students.Count < b.Students.Count; + } + + public static bool operator ==(Faculty a, Faculty b) + { + return a.Students.Count == b.Students.Count; + } + public static bool operator !=(Faculty a, Faculty b) + { + return a.Students.Count != b.Students.Count; + } + } + + static class InOutUtils + { + public static Faculty ReadFaculty(string FileName) + { + using (StreamReader reader = new StreamReader(FileName)) + { + string[] firstLine = reader.ReadLine().Split(','); + string facultyName = firstLine[0].Trim(); + int creditLimit = int.Parse(firstLine[1].Trim()); + List students = new List(); + + string line; + while ((line = reader.ReadLine()) != null) + { + string[] parts = line.Split(','); + string surname = parts[0].Trim(); + string name = parts[1].Trim(); + string group = parts[2].Trim(); + List credists = new List(); + for (int i = 3; i < parts.Length; i++) + { + credists.Add(int.Parse(parts[i].Trim())); + } + students.Add(new Student(name, surname, group, credists)); + } + + return new Faculty(facultyName, creditLimit, students); + } + } + + public static void PrintFaculty(Faculty faculty, string fileName, string header) + { + using (StreamWriter writer = File.AppendText(fileName)) + { + writer.WriteLine(new string('-', 63)); + writer.WriteLine("| {0, -59} |", header); + writer.WriteLine(new string('-', 63)); + if (faculty.Students.Count == 0) + { + writer.WriteLine("| {0, -59} |", "Nėra"); + } else + { + writer.WriteLine("| {0, -15} | {1, -15} | {2, -8} | {3, 10} |", "Pavardė", "Vardas", "Grupė", "Kreditų suma"); + writer.WriteLine(new string('-', 63)); + foreach (var s in faculty.Students) + { + writer.WriteLine("| {0, -15} | {1, -15} | {2, -8} | {3, 12} |", s.Surname, s.Name, s.Group, s.Sum(0)); + } + } + writer.WriteLine(new string('-', 63)); + writer.WriteLine(); + } + } + } + + static class TaskUtils + { + public static Faculty Select(Faculty faculty) + { + List FilteredStudents = new List(); + foreach (var student in faculty.Students) + { + if (student.Sum(0) > faculty.CreditLimit) + { + FilteredStudents.Add(student); + } + } + return new Faculty(faculty.Name, faculty.CreditLimit, FilteredStudents); + } + } + + internal class Program + { + static void Main(string[] args) + { + string outputFilename = "Rezultatai.txt"; + var faculty1 = InOutUtils.ReadFaculty("IF.csv"); + var faculty2 = InOutUtils.ReadFaculty("EL.csv"); + + faculty1.Sort(); + faculty2.Sort(); + + var overLimitStudents1 = TaskUtils.Select(faculty1); + var overLimitStudents2 = TaskUtils.Select(faculty2); + + overLimitStudents1.Sort(); + overLimitStudents2.Sort(); + + if (File.Exists(outputFilename)) + { + File.Delete(outputFilename); + } + + InOutUtils.PrintFaculty(faculty1, outputFilename, faculty1.Name); + InOutUtils.PrintFaculty(faculty2, outputFilename, faculty2.Name); + + using (StreamWriter writer = File.AppendText(outputFilename)) + { + if (overLimitStudents1 > overLimitStudents2) + { + writer.WriteLine("'{0}' fakultetas turi daugiau viršijančių kreditų limitą studentų kiekį.", overLimitStudents1.Name); + } + else if (overLimitStudents1 < overLimitStudents2) + { + writer.WriteLine("'{0}' fakultetas turi daugiau viršijančių kreditų limitą studentų kiekį.", overLimitStudents2.Name); + } + else + { + writer.WriteLine("Abu fakultetai turi po lygiai viršijančių kreditų limitą studentų kiekį."); + } + writer.WriteLine(); + } + + InOutUtils.PrintFaculty(overLimitStudents1, outputFilename, String.Format("Studentai kurie viršija limitą iš '{0}'", faculty1.Name)); + InOutUtils.PrintFaculty(overLimitStudents2, outputFilename, String.Format("Studentai kurie viršija limitą iš '{0}'", faculty2.Name)); + } + } +} diff --git a/Recursion/Recursion.sln b/Recursion/Recursion.sln index b9146af..e251d10 100644 --- a/Recursion/Recursion.sln +++ b/Recursion/Recursion.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.1.32210.238 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LD_24", "LD_24\LD_24.csproj", "{4E9466EF-CDF4-4620-B51E-125F5A014A04}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "K1", "K1\K1.csproj", "{A0347E54-427A-47ED-9155-004D724ABED6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {4E9466EF-CDF4-4620-B51E-125F5A014A04}.Debug|Any CPU.Build.0 = Debug|Any CPU {4E9466EF-CDF4-4620-B51E-125F5A014A04}.Release|Any CPU.ActiveCfg = Release|Any CPU {4E9466EF-CDF4-4620-B51E-125F5A014A04}.Release|Any CPU.Build.0 = Release|Any CPU + {A0347E54-427A-47ED-9155-004D724ABED6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0347E54-427A-47ED-9155-004D724ABED6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0347E54-427A-47ED-9155-004D724ABED6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0347E54-427A-47ED-9155-004D724ABED6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE