Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Zad_5
- {
- class Program
- {
- static bool PrawidłoweNawiasy(string w)
- {
- Stack<char> s = new Stack<char>();
- for (int i = 0; i < w.Length; i++)
- {
- if (w[i] == '(' || w[i] == '[') s.Push(w[i]);
- if (w[i] == ')')
- {
- if (s.Count == 0) return false;
- if (s.Pop() != '(') return false;
- }
- if (w[i] == ']')
- {
- if (s.Count == 0) return false;
- if (s.Pop() != '[') return false;
- }
- }
- //powinien stos byc pusty
- if (s.Count == 0)
- return true;
- else
- return false;
- }
- static void Main(string[] args)
- {
- string n1 = "a=)x]i[+5(*y;";
- string n2 = "a=(x[i]+5)*y;";
- string n3 = "a=(x[i)+5]*y;";
- string n4 = "a=(x(i]+5]*y;";
- Console.WriteLine(PrawidłoweNawiasy(n1));
- Console.WriteLine();
- Console.WriteLine(PrawidłoweNawiasy(n2));
- Console.WriteLine();
- Console.WriteLine(PrawidłoweNawiasy(n3));
- Console.WriteLine();
- Console.WriteLine(PrawidłoweNawiasy(n4));
- Console.WriteLine();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement