Дано выражение, содержащее скобки трех типов (круглые, квадратные и фигурные). Написать программу, которая проверяет, является ли заданное выражение правильным. Правильным считается выражение, в котором закрыты все открывающиеся скобки, причем скобка, открытая позже, закрывается раньше, и отсутствуют «лишние» закрывающиеся скобки.
Например, последовательности
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class Stack { public char Str; public Stack Next; public void add(char Str) { Stack Q = new Stack(); Q.Str = this.Str; this.Str = Str; Q.Next = this.Next; this.Next = Q; Count = Count + 1; } public char pop() { char q = this.Str; this.Str = this.Next.Str; this.Next = this.Next.Next; Count = Count - 1; return q; } public int Count; // Перехватов Д. 09мос(у) } class Program { static void Main(string[] args) { string str; char c; int w=0; bool t = false; Stack s = new Stack(); str = Console.ReadLine(); for (int i = 0; i < str.Length; i++) { if ((str[i] == '{') || (str[i] == '(') || (str[i] == '[')) { s.add(str[i]); } else if ((str[i] == ')') || (str[i] == ']') || (str[i] == '}')) { if (s.Count == 0) { Console.WriteLine("Не хватает скобки"); break; } c = (char)s.pop(); if (((c == '{') && (str[i] == '}')) || ((c == '[') && (str[i] == ']')) || ((c == '(') && (str[i] == ')'))) { if (w == (str.Length - 1)/2 ) t = true; w++; continue; } else { Console.WriteLine("Неправильная комбинация скобок"); break; } } else { continue; } } if (s.Count > 0) { Console.WriteLine("Не хватает закрывающей скобки"); } if (t) Console.WriteLine("Все верно!!!"); } } }
03 декабря 2009, 23:31