public static double Calcular(FilaSimbolos fila)
        {
            PilhaNumeros pilha = new PilhaNumeros();

            while (!fila.FilaVazia())
            {
                ElemSimbolo aux = fila.Desenfileirar();

                if (!Validador.EhOperador(aux.Simbolo))
                {
                    pilha.Push(new EleNumber(aux.Simbolo));
                }
                else
                {
                    EleNumber numberB = pilha.Pop();
                    EleNumber numberA = pilha.Pop();

                    double result = 0;

                    switch (aux.Simbolo)
                    {
                    case "+":
                        result = numberA.Dado + numberB.Dado;
                        break;

                    case "-":
                        result = numberA.Dado - numberB.Dado;
                        break;

                    case "*":
                        result = numberA.Dado * numberB.Dado;
                        break;

                    case "/":
                        result = numberA.Dado / numberB.Dado;
                        break;

                    case "^":
                        result = Math.Pow(numberA.Dado, numberB.Dado);
                        break;

                    case "√":
                        pilha.Push(numberA);
                        result = Math.Sqrt(numberB.Dado);
                        break;

                    case "l":
                        pilha.Push(numberA);
                        result = Math.Log10(numberB.Dado);
                        break;
                    }

                    EleNumber eleNumber = new EleNumber(result);
                    pilha.Push(eleNumber);
                }
            }

            return(pilha.Pop().Dado);
        }
Exemplo n.º 2
0
        public EleNumber Pop()
        {
            EleNumber aux = this.topo.Proximo;

            this.topo.Proximo = aux.Proximo;
            aux.Proximo       = null;
            return(aux);
        }
Exemplo n.º 3
0
 public EleNumber Push(EleNumber novo)
 {
     novo.Proximo      = this.topo.Proximo;
     this.topo.Proximo = novo;
     return(this.topo.Proximo);
 }
Exemplo n.º 4
0
 public PilhaNumeros()
 {
     this.topo = new EleNumber();
 }