Advertisement
Guest User

Untitled

a guest
May 26th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace calc.example
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. while (true)
  11. {
  12. var x = Calculator.GetValue();
  13.  
  14. Console.WriteLine();
  15.  
  16. var operation = Calculator.GetOperation();
  17.  
  18. Console.WriteLine();
  19.  
  20. var y = Calculator.GetValue();
  21.  
  22. Console.WriteLine();
  23.  
  24. Console.WriteLine($"Result: {operation(x, y)}");
  25.  
  26. Console.WriteLine("\n\nPress any key to continue...");
  27. Console.ReadLine();
  28. Console.Clear();
  29. }
  30. }
  31. }
  32.  
  33. static class Calculator
  34. {
  35. public static double GetValue()
  36. {
  37. Console.Write("Number: ");
  38.  
  39. var input = Console.ReadLine();
  40. if (double.TryParse(input, out double value))
  41. return value;
  42.  
  43. Console.WriteLine("Incorrect Format");
  44. return GetValue();
  45. }
  46.  
  47. private static Dictionary<char, Func<double, double, double>> operations = new Dictionary<char, Func<double, double, double>>()
  48. {
  49. { '+', (x, y) => x + y },
  50. { '-', (x, y) => x - y },
  51. { '*', (x, y) => x * y },
  52. { '/', (x, y) => x / y },
  53. { '%', (x, y) => x % y }
  54. };
  55.  
  56. public static Func<double, double, double> GetOperation()
  57. {
  58. Console.Write("Operation: ");
  59.  
  60. var input = Console.ReadLine();
  61. if (char.TryParse(input, out char character) && operations.TryGetValue(character, out Func<double, double, double> operation))
  62. return operation;
  63.  
  64. Console.WriteLine("Incorrect Format");
  65. return GetOperation();
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement