예제 #1
0
        public static Pila Infijo2Prefijo(String infijo)
        {
            infijo = '(' + infijo; // Agregamos al final del infijo un ')'
            int  tamaño          = infijo.Length;
            Pila PilasDefinitiva = new Pila(tamaño);
            Pila PilasTemp       = new Pila(tamaño);

            PilasTemp.push(')'); // Agregamos a la Pila temporal un '('
            for (int i = tamaño - 1; i > -1; i--)
            {
                char caracter = infijo[i];
                switch (caracter)
                {
                case ')':
                    PilasTemp.push(caracter);
                    break;

                case '+':
                case '-':
                case '^':
                case '*':
                case '/':
                    while (Jerarquia(caracter) > Jerarquia(PilasTemp.headValue()))
                    {
                        PilasDefinitiva.push(PilasTemp.pop());
                    }
                    PilasTemp.push(caracter);
                    break;

                case '(':
                    while (PilasTemp.headValue() != ')')
                    {
                        PilasDefinitiva.push(PilasTemp.pop());
                    }
                    PilasTemp.pop();
                    break;

                default:
                    PilasDefinitiva.push(caracter);
                    break;
                }
            }
            return(PilasDefinitiva);
        }
예제 #2
0
        //METODO QUE REGRESA UN StringBuilder (EXPRESION A POSTFIJA) Y RECIBE UN STRING(EXPRESION INFIJA)
        public StringBuilder ConvertirPosFija(string Ei)
        {
            // notacion_polaca objeto = new notacion_polaca();

            char[] Epos = new char[Ei.Length];

            int  tam   = Ei.Length;
            Pila stack = new Pila(Ei.Length);


            int i, pos = 0;

            for (i = 0; i < Epos.Length; i++)
            {
                char    car    = Ei[i];
                Simbolo actual = Tipo_y_Prescedencia(car);
                switch (actual)
                {
                case Simbolo.OPERANDO: Epos[pos++] = car; break;

                case Simbolo.SUMRES:
                {
                    while (!stack.pilaIsEmpty() && Tipo_y_Prescedencia((char)stack.headValue()) >= actual)
                    {
                        Epos[pos++] = (char)stack.pop();
                    }
                    stack.push(car);
                }
                break;

                case Simbolo.MULTDIV:
                {
                    while (!stack.pilaIsEmpty() && Tipo_y_Prescedencia((char)stack.headValue()) >= actual)
                    {
                        Epos[pos++] = (char)stack.pop();
                    }
                    stack.push(car);
                }
                break;

                case Simbolo.POW:
                {
                    while (!stack.pilaIsEmpty() && Tipo_y_Prescedencia((char)stack.headValue()) >= actual)
                    {
                        Epos[pos++] = (char)stack.pop();
                    }
                    stack.push(car);
                }
                break;

                case Simbolo.PIZQ: stack.push(car); break;

                case Simbolo.PDER:
                {
                    char x = (char)stack.pop();
                    while (Tipo_y_Prescedencia(x) != Simbolo.PIZQ)
                    {
                        Epos[pos++] = x;
                        x           = (char)stack.pop();
                    }
                }
                break;
                }
            }

            while (!stack.pilaIsEmpty())
            {
                if (pos < Epos.Length)
                {
                    Epos[pos++] = (char)stack.pop();
                }
                else
                {
                    break;
                }
            }

            StringBuilder regresa = new StringBuilder(Ei);

            for (int r = 0; r < Epos.Length; r++)
            {
                regresa[r] = Epos[r];
            }


            return(regresa);
        }