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); }
public int SumOperation(Expression l, Expression r) { return l.eval() + r.eval(); }
public int MultOperation(Expression l, Expression r) { return l.eval() * r.eval(); }
public int DivOperation(Expression l, Expression r) { return l.eval() / r.eval(); }
public int DiffOperation(Expression l, Expression r) { return l.eval() - r.eval(); }
public int DivOperation(Expression l, Expression r) { return(l.eval() / r.eval()); }
public int MultOperation(Expression l, Expression r) { return(l.eval() * r.eval()); }
public int SumOperation(Expression l, Expression r) { return(l.eval() + r.eval()); }