Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Collections;
- namespace TP2
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Cantidad de elementos en cada pila?: ");
- int n = Convert.ToInt32(Console.ReadLine());
- Stack pila1 = new Stack(n);
- Stack pila2 = new Stack(n);
- Stack aux1 = new Stack(n);
- Stack aux2 = new Stack(n);
- Console.Clear();
- CargarPilas(pila1,pila2,n);
- clonar(pila1, aux1, n);
- clonar(pila2, aux2, n);
- Console.Clear();
- MostrarPilas(pila1, pila2, n);
- switch (comparar(aux1, aux2))
- {
- case -1: Console.WriteLine("La pila 1 es menor a la pila 2");
- break;
- case 1: Console.WriteLine("La pila 1 es mayor a la pila 2");
- break;
- case 0: Console.WriteLine("Las pilas son iguales");
- break;
- default: Console.Write("");
- break;
- }
- Console.ReadKey();
- }
- static void CargarPilas(Stack pila1,Stack pila2,int n)
- {
- Console.WriteLine("INSERTE NUMEROS EN LAS PILAS");
- Console.WriteLine("****PILA 1****");
- for(int i=1; i<=n;i++)
- {
- Console.Write("NΒ°{0}: ",i);
- int valor = Convert.ToInt32(Console.ReadLine());
- pila1.Push(valor);
- }
- Console.WriteLine("****PILA 2****");
- for (int i = 1; i <= n; i++)
- {
- Console.Write("NΒ°{0}: ", i);
- int valor = Convert.ToInt32(Console.ReadLine());
- pila2.Push(valor);
- }
- }
- static void MostrarPilas(Stack pila1, Stack pila2, int n)
- {
- Console.WriteLine("PILA 1");
- for (int i = 0; i < n; i++)
- {
- string num = Convert.ToString(pila1.pop());
- Console.WriteLine("|_{0}_|",num);
- }
- Console.WriteLine("PILA 2");
- for (int i = 0; i < n; i++)
- {
- string num = Convert.ToString(pila2.pop());
- Console.WriteLine("|_{0}_|",num);
- }
- }
- static int comparar(Stack pila1, Stack pila2)
- {
- while (!pila1.estaVacia())
- {
- if (pila1.consultarCima() == pila2.consultarCima())//si los topes son iguales, decrementamos el tope para analizar el siguiente elemento
- {
- pila1.pop();
- pila2.pop();
- }
- else//si no son iguales, retornamos los valores(de ambos topes) para evaluar la relacion
- {
- if (pila1.consultarCima() < pila2.consultarCima())
- {
- return -1;
- }
- else
- {
- return 1;
- }
- }
- }
- if (pila1.estaVacia())//si la pila esta vacia, no se encontraron elementos distintos entre ambas pilas
- {
- return 0;
- }
- return 2;
- }
- static void clonar(Stack pila, Stack aux,int n)
- {
- Stack temp = new Stack(n);
- while (!pila.estaVacia())
- {
- temp.Push(pila.pop());
- }
- while (!temp.estaVacia())
- {
- int dato = temp.pop();
- pila.Push(dato);
- aux.Push(dato);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement