Advertisement
Guest User

Untitled

a guest
May 21st, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6.  
  7. namespace _2chSolver
  8. {
  9.     class Program
  10.     {
  11.         //Eng 97 122
  12.         //Rus 1072 1103
  13.         private static readonly char[] Vowels = new [] { 'a', 'e', 'y', 'u', 'i', 'o', 'а', 'у', 'е', 'э', 'о', 'ы', 'я', 'и', 'ю' };
  14.  
  15.         private enum PreviousCharType
  16.         {
  17.             Consonant,
  18.             Vowel,
  19.             None
  20.         }
  21.  
  22.         static void Main(string[] args)
  23.         {
  24.             int rus = 0;
  25.             int eng = 0;
  26.             int vowels = 0;
  27.             int consonantsAfterVowels = 0;
  28.             int vowelsAfterConsonants = 0;
  29.             PreviousCharType prevCharType = PreviousCharType.None;
  30.            
  31.  
  32.             var sw = new Stopwatch();
  33.             sw.Start();
  34.             using (var reader = new StreamReader("input.txt"))
  35.             {
  36.                 var inputString = reader.ReadToEnd().ToLower();
  37.  
  38.                 eng = inputString.ToCharArray().Count(ch => ch <= 122 && ch >= 97);
  39.                 rus = inputString.ToCharArray().Count(ch => ch <= 1103 && ch >= 1072);
  40.                 vowels = inputString.ToCharArray().Count(ch => Vowels.Contains(ch));
  41.  
  42.                 var pairs = new List<string>();
  43.                 for (int i = 0; i < inputString.Length - 1; i++)
  44.                 {
  45.                     pairs.Add(inputString[i] + inputString[i + 1].ToString());
  46.                 }
  47.  
  48.                 consonantsAfterVowels =
  49.                     pairs.Count(
  50.                         ch =>
  51.                             Vowels.Contains(ch[0]) && (ch[1] <= 122 && ch[1] >= 97 || ch[1] <= 1103 && ch[1] >= 1072) &&
  52.                             !Vowels.Contains(ch[1]));
  53.  
  54.  
  55.                 vowelsAfterConsonants =
  56.                     pairs.Count(
  57.                         ch =>
  58.                             Vowels.Contains(ch[1]) && (ch[0] <= 122 && ch[0] >= 97 || ch[0] <= 1103 && ch[0] >= 1072) &&
  59.                             !Vowels.Contains(ch[0]));
  60.  
  61.                 Console.WriteLine("Results");
  62.                 Console.WriteLine($"Rus: {rus}");
  63.                 Console.WriteLine($"Eng: {eng}");
  64.                 Console.WriteLine($"Vowels: {vowels * 1.0 / (rus + eng) * 100} %");
  65.                 Console.WriteLine($"Consonants: {(rus + eng - vowels) * 1.0 / (rus + eng) * 100} %");
  66.                 Console.WriteLine($"VowelsAfterConsonants: {vowelsAfterConsonants}");
  67.                 Console.WriteLine($"ConsonantsAfterVowels: {consonantsAfterVowels}");
  68.  
  69.             }
  70.             sw.Stop();
  71.             Console.WriteLine($"{sw.Elapsed.TotalSeconds} s");
  72.             Console.WriteLine();
  73.  
  74.  
  75.             sw.Reset();
  76.             sw.Start();
  77.             using (var reader = new StreamReader("input.txt"))
  78.             {
  79.                 rus = 0;
  80.                 eng = 0;
  81.                 vowels = 0;
  82.                 consonantsAfterVowels = 0;
  83.                 vowelsAfterConsonants = 0;
  84.  
  85.  
  86.                 do
  87.                 {
  88.  
  89.                     var ch = Char.ToLower((char) reader.Read());
  90.                     var currentCharType = PreviousCharType.None;
  91.  
  92.                     if (ch <= 122 && ch >= 97)
  93.                     {
  94.                         eng++;
  95.                         currentCharType = PreviousCharType.Consonant;
  96.                     }
  97.  
  98.                     if (ch <= 1103 && ch >= 1072)
  99.                     {
  100.                         rus++;
  101.                         currentCharType = PreviousCharType.Consonant;
  102.                     }
  103.  
  104.                     if (Vowels.Contains(ch))
  105.                     {
  106.                         vowels++;
  107.                         currentCharType = PreviousCharType.Vowel;
  108.                     }
  109.  
  110.                     if (prevCharType == PreviousCharType.Consonant && currentCharType == PreviousCharType.Vowel)
  111.                     {
  112.                         vowelsAfterConsonants++;
  113.                     }
  114.  
  115.                     if (prevCharType == PreviousCharType.Vowel && currentCharType == PreviousCharType.Consonant)
  116.                     {
  117.                         consonantsAfterVowels++;
  118.                     }
  119.  
  120.                     prevCharType = currentCharType;
  121.                 } while (!reader.EndOfStream);
  122.  
  123.                 Console.WriteLine("Results");
  124.                 Console.WriteLine($"Rus: {rus}");
  125.                 Console.WriteLine($"Eng: {eng}");
  126.                 Console.WriteLine($"Vowels: {vowels*1.0/(rus + eng)*100} %");
  127.                 Console.WriteLine($"Consonants: {(rus + eng - vowels)*1.0/(rus + eng)*100} %");
  128.                 Console.WriteLine($"VowelsAfterConsonants: {vowelsAfterConsonants}");
  129.                 Console.WriteLine($"ConsonantsAfterVowels: {consonantsAfterVowels}");
  130.             }
  131.             sw.Stop();
  132.             Console.WriteLine($"{sw.Elapsed.TotalSeconds} s");
  133.             Console.WriteLine();
  134.             Console.ReadKey();
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement