Advertisement
mrevilca31

TP 2 Punto 8 (2018)

Sep 25th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TP2Punto8
  8. {
  9.     class Program
  10.     {
  11.         static void analizarOperador(char item, Stack<char> operadores, ref string salida)
  12.         {
  13.             switch (item)
  14.             {
  15.                 case '*':
  16.                     if (operadores.Count == 0 || operadores.Peek() != '*')
  17.                     {
  18.                         operadores.Push(item);
  19.                     }
  20.                     else
  21.                     {
  22.                         while (operadores.Count != 0)
  23.                         {
  24.                             if(operadores.Peek() == '*')
  25.                             {
  26.                                 salida = salida + operadores.Pop();
  27.                             }
  28.                         }
  29.                         operadores.Push(item);
  30.                     }
  31.                     break;
  32.  
  33.                 case '/':
  34.                     if (operadores.Count == 0 || operadores.Peek() == '+' || operadores.Peek() == '-')
  35.                     {
  36.                         operadores.Push(item);
  37.                     }
  38.                     else
  39.                     {
  40.                         while (operadores.Count != 0)
  41.                         {
  42.                             if (operadores.Peek() == '*' || operadores.Peek() == '/')
  43.                             {
  44.                                 salida = salida + operadores.Pop();
  45.                             }
  46.                         }
  47.                         operadores.Push(item);
  48.                     }
  49.                     break;
  50.  
  51.                 case '+':
  52.                     if (operadores.Count == 0 || operadores.Peek() == '-')
  53.                     {
  54.                         operadores.Push(item);
  55.                     }
  56.                     else
  57.                     {
  58.                         while (operadores.Count!=0)
  59.                         {
  60.                             if (operadores.Peek() == '*' || operadores.Peek() == '/' || operadores.Peek() == '+')
  61.                             {
  62.                                 salida = salida + operadores.Pop();
  63.                             }
  64.                            
  65.                         }
  66.                         operadores.Push(item);
  67.                     }
  68.                     break;
  69.  
  70.                 case '-':
  71.                     if (operadores.Count == 0)
  72.                     {
  73.                         operadores.Push(item);
  74.                     }
  75.                     else
  76.                     {
  77.                         while (operadores.Count!=0)
  78.                         {
  79.                             salida = salida + operadores.Pop();
  80.                         }
  81.                         operadores.Push(item);
  82.                     }
  83.                     break;
  84.                 default:
  85.                     break;
  86.             }
  87.         }
  88.  
  89.         static string cambioNotacion(string entrada)
  90.         {
  91.             string salida=null;
  92.             Stack<char> operadores = new Stack<char>();
  93.  
  94.             foreach (char item in entrada)
  95.             {
  96.                 if (item == '+' || item == '-' || item == '*' || item == '/')
  97.                 {
  98.                     analizarOperador(item, operadores, ref salida);
  99.                 }
  100.                 else
  101.                 {
  102.                     salida += item;
  103.                 }
  104.             }
  105.  
  106.             while (operadores.Count!=0)
  107.             {
  108.                 salida = salida + operadores.Pop();
  109.             }
  110.  
  111.             return salida;
  112.         }
  113.         static void Main(string[] args)
  114.         {
  115.             ConsoleKeyInfo tecla;
  116.  
  117.             do
  118.             {
  119.                 Console.Clear();
  120.  
  121.                 string entrada;
  122.  
  123.                 Console.Write("Expresión en notación interfija: ");
  124.                 entrada = Console.ReadLine();
  125.  
  126.                 Console.Write("\nExpresión en notación posfija: "+cambioNotacion(entrada));
  127.  
  128.                 Console.WriteLine("\n\nPRESIONE:   (Esc) para salir.   (Otra Tecla) para ingresar una nueva epresión.");
  129.                 tecla = Console.ReadKey(true);
  130.  
  131.             } while (tecla.Key != ConsoleKey.Escape);
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement