1
0

Complete practice exam 'Recursion.K1'

This commit is contained in:
Rokas Puzonas 2022-02-23 18:40:44 +02:00
parent cd29aa2299
commit 4ff290fe72
5 changed files with 249 additions and 0 deletions

3
Recursion/K1/EL.csv Normal file
View File

@ -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
1 Elektronikos fakultetas, 10
2 Charlie, Charleson, EL-1/1, 1, 3, 1, 1, 1, 5
3 David, Davidson, EL-1/1, 1, 1, 1, 1, 2, 3, 1

5
Recursion/K1/IF.csv Normal file
View File

@ -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
1 Informatikos fakultetas, 20
2 Puzonas, Rokas, IF-1/1, 10, 8, 6, 4
3 Klaidas, Tadaravicius, IF-1/1, 8, 6, 4
4 Bob, Bobby, IF-1/2, 10, 8, 6
5 Alice, Alison, IF-1/1, 10

17
Recursion/K1/K1.csproj Normal file
View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Update="EL.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="IF.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

218
Recursion/K1/Program.cs Normal file
View File

@ -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<int> Credits { get; set; }
public Student(string name, string surname, string group, List<int> credits)
{
Name = name;
Surname = surname;
Group = group;
Credits = new List<int>(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<Student> Students { get; set; }
public Faculty(string name, int creditLimit, List<Student> students)
{
Name = name;
CreditLimit = creditLimit;
Students = new List<Student>(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<Student> students = new List<Student>();
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<int> credists = new List<int>();
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<Student> FilteredStudents = new List<Student>();
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));
}
}
}

View File

@ -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