Advertisement
vencinachev

BracketsC#

Oct 12th, 2020
2,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Program
  5. {
  6.    
  7.     public static bool checkBracketsX(string text)
  8.     {
  9.         Stack<char> check = new Stack<char>();
  10.         foreach (char ch in text)
  11.         {
  12.             if (ch == '(' || ch == '[' || ch == '{')
  13.             {
  14.                 check.Push(ch);
  15.             }
  16.             else if (ch == ')' || ch == ']' || ch == '}')
  17.             {
  18.                 if (check.Count == 0)
  19.                 {
  20.                     return false;
  21.                 }
  22.                 else
  23.                 {
  24.                     char skoba = check.Pop();
  25.                     switch (ch)
  26.                     {
  27.                         case ')':
  28.                             if (skoba != '(')
  29.                                 return false;
  30.                             break;
  31.                         case ']':
  32.                             if (skoba != '[')
  33.                                 return false;
  34.                             break;
  35.                         case '}':
  36.                             if (skoba != '{')
  37.                                 return false;
  38.                             break;
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.        
  44.         if (check.Count == 0)
  45.         {
  46.             return true;
  47.         }
  48.         else
  49.         {
  50.             return false;
  51.         }
  52.     }
  53.    
  54.    
  55.    
  56.    
  57.     public static bool checkBrackets(string text)
  58.     {
  59.         Stack<char> check = new Stack<char>();
  60.         foreach (char ch in text)
  61.         {
  62.             if (ch == '(')
  63.             {
  64.                 check.Push('(');
  65.             }
  66.             else if (ch == ')')
  67.             {
  68.                 if (check.Count == 0)
  69.                 {
  70.                     return false;
  71.                 }
  72.                 else
  73.                 {
  74.                     check.Pop();
  75.                 }
  76.             }
  77.         }
  78.        
  79.         if (check.Count == 0)
  80.         {
  81.             return true;
  82.         }
  83.         else
  84.         {
  85.             return false;
  86.         }
  87.     }
  88.    
  89.     public static void Main()
  90.     {
  91.         Console.Write("Enter text: ");
  92.         string text = Console.ReadLine();
  93.         bool flag = true;  
  94.         Stack<char> check = new Stack<char>();
  95.        
  96.         foreach (char ch in text)
  97.         {
  98.             if (ch == '(')
  99.             {
  100.                 check.Push('(');
  101.             }
  102.             else if (ch == ')')
  103.             {
  104.                 if (check.Count == 0)
  105.                 {
  106.                     flag = false;
  107.                     break;
  108.                 }
  109.                 else
  110.                 {
  111.                     check.Pop();
  112.                 }
  113.             }
  114.         }
  115.        
  116.         if (check.Count != 0)
  117.         {
  118.             flag = false;
  119.         }
  120.        
  121.         if (flag)
  122.         {
  123.             Console.WriteLine("Correct!");
  124.         }
  125.         else
  126.         {
  127.             Console.WriteLine("Incorrect!");
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement