Working calculator
This commit is contained in:
80
Server/Calculator.cs
Normal file
80
Server/Calculator.cs
Normal 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
2
Server/Program.cs
Normal 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
12
Server/Server.csproj
Normal 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>
|
||||
Reference in New Issue
Block a user