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
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Build

on: [push, pull_request]
on: [push]

jobs:
build:
Expand Down
31 changes: 31 additions & 0 deletions Calculator/Calculator.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.32328.378
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator", "Calculator\Calculator.csproj", "{AA661CFD-17A7-407A-A279-8E003F5DB812}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestCalculator", "TestCalculator\TestCalculator.csproj", "{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AA661CFD-17A7-407A-A279-8E003F5DB812}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA661CFD-17A7-407A-A279-8E003F5DB812}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA661CFD-17A7-407A-A279-8E003F5DB812}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA661CFD-17A7-407A-A279-8E003F5DB812}.Release|Any CPU.Build.0 = Release|Any CPU
{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6B5AF291-04A2-4889-A9EF-EA812796302C}
EndGlobalSection
EndGlobal
225 changes: 225 additions & 0 deletions Calculator/Calculator/Calculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
namespace Calculator;

/// <summary>
/// Class representing Calculator
/// </summary>
public class Calculator
{
private string? FirstOperand { get; set; }

private string? SecondOperand { get; set; }

private string? Operator { get; set; }

private string? SecondOperator { get; set; }

public string? Text { get; private set; }

/// <summary>
/// Is the string an operetor
/// </summary>
/// <param name="text">Input string</param>
/// <returns>True if the string is an operator</returns>
public static bool IsOperator(string text) => text == "/" || text == "*" || text == "-" || text == "+" || text == "±" || text == "=" || text == "√" || text == "C" || text == "⌫";

public string? CalculateExpression(string[] expression)
{
for (int i = 0; i < expression.Length; i++)
{
if(!AddOperatorOrOperand(expression[i]))
{
GetValue();
}
}
return FirstOperand;
}

/// <summary>
/// A function for calculating an expression of the form operator operand operator
/// </summary>
/// <returns>The resulting value</returns>
public string? GetValue()
{
if (FirstOperand == null || SecondOperand == null)
{
return FirstOperand;
}

float firstNumber = float.Parse(FirstOperand);
float secondNumber = float.Parse(SecondOperand);
switch (Operator)
{
case "+":
{
FirstOperand = (firstNumber + secondNumber).ToString();
break;
}
case "-":
{
FirstOperand = (firstNumber - secondNumber).ToString();
break;
}
case "*":
{
FirstOperand = (firstNumber * secondNumber).ToString();
break;
}
case "/":
{
if (Math.Abs(secondNumber - 0) < 0.000000000000000000000000000001)
{
FirstOperand = SecondOperand = Operator = SecondOperand = null;
}
break;
}
default:
{
return null;
}
}

SecondOperand = null;
Text = $"{FirstOperand} {SecondOperator} ";
Operator = SecondOperator;
return FirstOperand;
}

/// <summary>
/// Function for initializing an operator or operand
/// </summary>
/// <param name="text"></param>
public bool AddOperatorOrOperand(string text)
{
if (text == "")
{
return true;
}

if (!IsOperator(text))
{
if (text == ",")
{
if (Text!= null && Text.Contains(',') || FirstOperand == null)
{
return true;
}
}
if (Operator == null)
{
FirstOperand += text;
}
else
{
SecondOperand += text;
}

Text += text;
return true;
}

if (FirstOperand == null)
{
return true;
}

switch (text)
{
case "±":
{
Text = GetValue();
if (FirstOperand != null)
{
FirstOperand = FirstOperand[0] == '-' ? FirstOperand[1..FirstOperand.Length] : $"-{FirstOperand[0..FirstOperand.Length]}";
}

Operator = null;
Text = FirstOperand;
return true;
}

case "√":
{
Text = GetValue();
if (FirstOperand != null)
{
FirstOperand = FirstOperand[0] == '-' ? null : Math.Sqrt(double.Parse(FirstOperand)).ToString();
}

Text = FirstOperand;
return true;
}

case "⌫":
{
if (SecondOperand != null)
{
if (SecondOperand.Length == 1)
{
SecondOperand = null;
Text = $"{FirstOperand} {Operator} ";
}

else
{
SecondOperand = SecondOperand[0..(SecondOperand.Length - 1)];
Text = $"{FirstOperand} {Operator} {SecondOperand}";
}
}

else if (Operator != null)
{
Operator = null;
Text = $"{FirstOperand} ";
}

else if (FirstOperand != null)
{
if (FirstOperand.Length == 1)
{
FirstOperand = null;
Text = "";
}

else
{
FirstOperand = FirstOperand[0..(FirstOperand.Length - 1)];
Text = $"{FirstOperand}";
}
}

return true;
}

case "C":
{
Text = FirstOperand = SecondOperand = Operator = SecondOperator = null;
return false;
}

case "=":
{
Text = FirstOperand = GetValue();
SecondOperand = Operator = SecondOperator = null;
return true;
}

default:
{
Text += " " + text + " ";
if (Operator != null)
{
if (SecondOperand == null)
{
Operator = text;
}
SecondOperator = text;
return false;
}
Operator = text;
return false;
}
}
}
}


11 changes: 11 additions & 0 deletions Calculator/Calculator/Calculator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
Loading