Advertisement
damarijsilva

ejercicio8

Sep 15th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6.  
  7. namespace ejercicio8
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Stack<object> mipila = new Stack<object>();
  14.             Stack<object> posfija = new Stack<object>();
  15.  
  16.             carga(mipila);
  17.            
  18.             Console.Clear();
  19.  
  20.  
  21.             Console.WriteLine("PILA CON NOTACIÓN INTERFIJA");
  22.  
  23.             foreach (object i in mipila)
  24.             {
  25.                 Console.WriteLine("|_{0}_|",i);
  26.             }
  27.            
  28.  
  29.             string pal = transformacion(mipila);            
  30.  
  31.             for (int i = 0; i < pal.Length; i++)
  32.             {
  33.                 posfija.Push(pal.Substring(i, 1));                
  34.             }
  35.              
  36.             Console.WriteLine("PILA CON NOTACIÓN POSFIJA");
  37.             foreach (object i in posfija)
  38.             {
  39.                 Console.WriteLine("|_{0}_|", i);
  40.             }
  41.            
  42.             Console.ReadKey();
  43.         }
  44.  
  45.  
  46.         static void carga(Stack<object> mipila)
  47.         {
  48.             char sig;
  49.             int num1;
  50.             bool validar;
  51.            
  52.             do
  53.             {
  54.                 do
  55.                 {
  56.                     //Console.Clear();
  57.                     Console.Write("Ingrese numero: ");
  58.                     validar = Int32.TryParse(Console.ReadLine(), out num1);
  59.                     if (!validar || num1<1 || num1 > 9)
  60.                         Console.WriteLine("Debe ser un NUMERO entre 1 y 9");
  61.                 } while (!validar || num1<1 || num1 > 9);
  62.                 mipila.Push(num1);
  63.  
  64.                 do
  65.                 {
  66.                     Console.Write("Ingrese operador (E para finalizar carga): ");
  67.                     validar = char.TryParse(Console.ReadLine(), out sig);
  68.                     if (!validar)
  69.                         Console.WriteLine("Debe ser un operador");                    
  70.                 } while (!validar);
  71.                 if (sig == 'E' || sig == 'e')
  72.                     break;
  73.                 else
  74.                 {
  75.                     mipila.Push(sig);                  
  76.                 }
  77.             } while (sig != 'e');
  78.        
  79.         }
  80.  
  81.  
  82.         static String transformacion(Stack<object> mipila)
  83.         {
  84.             Stack<object> operadores = new Stack<object>();
  85.             string cadenaSalida = "";
  86.  
  87.             foreach (object item in mipila)
  88.             {
  89.                 if (EsNum(item))
  90.                 {
  91.                    cadenaSalida = cadenaSalida + item.ToString();
  92.                 }
  93.                 else
  94.                 {
  95.                     if (operadores.Count == 0)
  96.                     {
  97.                         operadores.Push(item);                        
  98.                     }
  99.                     else
  100.                     {
  101.                         int a = precedencia(Convert.ToChar(operadores.Peek()));
  102.                         int b = precedencia(Convert.ToChar(item));
  103.  
  104.                         while (operadores.Count > 0 && a>=b)
  105.                         {
  106.                                cadenaSalida = cadenaSalida + operadores.Pop();
  107.                         }
  108.                            operadores.Push(item);      
  109.                     }                  
  110.                 }
  111.             }
  112.             while (operadores.Count > 0)
  113.             {
  114.                 cadenaSalida = cadenaSalida + operadores.Pop().ToString();                
  115.             }
  116.             return cadenaSalida;    
  117.         }
  118.  
  119.         static bool EsNum(object a)
  120.         {
  121.             if (a.GetType() == Type.GetType("System.Int32"))
  122.                 return true;
  123.             else
  124.                 return false;
  125.         }
  126.  
  127.         static int precedencia(char o)
  128.         {
  129.             switch (o)
  130.             {
  131.                 case '*':
  132.                     return 2;
  133.                    
  134.                 case '/':
  135.                     return 2;
  136.                    
  137.                 case '+':
  138.                     return 1;
  139.                    
  140.                 case '-':
  141.                     return 1;
  142.                    
  143.                 default:
  144.                     return 3;
  145.             }
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement