Skip to content

Commit 8c940cf

Browse files
committed
Rollback of commits due to their uninformativeness
1 parent 3e33fb1 commit 8c940cf

15 files changed

Lines changed: 580 additions & 203 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32210.238
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StackCalculator", "StackCalculator\StackCalculator.csproj", "{8DBDDAE0-E403-49E6-9223-5AC9BF60294F}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestStackCalculator", "TestStackCalculator\TestStackCalculator.csproj", "{46FAACC4-DAD8-4735-943E-22F9ADAA23C6}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stack", "..\..\Stack\Stack\Stack.csproj", "{C66F855E-3C30-4EA4-AFBC-87B64CBCB781}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{8DBDDAE0-E403-49E6-9223-5AC9BF60294F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{8DBDDAE0-E403-49E6-9223-5AC9BF60294F}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{8DBDDAE0-E403-49E6-9223-5AC9BF60294F}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{8DBDDAE0-E403-49E6-9223-5AC9BF60294F}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{46FAACC4-DAD8-4735-943E-22F9ADAA23C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{46FAACC4-DAD8-4735-943E-22F9ADAA23C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{46FAACC4-DAD8-4735-943E-22F9ADAA23C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{46FAACC4-DAD8-4735-943E-22F9ADAA23C6}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{C66F855E-3C30-4EA4-AFBC-87B64CBCB781}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{C66F855E-3C30-4EA4-AFBC-87B64CBCB781}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{C66F855E-3C30-4EA4-AFBC-87B64CBCB781}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{C66F855E-3C30-4EA4-AFBC-87B64CBCB781}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {F3D2B963-4468-436F-A5AE-D78F83C046C7}
36+
EndGlobalSection
37+
EndGlobal
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using Stack;
3+
4+
namespace StackCalculator;
5+
6+
/// <summary>
7+
/// A class representing a stack calculator
8+
/// </summary>
9+
public class Calculator
10+
{
11+
private IStack<float> Stack = new StackOnLists<float>();
12+
13+
/// <summary>
14+
/// Function for counting expressions in postfix form
15+
/// </summary>
16+
/// <returns>Expression value</returns>
17+
public float CountTheExpressionInPostfixForm(string[] inputString)
18+
{
19+
int number = 0;
20+
for (int i = 0; i < inputString.Length; i++)
21+
{
22+
if (inputString[i] == "")
23+
{
24+
continue;
25+
}
26+
if (int.TryParse(inputString[i], out number))
27+
{
28+
Stack.Push(number);
29+
continue;
30+
}
31+
try
32+
{
33+
if (Stack.ReturnNumberOfElements() < 2)
34+
{
35+
throw new IncorrectExpressionException("Incorrect expression");
36+
}
37+
}
38+
catch (IncorrectExpressionException exception)
39+
{
40+
Console.WriteLine($"Ошибка: {exception.Message}");
41+
throw;
42+
}
43+
float secondNumber = 0;
44+
float firstNumber = 0;
45+
try
46+
{
47+
secondNumber = Stack.Pop();
48+
firstNumber = Stack.Pop();
49+
}
50+
catch (StackException)
51+
{
52+
Console.WriteLine("Incorrect expression");
53+
throw;
54+
}
55+
if (inputString[i] == "+")
56+
{
57+
Stack.Push(firstNumber + secondNumber);
58+
continue;
59+
}
60+
if (inputString[i] == "-")
61+
{
62+
Stack.Push(firstNumber - secondNumber);
63+
continue;
64+
}
65+
if (inputString[i] == "*")
66+
{
67+
Stack.Push(firstNumber * secondNumber);
68+
continue;
69+
}
70+
if (inputString[i] == "/")
71+
{
72+
try
73+
{
74+
if (secondNumber.CompareTo(0) == 0)
75+
{
76+
throw new DivideByZeroException("division by 0");
77+
}
78+
}
79+
catch (DivideByZeroException exception)
80+
{
81+
Console.WriteLine($"Ошибка: {exception.Message}");
82+
throw;
83+
}
84+
Stack.Push(firstNumber / secondNumber);
85+
}
86+
else
87+
{
88+
try
89+
{
90+
throw new InvalidCharacterException("Invalid character");
91+
}
92+
catch(InvalidCharacterException exception)
93+
{
94+
Console.WriteLine($"Ошибка: {exception.Message}");
95+
throw;
96+
}
97+
}
98+
}
99+
try
100+
{
101+
if (Stack.ReturnNumberOfElements() > 1)
102+
{
103+
throw new IncorrectExpressionException("Incorrect expression");
104+
}
105+
}
106+
catch (IncorrectExpressionException exception)
107+
{
108+
Console.WriteLine($"Ошибка: {exception.Message}");
109+
throw;
110+
}
111+
try
112+
{
113+
if (Stack.ReturnNumberOfElements() < 1)
114+
{
115+
throw new IncorrectExpressionException("Empty string");
116+
}
117+
}
118+
catch (IncorrectExpressionException exception)
119+
{
120+
Console.WriteLine($"Ошибка: {exception.Message}");
121+
throw;
122+
}
123+
return Stack.Pop();
124+
}
125+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace StackCalculator;
2+
3+
public class Solution
4+
{
5+
static void Main()
6+
{
7+
Console.WriteLine("Please, enter the expression");
8+
var inputString = Console.ReadLine();
9+
if (inputString == null)
10+
{
11+
return;
12+
}
13+
var subs = inputString.Split(' ');
14+
Calculator stackCalculator = new Calculator();
15+
Console.WriteLine($"{stackCalculator.CountTheExpressionInPostfixForm(subs)}");
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<StartupObject></StartupObject>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\..\Stack\Stack\Stack.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace StackCalculator;
2+
3+
/// <summary>
4+
/// A class for creating custom exceptions
5+
/// </summary>
6+
public class InvalidCharacterException : Exception
7+
{
8+
public InvalidCharacterException(string? message) : base(message) { }
9+
}
10+
11+
/// <summary>
12+
/// A class for creating custom exceptions
13+
/// </summary>
14+
public class IncorrectExpressionException : Exception
15+
{
16+
public IncorrectExpressionException(string? message) : base(message) { }
17+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using NUnit.Framework;
2+
using StackCalculator;
3+
using System;
4+
5+
namespace TestStackCalculator;
6+
7+
/// <summary>
8+
/// A class for testing a stack calculator
9+
/// </summary>
10+
public class TestsStackCalculator
11+
{
12+
Calculator? stackCalculator;
13+
[SetUp]
14+
public void Setup()
15+
{
16+
stackCalculator = new Calculator();
17+
}
18+
19+
[Test]
20+
public void DevideByZero()
21+
{
22+
string[] firstArray = { "123", "0", "/" };
23+
Assert.Throws<System.DivideByZeroException>(() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray));
24+
}
25+
26+
[Test]
27+
public void ExpressionFromInvalidCharacter()
28+
{
29+
string[] firstArray = { "123", "0", "a" };
30+
Assert.Throws<InvalidCharacterException> (() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray));
31+
}
32+
33+
[Test]
34+
public void FirstIncorrectExpression()
35+
{
36+
string[] firstArray = { "123", "4", "*", "-" };
37+
Assert.Throws<IncorrectExpressionException>(() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray));
38+
}
39+
40+
[Test]
41+
public void SecondIncorrectExpression()
42+
{
43+
string[] firstArray = { "123", "4", "9", "23", "*", "-" };
44+
Assert.Throws<IncorrectExpressionException>(() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray));
45+
}
46+
47+
[Test]
48+
public void ValidOneCharacterExpression()
49+
{
50+
string[] firstArray = { "12"};
51+
Assert.AreEqual(12, stackCalculator?.CountTheExpressionInPostfixForm(firstArray));
52+
}
53+
54+
55+
[Test]
56+
public void ValidExpressionFromZeros()
57+
{
58+
string[] firstArray = { "0", "0", "+", "0", "-" };
59+
Assert.AreEqual(0, stackCalculator?.CountTheExpressionInPostfixForm(firstArray));
60+
}
61+
62+
[Test]
63+
public void ExpressionWithoutNumbers()
64+
{
65+
string[] firstArray = {"+", "-"};
66+
Assert.Throws<IncorrectExpressionException>(() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray));
67+
}
68+
69+
[Test]
70+
public void EmptyString()
71+
{
72+
string[] firstArray = {" "};
73+
Assert.Throws<IncorrectExpressionException>(() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray));
74+
}
75+
76+
[Test]
77+
public void LongExpression()
78+
{
79+
string[] firstArray = { "0" , "120", "+", "45", "15", "/", "33", "+", "-5", "-13", "+", "9", "/", "/", "1", "123", "*", "+", "-" };
80+
Assert.AreEqual(15, stackCalculator?.CountTheExpressionInPostfixForm(firstArray));
81+
}
82+
}

Stack/TestStackOnArray/TestStack.csproj renamed to Homework2/StackCalculator/TestStackCalculator/TestStackCalculator.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
13-
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
12+
<PackageReference Include="NUnit" Version="3.13.2" />
13+
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
1414
<PackageReference Include="coverlet.collector" Version="3.1.0" />
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<ProjectReference Include="..\Stack\Stack.csproj" />
18+
<ProjectReference Include="..\StackCalculator\StackCalculator.csproj" />
1919
</ItemGroup>
2020

2121
</Project>

Stack/Stack.sln

Lines changed: 0 additions & 31 deletions
This file was deleted.

Stack/Stack/IStack.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace Stack;
2+
3+
/// <summary>
4+
/// A class representing the stack interface
5+
/// </summary>
6+
abstract public class IStack<T>
7+
{
8+
/// <summary>
9+
/// Function for checking the stack for emptiness
10+
/// </summary>
11+
/// <returns> True - if the stack is empty </returns>
12+
abstract public bool IsEmpty();
13+
14+
/// <summary>
15+
/// Function for adding an element to the stack
16+
/// </summary>
17+
/// <param name="value"> The value to add</param>
18+
abstract public void Push(T value);
19+
20+
/// <summary>
21+
/// Function for removing an element from the stack
22+
/// </summary>
23+
/// <returns> Remote value</returns>
24+
abstract public T? Pop();
25+
26+
/// <summary>
27+
/// Function that returns the number of elements in the stack
28+
/// </summary>
29+
/// <returns>Number of elements in stack</returns>
30+
abstract public int ReturnNumberOfElements();
31+
32+
/// <summary>
33+
/// Function that returns the top of the stack
34+
/// </summary>
35+
/// <returns>Top of the stack</returns>
36+
abstract public T? ReturnTopOfTheStack();
37+
38+
/// <summary>
39+
/// Function for stack printing
40+
/// </summary>
41+
abstract public void PrintStack();
42+
43+
/// <summary>
44+
/// Function for removing the stack
45+
/// </summary>
46+
abstract public void DeleteStack();
47+
}

0 commit comments

Comments
 (0)