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 Ejercicio7
- {
- class Program
- {
- static void Main(string[] args)
- {
- String cadena;
- Console.WriteLine("Ingrese su operacion");
- cadena = Console.ReadLine();
- if (ControlParentizacion(cadena, '(', ')') != 1)
- Console.WriteLine("Parentesis mal parentizados");
- else
- {
- if (ControlParentizacion(cadena, '[', ']') != 1)
- Console.WriteLine("Corchetes mal parentizados");
- else
- {
- if (ControlParentizacion(cadena, '{', '}') != 1)
- Console.WriteLine("Llaves mal parentizadas");
- else
- Console.WriteLine("BIEN! parentizacion correcta!");
- }
- }
- Console.ReadLine();
- }
- public static int ControlParentizacion(string cadena, char abre, char cierra)
- {
- int i = 0;
- int cont = 1;
- Stack<object> caracteres = new Stack<object>();
- while(i<cadena.Length && cont == 1)
- {
- char c = Convert.ToChar(cadena.Substring(i, 1));
- if (c == abre)
- {
- caracteres.Push(cadena.Substring(i, 1));
- }
- else
- {
- if (c==cierra)
- {
- if (caracteres.Count > 0)
- {
- caracteres.Pop();
- }
- else
- {
- cont = 0;
- }
- }
- }
- i++;
- }
- if (cont == 1 && caracteres.Count > 0)
- cont = 0;
- while (caracteres.Count > 0)
- {
- caracteres.Pop();
- }
- return cont;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement