Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 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<int>(
  29.                     values[1],
  30.                     int.Parse(values[0]),
  31.                     int.Parse(values[2]));
  32.  
  33.                 try
  34.                 {
  35.                     Console.WriteLine(calculator.Calculate());
  36.                 }
  37.                 catch (DivideByZeroException)
  38.                 {
  39.                     Console.WriteLine("Nie dziel cholero przez zero.");
  40.                    
  41.                 }
  42.             }
  43.         }
  44.     }
  45.  
  46.     public class CalculatorEngine<T>
  47.     {
  48.         private readonly Operations _operation;
  49.         private readonly T _x;
  50.         private readonly T _y;
  51.  
  52.         private enum Operations
  53.         {
  54.             Add,
  55.             Subtract,
  56.             Multiply,
  57.             Divide
  58.         }
  59.  
  60.  
  61.         private CalculatorEngine(Operations operation, T x, T y)
  62.         {
  63.             _operation = operation;
  64.             _x = x;
  65.             _y = y;
  66.         }
  67.  
  68.  
  69.         public CalculatorEngine(string operation, T x, T y) : this(ConvertOperation(operation), x, y)
  70.         {
  71.         }
  72.  
  73.         public T Calculate()
  74.         {
  75.             return (T) CalculateDynamic();
  76.         }
  77.  
  78.         private dynamic CalculateDynamic()
  79.         {
  80.             dynamic x = _x;
  81.             dynamic y = _y;
  82.  
  83.             switch (_operation)
  84.             {
  85.                 case Operations.Add:
  86.                     return x + y;
  87.                 case Operations.Subtract:
  88.                     return x - y;
  89.                 case Operations.Multiply:
  90.                     return x * y;
  91.                 case Operations.Divide:
  92.                     return x / y;
  93.                 default:
  94.                     throw new ArgumentOutOfRangeException(nameof(_operation), _operation, null);
  95.             }
  96.         }
  97.  
  98.         private static Operations ConvertOperation(string userInput)
  99.         {
  100.             if (userInput.Equals("add", StringComparison.OrdinalIgnoreCase)
  101.                 || userInput.Equals("+", StringComparison.OrdinalIgnoreCase))
  102.                 return Operations.Add;
  103.  
  104.             if (userInput.Equals("subtract", StringComparison.OrdinalIgnoreCase)
  105.                 || userInput.Equals("-", StringComparison.OrdinalIgnoreCase))
  106.                 return Operations.Subtract;
  107.  
  108.             if (userInput.Equals("multiply", StringComparison.OrdinalIgnoreCase)
  109.                 || userInput.Equals("*", StringComparison.OrdinalIgnoreCase)
  110.                 || userInput.Equals("x", StringComparison.OrdinalIgnoreCase))
  111.                 return Operations.Multiply;
  112.  
  113.             if (userInput.Equals("divide", StringComparison.OrdinalIgnoreCase)
  114.                 || userInput.Equals("/", StringComparison.OrdinalIgnoreCase))
  115.                 return Operations.Divide;
  116.  
  117.             throw new Exception("Co do ch**");
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement