Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build

on: [push]

jobs:
build-Windows:

runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- name: Build
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.x'
- name: Restore
run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {nuget restore $file.FullName}
- name: Build
run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet build $file.FullName}
- name: Test
run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName}

build-Ubuntu:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.x'
- name: Restore
run: for f in $(find . -name "*.sln"); do dotnet restore $f; done
- name: Build
run: for f in $(find . -name "*.sln"); do dotnet build $f; done
- name: Test
run: for f in $(find . -name "*.sln"); do dotnet test $f; done
31 changes: 31 additions & 0 deletions Homework2/StackCalculator/StackCalculator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32210.238
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StackCalculator", "StackCalculator\StackCalculator.csproj", "{8DBDDAE0-E403-49E6-9223-5AC9BF60294F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "TestStackCalculator\Tests.csproj", "{46FAACC4-DAD8-4735-943E-22F9ADAA23C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8DBDDAE0-E403-49E6-9223-5AC9BF60294F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8DBDDAE0-E403-49E6-9223-5AC9BF60294F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8DBDDAE0-E403-49E6-9223-5AC9BF60294F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8DBDDAE0-E403-49E6-9223-5AC9BF60294F}.Release|Any CPU.Build.0 = Release|Any CPU
{46FAACC4-DAD8-4735-943E-22F9ADAA23C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{46FAACC4-DAD8-4735-943E-22F9ADAA23C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46FAACC4-DAD8-4735-943E-22F9ADAA23C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46FAACC4-DAD8-4735-943E-22F9ADAA23C6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F3D2B963-4468-436F-A5AE-D78F83C046C7}
EndGlobalSection
EndGlobal
90 changes: 90 additions & 0 deletions Homework2/StackCalculator/StackCalculator/Calculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
namespace StackCalculator;

using Stack;

/// <summary>
/// A class representing a stack calculator
/// </summary>
public class Calculator
{
/// <summary>
/// Function for counting expressions in postfix form
/// </summary>
/// <returns>Expression value</returns>
public float CountTheExpressionInPostfixForm(string[] inputString, IStack<float> stack)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет-нет, хочется, чтобы калькулятор принимал строку, а не массив строк

{
for (int i = 0; i < inputString.Length; i++)
{
if (inputString[i] == "")
{
continue;
}

if (int.TryParse(inputString[i], out int number))
{
stack.Push(number);
continue;
}

if (stack.NumberOfElements() < 2)
{
throw new IncorrectExpressionException();
}

float secondNumber;
float firstNumber;
try
{
secondNumber = stack.Pop();
firstNumber = stack.Pop();
}
catch (StackIsEmptyException)
{
throw;
}
Comment on lines +41 to +44

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если вы думаете, что ваша жизнь бессмысленна, посмотрите на эти строки кода :)


switch (inputString[i])
{
case "+" :
{
stack.Push(firstNumber + secondNumber);
break;
}

case "-" :
{
stack.Push(firstNumber - secondNumber);
break;
}

case "*" :
{
stack.Push(firstNumber * secondNumber);
break;
}

case "/" :
{
if (Math.Abs(secondNumber - 0) < 0.0000000000000000000000000001)
{
throw new DivideByZeroException();
}
stack.Push(firstNumber / secondNumber);
break;
}

default :
{
throw new InvalidCharacterException();
}
}
}

if (stack.NumberOfElements() != 1)
{
throw new IncorrectExpressionException();
}

return stack.Pop();
}
}
47 changes: 47 additions & 0 deletions Homework2/StackCalculator/StackCalculator/IStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace Stack;

/// <summary>
/// A class representing the stack interface
/// </summary>
public interface IStack<T>
{
/// <summary>
/// Function for checking the stack for emptiness
/// </summary>
/// <returns> True - if the stack is empty </returns>
public bool IsEmpty();

/// <summary>
/// Function for adding an element to the stack
/// </summary>
/// <param name="value"> The value to add</param>
public void Push(T value);

/// <summary>
/// Function for removing an element from the stack
/// </summary>
/// <returns> Remote value</returns>
public T? Pop();

/// <summary>
/// Function that returns the number of elements in the stack
/// </summary>
/// <returns>Number of elements in stack</returns>
public int NumberOfElements();

/// <summary>
/// Function that returns the top of the stack
/// </summary>
/// <returns>Top of the stack</returns>
public T? TopOfTheStack();

/// <summary>
/// Function for stack printing
/// </summary>
public void PrintStack();

/// <summary>
/// Function for removing the stack
/// </summary>
public void ClearStack();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace StackCalculator;

/// <summary>
/// A class for creating custom exceptions
/// </summary>
public class IncorrectExpressionException : Exception
{
public IncorrectExpressionException() : base() { }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это тоже нечто ненужное. Если в классе не объявлен ни один конструктор, компилятор сам генерирует как раз это.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace StackCalculator;

/// <summary>
/// A class for creating custom exceptions
/// </summary>
public class InvalidCharacterException : Exception
{
public InvalidCharacterException() : base() { }
}
11 changes: 11 additions & 0 deletions Homework2/StackCalculator/StackCalculator/StackCalculator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartupObject></StartupObject>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Stack;

/// <summary>
/// A class for creating custom exceptions
/// </summary>
public class StackIsEmptyException : Exception
{
public StackIsEmptyException() : base() { }
}
99 changes: 99 additions & 0 deletions Homework2/StackCalculator/StackCalculator/StackOnArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
namespace Stack;

using System;

/// <summary>
/// A class representing the stack on arrays
/// </summary>
public class StackOnArray<T> : IStack<T>
{
private T[] values;
private int numberOfElements;

public StackOnArray()
{
values = new T[20];
}

/// <summary>
/// Function for checking the stack for emptiness
/// </summary>
/// <returns> True - if the stack is empty </returns>
public bool IsEmpty() => numberOfElements == 0;

/// <summary>
/// Function for adding an element to the stack
/// </summary>
/// <param name="value"> The value to add</param>
public void Push(T value)
{
if (values != null && numberOfElements == values.Length)
{
Array.Resize(ref values, values.Length + 20);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше увеличивать сразу вдвое. Иначе как у Вас операция Push работает в среднем за линию, а если стек растёт вдвое, то в среднем за константу.

}

numberOfElements++;
if (values == null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется, nullability-анализ гарантирует, что values не null, так что это довольно бессмысленная проверка

{
return;
}

values[numberOfElements - 1] = value;
}

/// <summary>
/// Function for removing an element from the stack
/// </summary>
/// <returns> Remote value</returns>
public T Pop()
{
if (numberOfElements == 0 || values == null)
{
throw new StackIsEmptyException();
}

T topOfSTack = values[numberOfElements - 1];
Array.Clear(values, numberOfElements - 1, 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это хитрый способ записать values[numberOfElements - 1] = default;

numberOfElements--;
return topOfSTack;
}

/// <summary>
/// Function that returns the top of the stack
/// </summary>
/// <returns>Top of the stack</returns>
public T TopOfTheStack()
{
if (numberOfElements == 0 || values == null)
{
throw new StackIsEmptyException();
}

return values[numberOfElements - 1];
}

/// <summary>
/// Function that returns the number of elements in the stack
/// </summary>
/// <returns>Number of elements in stack</returns>
public int NumberOfElements() => numberOfElements;

/// <summary>
/// Function for stack printing
/// </summary>
public void PrintStack()
{
for (int i = numberOfElements - 1; i >= 0; i--)
{
if (values != null)
{
Console.Write($"{values[i]} ");
}
}
}

/// <summary>
/// Function for removing the stack
/// </summary>
public void ClearStack() => values = new T[20];
}
Loading