Advertisement
FacuValverdi

EDTP02.-Ejercicio10

Oct 17th, 2019
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 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 EJ10
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Stack<int> pilaN = new Stack<int>();
  14.  
  15.             Console.WriteLine("Ingrese una ecuacion en orden posfija: ");
  16.             String ecuacion = Console.ReadLine();
  17.      
  18.  
  19.             foreach (char letra in ecuacion)
  20.             {
  21.                 if (Operando(letra) == true)
  22.                 {
  23.                     string unValor = letra.ToString();
  24.                     int numero = Int32.Parse(unValor);
  25.                     pilaN.Push(numero);
  26.                 }
  27.                 else if (Operadores(letra) == true)
  28.                 {
  29.  
  30.                     int ope2 = pilaN.Pop();
  31.                     int ope1 = pilaN.Pop();
  32.                     int cal = Calculos(ope1, ope2, letra);
  33.                     pilaN.Push(cal);
  34.                 }
  35.             }
  36.             if (pilaN.Count() != 0)
  37.             {
  38.                 Console.WriteLine("El valor del calculo es: " + pilaN.Peek());
  39.             }
  40.             Console.ReadKey();
  41.  
  42.         }
  43.         private static bool Operando(char ec1)
  44.         {
  45.             bool band = false;
  46.             String operando = "1234567890";
  47.             foreach (char num in operando.Reverse())
  48.             {
  49.                 if (ec1 == num)
  50.                 {
  51.                     band = true;
  52.                 }
  53.             }
  54.             return band;
  55.         }
  56.         private static bool Operadores(char ec2)
  57.         {
  58.             bool band2 = false;
  59.             String operador = "*/+-";
  60.             foreach (char simbolo in operador.Reverse())
  61.             {
  62.                 if (ec2 == simbolo)
  63.                 {
  64.                     band2 = true;
  65.                 }
  66.             }
  67.             return band2;
  68.         }
  69.         private static int Calculos(int opa, int opb, char ec)
  70.         {
  71.             int resultado = 0;
  72.             if (ec == '*')
  73.             {
  74.                 resultado = (opa * opb);
  75.             }
  76.             else if (ec == '/')
  77.             {
  78.                 resultado = (opa / opb);
  79.             }
  80.             if (ec == '+')
  81.             {
  82.                 resultado = (opa + opb);
  83.             }
  84.             else if (ec == '-')
  85.             {
  86.                 resultado = (opa - opb);
  87.             }
  88.             return resultado;
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement