public bool PrepareArithmeticExpression(ArithmeticExpression expr) { bool res = (expr != null) && !string.IsNullOrWhiteSpace(mExpression); if (res) { expr.Variables = mVariables; expr.Expression = mExpression; } return(res); }
static void Main(string[] args) { if (args.Length > 0) { var sp = new InputStringParser(); var ae = new ArithmeticExpression(); sp.InputString = args[0]; sp.PrepareArithmeticExpression(ae); Console.WriteLine("{0} = {1}", ae.Expression, ae.Result); } else { Console.WriteLine("Enter an arithmetic expression in form \"EXPRESSION[;VAR1;VAR2;VAR3;...]\" as an argument,\ne.g.: (6.4 + 3.654) - a; a = 5.5\nSupported operations: +, -, *, /, %, ^, ~, ()"); } }
private void Parse() { mVariables.Clear(); mExpression = ""; if (!string.IsNullOrWhiteSpace(mInputString)) { string[] a = mInputString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in a) { if (s.Contains("=")) { string[] ve = s.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (ve.Length >= 2) { mVariables.Add(ve[0].Trim(), ArithmeticExpression.GetValue(ve[1].Trim())); } } else { mExpression = s.Trim(); } } } }