Advertisement
sokolova4

Untitled

May 11th, 2021
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleApp3
  6. {
  7.     public class MyLib
  8.     {
  9.         static public string[] keywords = {
  10.     "abstract", "as", "base", "bool", "break",
  11.     "byte", "case", "catch", "char", "checked",
  12.     "class", "const", "continue", "decimal", "default",
  13.     "delegate", "do", "double", "else", "enum",
  14.     "event", "explicit", "extern", "false", "finally",
  15.     "fixed", "float", "for", "foreach", "goto",
  16.     "if", "implicit", "in", "int", "interface",
  17.     "internal", "is", "lock", "long", "namespace",
  18.     "new", "null", "object", "operator", "out",
  19.     "override", "params", "private", "protected", "public",
  20.     "readonly", "ref", "return", "sbyte", "sealed",
  21.     "short", "sizeof", "stackalloc", "static", "string",
  22.     "struct", "switch", "this", "throw", "true",
  23.     "try", "typeof", "uint", "ulong", "unchecked",
  24.     "unsafe", "ushort", "using", "virtual", "void",
  25.     "volatile", "while"  };  //  string[ ]
  26.     }   //  class MyLib
  27.  
  28.     // Слова по алфавиту - нумеруемая коллекция, пригодная для LINQ:
  29.     public class Words : IEnumerable<string[]>
  30.     {
  31.         string[] source; // Ссылка на исходный массив «слов»
  32.         public Words(string[] source)
  33.         {     // Конструктор
  34.             this.source = source;
  35.         }
  36.         public System.Collections.Generic.IEnumerator<string[]>
  37.             GetEnumerator()
  38.         {
  39.             string letters = "abcdefghijklmnopqrstuvwxyz";
  40.             for (int i = 0; i < letters.Length; i++)
  41.             {
  42.                 string[] resAr = (from word in source
  43.                                   where letters[i] == word[0]
  44.                                   select word).ToArray();
  45.                 yield return resAr;
  46.             }
  47.         }   //  GetEnumerator<>
  48.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  49.         { yield return "Заглушка!!!"; }
  50.     }   // class Words
  51.  
  52.     class Program
  53.     {
  54.         static void Main()
  55.         {
  56.             string[] test = { "one", "two", "three", "four", "five", "zero" };
  57.             Words words = new Words(test);
  58.             Console.WriteLine(words.Count());
  59.             Console.WriteLine("Названия цифр по алфавиту:");
  60.             foreach (var item in words)
  61.                 foreach (var el in item)
  62.                     Console.Write(el + "  ");
  63.             Console.WriteLine();
  64.             //.. Выберем из поcледовательности массив слов на букву 'f':
  65.             var res = from s in words
  66.                       where s.Length > 0 && s[0][0] == 'f'
  67.                       select s;
  68.             Console.WriteLine("Названия цифр на букву 'f':");
  69.             foreach (var item in res)
  70.                 foreach (var el in item)
  71.                     Console.WriteLine(el);
  72.             // Создадим объект, представляющий массивы ключевых слов:
  73.             Words keys = new Words(MyLib.keywords);
  74.             // Выберем из поcледовательности массив слов на букву 'i':
  75.             var set = keys.Where(s => s.Length > 0 && s[0][0] == 'i');
  76.             /*var set = from s in keys
  77.                       where s.Length > 0 && s[0][0] == 'i'
  78.                       select s;*/
  79.             Console.WriteLine("Служебные слова на букву 'i':");
  80.             foreach (var item in set)
  81.                 foreach (var el in item)
  82.                     Console.WriteLine(el);
  83.             Console.WriteLine(  set.ToArray<string[]>()[0].Length);
  84.         }   // Main
  85.  
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement