sábado, 15 de junio de 2013

EXAMEN U3 - PREGUNTA 2 (VERIFICACION DE CADENA)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace examenunidad3_pregunta_2
{
    class pila
    {
      
            char[] vec;
            int tam;
            int tope;
            bool vacia, llena;

            public pila(int tamaño)
            {
                vec = new char[tamaño];
                tam = tamaño;
                tope = 0;
                vacia = true;
                llena = false;
            }
          
 
            public void push(char valor)
            {
                vacia = false;
                vec[tope++] = valor;
                if (tope == tam)
                    llena = true;
            }

            public char pop()
            {
                if (--tope == 0)
                    vacia = true;
                llena = false;
                return vec[tope];
            }

            public bool estavacia
            {
                get
                {
                    return vacia;
                }
            }

            public int Tope
            {
                get { return tope; }
            }

            public int Max
            {
                get { return vec.Length; }
            }

            public bool esta_llena()
            {

                return llena;
            }
            public char this[int index]
            {
                get { return vec[index]; }
            }
        //aqui intente complicarme la vida jajaja xD



            public bool validarbien(string cadena)
            {
                char[] aux = cadena.ToCharArray();
                for (int i = 0; i < cadena.Length; i++)
                {
                    push(aux[i]);

                }


                //validamos
                int con1 = 0, con2 = 0, con3 = 0, con4 = 0, con5 = 0, con6 = 0;
                int j = 0;
                while (!estavacia)
                {
                    aux[j] = pop();
                    if (aux[j] == 40)
                    {
                        con1++;

                    }

                    else
                        if (aux[j] == 41)
                            con2++;
                        else

                            if (aux[j] == 91)
                                con3++;
                            else
                                if (aux[j] == 93)
                                    con4++;
                                else



                                    if (aux[j] == 123)
                                        con5++;
                                    else

                                        if (aux[j] == 125)
                                            con6++;

                    j++;
                }
                if (con1 == con2 && con3 == con4 && con5 == con6)
                    return true;
                else

                    return false;

            }
       
        
        }
    }

=============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace examenunidad3_pregunta_2
{
    class Program
    {
      
        static void Main(string[] args)
        {
            Console.WriteLine("ingrese una cadena para validar");
            string cadena = Console.ReadLine();
            pila pila = new pila(cadena.Length);

            Console.WriteLine("validamos bien : " + pila.validarbien(cadena));

            Console.ReadKey();
        }
    }
}




No hay comentarios:

Publicar un comentario