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
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
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.-
using System;
namespace ArrayDaniel
{
class Entorno
{
public void entorno()
{
Console.Title = " Array ordenado";
Console.WindowHeight = 30;
Console.WindowWidth = 70;
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Clear();
}
}
}
using System;
namespace ArrayDaniel
{
class ProgramaApp
{
static void Main(string[] argumentos)
{
Entorno ent = new Entorno();
ent.entorno();
AgregarNombres anc = new AgregarNombres();
anc.agrNombres();
Console.ReadKey();
}
}
}
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,2];
string codigo = "";
byte auxCodigo = 0;
for(byte i = 0; i<cant; i++)
{
auxCodigo++;
codigo = Convert.ToString(auxCodigo);
while (true)
{
Console.SetCursorPosition(02, _top);
Console.Write("Ingrese nombre....:");
string linea = Console.ReadLine();
if (!string.IsNullOrEmpty(linea) && reg.IsMatch(linea))
{
nombres[i, 0] = linea;
nombres[i, 1] = codigo;
break;
}
}
_top += 2;
}
Ordenar OrdenarArray = new Ordenar();
OrdenarArray.ordenar(nombres);
}
}
}
using System;
namespace ArrayDaniel
{
class Ordenar
{
public void ordenar(string [,] _array)
{
bool huboCambio = true;
byte _top = 2, col = 0;
int maxim = _array.Length/2;
while (huboCambio)
{
huboCambio = false;
for (byte Row = 0; Row < maxim-1; Row++)
{
if (_array[Row, col].CompareTo(_array[Row+1, col]) > 0)
{
string auxNomb = _array[Row, col];
_array[Row, col] = _array[Row+1, col];
_array[Row+1, col] = auxNomb;
huboCambio = true;
string auxCodi = _array[Row, col+1];
_array[Row, col+1] = _array[Row + 1, col+1];
_array[Row + 1, col+1] = auxCodi;
}
}
}
Console.Clear();
Console.SetCursorPosition(02, _top);
Console.Write(" === Ordenado por nombre ===");
_top +=2;
Console.SetCursorPosition(02,_top);
Console.Write(" Código nombre");
_top +=2;
for (byte Row = 0; Row < maxim; Row++)
{
Console.SetCursorPosition(02, _top);
Console.WriteLine(" {0} {01}", _array[Row, 1], _array[Row, 0]);
_top++;
}
}
}
}
Saludos y hasta la proxima.-
Daniel