public static Expression Parse(string expr, VariableStack[] vars) { var returns = new Expression {Term = expr}; expr = expr.Replace("²", "²0"); expr = expr.Replace("³", "³0"); expr = expr.Replace("sqrt[", "√"); expr = expr.Replace("pi", "π"); expr = expr.Replace("]", ""); expr = expr.Replace("√", "1√"); expr = expr.Replace("π", "1π1"); expr = expr.Replace("True", "1"); expr = expr.Replace("False", "0"); if (vars != null) expr = vars.Aggregate(expr, (current, var) => current.Replace(@var.Name, @var.Value.ToString())); char[] chars = expr.ToCharArray(); string result = ""; var group_stack = new GroupStack(new List<Stack>()); bool bracket_open = false; foreach (char c in chars) { if (isNum(c)) { result += c; } else { if (isOP(c)) { if (!bracket_open) { returns._stacks.Add(new NumStack(Convert.ToDouble(result))); returns._stacks.Add(new OpStack(c.ToString())); } else { group_stack.Value.Add(new NumStack(Convert.ToDouble(result))); group_stack.Value.Add(new OpStack(c.ToString())); } result = ""; } else if (c == '(') bracket_open = true; else if (c == ')') { bracket_open = false; // group_stack.Value.Add(returns.getLastNum(expr, group_stack.Value)); returns._stacks.Add(group_stack); group_stack = new GroupStack(new List<Stack>()); } } } group_stack.Value = returns.dotbeforestatement(group_stack.Value); returns.Calculate(); return returns; }
public static Expression Parse(string expr, Var[] vars) { var returns = new Expression { Term = expr }; expr = expr.Replace("²", "²0"); expr = expr.Replace("³", "³0"); expr = expr.Replace("sqrt[", "√"); expr = expr.Replace("pi", "π"); expr = expr.Replace("]", ""); expr = expr.Replace("√", "1√"); expr = expr.Replace("π", "1π1"); expr = expr.Replace("True", "1"); expr = expr.Replace("False", "0"); if (vars != null) { expr = vars.Aggregate(expr, (current, var) => current.Replace(@var.Name, @var.Value.ToString())); } char[] chars = expr.ToCharArray(); string result = ""; var group_stack = new GroupStack(new List <Stack>()); bool bracket_open = false; foreach (char c in chars) { if (isNum(c)) { result += c; } else { if (isOP(c)) { if (!bracket_open) { returns._stacks.Add(new NumStack(Convert.ToDouble(result))); returns._stacks.Add(new OpStack(c.ToString())); } else { group_stack.Value.Add(new NumStack(Convert.ToDouble(result))); group_stack.Value.Add(new OpStack(c.ToString())); } result = ""; } else if (c == '(') { bracket_open = true; } else if (c == ')') { bracket_open = false; // group_stack.Value.Add(returns.getLastNum(expr, group_stack.Value)); returns._stacks.Add(group_stack); group_stack = new GroupStack(new List <Stack>()); } } } group_stack.Value = returns.dotbeforestatement(group_stack.Value); returns.Calculate(); return(returns); }