Skip to content

Commit

Permalink
10
Browse files Browse the repository at this point in the history
  • Loading branch information
Зайка committed Dec 25, 2021
1 parent b739535 commit 35532a3
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 0 deletions.
84 changes: 84 additions & 0 deletions lab10/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Linq;
using lab10.consoler;

namespace lab10
{
internal static class Program
{
static void Main(string[] args)
{
Random rand = new();
School school = new();
var enumValues = Enum.GetValues<Worker.EPosition>();
Console.WriteLine("Генерируем людей");

Table table = new();
for (int i = 0; i < 10; i++)
{
school.Workers.Add(new Worker
{
Number = i,
CountLunch = rand.Next(1,100),
CountPhone = rand.Next(1,100),
Position = enumValues[rand.Next(enumValues.Length)]
});

Row row = new Row();
row.Cells.Add(new Cell()
{
TextAlign = Cell.TextAlignEnum.Center,
Text = "Человек " + i
});
table.Rows.Add(row);

row = new Row();
row.Cells.Add(new Cell
{
WidthMode = Cell.WidthModeEnum.Fixed,
FixedWidth = 20,
Text = "Кол-во обедов:"
});
row.Cells.Add(new Cell
{
Text = school.Workers.Last().CountLunch.ToString()
});
table.Rows.Add(row);

row = new Row();
row.Cells.Add(new Cell
{
WidthMode = Cell.WidthModeEnum.Fixed,
FixedWidth = 20,
Text = "Кол-во айфонов:"
});
row.Cells.Add(new Cell
{
Text = school.Workers.Last().CountPhone.ToString()
});
table.Rows.Add(row);

row = new Row();
row.Cells.Add(new Cell
{
WidthMode = Cell.WidthModeEnum.Fixed,
FixedWidth = 20,
Text = "Позиция:"
});
row.Cells.Add(new Cell
{
Text = school.Workers.Last().Position.ToString()
});
table.Rows.Add(row);
}

Consoler.PrintTable(table);

(Worker, Worker) workers = school.FindMinMaxEmployee();
School.Reward(ref workers);

Console.WriteLine($"Лучший работник (id: {workers.Item2.Number}) имеет позицию " + workers.Item2.Position + ". Он сделал телефонов: " + workers.Item2.CountPhone + ". Он получил обедов: " + workers.Item2.CountLunch);
Console.WriteLine($"Худший работник (id: {workers.Item1.Number}) имеет позицию " + workers.Item1.Position + ". Он сделал телефонов: " + workers.Item1.CountPhone + ". Он получил обедов: " + workers.Item1.CountLunch);
}
}
}
23 changes: 23 additions & 0 deletions lab10/School.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Linq;

namespace lab10
{
public class School
{
public List<Worker> Workers { get; } = new();

public (Worker, Worker) FindMinMaxEmployee()
{
Workers.Sort();

return (Workers.First(), Workers.Last());
}

public static void Reward(ref (Worker, Worker) students)
{
students.Item1.CountLunch--;
students.Item2.CountLunch++;
}
}
}
30 changes: 30 additions & 0 deletions lab10/Worker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace lab10
{
public struct Worker : IComparable<Worker>
{
public enum EPosition
{
PreSchool,
MiddleSchool,
HighSchool
}

public int Number { get; set; }
public int CountPhone { get; set; }
public int CountLunch { get; set; }
public EPosition Position { get; set; }


public int CompareTo(Worker worker)
{
if (CountPhone == worker.CountPhone)
{
return Position - worker.Position;
}

return CountPhone - worker.CountPhone;
}
}
}
77 changes: 77 additions & 0 deletions lab10/consoler/Cell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using Microsoft.VisualBasic;

namespace lab10.consoler
{
public class Cell
{
public enum TextAlignEnum
{
Left,
// ReSharper disable once UnusedMember.Global
Center,
Right,
}

public enum WidthModeEnum
{
// ReSharper disable once UnusedMember.Global
Auto,
FitContent,
Fixed,
}

public TextAlignEnum TextAlign { get; set; }
public WidthModeEnum WidthMode { get; set; }

public int AvailableWidth { get; set; }
public int FixedWidth { get; set; }

public string Text { get; set; }

public int Width()
{
if (WidthMode == WidthModeEnum.Fixed)
{
return FixedWidth;
}

return Text.Length + 2;
}

public override string ToString()
{
if (WidthMode == WidthModeEnum.Fixed && Text.Length > FixedWidth - 2)
{
return $" {Strings.Left(Text, FixedWidth - 2)} ";
}
if (WidthMode == WidthModeEnum.Fixed && Text.Length <= FixedWidth - 2) {
AvailableWidth = FixedWidth;
}

if (WidthMode == WidthModeEnum.FitContent)
{
return $" {Text} ";
}

// WidthMode = auto

if (TextAlign == TextAlignEnum.Left)
{
return $" {Text}{new string(' ', AvailableWidth - 1 - Text.Length)}";
}

if (TextAlign == TextAlignEnum.Right)
{
return $"{new string(' ', AvailableWidth - 1 - Text.Length)}{Text} ";
}

// Center

string leftPadding = new string(' ', (AvailableWidth - Text.Length) / 2);
string rightPadding = leftPadding + new string(' ', (AvailableWidth - Text.Length) % 2);

return leftPadding + Text + rightPadding;
}
}
}
15 changes: 15 additions & 0 deletions lab10/consoler/Consoler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace lab10.consoler
{
public class Consoler
{
public static int Width { get; set; }

public static void PrintTable(Table table)
{
Console.Write(table);
Console.Write("\n");
}
}
}
67 changes: 67 additions & 0 deletions lab10/consoler/Row.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;

namespace lab10.consoler
{
public class Row
{
public List<Cell> Cells { get; set; }

public int Width { get; set; }

public Row()
{
Cells = new List<Cell>();
}

public override string ToString()
{
int widthAvailable = Width;

widthAvailable -= Cells.Count - 1;

// Считаем сколько клеток с auto width

int auto = 0;

foreach (var cell in Cells)
{
if (cell.WidthMode == Cell.WidthModeEnum.Auto)
{
auto++;
}
else
{
widthAvailable -= cell.Width();
}
}

// Печатаем

string result = "|";
int autoWidth = 0;
int autoWidthLast = 0;

if (auto != 0)
{
autoWidth = widthAvailable / auto;
autoWidthLast = autoWidth + widthAvailable % auto;
}

foreach (var cell in Cells)
{
if (auto != 0 && cell.WidthMode == Cell.WidthModeEnum.Auto)
{
cell.AvailableWidth = auto == 1 ? autoWidthLast : autoWidth;
auto--;
}

result += cell.ToString();

result += "|";
}

return result;
}
}
}
45 changes: 45 additions & 0 deletions lab10/consoler/Table.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;

namespace lab10.consoler
{
public class Table
{
public List<Row> Rows { get; set; }

public int Width { get; set; }

public Table()
{
Rows = new List<Row>();
Width = 100;
}

public override string ToString()
{
var separator = "+" + new string('-', Width) + "+";

var result = separator + "\n";

var addSeparator = false;

foreach (var row in Rows)
{
if (addSeparator)
{
result += separator + "\n";
}

addSeparator = true;

row.Width = Width;

result += row + "\n";
}

result += separator;

return result;
}
}
}
8 changes: 8 additions & 0 deletions lab10/lab10.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

</Project>
6 changes: 6 additions & 0 deletions university.sln
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "lab7", "lab7\lab7.csproj",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "lab9", "lab9\lab9.csproj", "{4BAEBE13-2F0A-4F12-849E-F4A8659BFE71}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "lab10", "lab10\lab10.csproj", "{D0C2ED28-C199-4AF2-AEF9-CC1D6E60D13D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -130,5 +132,9 @@ Global
{4BAEBE13-2F0A-4F12-849E-F4A8659BFE71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BAEBE13-2F0A-4F12-849E-F4A8659BFE71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BAEBE13-2F0A-4F12-849E-F4A8659BFE71}.Release|Any CPU.Build.0 = Release|Any CPU
{D0C2ED28-C199-4AF2-AEF9-CC1D6E60D13D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0C2ED28-C199-4AF2-AEF9-CC1D6E60D13D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0C2ED28-C199-4AF2-AEF9-CC1D6E60D13D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0C2ED28-C199-4AF2-AEF9-CC1D6E60D13D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 35532a3

Please sign in to comment.