Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace ejercicio8
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stack<object> mipila = new Stack<object>();
- Stack<object> posfija = new Stack<object>();
- carga(mipila);
- Console.Clear();
- Console.WriteLine("PILA CON NOTACIÓN INTERFIJA");
- foreach (object i in mipila)
- {
- Console.WriteLine("|_{0}_|",i);
- }
- string pal = transformacion(mipila);
- for (int i = 0; i < pal.Length; i++)
- {
- posfija.Push(pal.Substring(i, 1));
- }
- Console.WriteLine("PILA CON NOTACIÓN POSFIJA");
- foreach (object i in posfija)
- {
- Console.WriteLine("|_{0}_|", i);
- }
- Console.ReadKey();
- }
- static void carga(Stack<object> mipila)
- {
- char sig;
- int num1;
- bool validar;
- do
- {
- do
- {
- //Console.Clear();
- Console.Write("Ingrese numero: ");
- validar = Int32.TryParse(Console.ReadLine(), out num1);
- if (!validar || num1<1 || num1 > 9)
- Console.WriteLine("Debe ser un NUMERO entre 1 y 9");
- } while (!validar || num1<1 || num1 > 9);
- mipila.Push(num1);
- do
- {
- Console.Write("Ingrese operador (E para finalizar carga): ");
- validar = char.TryParse(Console.ReadLine(), out sig);
- if (!validar)
- Console.WriteLine("Debe ser un operador");
- } while (!validar);
- if (sig == 'E' || sig == 'e')
- break;
- else
- {
- mipila.Push(sig);
- }
- } while (sig != 'e');
- }
- static String transformacion(Stack<object> mipila)
- {
- Stack<object> operadores = new Stack<object>();
- string cadenaSalida = "";
- foreach (object item in mipila)
- {
- if (EsNum(item))
- {
- cadenaSalida = cadenaSalida + item.ToString();
- }
- else
- {
- if (operadores.Count == 0)
- {
- operadores.Push(item);
- }
- else
- {
- int a = precedencia(Convert.ToChar(operadores.Peek()));
- int b = precedencia(Convert.ToChar(item));
- while (operadores.Count > 0 && a>=b)
- {
- cadenaSalida = cadenaSalida + operadores.Pop();
- }
- operadores.Push(item);
- }
- }
- }
- while (operadores.Count > 0)
- {
- cadenaSalida = cadenaSalida + operadores.Pop().ToString();
- }
- return cadenaSalida;
- }
- static bool EsNum(object a)
- {
- if (a.GetType() == Type.GetType("System.Int32"))
- return true;
- else
- return false;
- }
- static int precedencia(char o)
- {
- switch (o)
- {
- case '*':
- return 2;
- case '/':
- return 2;
- case '+':
- return 1;
- case '-':
- return 1;
- default:
- return 3;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement