Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: DanielPy en 24 Agosto 2013, 20:44 pm



Título: Arreglos Multidimensionales y ordenamiento
Publicado por: DanielPy en 24 Agosto 2013, 20:44 pm
Hola a todos.
Estoy tratando de aprender un poco más sobre matrices, el caso es que con una dimensión logro hacerlo correctamente pero cuando quiero agregarle otra para insertar los códigos no me sale.-
Otra cosita que me falta es, como debo hacer para que al ordenar por el nombre se corran los códigos como se muestra a continuación?.-
   
    Ingreso por teclado                  Ordenado por nombre

    01 - Daniel Virgili                      06 - Celeste Cid
    02 - Rosita Scardino                  04 - Claudio Raimonda
    03 - Pedro Cimarelli                  01 - Daniel Virgili
    04 - Claudio Raimonda              05 - Pedro Carestia
    05 - Pedro Carestia                   03 - Pedro Cimarelli
    06 - Celeste Cid                        02 - Rosita Scardino
 
Código:
using System;
using System.Text.RegularExpressions;

namespace ArrayDaniel
{
    class AgregarNombres
    {
        public void agrNombres()
        {
            int cant = 0;
            while(true)
            {
                Console.SetCursorPosition(02,02);
                Console.Write("Ingrese la cantidad de personas(máximo 10)...:");
                if(Int32.TryParse(Console.ReadLine(), out cant))
                {
                    if (cant > 0 && cant < 11) { break; }
                }
            }
           
            Regex reg = new Regex("^[A-Za-z ]+$");
            byte _top = 4;
            string[] nombres = new string[cant];
            for(int i = 0; i<cant; i++)
            {
                while(true)
                {
                    Console.SetCursorPosition(02, _top);
                    Console.Write("Ingrese nombre....:");
                    string linea = Console.ReadLine();
                    if (!string.IsNullOrEmpty(linea) && reg.IsMatch(linea))
                    {
                        nombres[i] = linea;
                        break;
                    }
                }
                _top += 2;
            }
           
            Array.Sort(nombres);
            _top += 2;
            Console.SetCursorPosition(02,_top);
            Console.Write(" ----- Se muestran los nombres ordenados ----- ");
            _top += 2;
            int a = 0;
            foreach(String i in nombres)
            {
                Console.SetCursorPosition(02, _top);
                Console.Write("Código...:      Nombres...:{0}", nombres[a]);
                a++;
                _top ++;
            }
        }
    }
}
Espero que no les resulte demasiado tedioso y puedan ayudarme.-

Un gran abrazo para todos.
Daniel


Título: Re: Arreglos Multidimensionales y ordenamiento
Publicado por: DanielPy en 26 Agosto 2013, 19:10 pm
Hola a todos.
Bueno creo haber logrado lo que pretendía, seguramente se puede mejorar pero para mis conocimientos actuales me sirve.-
Dejo el programa porque considero que puede ser de ayuda para algún principiante como en mi caso.-

Código
  1. using System;
  2.  
  3. namespace ArrayDaniel
  4. {
  5.    class Entorno
  6.    {
  7.        public void entorno()
  8.        {
  9.            Console.Title = " Array ordenado";
  10.            Console.WindowHeight = 30;
  11.            Console.WindowWidth  = 70;
  12.            Console.BackgroundColor = ConsoleColor.Blue;
  13.            Console.ForegroundColor = ConsoleColor.Yellow;
  14.            Console.Clear();
  15.        }
  16.    }
  17. }

Código
  1. using System;
  2.  
  3. namespace ArrayDaniel
  4. {
  5.    class ProgramaApp
  6.    {
  7.        static void Main(string[] argumentos)
  8.        {
  9.            Entorno ent = new Entorno();
  10.            ent.entorno();
  11.            AgregarNombres anc = new AgregarNombres();
  12.            anc.agrNombres();
  13.  
  14.            Console.ReadKey();
  15.        }
  16.    }
  17. }

Código
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace ArrayDaniel
  5. {
  6.    class AgregarNombres
  7.    {
  8.        public void agrNombres()
  9.        {
  10.            int cant = 0;
  11.            while(true)
  12.            {
  13.                Console.SetCursorPosition(02,02);
  14.                Console.Write("Ingrese la cantidad de personas(máximo 10)...:");
  15.                if(Int32.TryParse(Console.ReadLine(), out cant))
  16.                {
  17.                    if (cant > 0 && cant < 11) { break; }
  18.                }
  19.            }
  20.  
  21.            Regex reg = new Regex("^[A-Za-z ]+$");
  22.            byte _top = 4;
  23.            string[,] nombres = new string[cant,2];
  24.            string codigo = "";
  25.            byte auxCodigo = 0;
  26.            for(byte i = 0; i<cant; i++)
  27.            {
  28.                auxCodigo++;
  29.                codigo = Convert.ToString(auxCodigo);
  30.                while (true)
  31.                {
  32.                    Console.SetCursorPosition(02, _top);
  33.                    Console.Write("Ingrese nombre....:");
  34.                    string linea = Console.ReadLine();
  35.                    if (!string.IsNullOrEmpty(linea) && reg.IsMatch(linea))
  36.                    {
  37.  
  38.                        nombres[i, 0] = linea;
  39.                        nombres[i, 1] = codigo;
  40.                        break;
  41.                    }
  42.                }
  43.                _top += 2;
  44.            }
  45.            Ordenar OrdenarArray = new Ordenar();
  46.            OrdenarArray.ordenar(nombres);
  47.        }
  48.    }
  49. }

Código
  1. using System;
  2.  
  3. namespace ArrayDaniel
  4. {
  5.    class Ordenar
  6.    {
  7.        public void ordenar(string [,] _array)
  8.        {
  9.            bool huboCambio = true;
  10.            byte _top = 2, col = 0;
  11.            int maxim = _array.Length/2;
  12.            while (huboCambio)
  13.            {
  14.                huboCambio = false;
  15.                for (byte Row = 0; Row < maxim-1; Row++)
  16.                {
  17.                    if (_array[Row, col].CompareTo(_array[Row+1, col]) > 0)
  18.                    {
  19.                        string auxNomb  = _array[Row, col];
  20.                        _array[Row, col] = _array[Row+1, col];
  21.                        _array[Row+1, col] = auxNomb;
  22.                        huboCambio = true;
  23.                        string auxCodi = _array[Row, col+1];
  24.                        _array[Row, col+1] = _array[Row + 1, col+1];
  25.                        _array[Row + 1, col+1] = auxCodi;
  26.                    }
  27.                }
  28.            }
  29.            Console.Clear();
  30.            Console.SetCursorPosition(02, _top);
  31.            Console.Write(" === Ordenado por nombre ===");
  32.            _top +=2;
  33.            Console.SetCursorPosition(02,_top);
  34.            Console.Write(" Código    nombre");
  35.            _top +=2;
  36.  
  37.            for (byte Row = 0; Row < maxim; Row++)
  38.            {
  39.                Console.SetCursorPosition(02, _top);
  40.                Console.WriteLine("      {0}    {01}", _array[Row, 1], _array[Row, 0]);
  41.                _top++;
  42.            }
  43.        }
  44.    }
  45. }

Saludos y hasta la proxima.-
Daniel