Добавьте файлы проекта.
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue