using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ExpressionLib.Contexts { public class SimpleMath : IContext { public int NumParams(string oprtr) { if (oprtr == "+" || oprtr == "-" || oprtr == "*" || oprtr == "/" || oprtr == "^" || oprtr == "(max)") return 2; else if (oprtr == "(sqrt)") return 1; else return 0; } public double EvalOperator(string oprtr, List values) { if (oprtr == "+") return values[0] + values[1]; else if (oprtr == "-") return values[0] - values[1]; else if (oprtr == "*") return values[0] * values[1]; else if (oprtr == "/") return values[0] / values[1]; else if (oprtr == "^") return Math.Pow(values[0], values[1]); else if (oprtr == "(sqrt)") return Math.Sqrt(values[0]); else if (oprtr == "(max)") return Math.Max(values[0], values[1]); else throw new ParsingException("Unknown operator '" + oprtr + "' detected!"); } public bool IsValue(string c) { double r; return IsValue(c, out r); } public bool IsValue(string c, out double r) { return double.TryParse(c.Replace('.', ','), out r); } public bool IsOperator(string c) { return ( c == "^" || c == "*" || c == "/" || c == "+" || c == "-" ); } public int PriorityOf(string c) { if (c == "(sqrt)" || c == "(max)") return 4; else if (c == "^") return 3; else if (c == "*" || c == "/") return 2; else if (c == "+" || c == "-") return 1; else return 0; } public Associativity AssociativityOf(string c) { if (c == "^") return Associativity.Right; else return Associativity.Left; } } }