-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Зайка
committed
Dec 25, 2021
1 parent
b739535
commit 35532a3
Showing
9 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters