Program.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. namespace Calculator
  3. {
  4. internal class Program
  5. {
  6. static void Main()
  7. {
  8. Calculator calculator = new Calculator();
  9. while (Menu(calculator, out var operation))
  10. {
  11. int a = Argument("a");
  12. int b = Argument("b");
  13. Console.WriteLine($"Result: {operation(a, b)}");
  14. }
  15. }
  16. static bool Menu(Calculator calculator, out Func<int, int, float> operation)
  17. {
  18. while (true)
  19. {
  20. Console.WriteLine("Choose operation:");
  21. Console.WriteLine("1) Addition");
  22. Console.WriteLine("2) Subtraction");
  23. Console.WriteLine("3) Multiplication");
  24. Console.WriteLine("4) Division");
  25. Console.WriteLine("5) Power");
  26. Console.WriteLine("6) Exit");
  27. string input = Console.ReadLine();
  28. if (int.TryParse(input, out int result) && result >= 1 && result <= 6)
  29. {
  30. switch (result)
  31. {
  32. case 1:
  33. operation = calculator.Add;
  34. return true;
  35. case 2:
  36. operation = calculator.Subtract;
  37. return true;
  38. case 3:
  39. operation = calculator.Multiply;
  40. return true;
  41. case 4:
  42. operation = calculator.Divide;
  43. return true;
  44. case 5:
  45. operation = calculator.Power;
  46. return true;
  47. case 6:
  48. operation = null;
  49. return false;
  50. }
  51. }
  52. }
  53. }
  54. static int Argument(string name)
  55. {
  56. while (true)
  57. {
  58. Console.Write($"Enter {name}: ");
  59. string input = Console.ReadLine();
  60. if (int.TryParse(input, out int result))
  61. return result;
  62. }
  63. }
  64. }
  65. }