miércoles, 12 de octubre de 2011

Programa de Pila Estatica en C#

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

namespace PILA_STATICA
{
class Program
{
static void Main(string[] args)
{
Console.Title = ("Ejemplo de Pilas");
ConsoleKeyInfo x;
Pilas Elementos = new Pilas();
// Entrada Leer = new Entrada();
do
{
Console.Clear();
Console.WriteLine("====================");
Console.WriteLine("Pilas");
Console.WriteLine("Paul Quintana Barrandey");
Console.WriteLine("====================");
if (Elementos.vacia())
{
Console.WriteLine("La Pila esta vacía");
}
else
{
Elementos.consulta();
}

Console.WriteLine("Si Desea Agregar Elementos a la Pila Presione [A] ");
Console.WriteLine("Si Desea Sacar Elementos Presione [S] ");
Console.WriteLine("[ESC]Salir");
x = Console.ReadKey(true);

switch (x.Key)
{
case ConsoleKey.A:
if (!Elementos.llena())
{
Console.WriteLine("Ingrese Elementos: ");
string NC = Console.ReadLine();

Elementos.meter(NC);
}
break;
case ConsoleKey.S:
if (!Elementos.vacia())
Elementos.sacar();
break;
}
} while (x.Key != ConsoleKey.Escape);

}

}
class Pilas
{
int cima;
int max;
string elemento;
string[] arreglo;

public Pilas()
{
cima = -1;
max=4;
arreglo = new string[max + 1];


}
public void meter(string valor)
{
cima = cima + 1;
arreglo[cima] = valor;
}
public void sacar()
{
elemento = arreglo[cima];
cima = cima - 1;

}
public void consulta()
{
if (cima > -1)
for (int i = cima; i >= 0; i--)
Console.WriteLine(arreglo[i]);

}
public bool vacia()
{
if (cima == -1)
return true;
else
return false;
}
public bool llena()
{
if (cima == max)
return true;
else
return false;
}
}



}

No hay comentarios:

Publicar un comentario