sábado, 15 de junio de 2013

(VERIFICACION DE CADENA) corregido

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

namespace EXAMEN_UIII_2._1__VISUAL_
{
    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.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EXAMEN_UIII_2._1__VISUAL_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void butVerificar_Click(object sender, EventArgs e)
        {
            string cadena = Convert.ToString(textInsertar.Text);
            Pila pila = new Pila(cadena.Length);

            labelResultado.Text=/*"validamos bien : " +*/ Convert.ToString( pila.validarbien(cadena));
        }
    }
}


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();
        }
    }
}




EXAMEN U3 PREGUNTA 1 (PILA ENLAZADA)

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

using System.Windows.Forms;
namespace Examen_UIII_pregunta_1
{
    class Nodo
    {
        public int dato;//v
        public Nodo sig;//s

        public Nodo()
        {
        }

        public Nodo(int pdato, Nodo psig)
        {
            dato = pdato;//v=vv;
            sig = psig;//s=ss;
        }
    }

        class PE
        {
            Nodo u;
            public PE()
            {
                u = null;
            }

            public void Agregar(int dato)
            {
                Nodo nu = new Nodo(dato, u);
                u = nu;
            }

            public int Extraer()
            {
                Nodo naux = new Nodo();
                int val;
                if (u == null)
                    return -1;
                naux = u;
                u = naux.sig;
                val = naux.dato;
                return val;
            }         
        }
    }
==========================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Examen_UIII_pregunta_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           // dataGridView1.ColumnCount = 3;
            dataGridView1.RowCount = 9;//Se asignan los renglones al DataGrigView
            dataGridView1.Rows.Add();
        }
      
       
        PE exam = new PE();//Se declara un objeto global
        int tam = 9;//Un entero global para el tamaño del arreglo


        private void butAgregar_Click(object sender, EventArgs e)
        {
 
            int dato;
            try//Validar que sea de tipo entero
            {
                dato = Int32.Parse(textAgregar.Text);
                textAgregar.Text = "";

            }
            catch
            {
                MessageBox.Show("Solo numeros");
                textAgregar.Text = "";
                return;
            }

            if (tam != 0)//tam!=0
            {
                exam.Agregar(dato);//Se agrega un nodo a la lista.
                dataGridView1[0, tam].Value = "";
                dataGridView1[0, tam--].Value = dato;
            }
            else//Si no se puede agregar la pila tipo lista esta llena
            {
                dataGridView1[0, tam].Value = "LISTA LLENA";
                return;
            }

            if (tam >= 0)
                dataGridView1[0, tam].Value = "Tope";
            textAgregar.Select();
        }


        private void butExtraer_Click(object sender, EventArgs e)
        {
            if (tam >= 0 && tam < 9)
            {
                textExtraer.Text = exam.Extraer().ToString();//Se muestra el numero eliminado en este caso el atributo del nodo
                dataGridView1[0, tam].Value = "";
                dataGridView1[0, tam].Value = "";
                dataGridView1[0, ++tam].Value = "";
            }
            else//Si no se cumple la pila tipo lista esta vacia
            {
                dataGridView1[0, tam].Value = "NO HAY DATOS";
                return;
            }
            if (tam >= 0)
                dataGridView1[0, tam].Value = "Tope";
        }
    }

}


NOTACION POLACA

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


  class PILA <T>
    {   //ATRIBUTOS
        T[] vec;
        int tam;
        int tope;
        bool vacia;
        bool llena;

        //METODOS
        public PILA(int n)
        {
            tam = n;
            vec = new T[tam];
            tope = 0;
            vacia = true;
            llena = false;
        }

        public void Push(T valor)
        {
            vacia = false;
            vec[tope++] = valor;
            if (tope == tam)
                llena = true;
        }

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

        public bool esta_Vacia()
        {
            return vacia;
        }

        public bool esta_Llena()
        {
            return llena;
        }

        public T Tope()
        {
            return vec[tope - 1];
        }
        public bool Full
        {
            //Si tope == pila.Length - 1, la pila esta llena
            get { return tope == vec.Length; }
        }

        public bool Empty
        {
            //Si tope == 0, la pila esta vacia
            get { return tope == 0; }
        }
        public int Length
        {
            //Regrersamos el tamaño del arreglo
            get { return vec.Length; }
        }
    }
======================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


public class Not_Polaca
{
  public  enum Simbolo { OPERANDO, PIZQ, PDER, SUMARES, MULTDIV, POW };
    public StringBuilder convertir_pos(string Ei)
    {
        char[] Epos = new char[Ei.Length];
        int tam = Ei.Length;
        PILA<int> stack = new PILA<int>(tam);

        int i, pos = 0;
        for (i = 0; i < Epos.Length; i++)
        {
            char car = Ei[i];
            Simbolo actual = Tipo_y_Presendecia(car);
            switch (actual)
            {
                case Simbolo.OPERANDO: Epos[pos++] = car; break;
                case Simbolo.SUMARES:
                    {
                        while (!stack.Empty && Tipo_y_Presendecia((char)stack.Tope()) >= actual)
                        {
                            Epos[pos++] = (char)stack.Pop();
                        }
                        stack.Push(car);
                    } break;
                case Simbolo.MULTDIV:
                    {
                        while (!stack.Empty && Tipo_y_Presendecia((char)stack.Tope()) >= actual)
                        {
                            Epos[pos++] = (char)stack.Pop();
                        }
                        stack.Push(car);
                    } break;
                case Simbolo.POW:
                    {
                        while (!stack.Empty && Tipo_y_Presendecia((char)stack.Tope()) >= actual)
                        {
                            Epos[pos++] = (char)stack.Pop();
                        }
                        stack.Push(car);
                    } break;
                case Simbolo.PIZQ: stack.Push(car); break;
                case Simbolo.PDER:
                    {
                        char x = (char)stack.Pop();
                        while (Tipo_y_Presendecia(x) != Simbolo.PIZQ)
                        {
                            Epos[pos++] = x;
                            x = (char)stack.Pop();
                        }
                    } break;
            }
        }
        while (!stack.Empty)
        {
            if (pos < Epos.Length)
                Epos[pos++] = (char)stack.Pop();
            else
                break;
        }
        StringBuilder regresa = new StringBuilder(Ei);

        for (int r = 0; r < Epos.Length; r++)
            regresa[r] = Epos[r];
        return regresa;
    }
    public Simbolo Tipo_y_Presendecia(char s)
    {
        Simbolo simbolo;
        switch (s)
        {
            case '+': simbolo = Simbolo.SUMARES; break;
            case '-': simbolo = Simbolo.SUMARES; break;
            case '*': simbolo = Simbolo.MULTDIV; break;
            case '/': simbolo = Simbolo.MULTDIV; break;
            case '(': simbolo = Simbolo.PIZQ; break;
            case ')': simbolo = Simbolo.PDER; break;
            case '^': simbolo = Simbolo.POW; break;
            default: simbolo = Simbolo.OPERANDO; break;
        }
        return simbolo;
    }
}

========================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
     

        Not_Polaca notacion;
       
        PILA<int> pileishon;
        public Form1()
        {
            InitializeComponent();
        }

      
        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
          
                }

        private void button1_Click_1(object sender, EventArgs e)
        {

            notacion= new Not_Polaca();
            if (textBox1.Text != " ")
            {
                textBox3.Clear();
                textBox2.Text = notacion.convertir_pos(textBox1.Text).ToString();
            }

           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text="4+(2+8)/2";
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
             pileishon = new PILA<int>(textBox2.Text.Length);
             try
             {
                 string cad = textBox2.Text;
                 for (int i = 0; i < cad.Length; i++)
                 {
                     if (cad[i] >= 48 && cad[i] <= 57)
                     {
                         int num = cad[i] - 48;
                         pileishon.Push(num);
                     }
                     else
                     {
                         int resp = 0;
                         int num1 = pileishon.Pop();
                         int num2 = num1;
                         num1 = pileishon.Pop();
                         switch (cad[i])
                         {
                             case '+': resp = num1 + num2;
                                 break;
                             case '-': resp = num1 - num2;
                                 break;
                             case '*': resp = num1 * num2;
                                 break;
                             case '/': resp = num1 / num2;
                                 break;
                             case '^':
                                 {
                                     resp = num1;
                                     int j = 1;
                                     while (j < num2)
                                     {
                                         resp = resp * num1;
                                         j++;
                                     }
                                 }
                                 break;
                         }
                         pileishon.Push(resp);
                         textBox3.Text = resp.ToString();
                     }
                 }
             }
             catch
             {
                 MessageBox.Show("si ingresaste variables no se podra arrojar el resultado.. solo funciona con constantes");
             }

        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            textBox1.Text = "4+(2+8)/2";
        }
    }

 }