Ejemplo n.º 1
0
        public static double ResolverPosfixa(Expressao ExpPosfixa)
        {
            String posfixa = ExpPosfixa.expressao;

            Pilha <double> p = new Pilha <double>();

            for (int i = 0; i < posfixa.Length; i++)
            {
                if (!isOperador(posfixa[i]))
                {
                    if (posfixa[i] == '@')
                    {
                        p.Empilhar(p.Desempilhar() * -1);
                    }
                    else
                    {
                        p.Empilhar(ExpPosfixa.dicionario[posfixa[i]]);
                    }
                }
                else
                {
                    double operando2 = p.Desempilhar(),
                           operando1 = p.Desempilhar();

                    p.Empilhar(SubExpressao(operando1, operando2, posfixa[i]));
                }
            }

            return(p.Desempilhar());
        }
Ejemplo n.º 2
0
        public bool combinarParenteses(string expressao)
        {
            Pilha <char> parenteses = new Pilha <char>();

            foreach (char c in expressao)
            {
                if (c == '(')
                {
                    parenteses.Empilhar(c);
                }
                else if (c == ')')
                {
                    try
                    {
                        parenteses.Desempilhar();
                    }
                    catch (Exception)
                    {
                        return(false);
                    }
                }
            }

            if (!parenteses.EstaVazia())
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        public static Expressao TraduzirParaPosfixa(Expressao expressaoInfixa)
        {
            String infixa = expressaoInfixa.expressao;

            Pilha <char> p       = new Pilha <char>();
            String       posfixa = "";

            for (int i = 0; i < infixa.Length; i++)
            {
                bool unario = false;

                if (isOperador(infixa[i]))
                {
                    if (infixa[i] == '-')
                    {
                        if (i == 0 || infixa[i - 1] == '(')
                        {
                            p.Empilhar('@');
                            unario = true;
                        }
                    }

                    if (!unario)
                    {
                        bool parar = false;

                        while (!parar && !p.EstaVazia() && Precedencia(p.OTopo(), infixa[i]))
                        {
                            char operadorComMaiorPrec = p.OTopo();
                            if (operadorComMaiorPrec == '(')
                            {
                                parar = true;
                            }
                            else
                            {
                                posfixa += operadorComMaiorPrec;
                                p.Desempilhar();
                            }
                        }

                        if (infixa[i] != ')')
                        {
                            p.Empilhar(infixa[i]);
                        }
                        else
                        {
                            p.Desempilhar();
                        }
                    }
                }
                else
                {
                    posfixa += infixa[i];
                }
            }

            while (!p.EstaVazia())
            {
                char operadorComMaiorPrec = p.Desempilhar();
                if (operadorComMaiorPrec != '(')
                {
                    posfixa += operadorComMaiorPrec;
                }
            }

            return(new Expressao(posfixa, expressaoInfixa.dicionario));
        }
Ejemplo n.º 4
0
        private void PolonesInvertido(string expressao)
        {
            if (expressao.Length != 0)
            {
                int  cont = 0;
                bool number = false, operador = false;

                while (cont < expressao.Length && (Valor.IsNumber(expressao[cont]) || expressao[cont].Equals(',')))
                {
                    cont++;
                    number = true;
                }

                while (cont < expressao.Length && Operador.IsEspecial(expressao[cont].ToString()) && Operador.IsOperator(expressao[cont].ToString()) && number == false)
                {
                    cont++;
                    operador = true;
                }

                if (cont < expressao.Length && !number && (expressao[cont].Equals('(') || !Operador.IsEspecial(expressao[cont].ToString())))
                {
                    operador = true;
                }

                if (operador == true)
                {
                    Operador aux;
                    if (cont > 0)
                    {
                        aux = new Operador(expressao.Substring(0, cont));
                    }
                    else
                    {
                        aux = new Operador(expressao.Substring(0, 1));
                    }

                    if (aux.Op.Equals("("))
                    {
                        pilhaOperadores.Empilhar(null);
                        operador = false;
                        if (cont > 0)
                        {
                            PolonesInvertido(expressao.Substring(cont));
                        }
                        else
                        {
                            PolonesInvertido(expressao.Substring(cont + 1));
                        }
                        return;
                    }

                    if ((pilhaOperadores.OlharTopo() == null || pilhaOperadores.OlharTopo().CompareTo(aux) < 0) && !aux.Op.Equals(")"))
                    {
                        pilhaOperadores.Empilhar(aux);
                        operador = false;
                        if (cont > 0)
                        {
                            PolonesInvertido(expressao.Substring(cont));
                        }
                        else
                        {
                            PolonesInvertido(expressao.Substring(cont + 1));
                        }
                        return;
                    }
                    else
                    {
                        Operador auxOp;
                        if (aux.Prioridade != 0)
                        {
                            do
                            {
                                if (pilhaOperadores.OlharTopo() != null)
                                {
                                    auxOp = (Operador)pilhaOperadores.Desempilhar();
                                }
                                else
                                {
                                    auxOp = null;
                                }

                                if (auxOp != null)
                                {
                                    saida.Inserir(auxOp);
                                }
                            } while (auxOp != null);

                            pilhaOperadores.Empilhar(aux);
                        }
                        else
                        {
                            do
                            {
                                auxOp = (Operador)pilhaOperadores.Desempilhar();

                                if (auxOp != null)
                                {
                                    saida.Inserir(auxOp);
                                }
                            } while (auxOp != null);
                        }
                        operador = false;
                        if (cont > 0)
                        {
                            PolonesInvertido(expressao.Substring(cont));
                        }
                        else
                        {
                            PolonesInvertido(expressao.Substring(cont + 1));
                        }
                        return;
                    }
                }

                if (number == true)
                {
                    string auxString = expressao.Substring(0, cont);
                    if (Valor.IsNumber(auxString))
                    {
                        saida.Inserir(new Valor(double.Parse(auxString)));
                        number = false;
                        PolonesInvertido(expressao.Substring(cont));
                        return;
                    }
                    else
                    {
                        erro   = true;
                        number = false;
                    }
                }
            }
            else
            {
                while (pilhaOperadores.OlharTopo() != null)
                {
                    saida.Inserir(pilhaOperadores.Desempilhar());
                }
            }
        }