1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System;
- namespace Calculator
- {
- internal class Program
- {
- static void Main()
- {
- Calculator calculator = new Calculator();
- while (Menu(calculator, out var operation))
- {
- int a = Argument("a");
- int b = Argument("b");
- Console.WriteLine($"Result: {operation(a, b)}");
- }
- }
- static bool Menu(Calculator calculator, out Func<int, int, float> operation)
- {
- while (true)
- {
- Console.WriteLine("Choose operation:");
- Console.WriteLine("1) Addition");
- Console.WriteLine("2) Subtraction");
- Console.WriteLine("3) Multiplication");
- Console.WriteLine("4) Division");
- Console.WriteLine("5) Power");
- Console.WriteLine("6) Exit");
- string input = Console.ReadLine();
- if (int.TryParse(input, out int result) && result >= 1 && result <= 6)
- {
- switch (result)
- {
- case 1:
- operation = calculator.Add;
- return true;
- case 2:
- operation = calculator.Subtract;
- return true;
- case 3:
- operation = calculator.Multiply;
- return true;
- case 4:
- operation = calculator.Divide;
- return true;
- case 5:
- operation = calculator.Power;
- return true;
- case 6:
- operation = null;
- return false;
- }
- }
- }
- }
- static int Argument(string name)
- {
- while (true)
- {
- Console.Write($"Enter {name}: ");
- string input = Console.ReadLine();
- if (int.TryParse(input, out int result))
- return result;
- }
- }
- }
- }
|