yanchevilian

05. Applied Arithmetics / 2 variants in one

Sep 7th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Reflection.Metadata;
  6.  
  7. namespace _5._Applied_Arithmetics
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //---------------------------- VARIANT TYPE 1-------------------------------------------
  14.             //---------- Using ternary operator-----------------------------------------
  15.  
  16.             //List<int> numbers = new List<int>(Console.ReadLine().Split().Select(int.Parse).ToList());
  17.  
  18.             //Func<List<int>, string, List<int>> func = (collection, filter) =>
  19.             //        (filter == "add" ? collection.Select(n => n += 1) :
  20.             //        filter == "subtract" ? collection.Select(n => n -= 1) :
  21.             //        collection.Select(n => n *= 2)).ToList();
  22.  
  23.             //Action<List<int>> print = collection =>
  24.             //{
  25.             //    Console.WriteLine(string.Join(" ", collection));
  26.             //};
  27.  
  28.             //string command = Console.ReadLine();
  29.             //while (command?.ToLower() != "end")
  30.             //{
  31.             //    switch (command)
  32.             //    {
  33.             //        case "add":
  34.             //        case "subtract":
  35.             //        case "multiply":
  36.             //            numbers = func(numbers, command);
  37.             //            break;
  38.             //        case "print":
  39.             //            print(numbers);
  40.             //            break;
  41.             //    }
  42.  
  43.             //    command = Console.ReadLine();
  44.             //}
  45.  
  46.             List<int> numbers = new List<int>(Console.ReadLine().Split().Select(int.Parse).ToList());
  47.  
  48.             Action<List<int>> print = collection =>
  49.             {
  50.                 Console.WriteLine(string.Join(" ", collection));
  51.             };
  52.  
  53.             string command = Console.ReadLine();
  54.             while (command?.ToLower() != "end")
  55.             {
  56.                 switch (command)
  57.                 {
  58.                     case "add":
  59.                         numbers = ForEach(numbers, n => n += 1);
  60.                         break;
  61.                     case "subtract":
  62.                         numbers = ForEach(numbers, n => n -= 1);
  63.                         break;
  64.                     case "multiply":
  65.                         numbers = ForEach(numbers, n => n *= 2);
  66.                         break;
  67.                     case "print":
  68.                         print(numbers);
  69.                         break;
  70.                 }
  71.                 command = Console.ReadLine();
  72.             }
  73.         }
  74.         public static List<int> ForEach(List<int> numbers, Func<int, int> func) => (numbers.Select(number => func(number)).ToList());
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment