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
208 changes: 0 additions & 208 deletions BWT/BWT/Program.cs

This file was deleted.

12 changes: 6 additions & 6 deletions BWT/BWT.sln → stackCalculator/stackCalculator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33403.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BWT", "BWT\BWT.csproj", "{97F74F11-2F27-4B42-A5B1-8D88573DFB01}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StackCalculator", "stackCalculator\StackCalculator.csproj", "{89A2482C-4A20-41A4-9A42-1623A4FA88C3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{97F74F11-2F27-4B42-A5B1-8D88573DFB01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97F74F11-2F27-4B42-A5B1-8D88573DFB01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97F74F11-2F27-4B42-A5B1-8D88573DFB01}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97F74F11-2F27-4B42-A5B1-8D88573DFB01}.Release|Any CPU.Build.0 = Release|Any CPU
{89A2482C-4A20-41A4-9A42-1623A4FA88C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89A2482C-4A20-41A4-9A42-1623A4FA88C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89A2482C-4A20-41A4-9A42-1623A4FA88C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89A2482C-4A20-41A4-9A42-1623A4FA88C3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {65A6BC48-3287-4942-BACC-A033A3982A41}
SolutionGuid = {E6D50142-7CF4-466F-9F0D-9262A98C0B2C}
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions stackCalculator/stackCalculator/InterfaceForStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace StackCalculator;

// Interface for the stack
interface IOperationsWithStack
{
// Add element to stack
void AddElement(double value);

// Remove element in stack and return deleted item
(bool, double) RemoveElement();

// Print all elements
void PrintTheElements();

// Checking that the stack is empty
bool IsEmpty();
}
31 changes: 31 additions & 0 deletions stackCalculator/stackCalculator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using StackCalculator;

Test test = new Test();
if (test.TestForProgram())
{
Console.WriteLine("All tests correct");
}
else
{
Console.WriteLine("Problems...");
return;
}

Console.WriteLine("Enter an example in the postfix form");
var stringWithExpression = Console.ReadLine();

PostfixCalculator calculator = new PostfixCalculator();
if (stringWithExpression == null)
{
return;
}

var stackList = new StackList();
(bool isCorrectWork, double result) = calculator.ConvertToAResponse(stringWithExpression, stackList);
if (!isCorrectWork)
{
Console.WriteLine("Problems with expression or you tried to divide by zero!");
return;
}
Console.WriteLine(result);
17 changes: 17 additions & 0 deletions stackCalculator/stackCalculator/Stack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace StackCalculator;

//Standart stack
abstract public class Stack : IOperationsWithStack
{
// Add element to stack
virtual public void AddElement(double value) { }

// Remove element in stack and return deleted item
virtual public (bool, double) RemoveElement() { return (false, 0); }

// Print all elements
virtual public void PrintTheElements() { }

// Checking that the stack is empty
virtual public bool IsEmpty() { return false; }
}
73 changes: 73 additions & 0 deletions stackCalculator/stackCalculator/StackCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
namespace StackCalculator;

// Calculator that counts algebraic expressions in postfix form
public class PostfixCalculator
{
private const double delta = 0.0000000000001;

// Receives the input string in which the expression is written in postfix form, finds the result
public (bool, double) ConvertToAResponse(string stringWithExpression, Stack stackExpression)
{
int i = 0;
string[] expressionArray = stringWithExpression.Split(' ');
while (i < expressionArray.Length)
{
var isCorrectNumber = Int32.TryParse(expressionArray[i], out var number);
if (isCorrectNumber)
{
stackExpression.AddElement(number);
}
else
{
if (expressionArray[i].Length != 1)
{
return (false, 0);
}
double numberAfter = 0;
(var isCorrect, var firstNumber) = stackExpression.RemoveElement();

if (!isCorrect)
{
return (false, 0);
}

(isCorrect, var secondNumber) = stackExpression.RemoveElement();

if (!isCorrect)
{
return (false, 0);
}

switch (expressionArray[i][0])
{
case '*':
numberAfter = firstNumber * secondNumber;
break;
case '+':
numberAfter = firstNumber + secondNumber;
break;
case '-':
numberAfter = secondNumber - firstNumber;
break;
case '/':
if (Math.Abs(firstNumber) < delta)
{
return (false, 0);
}
numberAfter = secondNumber / firstNumber;
break;
default:
return (false, 0);
}
stackExpression.AddElement(numberAfter);
}
++i;
}
(var isCorrectExpression, var result) = stackExpression.RemoveElement();
if (!isCorrectExpression)
{
return (false, 0);
}
return stackExpression.IsEmpty() ? (true, result) : (false, 0);
}
}
Loading