Add Lesson 6 Task 1

This commit is contained in:
Stanislav Mykhailenko 2023-04-12 17:51:12 +03:00
parent a65d1400cb
commit 09ec4d3cbd
GPG key ID: 1E95E66A9C9D6A36
6 changed files with 129 additions and 0 deletions

View file

@ -0,0 +1,58 @@
namespace Lesson6.Classes;
using Lesson6.Interfaces;
public class Calculator : ICalculator
{
public event ICalculator.CalculationPerformedEventHandler? CalculationPerformed;
public void Calculate(string expression)
{
string[] tokens = expression.Split(' ');
double firstNumber;
double secondNumber;
string inputOperation;
double result;
Operation operation = new Operation();
Operation.Do oper;
try
{
firstNumber = double.Parse(tokens[0]);
secondNumber = double.Parse(tokens[2]);
inputOperation = tokens[1];
}
catch
{
Console.WriteLine("Invalid format.");
return;
}
switch (inputOperation)
{
case "+":
oper = operation.Add;
break;
case "-":
oper = operation.Subtract;
break;
case "*":
oper = operation.Multiply;
break;
case "/":
oper = operation.Divide;
break;
default:
Console.WriteLine("Invalid operation.");
return;
}
result = oper.Invoke(firstNumber, secondNumber);
// raise the event
CalculationPerformed?.Invoke(result);
}
public delegate void CalculationPerformedEventHandler(double result);
}

View file

@ -0,0 +1,12 @@
namespace Lesson6.Classes;
using Lesson6.Interfaces;
public class Operation : IOperation
{
public double Add(double firstSummand, double secondSummand) => firstSummand + secondSummand;
public double Subtract(double minuend, double subtrahend) => minuend - subtrahend;
public double Multiply(double firstFactor, double secondFactor) => firstFactor * secondFactor;
public double Divide(double dividend, double divisor) => dividend / divisor;
public delegate double Do(double firstNumber, double secondNumber);
}

View file

@ -0,0 +1,10 @@
namespace Lesson6.Interfaces;
public interface ICalculator
{
event CalculationPerformedEventHandler? CalculationPerformed;
void Calculate(string expression);
delegate void CalculationPerformedEventHandler(double result);
}

View file

@ -0,0 +1,10 @@
namespace Lesson6.Interfaces;
public interface IOperation
{
double Add(double firstSummand, double secondSummand);
double Subtract(double minuend, double subtrahend);
double Multiply(double firstFactor, double secondFactor);
double Divide(double dividend, double divisor);
delegate double Do(double x, double y);
}

View file

@ -0,0 +1,29 @@
using Lesson6.Classes;
namespace Lesson6
{
class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator();
// subscribe to the event
calc.CalculationPerformed += result => Console.WriteLine($"Result: {result}");
while (true)
{
Console.Write("Enter expression (e.g. 2 + 2) or exit: ");
string? expression = Console.ReadLine();
if (expression == null)
continue;
if (expression == "exit")
break;
calc.Calculate(expression);
}
}
}
}

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>