Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace WykopowyKalkulator1
  5. {
  6.     internal static class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Console.WriteLine("Write 'exit' to exit xD");
  11.             Console.WriteLine("Value1 operation Value2 i.e.");
  12.             Console.WriteLine("50 / 50");
  13.  
  14.             while (true)
  15.             {
  16.                 Console.WriteLine("Enter calculation:");
  17.                 var read = Console.ReadLine();
  18.                 if (read != null && read.Equals("exit", StringComparison.OrdinalIgnoreCase))
  19.                 {
  20.                     Console.WriteLine("Elo");
  21.                     break;
  22.                 }
  23.  
  24.                 var values = read?.Split(' ').Select(e => e.Trim()).ToArray();
  25.  
  26.                 if (values == null) continue;
  27.  
  28.                 var calculator = new CalculatorEngine<double>(
  29.                     values[1],
  30.                     Double.Parse(values[0]),
  31.                     Double.Parse(values[2]));
  32.                
  33.                 Console.WriteLine(calculator.Calculate());
  34.             }
  35.         }
  36.     }
  37.  
  38.     public class CalculatorEngine<T>
  39.     {
  40.         private readonly Operations _operation;
  41.         private readonly T _x;
  42.         private readonly T _y;
  43.  
  44.         private enum Operations
  45.         {
  46.             Add,
  47.             Subtract,
  48.             Multiply,
  49.             Divide
  50.         }
  51.  
  52.  
  53.         private CalculatorEngine(Operations operation, T x, T y)
  54.         {
  55.             _operation = operation;
  56.             _x = x;
  57.             _y = y;
  58.         }
  59.  
  60.  
  61.         public CalculatorEngine(string operation, T x, T y) : this(ConvertOperation(operation), x, y)
  62.         {
  63.         }
  64.  
  65.         public T Calculate()
  66.         {
  67.             return (T) CalculateDynamic();
  68.         }
  69.  
  70.         private dynamic CalculateDynamic()
  71.         {
  72.             dynamic x = _x;
  73.             dynamic y = _y;
  74.  
  75.             switch (_operation)
  76.             {
  77.                 case Operations.Add:
  78.                     return x + y;
  79.                 case Operations.Subtract:
  80.                     return x - y;
  81.                 case Operations.Multiply:
  82.                     return x * y;
  83.                 case Operations.Divide:
  84.                     return x / y;
  85.                 default:
  86.                     throw new ArgumentOutOfRangeException(nameof(_operation), _operation, null);
  87.             }
  88.         }
  89.  
  90.         private static Operations ConvertOperation(string userInput)
  91.         {
  92.             if (userInput.Equals("add", StringComparison.OrdinalIgnoreCase)
  93.                 || userInput.Equals("+", StringComparison.OrdinalIgnoreCase))
  94.                 return Operations.Add;
  95.  
  96.             if (userInput.Equals("subtract", StringComparison.OrdinalIgnoreCase)
  97.                 || userInput.Equals("-", StringComparison.OrdinalIgnoreCase))
  98.                 return Operations.Subtract;
  99.  
  100.             if (userInput.Equals("multiply", StringComparison.OrdinalIgnoreCase)
  101.                 || userInput.Equals("*", StringComparison.OrdinalIgnoreCase)
  102.                 || userInput.Equals("x", StringComparison.OrdinalIgnoreCase))
  103.                 return Operations.Multiply;
  104.  
  105.             if (userInput.Equals("divide", StringComparison.OrdinalIgnoreCase)
  106.                 || userInput.Equals("/", StringComparison.OrdinalIgnoreCase))
  107.                 return Operations.Divide;
  108.  
  109.             throw new Exception("Co do ch**");
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement