Advertisement
damarijsilva

Program.cs

Sep 14th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Collections;
  7.  
  8.  
  9. namespace TP2
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Console.Write("Cantidad de elementos en cada pila?: ");
  16.             int n = Convert.ToInt32(Console.ReadLine());
  17.  
  18.             Stack pila1 = new Stack(n);
  19.             Stack pila2 = new Stack(n);
  20.             Stack aux1 = new Stack(n);          
  21.             Stack aux2 = new Stack(n);
  22.            
  23.             Console.Clear();
  24.  
  25.             CargarPilas(pila1,pila2,n);
  26.  
  27.             clonar(pila1, aux1, n);
  28.  
  29.             clonar(pila2, aux2, n);
  30.  
  31.             Console.Clear();  
  32.        
  33.             MostrarPilas(pila1, pila2, n);
  34.    
  35.             switch (comparar(aux1, aux2))
  36.             {
  37.                 case -1: Console.WriteLine("La pila 1 es menor a la pila 2");
  38.                     break;
  39.                 case 1: Console.WriteLine("La pila 1 es mayor a la pila 2");
  40.                     break;
  41.                 case 0: Console.WriteLine("Las pilas son iguales");
  42.                     break;
  43.                 default: Console.Write("");                        
  44.                     break;
  45.             }
  46.             Console.ReadKey();
  47.         }
  48.  
  49.  
  50.         static void CargarPilas(Stack pila1,Stack pila2,int n)
  51.         {
  52.             Console.WriteLine("INSERTE NUMEROS EN LAS PILAS");
  53.             Console.WriteLine("****PILA 1****");
  54.             for(int i=1; i<=n;i++)
  55.             {
  56.                 Console.Write("NΒ°{0}: ",i);
  57.                 int valor = Convert.ToInt32(Console.ReadLine());
  58.                 pila1.Push(valor);
  59.             }
  60.             Console.WriteLine("****PILA 2****");
  61.             for (int i = 1; i <= n; i++)
  62.             {
  63.                 Console.Write("NΒ°{0}: ", i);
  64.                 int valor = Convert.ToInt32(Console.ReadLine());
  65.                 pila2.Push(valor);
  66.             }            
  67.         }
  68.  
  69.         static void MostrarPilas(Stack pila1, Stack pila2, int n)
  70.         {            
  71.                 Console.WriteLine("PILA 1");
  72.                 for (int i = 0; i < n; i++)
  73.                 {
  74.                     string num = Convert.ToString(pila1.pop());
  75.                     Console.WriteLine("|_{0}_|",num);
  76.                 }
  77.                 Console.WriteLine("PILA 2");
  78.                 for (int i = 0; i < n; i++)
  79.                 {
  80.                     string num = Convert.ToString(pila2.pop());
  81.                     Console.WriteLine("|_{0}_|",num);
  82.                 }            
  83.         }
  84.        
  85.         static int comparar(Stack pila1, Stack pila2)
  86.         {
  87.            while (!pila1.estaVacia())
  88.             {
  89.                 if (pila1.consultarCima() == pila2.consultarCima())//si los topes son iguales, decrementamos el tope para analizar el siguiente elemento
  90.                 {
  91.                     pila1.pop();
  92.                     pila2.pop();                    
  93.                 }
  94.                 else//si no son iguales, retornamos los valores(de ambos topes) para evaluar la relacion
  95.                 {
  96.                     if (pila1.consultarCima() < pila2.consultarCima())
  97.                     {
  98.                         return -1;                        
  99.                     }
  100.                     else
  101.                     {
  102.                         return 1;                        
  103.                     }                    
  104.                 }                                
  105.             }
  106.            
  107.             if (pila1.estaVacia())//si la pila esta vacia, no se encontraron elementos distintos entre ambas pilas
  108.                 {
  109.                     return 0;                  
  110.                 }
  111.             return 2;            
  112.         }
  113.  
  114.        
  115.         static void clonar(Stack pila, Stack aux,int n)
  116.         {
  117.             Stack temp = new Stack(n);
  118.             while (!pila.estaVacia())
  119.             {
  120.                 temp.Push(pila.pop());
  121.             }
  122.             while (!temp.estaVacia())
  123.             {
  124.                 int dato = temp.pop();
  125.                 pila.Push(dato);
  126.                 aux.Push(dato);
  127.             }            
  128.         }
  129.    
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement