Working calculator

This commit is contained in:
2024-02-08 17:18:38 +01:00
commit 719444c06e
15 changed files with 1476 additions and 0 deletions

80
Server/Calculator.cs Normal file
View File

@@ -0,0 +1,80 @@
using System.Globalization;
namespace GrpcCalculator.Server;
public enum CalculatorState
{
AfterEquals,
AfterOperator,
AfterDigit,
}
public enum CalculatorOperator
{
Addition,
Subtraction,
Multiplication,
Division,
}
public static class CalculatorOperatorMethods
{
public static string Evaluate(this CalculatorOperator op, string leftOperand, string rightOperand)
{
var leftOperandValue = double.Parse(leftOperand);
var rightOperandValue = double.Parse(rightOperand);
return op switch
{
CalculatorOperator.Addition => (leftOperandValue + rightOperandValue).ToString(CultureInfo.InvariantCulture),
CalculatorOperator.Subtraction => (leftOperandValue - rightOperandValue).ToString(CultureInfo.InvariantCulture),
CalculatorOperator.Multiplication => (leftOperandValue * rightOperandValue).ToString(CultureInfo.InvariantCulture),
CalculatorOperator.Division => (leftOperandValue / rightOperandValue).ToString(CultureInfo.InvariantCulture),
};
}
}
public record Calculator(CalculatorState State, string LeftOperand, CalculatorOperator? Operator, string RightOperand)
{
private string Evaluate() => Operator.HasValue switch {
true => Operator.Value.Evaluate(LeftOperand, RightOperand),
false => RightOperand,
};
public Calculator() : this(CalculatorState.AfterEquals, "0", null, "0") { }
public Calculator OperatorPressed(CalculatorOperator op) => this with
{
LeftOperand = State switch {
CalculatorState.AfterOperator or CalculatorState.AfterEquals => RightOperand,
CalculatorState.AfterDigit => Evaluate(),
},
Operator = op,
State = CalculatorState.AfterOperator,
};
public Calculator EqualsPressed() => this with {
LeftOperand = Evaluate(),
State = CalculatorState.AfterEquals,
};
public Calculator DigitPressed(string digit) => this with {
LeftOperand = State switch {
CalculatorState.AfterEquals or CalculatorState.AfterOperator => RightOperand,
CalculatorState.AfterDigit => LeftOperand,
},
RightOperand = State switch {
CalculatorState.AfterOperator or CalculatorState.AfterEquals => digit,
CalculatorState.AfterDigit => RightOperand + digit,
},
State = CalculatorState.AfterDigit,
};
public string Display =>
State switch
{
CalculatorState.AfterDigit => RightOperand,
CalculatorState.AfterOperator or CalculatorState.AfterEquals => LeftOperand,
};
}

2
Server/Program.cs Normal file
View File

@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

12
Server/Server.csproj Normal file
View File

@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>GrpcCalculator.Server</RootNamespace>
<AssemblyName>GrpcCalculator.Server</AssemblyName>
</PropertyGroup>
</Project>