You are on page 1of 47

Solucion clase 1: Estructura de vector

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

namespace PruebaVector4
{
class PruebaVector4
{
private int[] vec;

public void Cargar()
{
vec=new int[8];
for(int f = 0; f < 8; f++)
{
Console.Write("Ingrese elemento:");
string linea;
linea = Console.ReadLine();
vec[f]=int.Parse(linea);
}
}

public void AcumularElementos()
{
int suma=0;
for(int f = 0; f < 8; f++)
{
suma=suma+vec[f];
}
Console.WriteLine("La suma de los 8 elementos
es:"+suma);
}

public void AcumularMayores36()
{
int suma=0;
for(int f = 0; f < 8; f++)
{
if (vec[f] > 36)
{
suma=suma+vec[f];
}
}
Console.WriteLine("La suma de los elementos
mayores a 36 es:"+suma);
}

public void CantidadMayores50()
{
int cant=0;
for(int f = 0; f < 8; f++)
{
if (vec[f] > 50)
{
cant++;
}
}
Console.WriteLine("La cantidad de valores mayores
a 50 es:"+cant);
Console.ReadKey();
}

static void Main(string[] args)
{
PruebaVector4 pv = new PruebaVector4();
pv.Cargar();
pv.AcumularElementos();
pv.AcumularMayores36();
pv.CantidadMayores50();
}
}
}





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

namespace PruebaVector5
{
class PruebaVector5
{
private int[] vec1;
private int[] vec2;
private int[] vecSuma;

public void Cargar()
{
vec1=new int[4];
vec2=new int[4];
Console.WriteLine("Carga del primer vector.");
for(int f = 0;f < 4; f++)
{
Console.Write("Ingrese elemento:");
string linea;
linea=Console.ReadLine();
vec1[f]=int.Parse(linea);
}
Console.WriteLine("Carga del segundo vector.");
for(int f = 0; f < 4; f++)
{
Console.Write("Ingrese elemento:");
string linea;
linea = Console.ReadLine();
vec2[f] = int.Parse(linea);
}
}

public void SumarizarVectores()
{
vecSuma=new int[4];
for(int f = 0;f < 4; f++)
{
vecSuma[f]=vec1[f]+vec2[f];
}
Console.WriteLine("Vector resultante.");
for(int f = 0; f < 4; f++)
{
Console.WriteLine(vecSuma[f]);
}
Console.ReadKey();
}

static void Main(string[] args)
{
PruebaVector5 pv = new PruebaVector5();
pv.Cargar();
pv.SumarizarVectores();
}
}
}




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

namespace PruebaVector6
{
class PruebaVector6
{
private int[] cursoa;
private int[] cursob;
private int[] vecSuma;

public void Cargar()
{
cursoa=new int[5];
cursob=new int[5];
Console.WriteLine("Carga de notas del curso A");
for(int f = 0; f < 5; f++)
{
Console.Write("Ingrese nota:");
string linea;
linea = Console.ReadLine();
cursoa[f]=int.Parse(linea);
}
Console.WriteLine("Carga del notas del curso B");
for(int f = 0; f < 5; f++)
{
Console.Write("Ingrese nota:");
string linea;
linea = Console.ReadLine();
cursob[f] =int.Parse(linea);
}
}

public void CalcularPromedios()
{
int suma1=0;
int suma2=0;
for(int f=0;f<5;f++)
{
suma1=suma1+cursoa[f];
suma2=suma2+cursob[f];
}
int promedioa=suma1/5;
int promediob=suma2/5;
if (promedioa>promediob)
{
Console.WriteLine("El curso A tiene un
promedio mayor.");
}
else
{
Console.WriteLine("El curso B tiene un
promedio mayor.");
}
Console.ReadKey();
}

static void Main(string[] args)
{
PruebaVector6 pv = new PruebaVector6();
pv.Cargar();
pv.CalcularPromedios();
}
}
}





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

namespace PruebaVector7
{
class PruebaVector7
{
private int[] vec;

public void Cargar()
{
vec=new int[10];
for(int f = 0; f < 10; f++)
{
Console.Write("Ingrese elemento:");
string linea = Console.ReadLine();
vec[f]=int.Parse(linea);
}
}

public void VerificarOrdenado()
{
int orden=1;
for(int f = 0; f < 9; f++)
{
if (vec[f+1] < vec[f])
{
orden=0;
}
}
if (orden==1)
{
Console.WriteLine("Esta ordenado de menor a
mayor");
}
else
{
Console.WriteLine("No esta ordenado de menor
a mayor");
}
Console.ReadKey();
}

static void Main(string[] args)
{
PruebaVector7 pv = new PruebaVector7();
pv.Cargar();
pv.VerificarOrdenado();
}
}
}

- Vector (Tamao de un vector)

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

namespace PruebaVector9
{
class PruebaVector9
{
private int[] vec;

public void Cargar()
{
Console.Write("Cuantos elementos tiene el vector:");
string linea=Console.ReadLine();
int n;
n=int.Parse(linea);
vec=new int[n];
for(int f = 0; f < vec.Length; f++)
{
Console.Write("Ingrese elemento:");
linea=Console.ReadLine();
vec[f]=int.Parse(linea);
}
}

public void AcumularElementos()
{
int suma=0;
for(int f = 0; f < vec.Length; f++)
{
suma=suma+vec[f];
}
Console.WriteLine("La suma de los elementos
es:"+suma);
Console.ReadKey();
}

static void Main(string[] args)
{
PruebaVector9 pv = new PruebaVector9();
pv.Cargar();
pv.AcumularElementos();
}
}
}


- Vectores (mayor y menor elemento)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PruebaVector12
{
class PruebaVector12
{
private int[] vec;
private int menor;

public void Cargar()
{
Console.Write("Cuantos elementos desea cargar:");
string linea;
linea = Console.ReadLine();
int n=int.Parse(linea);
vec=new int[n];
for(int f=0;f < vec.Length;f++)
{
Console.Write("Ingrese componente:");
linea = Console.ReadLine();
vec[f]=int.Parse(linea);
}
}

public void MenorElemento()
{
menor=vec[0];
for(int f=1;f < vec.Length;f++)
{
if (vec[f] < menor)
{
menor=vec[f];
}
}
Console.WriteLine("El elemento menor es:"+menor);
}

public void RepiteMenor()
{
int cant=0;
for(int f=0;f < vec.Length;f++)
{
if (vec[f]==menor)
{
cant++;
}
}
if (cant > 1)
{
Console.WriteLine("Se repite el menor en el
vector.");
}
else
{
Console.WriteLine("No se repite el menor en
el vector.");
}
Console.ReadLine();
}

static void Main(string[] args)
{
PruebaVector12 pv = new PruebaVector12();
pv.Cargar();
pv.MenorElemento();
pv.RepiteMenor();
}
}
}

- Vectores (ordenamiento)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PruebaVector15
{
class PruebaVector15
{
private int[] vec;

public void Cargar()
{
Console.Write("Cuantos elementos tendr el
vector:");
string linea;
linea = Console.ReadLine();
int cant;
cant=int.Parse(linea);
vec=new int[cant];
for(int f=0;f < vec.Length;f++)
{
Console.Write("Ingrese elemento:");
linea = Console.ReadLine();
vec[f]=int.Parse(linea);
}
}

public void Ordenar()
{
for (int k = 0; k < vec.Length; k++)
{
for (int f = 0; f < vec.Length - 1 - k; f++)
{
if (vec[f] > vec[f + 1])
{
int aux;
aux = vec[f];
vec[f] = vec[f + 1];
vec[f + 1] = aux;
}
}
}
}

public void Imprimir()
{
Console.WriteLine("Vector ordenados de menor a
mayor.");
for(int f=0;f < vec.Length;f++)
{
Console.WriteLine(vec[f]);
}
Console.ReadKey();
}


static void Main(string[] args)
{
PruebaVector15 pv = new PruebaVector15();
pv.Cargar();
pv.Ordenar();
pv.Imprimir();
}
}
}
- Vectores (ordenamiento con
vectores paralelos)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PruebaVector17
{
class PruebaVector17
{
private string[] paises;
private int[] habitantes;

public void Cargar()
{
paises=new string[5];
habitantes=new int[5];
Console.WriteLine("Carga de paises y
habitantes");
for(int f=0;f < paises.Length;f++)
{
Console.Write("Ingese el nombre del pais:");
paises[f]=Console.ReadLine();
Console.Write("Ingrese la cantidad de
habitantes:");
string linea;
linea = Console.ReadLine();
habitantes[f]=int.Parse(linea);
}
}

public void OrdenarPorNombres()
{
for (int k = 0; k < paises.Length; k++)
{
for (int f = 0; f < paises.Length - 1 - k;
f++)
{
if (paises[f].CompareTo(paises[f + 1]) >
0)
{
string auxpais;
auxpais = paises[f];
paises[f] = paises[f + 1];
paises[f + 1] = auxpais;
int auxhabitante;
auxhabitante = habitantes[f];
habitantes[f] = habitantes[f + 1];
habitantes[f + 1] = auxhabitante;
}
}
}
}

public void OrdenarPorHabitantes()
{
for (int k = 0; k < paises.Length; k++)
{
for (int f = 0; f < paises.Length - 1 - k;
f++)
{
if (habitantes[f] < habitantes[f + 1])
{
string auxpais;
auxpais = paises[f];
paises[f] = paises[f + 1];
paises[f + 1] = auxpais;
int auxhabitante;
auxhabitante = habitantes[f];
habitantes[f] = habitantes[f + 1];
habitantes[f + 1] = auxhabitante;
}
}
}
}

public void Imprimir()
{
for(int f=0;f < paises.Length;f++)
{
Console.WriteLine(paises[f] + " - " +
habitantes[f]);
}
}

static void Main(string[] args)
{
PruebaVector17 pv = new PruebaVector17();
pv.Cargar();
pv.OrdenarPorNombres();
Console.WriteLine("Ordenados alfabticamente");
pv.Imprimir();
pv.OrdenarPorHabitantes();
Console.WriteLine("Ordenados por cantidad de
habitnates");
pv.Imprimir();
Console.ReadKey();
}
}
}


- Estructura de datos tipo matriz
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Matriz4
{
class Matriz4
{
private int[,] mat;

public void Cargar()
{
mat=new int[2,5];
Console.WriteLine("Carga de la matriz por
columna:");
for(int c = 0; c < 5; c++)
{
for(int f = 0; f < 2; f++)
{
Console.Write("Ingrese componente " + "
de la fila " + f + " y la columna "+ c + " :");
string linea;
linea = Console.ReadLine();
mat[f,c]=int.Parse(linea);
}
}
}

public void Imprimir()
{
for(int f = 0; f < 2; f++)
{
for(int c = 0; c < 5; c++)
{
Console.Write(mat[f,c]+" ");
}
Console.WriteLine();
}
Console.ReadKey();
}

static void Main(string[] args)
{
Matriz4 ma = new Matriz4();
ma.Cargar();
ma.Imprimir();
}
}
}
- Matrices (cantidad de filas y columnas)

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

namespace Matriz7
{
class Matriz7
{
private int[,] mat;

public void Cargar()
{
Console.Write("Cuantas fila tiene la matriz:");
string linea;
linea=Console.ReadLine();
int filas=int.Parse(linea);
Console.Write("Cuantas columnas tiene la
matriz:");
linea=Console.ReadLine();
int columnas=int.Parse(linea);
mat=new int[filas,columnas];
for(int f = 0; f < mat.GetLength(0); f++)
{
for(int c = 0;c < mat.GetLength(1); c++)
{
Console.Write("Ingrese componente:");
linea = Console.ReadLine();
mat[f,c]=int.Parse(linea);
}
}
}

public void Intercambiar()
{
for (int c = 0; c < mat.GetLength(0); c++)
{
int aux = mat[0,c];
mat[0,c] = mat[1,c];
mat[1,c] = aux;
}
}

public void Imprimir()
{
for(int f = 0; f < mat.GetLength(0); f++)
{
for(int c = 0; c < mat.GetLength(1); c++)
{
Console.Write(mat[f,c]+" ");
}
Console.WriteLine();
}
Console.ReadKey();
}

static void Main(string[] args)
{
Matriz7 ma = new Matriz7();
ma.Cargar();
ma.Intercambiar();
ma.Imprimir();
}
}
}




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

namespace Matriz8
{
class Matriz8
{
private int[,] mat;

public void Cargar()
{
Console.Write("Cuantas fila tiene la matriz:");
string linea;
linea=Console.ReadLine();
int filas=int.Parse(linea);
Console.Write("Cuantas columnas tiene la
matriz:");
linea=Console.ReadLine();
int columnas=int.Parse(linea);
mat=new int[filas,columnas];
for(int f = 0; f < mat.GetLength(0); f++)
{
for(int c = 0; c < mat.GetLength(1); c++)
{
Console.Write("Ingrese componente:");
linea = Console.ReadLine();
mat[f,c]=int.Parse(linea);
}
}
}

public void ImprimirVertices()
{
Console.WriteLine("Vrtice superior izquierdo:");
Console.WriteLine(mat[0,0]);
Console.WriteLine("Vrtice superior derecho:");
Console.WriteLine(mat[0,mat.GetLength(1)-1]);
Console.WriteLine("Vrtice inferior izquierdo:");
Console.WriteLine(mat[mat.GetLength(0)-1,0]);
Console.WriteLine("Vrtice inferior derecho:");
Console.WriteLine(mat[mat.GetLength(0)-
1,mat.GetLength(1)-1]);
Console.ReadKey();
}

static void Main(string[] args)
{
Matriz8 ma = new Matriz8();
ma.Cargar();
ma.ImprimirVertices();
}
}
}
- Matrices y vectores paralelos
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Matriz10
{
class Matriz10
{
private string[] paises;
private int[,] tempmen;
private int[] temptri;

public void Cargar()
{
paises=new String[4];
tempmen=new int[4,3];
for(int f = 0; f < paises.Length; f++)
{
Console.Write("Ingrese el nombre del pas:");
paises[f]=Console.ReadLine();
for(int c = 0; c < tempmen.GetLength(1); c++)
{
Console.Write("Ingrese temperatura
mensual:");
string linea = Console.ReadLine();
tempmen[f,c]=int.Parse(linea);
}
}
}

public void ImprimirTempMensuales()
{
for(int f = 0; f < paises.Length; f++)
{
Console.Write("Pais:" + paises[f]+":");
for(int c = 0; c < tempmen.GetLength(1); c++)
{
Console.Write(tempmen[f,c]+" ");
}
Console.WriteLine();
}
}

public void CalcularTemperaturaTri()
{
temptri = new int[4];
for (int f = 0; f < tempmen.GetLength(0); f++)
{
int suma = 0;
for (int c = 0; c < tempmen.GetLength(1);
c++)
{
suma = suma + tempmen[f,c];
}
temptri[f] = suma / 3;
}
}

public void ImprimirTempTrimestrales()
{
Console.WriteLine("Temperaturas trimestrales.");
for(int f = 0; f < paises.Length; f++)
{
Console.WriteLine(paises[f]+" "+temptri[f]);
}
}

public void PaisMayorTemperaturaTri()
{
int may=temptri[0];
string nom=paises[0];
for(int f = 0; f < paises.Length; f++)
{
if (temptri[f] > may)
{
may=temptri[f];
nom=paises[f];
}
}
Console.WriteLine("Pais con temperatura
trimestral mayor es "+ nom + " que tiene una temperatura de
"+may);
}

static void Main(string[] args)
{
Matriz10 ma = new Matriz10();
ma.Cargar();
ma.ImprimirTempMensuales();
ma.CalcularTemperaturaTri();
ma.ImprimirTempTrimestrales();
ma.PaisMayorTemperaturaTri();
Console.ReadKey();
}
}
}
- Matrices irregulares o dentadas
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatrizIrregular2
{
class MatrizIrregular2
{
private int[][] mat;

public void Cargar()
{
mat=new int[5][];
for(int f = 0; f < mat.Length; f++)
{
mat[f]=new int[f+1];
for(int c = 0; c < mat[f].Length; c++)
{
Console.Write("Ingrese componente:");
string linea = Console.ReadLine();
mat[f][c]=int.Parse(linea);
}
}
}

public void Imprimir()
{
for(int f = 0; f < mat.Length; f++)
{
for(int c = 0; c < mat[f].Length; c++)
{
Console.Write(mat[f][c]+" ");
}
Console.WriteLine();
}
Console.ReadKey();
}

static void Main(string[] args)
{
MatrizIrregular2 ma = new MatrizIrregular2();
ma.Cargar();
ma.Imprimir();
}
}
}




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

namespace MatrizIrregular3
{
class MatrizIrregular3
{
private string[] nombres;
private int[][] dias;

public void Cargar()
{
nombres=new string[3];
dias=new int[3][];
for(int f = 0; f < nombres.Length; f++)
{
Console.Write("Ingrese el nombre del
empleado:");
nombres[f]=Console.ReadLine();
Console.Write("Cuantas das falt el
empleado:");
string linea = Console.ReadLine();
int faltas=int.Parse(linea);
dias[f]=new int[faltas];
for(int c = 0; c < dias[f].Length; c++)
{
Console.Write("Ingrese nro de da:");
linea = Console.ReadLine();
dias[f][c]=int.Parse(linea);
}
}
}

public void Inasistencias()
{
for(int f = 0; f < nombres.Length; f++)
{
Console.WriteLine(nombres[f] + " falt " +
dias[f].Length + " das");
}
}

public void EmpleadoMensosFaltas()
{
int faltas=dias[0].Length;
string nom=nombres[0];
for(int f = 1; f < dias.Length; f++)
{
if (dias[f].Length < faltas)
{
faltas=dias[f].Length;
nom=nombres[f];
}
}
Console.WriteLine("El empleado que falt menos es
"+nom+" con "+faltas+" faltas.");
Console.ReadKey();
}

static void Main(string[] args)
{
MatrizIrregular3 ma = new MatrizIrregular3();
ma.Cargar();
ma.Inasistencias();
ma.EmpleadoMensosFaltas();
}
}
}
- Estructuras dinmicas: Listas tipo
Pila
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ListasTipoPila1
{
class Pila
{
class Nodo
{
public int info;
public Nodo sig;
}

private Nodo raiz;

public Pila()
{
raiz = null;
}

public void Insertar(int x)
{
Nodo nuevo;
nuevo = new Nodo();
nuevo.info = x;
if (raiz == null)
{
nuevo.sig = null;
raiz = nuevo;
}
else
{
nuevo.sig = raiz;
raiz = nuevo;
}
}

public int Extraer()
{
if (raiz != null)
{
int informacion = raiz.info;
raiz = raiz.sig;
return informacion;
}
else
{
return int.MaxValue;
}
}

public void Imprimir()
{
Nodo reco=raiz;
Console.WriteLine("Listado de todos los elementos
de la pila.");
while (reco!=null)
{
Console.Write(reco.info+"-");
reco=reco.sig;
}
Console.WriteLine();
}

public bool Vacia()
{
if (raiz == null)
{
return true;
}
else
{
return false;
}
}

public int Cantidad()
{
int cant = 0;
Nodo reco = raiz;
while (reco != null)
{
cant++;
reco = reco.sig;
}
return cant;
}

public int Retornar()
{
if (raiz != null)
{
int informacion = raiz.info;
return informacion;
}
else
{
return int.MaxValue;
}
}


static void Main(string[] args)
{
Pila pila1=new Pila();
pila1.Insertar(10);
pila1.Insertar(40);
pila1.Insertar(3);
pila1.Imprimir();
Console.WriteLine("Extraemos de la
pila:"+pila1.Extraer());
pila1.Imprimir();
Console.WriteLine("Retornamos primero de la
pila:"+pila1.Retornar());
pila1.Imprimir();
Console.ReadKey();
}
}
}
- Estructuras dinmicas: Listas tipo
Cola - Problemas de aplicacin
Archivo: Cola.cs

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

namespace ListaTipoCola3
{
class Cola
{
class Nodo
{
public int info;
public Nodo sig;
}

private Nodo raiz, fondo;

public Cola()
{
raiz = null;
fondo = null;
}

public bool Vacia()
{
if (raiz == null)
return true;
else
return false;
}

public void Insertar(int info)
{
Nodo nuevo;
nuevo = new Nodo();
nuevo.info = info;
nuevo.sig = null;
if (Vacia())
{
raiz = nuevo;
fondo = nuevo;
}
else
{
fondo.sig = nuevo;
fondo = nuevo;
}
}

public int Extraer()
{
if (!Vacia())
{
int informacion = raiz.info;
if (raiz == fondo)
{
raiz = null;
fondo = null;
}
else
{
raiz = raiz.sig;
}
return informacion;
}
else
return int.MaxValue;
}

public int Cantidad()
{
int cant = 0;
Nodo reco = raiz;
while (reco != null)
{
cant++;
reco = reco.sig;
}
return cant;
}
}
}


Archivo: Form1.cs

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 ListaTipoCola3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs
e)
{
Random ale = new Random();
int estado1 = 0, estado2 = 0, estado3 = 0;
int marchan = 0;
int llegada = 2 + ale.Next(0, 2);
int salida1 = -1, salida2 = -1, salida3 = -1;
int cantAte1 = 0, cantAte2 = 0, cantAte3 = 0;
int tiempoEnCola = 0;
int cantidadEnCola = 0;
Cola cola1 = new Cola();
Cola cola2 = new Cola();
Cola cola3 = new Cola();
for (int minuto = 0; minuto < 600; minuto++)
{
if (llegada == minuto)
{
if (estado1 == 0)
{
estado1 = 1;
salida1 = minuto + 7 + ale.Next(0,
5);
}
else
{
if (estado2 == 0)
{
estado2 = 1;
salida2 = minuto + 7 +
ale.Next(0, 5);
}
else
{
if (estado3 == 0)
{
estado3 = 1;
salida3 = minuto + 7 +
ale.Next(0, 5);
}
else
{
if (cola1.Cantidad() == 6 &&
cola2.Cantidad() == 6 && cola3.Cantidad() == 6)
{
marchan++;
}
else
{
if (cola1.Cantidad() <=
cola2.Cantidad() && cola1.Cantidad() <= cola3.Cantidad())
{

cola1.Insertar(minuto);
}
else
{
if (cola2.Cantidad()
<= cola3.Cantidad())
{

cola2.Insertar(minuto);
}
else
{

cola3.Insertar(minuto);
}
}
}
}
}

}
llegada = minuto + 2 + ale.Next(0, 2);
}
if (salida1 == minuto)
{
cantAte1++;
estado1 = 0;
if (!cola1.Vacia())
{
estado1 = 1;
int m = cola1.Extraer();
salida1 = minuto + 7 + ale.Next(0,
5);
tiempoEnCola = tiempoEnCola + (minuto
- m);
cantidadEnCola++;
}
}
if (salida2 == minuto)
{
cantAte2++;
estado2 = 0;
if (!cola2.Vacia())
{
estado2 = 1;
int m = cola2.Extraer();
salida2 = minuto + 7 + ale.Next(0,
5);
tiempoEnCola = tiempoEnCola + (minuto
- m);
cantidadEnCola++;
}
}
if (salida3 == minuto)
{
cantAte3++;
estado3 = 0;
if (!cola3.Vacia())
{
estado3 = 1;
int m = cola3.Extraer();
salida3 = minuto + 7 + ale.Next(0,
5);
tiempoEnCola = tiempoEnCola + (minuto
- m);
cantidadEnCola++;
}
}
}
label1.Text="Clientes atendidos por caja: caja1="
+ cantAte1.ToString() + " caja2=" + cantAte2.ToString() + "
caja3=" + cantAte3.ToString();
label2.Text="Se marchan sin hacer compras:" +
marchan.ToString();
if (cantidadEnCola > 0)
{
int tiempoPromedio = tiempoEnCola /
cantidadEnCola;
label3.Text="Tiempo promedio en cola:" +
tiempoPromedio.ToString();
}
}
}
}
- Estructuras dinmicas: Listas
genricas
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ListaGenerica2
{
class ListaGenerica
{
class Nodo
{
public int info;
public Nodo sig;
}

private Nodo raiz;

public ListaGenerica()
{
raiz = null;
}

void InsertarPrimero(int x)
{
Nodo nuevo = new Nodo();
nuevo.info = x;
nuevo.sig = raiz;
raiz = nuevo;
}

public void InsertarUtlimo(int x)
{
Nodo nuevo = new Nodo();
nuevo.info = x;
if (raiz == null)
raiz = nuevo;
else
{
Nodo reco = raiz;
while (reco.sig != null)
{
reco = reco.sig;
}
reco.sig = nuevo;
}
}

public void InsertarSegundo(int x)
{
if (raiz != null)
{
Nodo nuevo = new Nodo();
nuevo.info = x;
if (raiz.sig == null)
{
//Hay un solo nodo.
raiz.sig = nuevo;
}
else
{
nuevo.sig = raiz.sig;
raiz.sig = nuevo;
}
}
}

public void InsertarAnteUltimo(int x)
{
if (raiz != null)
{
Nodo nuevo = new Nodo();
nuevo.info = x;
if (raiz.sig == null)
{
//Hay un solo nodo.
nuevo.sig = raiz;
raiz = nuevo;
}
else
{
Nodo atras = raiz;
Nodo reco = raiz.sig;
while (reco.sig != null)
{
atras = reco;
reco = reco.sig;
}
nuevo.sig = atras.sig;
atras.sig = nuevo;
}
}
}

public void BorrarPrimero()
{
if (raiz != null)
{
raiz = raiz.sig;
}
}

public void BorrarSegundo()
{
if (raiz != null)
{
if (raiz.sig != null)
{
Nodo tercero = raiz.sig;
tercero = tercero.sig;
raiz.sig = tercero;
}
}
}

public void BorrarUltimo()
{
if (raiz != null)
{
if (raiz.sig == null)
{
raiz = null;
}
else
{
Nodo reco = raiz.sig;
Nodo atras = reco;
while (reco.sig != null)
{
atras = reco;
reco = reco.sig;
}
atras.sig = null;
}
}

}
public void Imprimir()
{
Nodo reco = raiz;
while (reco != null)
{
Console.Write (reco.info + "-");
reco = reco.sig;
}
Console.WriteLine();
}

public void BorrarMayor()
{
if (raiz != null)
{
Nodo reco = raiz;
int may = raiz.info;
while (reco != null)
{
if (reco.info > may)
{
may = reco.info;
}
reco = reco.sig;
}
reco = raiz;
Nodo atras = raiz;
while (reco != null)
{
if (reco.info == may)
{
if (reco == raiz)
{
raiz = raiz.sig;
atras = raiz;
reco = raiz;
}
else
{
atras.sig = reco.sig;
reco = reco.sig;
}
}
else
{
atras = reco;
reco = reco.sig;
}
}
}
}


static void Main(string[] args)
{
ListaGenerica lg=new ListaGenerica();
lg.InsertarPrimero (10);
lg.InsertarPrimero(45);
lg.InsertarPrimero(23);
lg.InsertarPrimero(89);
lg.Imprimir();
Console.WriteLine("Insertamos un nodo al
final:");
lg.InsertarUtlimo(160);
lg.Imprimir();
Console.WriteLine("Insertamos un nodo en la
segunda posicin:");
lg.InsertarSegundo(13);
lg.Imprimir();
Console.WriteLine("Insertamos un nodo en la
anteultima posicin:");
lg.InsertarAnteUltimo(600);
lg.Imprimir();
Console.WriteLine("Borramos el primer nodo de la
lista:");
lg.BorrarPrimero();
lg.Imprimir();
Console.WriteLine("Borramos el segundo nodo de la
lista:");
lg.BorrarSegundo();
lg.Imprimir();
Console.WriteLine("Borramos el ultimo nodo de la
lista:");
lg.BorrarUltimo();
lg.Imprimir();
Console.WriteLine("Borramos el mayor de la
lista:");
lg.BorrarMayor();
lg.Imprimir();
Console.ReadKey();
}
}
}
- Estructuras dinmicas: Listas
genricas doblemente encadenadas
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ListaGenericaDoble2
{
class ListaGenericaDoble
{
class Nodo
{
public int info;
public Nodo ant, sig;
}

private Nodo raiz;

public ListaGenericaDoble()
{
raiz = null;
}

void InsertarPrimero(int x)
{
Nodo nuevo = new Nodo();
nuevo.info = x;
nuevo.sig = raiz;
if (raiz != null)
raiz.ant = nuevo;
raiz = nuevo;
}

public void InsertarUtlimo(int x)
{
Nodo nuevo = new Nodo();
nuevo.info = x;
if (raiz == null)
raiz = nuevo;
else
{
Nodo reco = raiz;
while (reco.sig != null)
{
reco = reco.sig;
}
reco.sig = nuevo;
nuevo.ant = reco;
}
}

public void InsertarSegundo(int x)
{
if (raiz != null)
{
Nodo nuevo = new Nodo();
nuevo.info = x;
if (raiz.sig == null)
{
//Hay un solo nodo.
raiz.sig = nuevo;
nuevo.ant = raiz;
}
else
{
Nodo tercero = raiz.sig;
nuevo.sig = tercero;
tercero.ant = nuevo;
raiz.sig = nuevo;
nuevo.ant = raiz;
}
}
}

public void InsertarAnteUltimo(int x)
{
if (raiz != null)
{
Nodo nuevo = new Nodo();
nuevo.info = x;
if (raiz.sig == null)
{
//Hay un solo nodo.
nuevo.sig = raiz;
raiz = nuevo;
}
else
{
Nodo reco = raiz;
while (reco.sig != null)
{
reco = reco.sig;
}
Nodo anterior = reco.ant;
nuevo.sig = reco;
nuevo.ant = anterior;
anterior.sig = nuevo;
reco.ant = nuevo;
}
}
}

public void BorrarPrimero()
{
if (raiz != null)
{
raiz = raiz.sig;
}
}

public void BorrarSegundo()
{
if (raiz != null)
{
if (raiz.sig != null)
{
Nodo tercero = raiz.sig;
tercero = tercero.sig;
raiz.sig = tercero;
if (tercero != null)
tercero.ant = raiz;
}
}
}

public void BorrarUltimo()
{
if (raiz != null)
{
if (raiz.sig == null)
{
raiz = null;
}
else
{
Nodo reco = raiz;
while (reco.sig != null)
{
reco = reco.sig;
}
reco = reco.ant;
reco.sig = null;
}
}

}
public void Imprimir()
{
Nodo reco = raiz;
while (reco != null)
{
Console.Write(reco.info + "-");
reco = reco.sig;
}
Console.WriteLine();
}

public void BorrarMayor()
{
if (raiz != null)
{
Nodo reco = raiz;
int may = raiz.info;
while (reco != null)
{
if (reco.info > may)
{
may = reco.info;
}
reco = reco.sig;
}
reco = raiz;
while (reco != null)
{
if (reco.info == may)
{
if (reco == raiz)
{
raiz = raiz.sig;
if (raiz != null)
raiz.ant = null;
reco = raiz;
}
else
{
Nodo atras = reco.ant;
atras.sig = reco.sig;
reco = reco.sig;
if (reco != null)
reco.ant = atras;
}
}
else
{
reco = reco.sig;
}
}
}
}


static void Main(string[] args)
{
ListaGenericaDoble lg=new ListaGenericaDoble();
lg.InsertarPrimero (10);
lg.InsertarPrimero(45);
lg.InsertarPrimero(23);
lg.InsertarPrimero(89);
lg.Imprimir();
Console.WriteLine("Insertamos un nodo al
final:");
lg.InsertarUtlimo(160);
lg.Imprimir();
Console.WriteLine("Insertamos un nodo en la
segunda posicin:");
lg.InsertarSegundo(13);
lg.Imprimir();
Console.WriteLine("Insertamos un nodo en la
anteultima posicin:");
lg.InsertarAnteUltimo(600);
lg.Imprimir();
Console.WriteLine("Borramos el primer nodo de la
lista:");
lg.BorrarPrimero();
lg.Imprimir();
Console.WriteLine("Borramos el segundo nodo de la
lista:");
lg.BorrarSegundo();
lg.Imprimir();
Console.WriteLine("Borramos el ultimo nodo de la
lista:");
lg.BorrarUltimo();
lg.Imprimir();
Console.WriteLine("Borramos el mayor de la
lista:");
lg.BorrarMayor();
lg.Imprimir();
Console.ReadKey();
}
}
}
- Recursividad: Problemas donde
conviene aplicar la recursividad
archivo: Form1.Designer.cs


namespace Buscaminas
{
partial class Form1
{
///
/// Variable del diseador requerida.
///
private System.ComponentModel.IContainer components =
null;

///
/// Limpiar los recursos que se estn utilizando.
///
/// true si los recursos administrados se deben
eliminar; false en caso contrario, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Cdigo generado por el Diseador de Windows
Forms

///
/// Mtodo necesario para admitir el Diseador. No se
puede modificar
/// el contenido del mtodo con el editor de cdigo.
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new
System.Drawing.Point(13, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75,
23);
this.button1.TabIndex = 0;
this.button1.Text = "Reiniciar";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new
System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(496,
448);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new
System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
}
}



archivo: Form1.cs


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 Buscaminas
{
public partial class Form1 : Form
{
private Button[,] mat;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
int x = 10;
int y = 50;
mat = new Button[10, 10];
for (int fil = 0; fil < mat.GetLength(0); fil++)
{
for (int col = 0; col < mat.GetLength(1);
col++)
{
mat[fil, col] = new Button();
mat[fil, col].Text = "0";
mat[fil, col].Location = new Point(x, y);
mat[fil, col].Size = new Size(30, 30);
mat[fil, col].Click += Presionado;
Controls.Add(mat[fil, col]);
x = x + 30;
}
y = y + 30;
x = 10;
}
Reiniciar();
}

void Reiniciar()
{
Text="";
for (int f = 0; f < 10; f++)
{
for (int c = 0; c < 10; c++)
{
mat[f,c].Text="0";
mat[f,c].Enabled = true; ;
mat[f,c].ForeColor=Color.LightGray;
mat[f,c].BackColor=Color.LightGray;
}
}
DisponerBombas();
ContarBombasPerimetro();
}

void DisponerBombas()
{
int cantidad = 10;
Random ale = new Random();
do
{
int fila = ale.Next(0, 10);
int columna = ale.Next(0, 10);
if (mat[fila,columna].Text!="b")
{
mat[fila,columna].Text="b";
cantidad--;
}
}
while (cantidad != 0);
}

void ContarBombasPerimetro()
{
for (int f = 0; f < 10; f++)
{
for (int c = 0; c < 10; c++)
{
if (mat[f,c].Text=="0")
{
int cant = ContarCoordenada(f, c);
mat[f,c].Text=cant.ToString();
}
}
}
}

int ContarCoordenada(int fila, int columna)
{
int total = 0;
if (fila - 1 >= 0 && columna - 1 >= 0)
{
if (mat[fila - 1,columna - 1].Text=="b")
total++;
}
if (fila - 1 >= 0)
{
if (mat[fila - 1,columna].Text=="b")
total++;
}
if (fila - 1 >= 0 && columna + 1 < 10)
{
if (mat[fila - 1,columna + 1].Text=="b")
total++;
}

if (columna + 1 < 10)
{
if (mat[fila,columna + 1].Text=="b")
total++;
}
if (fila + 1 < 10 && columna + 1 < 10)
{
if (mat[fila + 1,columna + 1].Text=="b")
total++;
}
if (fila + 1 < 10)
{
if (mat[fila + 1,columna].Text=="b")
total++;
}
if (fila + 1 < 10 && columna - 1 >= 0)
{
if (mat[fila + 1,columna - 1].Text=="b")
total++;
}
if (columna - 1 >= 0)
{
if (mat[fila,columna - 1].Text=="b")
total++;
}
return total;
}

private void button1_Click(object sender, EventArgs
e)
{
Reiniciar();
}

private void Presionado(object sender, EventArgs e)
{
for (int f = 0; f < 10; f++)
{
for (int c = 0; c < 10; c++)
{
if (sender == mat[f,c])
{
if (mat[f,c].Text=="b")
{
Text="Boooooooooooooomm";
DesactivarJuego();
}
else
if (mat[f,c].Text=="0")
{
Recorrer(f, c);
}
else
if (mat[f,c].Text=="1" ||
mat[f,c].Text=="2" ||
mat[f,c].Text=="3" ||
mat[f,c].Text=="4" ||
mat[f,c].Text=="5" ||
mat[f,c].Text=="6" ||
mat[f,c].Text=="7" ||
mat[f,c].Text=="8")
{

mat[f,c].BackColor=Color.Yellow;

mat[f,c].ForeColor=Color.Black;
}
}
}
}
VerificarTriunfo();
}

void DesactivarJuego()
{
for (int f = 0; f < 10; f++)
{
for (int c = 0; c < 10; c++)
{
mat[f,c].Enabled=false;
}
}
}

void VerificarTriunfo()
{
int cant = 0;
for (int f = 0; f < 10; f++)
{
for (int c = 0; c < 10; c++)
{
Color col = mat[f,c].BackColor;
if (col == Color.Yellow)
cant++;
}
}
if (cant == 90)
{
Text="Ganooooooooo";
DesactivarJuego();
}
}

void Recorrer(int fil, int col)
{
if (fil >= 0 && fil < 10 && col >= 0 && col < 10)
{
if (mat[fil,col].Text=="0")
{
mat[fil,col].Text=" ";
mat[fil,col].BackColor=Color.Yellow;
Recorrer(fil, col + 1);
Recorrer(fil, col - 1);
Recorrer(fil + 1, col);
Recorrer(fil - 1, col);
Recorrer(fil - 1, col - 1);
Recorrer(fil - 1, col + 1);
Recorrer(fil + 1, col + 1);
Recorrer(fil + 1, col - 1);
}
else
if (mat[fil,col].Text=="1" ||
mat[fil,col].Text == "2" ||
mat[fil,col].Text == "3" ||
mat[fil,col].Text == "4" ||
mat[fil,col].Text == "5" ||
mat[fil,col].Text == "6" ||
mat[fil,col].Text == "7" ||
mat[fil,col].Text == "8")
{
mat[fil,col].BackColor=Color.Yellow;
mat[fil,col].ForeColor=Color.Black;
}
}
}
}
}

You might also like