static void Main() { const int numberOfConverters = 4; delegateConvertTemperature[] converters = new delegateConvertTemperature[numberOfConverters]; converters[(int)Temperature.CToF] = StaticTempConverters.CelciusToFahrenheit; converters[(int)Temperature.CToK] = StaticTempConverters.CelciusToKelvin; converters[(int)Temperature.CToRa] = StaticTempConverters.CelciusToRankine; converters[(int)Temperature.CToRe] = StaticTempConverters.CelciusToReaumur; do { Console.Clear(); double temperature = InputChecker.InputVar <double>("temperature in Celcius"); Console.WriteLine($"Celcius: {temperature:F3}"); for (int i = 0; i < numberOfConverters; ++i) { Console.WriteLine($"{converters[i].Method.Name}: {converters[i](temperature):F3}"); } Console.WriteLine("Press Esc to exit. Press any other key to continue."); } while (Console.ReadKey(true).Key != ConsoleKey.Escape); }
static void Main() { MyDel f = TestClass.TestMethod; do { Console.Clear(); double a = InputChecker.InputVar <double>("first real number"); double b = InputChecker.InputVar <double>("second real number"); Console.WriteLine($"Sum of integer parts: {f(a, b)}"); Console.WriteLine("Press Esc to exit. Press any other key to continue."); } while (Console.ReadKey(true).Key != ConsoleKey.Escape); }
static void Main() { MyDel f = TestClass.TestMethod; do { Console.Clear(); int a = InputChecker.InputVar <int>("first integer"); int b = InputChecker.InputVar <int>("second integer"); Console.WriteLine($"Maximum: {f(a, b)}"); Console.WriteLine("Press Esc to exit. Press any other key to continue."); } while (Console.ReadKey(true).Key != ConsoleKey.Escape); }
static void Main() { Multiplication m = (left, right, f) => { double result = 1; for (int i = left; i <= right; ++i) { result *= f(i); } return(result); }; Sum s = (left, right, f) => { double result = 0; for (int i = left; i <= right; ++i) { result += f(i); } return(result); }; do { Console.Clear(); double k = InputChecker.InputVar <double>("x"); int leftBorder = 1, rightBorder = 5; Console.WriteLine("Answer: " + s(leftBorder, rightBorder, y => m(1, 5, x => y * k / x))); double ans = 0; for (int i = leftBorder; i <= rightBorder; ++i) { double tmpResult = 1; for (int j = leftBorder; j <= rightBorder; ++j) { tmpResult *= i * k / j; } ans += tmpResult; } Console.WriteLine("Correct answer: " + ans); Console.WriteLine("Press Esc to exit. Press any other key to continue."); } while (Console.ReadKey(true).Key != ConsoleKey.Escape); }
static void Main() { Action movement = null; const string allowedCommands = "RLFB"; robot.PositionChanged += MarkPosition; Console.ResetColor(); do { Console.Clear(); movement = null; robot.Reset(); int width = InputChecker.InputVar <int>("width of field (1 - 20)", x => (x > 0) && (x <= 20)); int height = InputChecker.InputVar <int>("height of field (1 - 20)", x => (x > 0) && (x <= 20)); field = new char[height, width]; for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { field[i, j] = '-'; } } field[0, 0] = '+'; Console.WriteLine(robot.Position()); Console.Write("Enter commands (string of R, L, F and B): "); string commands = Console.ReadLine(); while (commands == null || !commands.All(allowedCommands.Contains)) { Console.WriteLine("Invalid input format! Try again!"); Console.Write($"Enter commands (string of R, L, F and B): "); commands = Console.ReadLine(); } bool outOfRange = false; foreach (char c in commands) { switch (c) { case 'R': movement += robot.Right; break; case 'L': movement += robot.Left; break; case 'F': movement += robot.Forward; break; case 'B': movement += robot.Backward; break; default: throw new ArgumentException("Invalid commands."); } } try { movement?.Invoke(); } catch (ArgumentException e) { Console.WriteLine(e.Message); outOfRange = true; } if (!outOfRange) { field[robot.Y, robot.X] = '*'; for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { switch (field[i, j]) { case '+': Console.ForegroundColor = ConsoleColor.Gray; break; case '*': Console.ForegroundColor = ConsoleColor.Red; break; default: Console.ResetColor(); break; } Console.Write(field[i, j]); } Console.WriteLine(); } Console.ResetColor(); Console.WriteLine(robot.Position()); } Console.WriteLine("Press Esc to exit. Press any other key to continue."); } while (Console.ReadKey(true).Key != ConsoleKey.Escape); }
static void Main() { Sum sum1 = (n) => { double result = 0; for (int i = 1; i <= n; ++i) { result += 1.0 / i; } return(result); }; Sum doubleSum1 = (n) => { double result = 0; for (int i = 1; i <= n; ++i) { result += sum1(i); } return(result); }; Sum sum2 = (n) => { double result = 0; for (int i = 1; i <= n; ++i) { result += 1.0 / Math.Pow(2, i); } return(result); }; Sum doubleSum2 = (n) => { double result = 0; for (int i = 1; i <= n; ++i) { result += sum2(i); } return(result); }; const int maxInput = 1000; do { Console.Clear(); int n = InputChecker.InputVar <int>($"positive integer N (1 - {maxInput})", x => (x > 0) && (x <= maxInput)); double sum1Check = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= i; ++j) { sum1Check += 1.0 / j; } } double sum2Check = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= i; ++j) { sum2Check += 1.0 / Math.Pow(2, j); } } Console.WriteLine($"Sum1 : {doubleSum1(n):F3}"); Console.WriteLine($"Correct Sum1 : {sum1Check:F3}"); Console.WriteLine($"Sum2 : {doubleSum2(n):F3}"); Console.WriteLine($"Correct Sum2 : {sum2Check:F3}"); Console.WriteLine("Press Esc to exit. Press any other key to continue."); } while (Console.ReadKey(true).Key != ConsoleKey.Escape); }