Advertisement
GraionDilach

Zárójel_2

Apr 11th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace zarojel1
  7. {
  8.     class Stack<T>
  9.     {
  10.         T[] t;
  11.         int pointer;
  12.         readonly int size;
  13.  
  14.         public Stack()
  15.             : this(100)
  16.         { }
  17.  
  18.         public Stack(int capacity)
  19.         {
  20.             t = new T[capacity];
  21.             size = capacity;
  22.             pointer = 0;
  23.         }
  24.  
  25.         public void Push(T _in)
  26.         {
  27.             if (pointer >= size)
  28.             {
  29.                 throw new Exception();
  30.             }
  31.             t[pointer] = _in;
  32.             ++pointer;
  33.         }
  34.  
  35.         public T Pop()
  36.         {
  37.             if (pointer > 0)
  38.             {
  39.                 --pointer;
  40.                 return t[pointer];
  41.             }
  42.             throw new Exception();
  43.         }
  44.     }
  45.  
  46.     class Program
  47.     {
  48.         static void Main(string[] args)
  49.         {
  50.             Stack<int> s = new Stack<int>(10);
  51.  
  52.             Console.WriteLine("\nKérek egy zárójeles kifejezést!");
  53.             string kif;
  54.             kif = Console.ReadLine();
  55.  
  56.             Console.WriteLine("\nÖsszetartozó zárójelek pozíciói:");
  57.             bool jel = true;
  58.             int nyit, i;
  59.             for (i = 0; i < kif.Length; i++)
  60.             {
  61.                 if (kif[i] == '(' || kif[i] == '[' || kif[i] == '{')
  62.                 {
  63.                     try
  64.                     {
  65.                         s.Push(i);
  66.                     }
  67.                     catch
  68.                     {
  69.                         Console.WriteLine("\nTele van a stack! Nem oldható meg a feladat!");
  70.                         jel = false;
  71.                         break;
  72.                     }
  73.                 }
  74.                 if (kif[i] == ')' || kif[i] == ']' || kif[i] == '}')
  75.                 {
  76.                     try
  77.                     {
  78.                         nyit = s.Pop();
  79.                         if (!(kif[nyit] == '(' && kif[i] == ')') && !(kif[nyit] == '[' && kif[i] == ']') && !(kif[nyit] == '{' && kif[i] == '}'))
  80.                         {
  81.                             throw new Exception();
  82.                         }
  83.                     }
  84.                     catch
  85.                     {
  86.                         Console.WriteLine("\nHiba a zárójelezésben a {0}. pozíción!", i + 1);
  87.                         jel = false;
  88.                         break;
  89.                     }
  90.                     Console.WriteLine("{0} és {1}", nyit + 1, i + 1);
  91.                 }
  92.             }
  93.             if (jel)
  94.             {
  95.                 try
  96.                 {
  97.                     nyit = s.Pop();
  98.                     Console.WriteLine("\nTöbb a nyitó zárójel!");
  99.                 }
  100.                 catch
  101.                 {
  102.                     Console.WriteLine("\nHelyes a zárójelezés!");
  103.                 }
  104.             }
  105.             Console.ReadKey();
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement