Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace calc.example
- {
- class Program
- {
- static void Main(string[] args)
- {
- while (true)
- {
- var x = Calculator.GetValue();
- Console.WriteLine();
- var operation = Calculator.GetOperation();
- Console.WriteLine();
- var y = Calculator.GetValue();
- Console.WriteLine();
- Console.WriteLine($"Result: {operation(x, y)}");
- Console.WriteLine("\n\nPress any key to continue...");
- Console.ReadLine();
- Console.Clear();
- }
- }
- }
- static class Calculator
- {
- public static double GetValue()
- {
- Console.Write("Number: ");
- var input = Console.ReadLine();
- if (double.TryParse(input, out double value))
- return value;
- Console.WriteLine("Incorrect Format");
- return GetValue();
- }
- private static Dictionary<char, Func<double, double, double>> operations = new Dictionary<char, Func<double, double, double>>()
- {
- { '+', (x, y) => x + y },
- { '-', (x, y) => x - y },
- { '*', (x, y) => x * y },
- { '/', (x, y) => x / y },
- { '%', (x, y) => x % y }
- };
- public static Func<double, double, double> GetOperation()
- {
- Console.Write("Operation: ");
- var input = Console.ReadLine();
- if (char.TryParse(input, out char character) && operations.TryGetValue(character, out Func<double, double, double> operation))
- return operation;
- Console.WriteLine("Incorrect Format");
- return GetOperation();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement