예제 #1
0
        static void Main(string[] args)
        {
            string input;

            Console.WriteLine("Calculadora");
            Console.Write("Operacion Infix: ");
            input = Console.ReadLine();

            Tokenizer(input);
            string postfix = expr();

            Console.WriteLine("\n conversion Postfix = {0}", postfix);
            LaLista.Clear();

            #region AST
            Tokenizer(postfix.Replace(" ", string.Empty));
            Expression AST = null;

            Stack <Expression> LaPila = new Stack <Expression>();

            foreach (Token ElToken in LaLista)
            {
                //El token es numero
                if (ElToken.Tipo == TipoToken.NUM)
                {
                    LaPila.Push(new UnaryExpression(ElToken));
                }

                //El token es operador
                else if (ElToken.Tipo == TipoToken.OP)
                {
                    Expression Pop1       = LaPila.Pop();
                    Expression Pop2       = LaPila.Pop();
                    Expression NuevoArbol = new BinaryExpression(ElToken, Pop2, Pop1);
                    LaPila.Push(NuevoArbol);
                }
                else if (ElToken.Tipo == TipoToken.OPEN_PARENS || ElToken.Tipo == TipoToken.CLOSING_PARENS)
                {
                    continue;
                }
            }
            AST = LaPila.Pop();
            Console.WriteLine("\nEl resultado es: " + AST.eval());
            #endregion

            Console.ReadKey(true);
        }
예제 #2
0
 public int SumOperation(Expression l, Expression r)
 {
     return l.eval() + r.eval();
 }
예제 #3
0
 public int MultOperation(Expression l, Expression r)
 {
     return l.eval() * r.eval();
 }
예제 #4
0
 public int DivOperation(Expression l, Expression r)
 {
     return l.eval() / r.eval();
 }
예제 #5
0
 public int DiffOperation(Expression l, Expression r)
 {
     return l.eval() - r.eval();
 }
예제 #6
0
 public int DivOperation(Expression l, Expression r)
 {
     return(l.eval() / r.eval());
 }
예제 #7
0
 public int MultOperation(Expression l, Expression r)
 {
     return(l.eval() * r.eval());
 }
예제 #8
0
 public int SumOperation(Expression l, Expression r)
 {
     return(l.eval() + r.eval());
 }