Add Lesson 6 Task 1
This commit is contained in:
parent
a65d1400cb
commit
09ec4d3cbd
6 changed files with 129 additions and 0 deletions
58
Lesson_6/Task_1/Classes/Calculator.cs
Normal file
58
Lesson_6/Task_1/Classes/Calculator.cs
Normal 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);
|
||||||
|
}
|
12
Lesson_6/Task_1/Classes/Operation.cs
Normal file
12
Lesson_6/Task_1/Classes/Operation.cs
Normal 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);
|
||||||
|
}
|
10
Lesson_6/Task_1/Interfaces/ICalculator.cs
Normal file
10
Lesson_6/Task_1/Interfaces/ICalculator.cs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
namespace Lesson6.Interfaces;
|
||||||
|
|
||||||
|
public interface ICalculator
|
||||||
|
{
|
||||||
|
event CalculationPerformedEventHandler? CalculationPerformed;
|
||||||
|
|
||||||
|
void Calculate(string expression);
|
||||||
|
|
||||||
|
delegate void CalculationPerformedEventHandler(double result);
|
||||||
|
}
|
10
Lesson_6/Task_1/Interfaces/IOperation.cs
Normal file
10
Lesson_6/Task_1/Interfaces/IOperation.cs
Normal 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);
|
||||||
|
}
|
29
Lesson_6/Task_1/Program.cs
Normal file
29
Lesson_6/Task_1/Program.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
Lesson_6/Task_1/Task_1.csproj
Normal file
10
Lesson_6/Task_1/Task_1.csproj
Normal 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>
|
Reference in a new issue