Advertisement
elena1234

AppliedArithmetics- how to use func<>

Jan 26th, 2021
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace AppliedArithmetics
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.             Func<int, int> add = x => x + 1;
  12.             Func<int, int> multiply = x => x * 2;
  13.             Func<int, int> subtract = x => x - 1;
  14.             Action<int[]> print = x => Console.WriteLine(string.Join(" ", x));
  15.  
  16.             string command;
  17.             while ((command = Console.ReadLine()) != "end")
  18.             {
  19.                 switch (command)
  20.                 {
  21.                     case "add":
  22.                        numbers= numbers.Select(add).ToArray();
  23.                         break;
  24.                     case "multiply":
  25.                       numbers=  numbers.Select(multiply).ToArray();
  26.                         break;
  27.                     case "subtract":
  28.                        numbers= numbers.Select(subtract).ToArray();
  29.                         break;
  30.                     case "print":
  31.                         print(numbers);
  32.                         break;
  33.                     default:
  34.                         break;
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement