PhotoShaman

ПР #4

May 12th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace PR4
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             //-----------1
  15.             Console.Write("Введите строку: ");
  16.             string text1 = Console.ReadLine();
  17.  
  18.             text1 = Regex.Replace(text1, "[ ]+", " "); //Пользуюсь регулярными выражениями, так как недавно читал про них)
  19.  
  20.             Console.WriteLine("Строка без лишних пробелов: " + text1);
  21.  
  22.             //-----------2
  23.             string[] textArray = text1.Split(' ');
  24.             string maxWord = ""; /*Code protected by ZAV*/
  25.             int maxWordCount = 0;
  26.             foreach (var k in textArray)
  27.             {
  28.                 if (k.Length > maxWord.Length)
  29.                 {
  30.                     maxWord = k;
  31.                 }
  32.                 else if (k == maxWord)
  33.                 {
  34.                     maxWordCount++;
  35.                 }
  36.             }
  37.             Console.WriteLine("Максимальное слово: \"{0}\" повторяется ({1}) раз.", maxWord, maxWordCount);
  38.  
  39.             //-----------3
  40.             Console.Write("Введите строку: ");
  41.             string text2 = Console.ReadLine();
  42.             ParseBrackets(text2);
  43.  
  44.             Console.ReadLine();
  45.         }
  46.  
  47.         static int ParseBrackets(string text)
  48.         {
  49.             int temp = 0;
  50.             for (int i = 0; i < text.Length; i++)
  51.             {
  52.                 if (text[i] == '(')
  53.                 {
  54.                     temp++;
  55.                 }
  56.                 else if (text[i] == ')')
  57.                 {
  58.                     temp--;
  59.                 }
  60.                 if (temp < 0)
  61.                 {
  62.                     Console.WriteLine("Ошибка в {0} символе", (i + 1));
  63.                     return temp;
  64.                 }
  65.             }
  66.             if (temp > 0)
  67.             {
  68.                 Console.WriteLine("Ошибка. Лишние откр. скобки: {0} шт", temp);
  69.                 return -1;
  70.             }
  71.             Console.WriteLine("Все верно.");
  72.             return 0;
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment