diff --git a/Lesson_6/Task_1/Classes/Calculator.cs b/Lesson_6/Task_1/Classes/Calculator.cs new file mode 100644 index 0000000..e251d12 --- /dev/null +++ b/Lesson_6/Task_1/Classes/Calculator.cs @@ -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); +} diff --git a/Lesson_6/Task_1/Classes/Operation.cs b/Lesson_6/Task_1/Classes/Operation.cs new file mode 100644 index 0000000..e04eec4 --- /dev/null +++ b/Lesson_6/Task_1/Classes/Operation.cs @@ -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); +} diff --git a/Lesson_6/Task_1/Interfaces/ICalculator.cs b/Lesson_6/Task_1/Interfaces/ICalculator.cs new file mode 100644 index 0000000..f07be51 --- /dev/null +++ b/Lesson_6/Task_1/Interfaces/ICalculator.cs @@ -0,0 +1,10 @@ +namespace Lesson6.Interfaces; + +public interface ICalculator +{ + event CalculationPerformedEventHandler? CalculationPerformed; + + void Calculate(string expression); + + delegate void CalculationPerformedEventHandler(double result); +} diff --git a/Lesson_6/Task_1/Interfaces/IOperation.cs b/Lesson_6/Task_1/Interfaces/IOperation.cs new file mode 100644 index 0000000..6c29e07 --- /dev/null +++ b/Lesson_6/Task_1/Interfaces/IOperation.cs @@ -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); +} diff --git a/Lesson_6/Task_1/Program.cs b/Lesson_6/Task_1/Program.cs new file mode 100644 index 0000000..c5d393d --- /dev/null +++ b/Lesson_6/Task_1/Program.cs @@ -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); + } + } + } +} \ No newline at end of file diff --git a/Lesson_6/Task_1/Task_1.csproj b/Lesson_6/Task_1/Task_1.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Lesson_6/Task_1/Task_1.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + +