Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace zarojel1
- {
- class Stack<T>
- {
- T[] t;
- int pointer;
- readonly int size;
- public Stack()
- : this(100)
- { }
- public Stack(int capacity)
- {
- t = new T[capacity];
- size = capacity;
- pointer = 0;
- }
- public void Push(T _in)
- {
- if (pointer >= size)
- {
- throw new Exception();
- }
- t[pointer] = _in;
- ++pointer;
- }
- public T Pop()
- {
- if (pointer > 0)
- {
- --pointer;
- return t[pointer];
- }
- throw new Exception();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Stack<int> s = new Stack<int>(10);
- Console.WriteLine("\nKérek egy zárójeles kifejezést!");
- string kif;
- kif = Console.ReadLine();
- Console.WriteLine("\nÖsszetartozó zárójelek pozíciói:");
- bool jel = true;
- int nyit, i;
- for (i = 0; i < kif.Length; i++)
- {
- if (kif[i] == '(' || kif[i] == '[' || kif[i] == '{')
- {
- try
- {
- s.Push(i);
- }
- catch
- {
- Console.WriteLine("\nTele van a stack! Nem oldható meg a feladat!");
- jel = false;
- break;
- }
- }
- if (kif[i] == ')' || kif[i] == ']' || kif[i] == '}')
- {
- try
- {
- nyit = s.Pop();
- if (!(kif[nyit] == '(' && kif[i] == ')') && !(kif[nyit] == '[' && kif[i] == ']') && !(kif[nyit] == '{' && kif[i] == '}'))
- {
- throw new Exception();
- }
- }
- catch
- {
- Console.WriteLine("\nHiba a zárójelezésben a {0}. pozíción!", i + 1);
- jel = false;
- break;
- }
- Console.WriteLine("{0} és {1}", nyit + 1, i + 1);
- }
- }
- if (jel)
- {
- try
- {
- nyit = s.Pop();
- Console.WriteLine("\nTöbb a nyitó zárójel!");
- }
- catch
- {
- Console.WriteLine("\nHelyes a zárójelezés!");
- }
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement