Добавьте файлы проекта.
This commit is contained in:
parent
0396adddb6
commit
dcfd7c6e70
21 changed files with 1928 additions and 0 deletions
60
Services/Caculyator.cs
Normal file
60
Services/Caculyator.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
using System.Globalization;
|
||||
|
||||
namespace calculator.Services
|
||||
{
|
||||
internal class Caculyator : ICaculator
|
||||
{
|
||||
public string Calculate(string n, string m, string operation)
|
||||
{
|
||||
if (double.TryParse(n, out var numberone) == false)
|
||||
return string.Empty;
|
||||
if (double.TryParse(m, out var numbertwo) == false)
|
||||
return string.Empty;
|
||||
|
||||
return ApplyOperator(operation, numberone, numbertwo).ToString("0.#####");
|
||||
}
|
||||
|
||||
public string SingleOperation(string operand, string operation)
|
||||
{
|
||||
if (double.TryParse(operand, out var numberone) == false)
|
||||
return string.Empty;
|
||||
return ApplyOperator(operation, numberone).ToString("0.#####");
|
||||
}
|
||||
|
||||
private static double ApplyOperator(string token, double operand1, double operand2)
|
||||
{
|
||||
switch (token)
|
||||
{
|
||||
case "+":
|
||||
return operand1 + operand2;
|
||||
case "-":
|
||||
return operand1 - operand2;
|
||||
case "*":
|
||||
return operand1 * operand2;
|
||||
case "/":
|
||||
return operand1 / operand2;
|
||||
case "^":
|
||||
return Math.Pow(operand1, operand2);
|
||||
default:
|
||||
throw new ArgumentException($"Invalid operator: {token}");
|
||||
}
|
||||
}
|
||||
|
||||
private static double ApplyOperator(string token, double operand1)
|
||||
{
|
||||
switch (token)
|
||||
{
|
||||
case "1/":
|
||||
return 1 / operand1;
|
||||
case "√":
|
||||
return Math.Sqrt(operand1);
|
||||
case "%":
|
||||
return operand1 / 100;
|
||||
case "-1":
|
||||
return operand1 * -1;
|
||||
default:
|
||||
throw new ArgumentException($"Invalid operator: {token}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Services/ICaculator.cs
Normal file
7
Services/ICaculator.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace calculator.Services;
|
||||
|
||||
public interface ICaculator
|
||||
{
|
||||
string Calculate(string op1, string op2, string operation);
|
||||
string SingleOperation(string operand, string operation);
|
||||
}
|
||||
7
Services/IInputService.cs
Normal file
7
Services/IInputService.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace calculator.Services
|
||||
{
|
||||
public interface IInputService
|
||||
{
|
||||
public string TryInput(string input);
|
||||
}
|
||||
}
|
||||
23
Services/InputService.cs
Normal file
23
Services/InputService.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
namespace calculator.Services
|
||||
{
|
||||
internal class InputService : IInputService
|
||||
{
|
||||
public string TryInput(string input)
|
||||
{
|
||||
//если есть знак "бесконечность" - не даёт писать цифры после него
|
||||
if (input.Contains('∞'))
|
||||
{
|
||||
input = input[..^1];
|
||||
}
|
||||
|
||||
//ситуация: слева ноль, а после него НЕ запятая, тогда ноль можно удалить
|
||||
if (input[0] == '0' && (input.IndexOf(".", StringComparison.Ordinal) != 1))
|
||||
{
|
||||
input = input.Remove(0, 1);
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue