elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a la Factorización De Semiprimos (RSA)


  Mostrar Mensajes
Páginas: 1 2 3 [4]
31  Programación / .NET (C#, VB.NET, ASP) / Re: Ejercicios básicos C# una ayuda con arboles binarios en: 19 Diciembre 2007, 15:31 pm
es una arbol binario,insercion,eliminar,nivel
-******codigo********
using System;

namespace ConsoleApplication6
{
   
   class Class1
   {
      public struct arbol
      {
         public  int dato;
         public int izquierda;
         public int derecha;
      }
      static  int ingresar (int vmin,int vmax,string p)
      {
         int dato;
         do
         {
            Console.WriteLine("ingresar numero de la {0} :",p);
            dato=int.Parse(Console.ReadLine());
         }
         while (dato<vmin || dato>vmax);
         return dato;
      }
      static char continuar (string  msj)
      {
         char respuesta;
         do
         {
            Console.Write("que desea {0}s/n:",msj);
            respuesta =char.Parse(Console.ReadLine());
         }
         while (respuesta!='s' && respuesta !='n' && respuesta !='S'&& respuesta !='N');
         return respuesta;
      }
      static void mostrar (arbol[] a,int numerodato)
      {
         Console.WriteLine();
         if(numerodato!=0)
         {
            Console.WriteLine("tabla que representa a la estructura \n");
            Console.WriteLine("---------------------------------");
            Console.WriteLine("| Valor | Izquierda | Derecha||");
            Console.WriteLine("------------------------");
            for(int i=0;i<a.Length;i++)
            {
               if(i<10)Console.Write("| {0}     ",i);
               else Console.Write("  | {0}    ",i);
               if(a.dato<10)Console.Write("|          {0}    ",a.dato);
               else Console.Write("|   {0}   ",a.dato);
               if(a.izquierda<10)Console.Write(" |  {0}  ",a.izquierda);
               else Console.Write("|  {0}    ",a.izquierda);
               if(a.derecha<10)Console.Write(" |  {0}  ",a.derecha);
               else Console.Write("|  {0}    ",a.derecha);
               Console.WriteLine();
            }
            Console.WriteLine("----------------------");
         }
         else Console.WriteLine("el arbol esat vaci......");
      }
      public  static int insertar (arbol[]a,int numerodato)
      {
         int valor,posicion ,f,n;
         char resp=' ';
         string e="";
         Console.WriteLine();
         Console.WriteLine("\n ------------------------ ");
         Console.WriteLine("| insertar dato| ");
         Console.WriteLine(" - - - - - - - - - - - - - - -  - - - - -\n");
         do
         {
            if (numerodato>=a.Length)
            {
               Console.WriteLine("arbol binario lleno !!!");
               resp ='n';
            }
            else
            {
               numerodato++;
               Console.Write("---- ");
               valor=int.Parse(Console.ReadLine());
               posicion=0;
               f=0;
               do
               {
                  if(valor==0)
                  {
                     Console.WriteLine(" dato no admitido,por favor inserte otro  valor \n");
                     
                  }
                  else
                     if(a[posicion].dato==0)
                  {
                     a[posicion].dato=valor;
                     f=1;
                     if(numerodato!=1)
                     {
                        if(e=="derecha")
                        {
                           n=(posicion-2)/2;
                           a[n].derecha=posicion;
                        }
                        else
                        {
                           n=(posicion-1)/2;
                           a[n].izquierda=posicion;
                        }
                     }
                  }
                  else
                     if(valor<=a[posicion].dato)
                  {
                     posicion=(2*posicion)+1;
                     e="izquierdo";
                  }
                  else
                  {
                     posicion=(2*posicion)+2;
                     e="derecho";
                  }
               }
               while(f==0&&posicion<a.Length);
               if(posicion>=a.Length)Console.WriteLine("la posicion no existe");
               if(numerodato<a.Length)resp=continuar("desea insertar otro valor");
            }
         }
         while (numerodato<a.Length && (resp!='N') && (resp!='n'));
         return numerodato;
      }
      public static  int eliminar(arbol[]a,int numerodato)
      {
         Console.WriteLine();
         int i,elem,f;
         char op=' ';
         if(numerodato!=0)
         {
            
            Console.WriteLine("|eliminar de datos |");
            
            do
            {
               f=0;
               elem =ingresar ("para eliminar");
               for(i=0;i<a.Length;i++)
                  if(elem==a.dato)
                  {
                     if(i!=0)
                     {
                        Console.WriteLine("elemento encontrado posicion: "+i);
                        op=continuar("eliminar dato");
                        if(op=='s'||op=='S')
                        {
                           if(a.derecha==0&&a.izquierda==0)
                           {
                              a.dato=0;
                              if(i%2==0)
                                 a[(i-2)/2].derecha=0;
                              else
                                 a[(i-1)/2].izquierda=0;
                              numerodato--;
                           }
                           else
                              Console.WriteLine("el dato no se puede eliminar por seer padre");
                        }
                     }
                     else
                        Console.WriteLine("el dato no se puede eliminra por que es raiz ");
                     f=1;
                  }
               if(f==0)
                  Console.WriteLine("el dto no fue encontrado");
               op=continuar("eliminar otro dato??");
            }
            while((op=='S' || op=='s')&& numerodato !=0);
         }
         else
            Console.WriteLine("arbol vacio");
         return numerodato;
      }
      public static int ingresar(string msj)
      {
         int busc;
         do
         {
            Console.WriteLine("ingresar dato{0},excepto el 0",msj);
            busc=int.Parse(Console.ReadLine());
            if(busc==0)
               Console.WriteLine(" dato no admitido");
         }
         while(busc==0);
         return busc;
      }
      public  static void buscar(arbol[]a,int numerodato)
      {
         int  i,f=0,buscado;
         char op=' ';
         Console.WriteLine();
         do
         {
            if(numerodato!=0)
            {
               buscado=ingresar ("para buscar");
               for(i=0;i>a.Length;i++)
                  if(buscado==a.dato)
                  {
                     Console.Write("elemento encontrado en la posicion  "+i);
                     f=1;
                     if(a.izquierda !=0)
                  
                        Console.Write(" ,su  hijo izquierdo  es" +a[a.izquierda].dato);
                     
                     if(a.derecha!=0)
                     
                        Console.Write(" ,su  hijo derecho  es" +a[a.derecha].dato);
                  }
               if(f==0)
                  Console.WriteLine("elemento no encontrado");
               op=continuar("buscar otro elemento");
            }
            else
               Console.WriteLine("arbol vacio");
         }
         while(op=='S'|| op=='s');
      }
      public static void altura(arbol[]a,int numerodato)
      {
         int  i,alt=0,ubic=0;
         Console.WriteLine();
         if(numerodato!=0)
         {
            for(i=a.Length-1;i>0;i--)
               if(a.dato!=0)
               {
                  ubic=i;
                  goto salir;
               }
         salir:
            for(i=1;i<Math.Sqrt(a.Length+1);i++)
               if(ubic<=(Math.Pow(2,i)-2))
               {
                  alt=i;
                  goto fuera_de_alcance;
               }
         fuera_de_alcance:
            Console.WriteLine("la altura actual del arbol es:"+alt);
         }
         else
            Console.WriteLine("arbol vacio");
      }
      static void Main(string[] args)
      {
         
            int  i,opc ,nivel=8,numerodato=0;
            char op=' ';
            arbol[] a=new arbol[Convert.ToInt16(Math.Pow(2,nivel)-1)];
            for(i=0;i<a.Length;i++)
               a.dato=a.izquierda=a.derecha=0;
            do
            {

               Console.WriteLine("Menu Principal");
               Console.WriteLine("1.ingresar  dato");
               Console.WriteLine("2.buscar dato");
               Console.WriteLine("3.eliminar dato hijo");
               Console.WriteLine("4.mostrar dato");
               Console.WriteLine("5.calcular altura");
               opc=ingresar(1,5,"la opcion escogida");
               switch(opc)
               {
                  case 1: numdato=insertar (a,numerodato);
               break;
                  case 2:buscar(a,numerodato);
               break;
                  case 3: numerodato=eliminar (a,numdato);
               break;
                  case 4: mostrar(a,numerodato);
               break;
                  case 5: altura (a,numerodato);
               break;
               }
               op=continuar ("desea continuar ");
                     }
            while(op=='s');
         
         
         

         }
      } 
   }   
}
aver si lo complementan 

      
32  Programación / Ejercicios / ejercicio en c#/tipo consola en: 7 Diciembre 2007, 17:41 pm
publico mi tarea de ordenamiento con menu que me tomo un tiempo:::  si lo mejorarian estaria ok.
ojala que les sirva:


********codigo***********

using System;
//using System.Collections.Generic; uso en 2005
using System.Text;

namespace menus_ex
{
    class Program
    {
       
        static void asburbuja(int []le,int t)//burbuja ascendente
        {int temp, nb = 0;
         for (int i = 0; i < t; i++)
         {for (int j = i; j < t; j++)
           {nb++;
            if (le > le[j])
              {temp = le;
               le = le[j];
               le[j] = temp;
              }
            }
          }
        }
        static void desburbuja(int []le,int t)//burbuja descendente
        {int temp, nb = 0;
         for (int i = 0; i < t; i++)
            {for (int j = i; j < t; j++)
             {nb++;
              if (le < le[j])
                {temp = le;
                 le = le[j];
                 le[j] = temp;
                }
              }
           }
        }
        static void selecionas(int []le,int t)//selecion ascendente
        {int menor;
         for (int i = 0; i < t; i++)
           { menor = i;
             for (int j = i + 1; j < t; j++)
             if (le[j] < le[menor])
             menor = j;
             int auxi = le;
             le = le[menor];
             le[menor] = auxi;
           }
        }
        static void seleciondes(int []le,int t)//selecion descendente
        {int menor;
          for (int i = 0; i < t; i++)
           { menor = i;
             for (int j = i + 1; j < t; j++)
             if (le[j] > le[menor])
             menor = j;
             int auxi = le;
             le = le[menor];
             le[menor] = auxi;
           }
        }
        static void insercionas(int []le,int t)//intersecion ascendente
        {int w, ij, auxis;
         bool encontradositio;
         for (w = 1; w < t; w++)
           { auxis = le[w];
             ij = w - 1;
             encontradositio = false;
             while (ij >= 0 && !encontradositio)
             if (le[ij] > auxis)
              { le[ij + 1] = le[ij];
                ij--;
              }
             else
               encontradositio = true;
               le[ij + 1] = auxis;
           }
        }
        static void inserciondes(int []le,int t) //intersecion descendente
        { int w, ij, auxis;
          bool encontradositio;
          for (w = 1; w < t; w++)
            { auxis = le[w];
              ij = w - 1;
              encontradositio = false;
              while (ij >= 0 && !encontradositio)
              if (le[ij] < auxis)
               { le[ij + 1] = le[ij];
                 ij--;
               }
               else
                encontradositio = true;
                le[ij + 1] = auxis;
           }
        }
        static void shell(int []le,int t) //shell ascendente
        { int il, jl, kl, salto;
          salto = t / 2;
          while (salto > 0)
           { for (il = salto; il < t; il++)
              { jl = il - salto;
                while (jl >= 0)
                 { kl = jl + salto;
                   if (le[jl] <= le[kl])
                   jl = -1;
                   else
                    { int auvi = le[jl];
                      le[jl] = le[kl];
                      le[kl] = auvi;
                      jl -= salto;
                     }
                  }
               }
                salto /= 2;
            }
        }
        static void shell1(int []le,int t) //shell descendente
       { int il, jl, kl, salto;
         salto = t / 2;
         while (salto > 0)
          { for (il = salto; il < t; il++)
             {  jl = il - salto;
                while (jl >= 0)
                 {  kl = jl + salto;
                    if (le[jl] >= le[kl])
                    jl = -1;
                    else
                     { int auvi = le[jl];
                       le[jl] = le[kl];
                       le[kl] = auvi;
                       jl -= salto;
                     }

                  }
               }
             salto /= 2;
           }
        }
        public static void quicksort(int[] le, int iz, int de) //quickshort ascendente
        {
            int i = iz;
            int j = de;
            int pivote = le[(iz + de) / 2];
            do
            {
                while (le < pivote)
                {
                    i++;
                }
                while (le[j] > pivote)
                { j--; }
                if (i <= j)
                {
                    int auxi = le;
                    le = le[j];
                    le[j] = auxi;
                    i++;
                    j--;
                }
            } while (i <= j);
            if (j > iz)
                quicksort(le, iz, j);
            if (i < de)
                quicksort(le, i, de);
        }
        public static void quicksort2(int[] le, int iz1, int de1) //quickshort descendente
        {
            int i = iz1;
            int j = de1;
            int pivote = le[(iz1 + de1) / 2];
            do
            {
                while (le > pivote)
                {
                    i++;
                }
                while (le[j] < pivote)
                { j--; }
                if (i <= j)
                {
                    int auxi = le;
                    le = le[j];
                    le[j] = auxi;
                    i++;
                    j--;
                }
            } while (i <= j);
            if (j > iz1)
                quicksort2(le, iz1, j);
            if (i < de1)
                quicksort2(le, i, de1);
        }
        static void recorrer(int[] le,int t)
      {
         for (int i = 0; i < t; i++)
            Console.Write(le + " ");
             Console.WriteLine();
        }
      static void Main(string[] args)
        {   int t;
            Console.WriteLine("--------- MENU ARRAYS---------");
            Console.WriteLine("********llenado******** ");
            Console.WriteLine("ingrese tamaño");
            t = int.Parse(Console.ReadLine());
            int[] le = new int[t];
            for (int i = 0; i < t; i++)
            {
                Console.WriteLine("ingrese [{0}] valor", i + 1);
                le = int.Parse(Console.ReadLine());
            }
            Console.WriteLine("--------- MENU ---------");
            Console.WriteLine("1. burbuja");
            Console.WriteLine("2. seleccion");
            Console.WriteLine("3. insercion");
            Console.WriteLine("4. shell");
            Console.WriteLine("5. quicksort");
            int a = int.Parse(Console.ReadLine());
            switch (a)
            {
             case 1:
                {   Console.WriteLine("ascendente o descendente [a/d] ");
                    string r=Console.ReadLine();
                    if (r == "a")
                    {   asburbuja(le,t);
                        recorrer(le,t);
                    }
                    if (r == "d")
                    {   desburbuja(le, t);
                        recorrer(le, t);
                    }
                 break;
                }
            case 2:
                {
                    Console.WriteLine("ascendente o descendente [a/d] ");
                    string r = Console.ReadLine();
                    if (r == "a")
                    {   selecionas(le, t);
                        recorrer(le, t);
                    }
                    if (r == "d")
                    {   seleciondes(le, t);
                        recorrer(le, t);
                    }
                    break;
                }
            case 3:
                {
                    Console.WriteLine("ascendente o descendente [a/d] ");
                    string r = Console.ReadLine();
                    if (r == "a")
                    {   insercionas(le, t);
                        recorrer(le, t);
                    }
                    if (r == "d")
                    {   inserciondes(le, t);
                        recorrer(le, t);
                    }
                    break;
                }
            case 4:
                {
                    Console.WriteLine("ascendente o descendente [a/d] ");
                    string r = Console.ReadLine();
                    if (r == "a")
                    {   shell(le, t);
                        recorrer(le, t);
                    }
                    if (r == "d")
                    {   shell1(le, t);
                        recorrer(le, t);
                    }
                    break;
                }
            case 5:
                {
                    Console.WriteLine("ascendente o descendente [a/d] ");
                    string r = Console.ReadLine();
                    if (r == "a")
                    {
                        quicksort(le, 0, le.Length - 1);
                        recorrer(le, t);
                    }
                    if (r == "d")
                    {   quicksort2(le, 0, le.Length - 1);
                        recorrer(le, t);
                    }
                    break;
                }
            }
            //fin Main
        }
    }
}
Páginas: 1 2 3 [4]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines