Advertisement
damarijsilva

CalcularPosfija

Oct 2nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.79 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.             string pal = transformacion(mipila);      
  21.  
  22.             for (int i = 0; i < pal.Length; i++)
  23.             {
  24.                 posfija.Push(pal.Substring(i, 1));
  25.             }
  26.  
  27.             Console.WriteLine("PILA CON NOTACIΓ“N POSFIJA");
  28.             foreach (object i in posfija)
  29.             {
  30.                 Console.WriteLine("|_{0}_|", i);
  31.             }
  32.  
  33.             Console.WriteLine("Resultado con notacion posfija: " + Resultado(pal));
  34.            
  35.  
  36.             Console.ReadKey();
  37.         }
  38.  
  39.  
  40.         static void carga(Stack<object> mipila)
  41.         {
  42.             char sig;
  43.             int num1;
  44.             bool validar;
  45.  
  46.             do
  47.             {
  48.                 do
  49.                 {
  50.                     Console.Write("Ingrese numero: ");
  51.                     validar = Int32.TryParse(Console.ReadLine(), out num1);
  52.                     if (!validar || num1 < 1 || num1 > 9)                        
  53.                         Console.WriteLine("Debe ser un NUMERO entre 1 y 9");
  54.                 } while (!validar || num1 < 1 || num1 > 9);
  55.                 mipila.Push(num1);
  56.  
  57.                 do
  58.                 {
  59.                     Console.Write("Ingrese operador (E para finalizar carga): ");
  60.                     validar = char.TryParse(Console.ReadLine(), out sig);
  61.                     if (!validar)
  62.                         Console.WriteLine("Debe ser un operador");
  63.                 } while (!validar);
  64.  
  65.                 if (sig == 'E' || sig == 'e')
  66.                     break;
  67.                 else
  68.                 {
  69.                     mipila.Push(sig);
  70.                 }
  71.  
  72.             } while (sig != 'e');
  73.         }
  74.  
  75.  
  76.         static String transformacion(Stack<object> mipila)
  77.         {
  78.             Stack<object> operadores = new Stack<object>();
  79.             string cadenaSalida = "";
  80.  
  81.             foreach (object item in mipila)
  82.             {
  83.                 if (EsNum(item))
  84.                 {
  85.                     cadenaSalida = cadenaSalida + item.ToString();
  86.                 }
  87.                 else
  88.                 {
  89.                     if (operadores.Count == 0)
  90.                     {
  91.                         operadores.Push(item);
  92.                     }
  93.                     else
  94.                     {
  95.                         int a = precedencia(Convert.ToChar(operadores.Peek()));
  96.                         int b = precedencia(Convert.ToChar(item));
  97.  
  98.                         while (operadores.Count > 0 && a >= b)
  99.                         {
  100.                             cadenaSalida = cadenaSalida + operadores.Pop();
  101.                         }
  102.                         operadores.Push(item);
  103.                     }
  104.                 }
  105.             }
  106.             while (operadores.Count > 0)
  107.             {
  108.                 cadenaSalida = cadenaSalida + operadores.Pop().ToString();
  109.             }
  110.             return cadenaSalida;
  111.         }
  112.  
  113.  
  114.         public static int Resultado(string exp)
  115.         {
  116.             Stack<int> resultado = new Stack<int>();
  117.             foreach (char value in exp)
  118.             {
  119.                 if (Char.IsNumber(value))
  120.                 {
  121.                     resultado.Push(Int32.Parse(value.ToString()));
  122.                 }
  123.                 else
  124.                 {
  125.                     resultado.Push(Calcular(resultado.Pop(), resultado.Pop(), Convert.ToChar(value)));
  126.                 }
  127.             }
  128.             return resultado.Pop();
  129.         }
  130.  
  131.        
  132.         static bool EsNum(object a)
  133.         {
  134.             if (a.GetType() == Type.GetType("System.Int32"))
  135.                 return true;
  136.             else
  137.                 return false;
  138.         }
  139.  
  140.         static int precedencia(char o)
  141.         {
  142.             switch (o)
  143.             {
  144.                 case '*':
  145.                     return 2;
  146.  
  147.                 case '/':
  148.                     return 2;
  149.  
  150.                 case '+':
  151.                     return 1;
  152.  
  153.                 case '-':
  154.                     return 1;
  155.  
  156.                 default:
  157.                     return 3;
  158.             }
  159.         }
  160.  
  161.        
  162.  
  163.         static int Calcular(int n1, int n2, char signo)
  164.         {
  165.             switch (signo)
  166.             {
  167.                 case '+': return (n1 + n2);
  168.                 case '-': return (n1 - n2);
  169.                 case '*': return (n1 * n2);
  170.                 case '/': return (n1 / n2);                
  171.             }
  172.             return 0;
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement