Advertisement
Danielos168

Nawiasy

Jan 19th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. namespace Zad_5
  2. {
  3.     class Program
  4.     {
  5.         static bool PrawidłoweNawiasy(string w)
  6.         {
  7.             Stack<char> s = new Stack<char>();
  8.             for (int i = 0; i < w.Length; i++)
  9.             {
  10.                 if (w[i] == '(' || w[i] == '[') s.Push(w[i]);
  11.                 if (w[i] == ')')
  12.                 {
  13.                     if (s.Count == 0) return false;
  14.                     if (s.Pop() != '(') return false;
  15.                 }
  16.                 if (w[i] == ']')
  17.                 {
  18.                     if (s.Count == 0) return false;
  19.                     if (s.Pop() != '[') return false;
  20.                 }
  21.             }
  22.             //powinien stos byc pusty
  23.             if (s.Count == 0)
  24.                 return true;
  25.             else
  26.                 return false;
  27.         }
  28.         static void Main(string[] args)
  29.         {
  30.             string n1 = "a=)x]i[+5(*y;";
  31.             string n2 = "a=(x[i]+5)*y;";
  32.             string n3 = "a=(x[i)+5]*y;";
  33.             string n4 = "a=(x(i]+5]*y;";
  34.             Console.WriteLine(PrawidłoweNawiasy(n1));
  35.             Console.WriteLine();
  36.             Console.WriteLine(PrawidłoweNawiasy(n2));
  37.             Console.WriteLine();
  38.             Console.WriteLine(PrawidłoweNawiasy(n3));
  39.             Console.WriteLine();
  40.             Console.WriteLine(PrawidłoweNawiasy(n4));
  41.             Console.WriteLine();
  42.             Console.ReadKey();
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement